Skip to content

Commit

Permalink
InputBag - compatibility with Symfony 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 31, 2021
1 parent 6a367d0 commit 716a70d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
21 changes: 15 additions & 6 deletions src/Type/Symfony/InputBagDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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()]));
}

}
12 changes: 6 additions & 6 deletions tests/Type/Symfony/data/input_bag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, array<string>|string>', $bag->all());
assertType('array<string>', $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<string, array<bool|float|int|string>|bool|float|int|string>', $bag->all());
assertType('array<bool|float|int|string>', $bag->all('bar'));

0 comments on commit 716a70d

Please sign in to comment.