Skip to content

Commit

Permalink
Specify type after calling InputBag::has
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored and ondrejmirtes committed Feb 3, 2022
1 parent df5ce32 commit 173610b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
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

0 comments on commit 173610b

Please sign in to comment.