Skip to content

Remove nullable type on InputBag::get() after InputBag::has() call #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,8 @@ services:
-
factory: PHPStan\Type\Symfony\ResponseHeaderBagDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# InputBag::get() type specification
-
factory: PHPStan\Type\Symfony\InputBagTypeSpecifyingExtension
tags: [phpstan.typeSpecifier.methodTypeSpecifyingExtension]
68 changes: 68 additions & 0 deletions src/Type/Symfony/InputBagTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\TypeCombinator;
use Symfony\Component\HttpFoundation\InputBag;

final class InputBagTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

private const INPUT_BAG_CLASS = InputBag::class;
private const HAS_METHOD_NAME = 'has';
private const GET_METHOD_NAME = 'get';

/** @var ReflectionProvider */
private $reflectionProvider;

/** @var TypeSpecifier */
private $typeSpecifier;

public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}

public function getClass(): string
{
return self::INPUT_BAG_CLASS;
}

public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool
{
return $methodReflection->getName() === self::HAS_METHOD_NAME && !$context->null();
}

public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$classReflection = $this->reflectionProvider->getClass(self::INPUT_BAG_CLASS);
$methodVariants = $classReflection->getNativeMethod(self::GET_METHOD_NAME)->getVariants();
$returnType = ParametersAcceptorSelector::selectSingle($methodVariants)->getReturnType();

if (!TypeCombinator::containsNull($returnType)) {
return new SpecifiedTypes();
}

return $this->typeSpecifier->create(
new MethodCall($node->var, self::GET_METHOD_NAME, $node->getArgs()),
TypeCombinator::removeNull($returnType),
$context
);
}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

}
6 changes: 6 additions & 0 deletions tests/Type/Symfony/data/input_bag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
$bag = new \Symfony\Component\HttpFoundation\InputBag(['foo' => 'bar', 'bar' => ['x']]);

assertType('bool|float|int|string|null', $bag->get('foo'));

if ($bag->has('foo')) {
assertType('bool|float|int|string', $bag->get('foo'));
assertType('bool|float|int|string|null', $bag->get('bar'));
}

assertType('bool|float|int|string|null', $bag->get('foo', null));
assertType('bool|float|int|string', $bag->get('foo', ''));
assertType('bool|float|int|string', $bag->get('foo', 'baz'));
Expand Down