Skip to content

Commit 716a70d

Browse files
committed
InputBag - compatibility with Symfony 5.3
1 parent 6a367d0 commit 716a70d

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

Diff for: src/Type/Symfony/InputBagDynamicReturnTypeExtension.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
use PHPStan\Reflection\ParametersAcceptorSelector;
99
use PHPStan\ShouldNotHappenException;
1010
use PHPStan\Type\ArrayType;
11+
use PHPStan\Type\BooleanType;
1112
use PHPStan\Type\DynamicMethodReturnTypeExtension;
13+
use PHPStan\Type\FloatType;
14+
use PHPStan\Type\IntegerType;
1215
use PHPStan\Type\MixedType;
1316
use PHPStan\Type\NullType;
1417
use PHPStan\Type\StringType;
1518
use PHPStan\Type\Type;
19+
use PHPStan\Type\TypeCombinator;
1620
use PHPStan\Type\UnionType;
1721

1822
final class InputBagDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -54,10 +58,8 @@ private function getGetTypeFromMethodCall(
5458
if (isset($methodCall->args[1])) {
5559
$argType = $scope->getType($methodCall->args[1]->value);
5660
$isNull = (new NullType())->isSuperTypeOf($argType);
57-
$isString = (new StringType())->isSuperTypeOf($argType);
58-
$compare = $isNull->compareTo($isString);
59-
if ($compare === $isString) {
60-
return new StringType();
61+
if ($isNull->no()) {
62+
return TypeCombinator::removeNull(ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType());
6163
}
6264
}
6365

@@ -68,11 +70,18 @@ private function getAllTypeFromMethodCall(
6870
MethodCall $methodCall
6971
): Type
7072
{
73+
$types = [
74+
new BooleanType(),
75+
new FloatType(),
76+
new IntegerType(),
77+
new StringType(),
78+
];
79+
$oneParameterType = new UnionType($types);
7180
if (isset($methodCall->args[0])) {
72-
return new ArrayType(new MixedType(), new StringType());
81+
return new ArrayType(new MixedType(), $oneParameterType);
7382
}
7483

75-
return new ArrayType(new StringType(), new UnionType([new StringType(), new ArrayType(new MixedType(), new StringType())]));
84+
return new ArrayType(new StringType(), new UnionType([new ArrayType(new MixedType(), $oneParameterType), new BooleanType(), new FloatType(), new IntegerType(), new StringType()]));
7685
}
7786

7887
}

Diff for: tests/Type/Symfony/data/input_bag.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
$bag = new \Symfony\Component\HttpFoundation\InputBag(['foo' => 'bar', 'bar' => ['x']]);
66

7-
assertType('string|null', $bag->get('foo'));
8-
assertType('string|null', $bag->get('foo', null));
9-
assertType('string', $bag->get('foo', ''));
10-
assertType('string', $bag->get('foo', 'baz'));
11-
assertType('array<string, array<string>|string>', $bag->all());
12-
assertType('array<string>', $bag->all('bar'));
7+
assertType('bool|float|int|string|null', $bag->get('foo'));
8+
assertType('bool|float|int|string|null', $bag->get('foo', null));
9+
assertType('bool|float|int|string', $bag->get('foo', ''));
10+
assertType('bool|float|int|string', $bag->get('foo', 'baz'));
11+
assertType('array<string, array<bool|float|int|string>|bool|float|int|string>', $bag->all());
12+
assertType('array<bool|float|int|string>', $bag->all('bar'));

0 commit comments

Comments
 (0)