Skip to content

Commit

Permalink
Change parameterbag scalar type inference to generalized type
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored and ondrejmirtes committed May 3, 2021
1 parent 4e4353e commit 8377e22
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 73 deletions.
3 changes: 2 additions & 1 deletion src/Type/Symfony/ParameterDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use function in_array;

Expand Down Expand Up @@ -97,7 +98,7 @@ private function getGetTypeFromMethodCall(
if ($parameterKey !== null) {
$parameter = $this->parameterMap->getParameter($parameterKey);
if ($parameter !== null) {
return $scope->getTypeFromValue($parameter->getValue());
return TypeUtils::generalizeType($scope->getTypeFromValue($parameter->getValue()));
}
}

Expand Down
72 changes: 36 additions & 36 deletions tests/Type/Symfony/data/ExampleAbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,42 @@ public function parameters(ContainerInterface $container, ParameterBagInterface
assertType('array|bool|float|int|string|null', $container->getParameter('unknown'));
assertType('array|bool|float|int|string|null', $parameterBag->get('unknown'));
assertType('array|bool|float|int|string|null', $this->getParameter('unknown'));
assertType("'abcdef'", $container->getParameter('app.string'));
assertType("'abcdef'", $parameterBag->get('app.string'));
assertType("'abcdef'", $this->getParameter('app.string'));
assertType('123', $container->getParameter('app.int'));
assertType('123', $parameterBag->get('app.int'));
assertType('123', $this->getParameter('app.int'));
assertType("'123'", $container->getParameter('app.int_as_string'));
assertType("'123'", $parameterBag->get('app.int_as_string'));
assertType("'123'", $this->getParameter('app.int_as_string'));
assertType('123.45', $container->getParameter('app.float'));
assertType('123.45', $parameterBag->get('app.float'));
assertType('123.45', $this->getParameter('app.float'));
assertType("'123.45'", $container->getParameter('app.float_as_string'));
assertType("'123.45'", $parameterBag->get('app.float_as_string'));
assertType("'123.45'", $this->getParameter('app.float_as_string'));
assertType('true', $container->getParameter('app.boolean'));
assertType('true', $parameterBag->get('app.boolean'));
assertType('true', $this->getParameter('app.boolean'));
assertType("'true'", $container->getParameter('app.boolean_as_string'));
assertType("'true'", $parameterBag->get('app.boolean_as_string'));
assertType("'true'", $this->getParameter('app.boolean_as_string'));
assertType("array('en', 'es', 'fr')", $container->getParameter('app.list'));
assertType("array('en', 'es', 'fr')", $parameterBag->get('app.list'));
assertType("array('en', 'es', 'fr')", $this->getParameter('app.list'));
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $container->getParameter('app.list_of_list'));
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $parameterBag->get('app.list_of_list'));
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $this->getParameter('app.list_of_list'));
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $container->getParameter('app.map'));
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $parameterBag->get('app.map'));
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $this->getParameter('app.map'));
assertType("'This is a Bell char '", $container->getParameter('app.binary'));
assertType("'This is a Bell char '", $parameterBag->get('app.binary'));
assertType("'This is a Bell char '", $this->getParameter('app.binary'));
assertType("'Y-m-d\\\\TH:i:sP'", $container->getParameter('app.constant'));
assertType("'Y-m-d\\\\TH:i:sP'", $parameterBag->get('app.constant'));
assertType("'Y-m-d\\\\TH:i:sP'", $this->getParameter('app.constant'));
assertType("string", $container->getParameter('app.string'));
assertType("string", $parameterBag->get('app.string'));
assertType("string", $this->getParameter('app.string'));
assertType('int', $container->getParameter('app.int'));
assertType('int', $parameterBag->get('app.int'));
assertType('int', $this->getParameter('app.int'));
assertType("string", $container->getParameter('app.int_as_string'));
assertType("string", $parameterBag->get('app.int_as_string'));
assertType("string", $this->getParameter('app.int_as_string'));
assertType('float', $container->getParameter('app.float'));
assertType('float', $parameterBag->get('app.float'));
assertType('float', $this->getParameter('app.float'));
assertType("string", $container->getParameter('app.float_as_string'));
assertType("string", $parameterBag->get('app.float_as_string'));
assertType("string", $this->getParameter('app.float_as_string'));
assertType('bool', $container->getParameter('app.boolean'));
assertType('bool', $parameterBag->get('app.boolean'));
assertType('bool', $this->getParameter('app.boolean'));
assertType("string", $container->getParameter('app.boolean_as_string'));
assertType("string", $parameterBag->get('app.boolean_as_string'));
assertType("string", $this->getParameter('app.boolean_as_string'));
assertType("array<int, string>&nonEmpty", $container->getParameter('app.list'));
assertType("array<int, string>&nonEmpty", $parameterBag->get('app.list'));
assertType("array<int, string>&nonEmpty", $this->getParameter('app.list'));
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $container->getParameter('app.list_of_list'));
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $parameterBag->get('app.list_of_list'));
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $this->getParameter('app.list_of_list'));
assertType("array<string, string>&nonEmpty", $container->getParameter('app.map'));
assertType("array<string, string>&nonEmpty", $parameterBag->get('app.map'));
assertType("array<string, string>&nonEmpty", $this->getParameter('app.map'));
assertType("string", $container->getParameter('app.binary'));
assertType("string", $parameterBag->get('app.binary'));
assertType("string", $this->getParameter('app.binary'));
assertType("string", $container->getParameter('app.constant'));
assertType("string", $parameterBag->get('app.constant'));
assertType("string", $this->getParameter('app.constant'));

assertType('false', $container->hasParameter('unknown'));
assertType('false', $parameterBag->has('unknown'));
Expand Down
72 changes: 36 additions & 36 deletions tests/Type/Symfony/data/ExampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,42 @@ public function parameters(ContainerInterface $container, ParameterBagInterface
assertType('array|bool|float|int|string|null', $container->getParameter('unknown'));
assertType('array|bool|float|int|string|null', $parameterBag->get('unknown'));
assertType('array|bool|float|int|string|null', $this->getParameter('unknown'));
assertType("'abcdef'", $container->getParameter('app.string'));
assertType("'abcdef'", $parameterBag->get('app.string'));
assertType("'abcdef'", $this->getParameter('app.string'));
assertType('123', $container->getParameter('app.int'));
assertType('123', $parameterBag->get('app.int'));
assertType('123', $this->getParameter('app.int'));
assertType("'123'", $container->getParameter('app.int_as_string'));
assertType("'123'", $parameterBag->get('app.int_as_string'));
assertType("'123'", $this->getParameter('app.int_as_string'));
assertType('123.45', $container->getParameter('app.float'));
assertType('123.45', $parameterBag->get('app.float'));
assertType('123.45', $this->getParameter('app.float'));
assertType("'123.45'", $container->getParameter('app.float_as_string'));
assertType("'123.45'", $parameterBag->get('app.float_as_string'));
assertType("'123.45'", $this->getParameter('app.float_as_string'));
assertType('true', $container->getParameter('app.boolean'));
assertType('true', $parameterBag->get('app.boolean'));
assertType('true', $this->getParameter('app.boolean'));
assertType("'true'", $container->getParameter('app.boolean_as_string'));
assertType("'true'", $parameterBag->get('app.boolean_as_string'));
assertType("'true'", $this->getParameter('app.boolean_as_string'));
assertType("array('en', 'es', 'fr')", $container->getParameter('app.list'));
assertType("array('en', 'es', 'fr')", $parameterBag->get('app.list'));
assertType("array('en', 'es', 'fr')", $this->getParameter('app.list'));
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $container->getParameter('app.list_of_list'));
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $parameterBag->get('app.list_of_list'));
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $this->getParameter('app.list_of_list'));
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $container->getParameter('app.map'));
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $parameterBag->get('app.map'));
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $this->getParameter('app.map'));
assertType("'This is a Bell char '", $container->getParameter('app.binary'));
assertType("'This is a Bell char '", $parameterBag->get('app.binary'));
assertType("'This is a Bell char '", $this->getParameter('app.binary'));
assertType("'Y-m-d\\\\TH:i:sP'", $container->getParameter('app.constant'));
assertType("'Y-m-d\\\\TH:i:sP'", $parameterBag->get('app.constant'));
assertType("'Y-m-d\\\\TH:i:sP'", $this->getParameter('app.constant'));
assertType("string", $container->getParameter('app.string'));
assertType("string", $parameterBag->get('app.string'));
assertType("string", $this->getParameter('app.string'));
assertType('int', $container->getParameter('app.int'));
assertType('int', $parameterBag->get('app.int'));
assertType('int', $this->getParameter('app.int'));
assertType("string", $container->getParameter('app.int_as_string'));
assertType("string", $parameterBag->get('app.int_as_string'));
assertType("string", $this->getParameter('app.int_as_string'));
assertType('float', $container->getParameter('app.float'));
assertType('float', $parameterBag->get('app.float'));
assertType('float', $this->getParameter('app.float'));
assertType("string", $container->getParameter('app.float_as_string'));
assertType("string", $parameterBag->get('app.float_as_string'));
assertType("string", $this->getParameter('app.float_as_string'));
assertType('bool', $container->getParameter('app.boolean'));
assertType('bool', $parameterBag->get('app.boolean'));
assertType('bool', $this->getParameter('app.boolean'));
assertType("string", $container->getParameter('app.boolean_as_string'));
assertType("string", $parameterBag->get('app.boolean_as_string'));
assertType("string", $this->getParameter('app.boolean_as_string'));
assertType("array<int, string>&nonEmpty", $container->getParameter('app.list'));
assertType("array<int, string>&nonEmpty", $parameterBag->get('app.list'));
assertType("array<int, string>&nonEmpty", $this->getParameter('app.list'));
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $container->getParameter('app.list_of_list'));
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $parameterBag->get('app.list_of_list'));
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $this->getParameter('app.list_of_list'));
assertType("array<string, string>&nonEmpty", $container->getParameter('app.map'));
assertType("array<string, string>&nonEmpty", $parameterBag->get('app.map'));
assertType("array<string, string>&nonEmpty", $this->getParameter('app.map'));
assertType("string", $container->getParameter('app.binary'));
assertType("string", $parameterBag->get('app.binary'));
assertType("string", $this->getParameter('app.binary'));
assertType("string", $container->getParameter('app.constant'));
assertType("string", $parameterBag->get('app.constant'));
assertType("string", $this->getParameter('app.constant'));

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

0 comments on commit 8377e22

Please sign in to comment.