From 716a70de8524928036d5bc0d880d1117fa77de82 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 31 May 2021 17:29:33 +0200 Subject: [PATCH] InputBag - compatibility with Symfony 5.3 --- .../InputBagDynamicReturnTypeExtension.php | 21 +++++++++++++------ tests/Type/Symfony/data/input_bag.php | 12 +++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Type/Symfony/InputBagDynamicReturnTypeExtension.php b/src/Type/Symfony/InputBagDynamicReturnTypeExtension.php index 51e5ff19..a403af7f 100644 --- a/src/Type/Symfony/InputBagDynamicReturnTypeExtension.php +++ b/src/Type/Symfony/InputBagDynamicReturnTypeExtension.php @@ -8,11 +8,15 @@ use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\ShouldNotHappenException; use PHPStan\Type\ArrayType; +use PHPStan\Type\BooleanType; use PHPStan\Type\DynamicMethodReturnTypeExtension; +use PHPStan\Type\FloatType; +use PHPStan\Type\IntegerType; use PHPStan\Type\MixedType; use PHPStan\Type\NullType; use PHPStan\Type\StringType; use PHPStan\Type\Type; +use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; final class InputBagDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension @@ -54,10 +58,8 @@ private function getGetTypeFromMethodCall( if (isset($methodCall->args[1])) { $argType = $scope->getType($methodCall->args[1]->value); $isNull = (new NullType())->isSuperTypeOf($argType); - $isString = (new StringType())->isSuperTypeOf($argType); - $compare = $isNull->compareTo($isString); - if ($compare === $isString) { - return new StringType(); + if ($isNull->no()) { + return TypeCombinator::removeNull(ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType()); } } @@ -68,11 +70,18 @@ private function getAllTypeFromMethodCall( MethodCall $methodCall ): Type { + $types = [ + new BooleanType(), + new FloatType(), + new IntegerType(), + new StringType(), + ]; + $oneParameterType = new UnionType($types); if (isset($methodCall->args[0])) { - return new ArrayType(new MixedType(), new StringType()); + return new ArrayType(new MixedType(), $oneParameterType); } - return new ArrayType(new StringType(), new UnionType([new StringType(), new ArrayType(new MixedType(), new StringType())])); + return new ArrayType(new StringType(), new UnionType([new ArrayType(new MixedType(), $oneParameterType), new BooleanType(), new FloatType(), new IntegerType(), new StringType()])); } } diff --git a/tests/Type/Symfony/data/input_bag.php b/tests/Type/Symfony/data/input_bag.php index cdfde92b..c99fe6b6 100644 --- a/tests/Type/Symfony/data/input_bag.php +++ b/tests/Type/Symfony/data/input_bag.php @@ -4,9 +4,9 @@ $bag = new \Symfony\Component\HttpFoundation\InputBag(['foo' => 'bar', 'bar' => ['x']]); -assertType('string|null', $bag->get('foo')); -assertType('string|null', $bag->get('foo', null)); -assertType('string', $bag->get('foo', '')); -assertType('string', $bag->get('foo', 'baz')); -assertType('array|string>', $bag->all()); -assertType('array', $bag->all('bar')); +assertType('bool|float|int|string|null', $bag->get('foo')); +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')); +assertType('array|bool|float|int|string>', $bag->all()); +assertType('array', $bag->all('bar'));