Skip to content

Commit

Permalink
Generalize empty array parameters too
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 4, 2021
1 parent 8377e22 commit c5fb143
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Type/Symfony/ParameterDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Symfony\ParameterMap;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\FloatType;
Expand All @@ -18,7 +19,7 @@
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use function in_array;

Expand Down Expand Up @@ -98,7 +99,21 @@ private function getGetTypeFromMethodCall(
if ($parameterKey !== null) {
$parameter = $this->parameterMap->getParameter($parameterKey);
if ($parameter !== null) {
return TypeUtils::generalizeType($scope->getTypeFromValue($parameter->getValue()));
return TypeTraverser::map($scope->getTypeFromValue($parameter->getValue()), static function (\PHPStan\Type\Type $type, callable $traverse): Type {
if ($type instanceof \PHPStan\Type\ConstantType) {
$generalized = $type->generalize();
if ($generalized instanceof ConstantArrayType) {
if (count($generalized->getValueTypes()) !== 0) {
throw new \PHPStan\ShouldNotHappenException();
}

return new ArrayType(new MixedType(), new MixedType());
}

return $generalized;
}
return $traverse($type);
});
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/Type/Symfony/container.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
</parameter>
<parameter key="app.binary" type="binary">VGhpcyBpcyBhIEJlbGwgY2hhciAH</parameter>
<parameter key="app.constant">Y-m-d\TH:i:sP</parameter>
<parameter key="test_collection" type="collection"/>
</parameters>

<services>
Expand Down
1 change: 1 addition & 0 deletions tests/Type/Symfony/data/ExampleAbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function parameters(ContainerInterface $container, ParameterBagInterface
assertType("string", $container->getParameter('app.constant'));
assertType("string", $parameterBag->get('app.constant'));
assertType("string", $this->getParameter('app.constant'));
assertType("array", $this->getParameter('test_collection'));

assertType('false', $container->hasParameter('unknown'));
assertType('false', $parameterBag->has('unknown'));
Expand Down

0 comments on commit c5fb143

Please sign in to comment.