Skip to content

Commit af91203

Browse files
Change parameterbag scalar type inference to generalized type
1 parent 98b981d commit af91203

File tree

3 files changed

+74
-73
lines changed

3 files changed

+74
-73
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\Type\NullType;
1919
use PHPStan\Type\StringType;
2020
use PHPStan\Type\Type;
21+
use PHPStan\Type\TypeUtils;
2122
use PHPStan\Type\UnionType;
2223
use function in_array;
2324

@@ -97,7 +98,7 @@ private function getGetTypeFromMethodCall(
9798
if ($parameterKey !== null) {
9899
$parameter = $this->parameterMap->getParameter($parameterKey);
99100
if ($parameter !== null) {
100-
return $scope->getTypeFromValue($parameter->getValue());
101+
return TypeUtils::generalizeType($scope->getTypeFromValue($parameter->getValue()));
101102
}
102103
}
103104

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

+36-36
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,42 @@ public function parameters(ContainerInterface $container, ParameterBagInterface
2828
assertType('array|bool|float|int|string|null', $container->getParameter('unknown'));
2929
assertType('array|bool|float|int|string|null', $parameterBag->get('unknown'));
3030
assertType('array|bool|float|int|string|null', $this->getParameter('unknown'));
31-
assertType("'abcdef'", $container->getParameter('app.string'));
32-
assertType("'abcdef'", $parameterBag->get('app.string'));
33-
assertType("'abcdef'", $this->getParameter('app.string'));
34-
assertType('123', $container->getParameter('app.int'));
35-
assertType('123', $parameterBag->get('app.int'));
36-
assertType('123', $this->getParameter('app.int'));
37-
assertType("'123'", $container->getParameter('app.int_as_string'));
38-
assertType("'123'", $parameterBag->get('app.int_as_string'));
39-
assertType("'123'", $this->getParameter('app.int_as_string'));
40-
assertType('123.45', $container->getParameter('app.float'));
41-
assertType('123.45', $parameterBag->get('app.float'));
42-
assertType('123.45', $this->getParameter('app.float'));
43-
assertType("'123.45'", $container->getParameter('app.float_as_string'));
44-
assertType("'123.45'", $parameterBag->get('app.float_as_string'));
45-
assertType("'123.45'", $this->getParameter('app.float_as_string'));
46-
assertType('true', $container->getParameter('app.boolean'));
47-
assertType('true', $parameterBag->get('app.boolean'));
48-
assertType('true', $this->getParameter('app.boolean'));
49-
assertType("'true'", $container->getParameter('app.boolean_as_string'));
50-
assertType("'true'", $parameterBag->get('app.boolean_as_string'));
51-
assertType("'true'", $this->getParameter('app.boolean_as_string'));
52-
assertType("array('en', 'es', 'fr')", $container->getParameter('app.list'));
53-
assertType("array('en', 'es', 'fr')", $parameterBag->get('app.list'));
54-
assertType("array('en', 'es', 'fr')", $this->getParameter('app.list'));
55-
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $container->getParameter('app.list_of_list'));
56-
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $parameterBag->get('app.list_of_list'));
57-
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $this->getParameter('app.list_of_list'));
58-
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $container->getParameter('app.map'));
59-
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $parameterBag->get('app.map'));
60-
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $this->getParameter('app.map'));
61-
assertType("'This is a Bell char '", $container->getParameter('app.binary'));
62-
assertType("'This is a Bell char '", $parameterBag->get('app.binary'));
63-
assertType("'This is a Bell char '", $this->getParameter('app.binary'));
64-
assertType("'Y-m-d\\\\TH:i:sP'", $container->getParameter('app.constant'));
65-
assertType("'Y-m-d\\\\TH:i:sP'", $parameterBag->get('app.constant'));
66-
assertType("'Y-m-d\\\\TH:i:sP'", $this->getParameter('app.constant'));
31+
assertType("string", $container->getParameter('app.string'));
32+
assertType("string", $parameterBag->get('app.string'));
33+
assertType("string", $this->getParameter('app.string'));
34+
assertType('int', $container->getParameter('app.int'));
35+
assertType('int', $parameterBag->get('app.int'));
36+
assertType('int', $this->getParameter('app.int'));
37+
assertType("string", $container->getParameter('app.int_as_string'));
38+
assertType("string", $parameterBag->get('app.int_as_string'));
39+
assertType("string", $this->getParameter('app.int_as_string'));
40+
assertType('float', $container->getParameter('app.float'));
41+
assertType('float', $parameterBag->get('app.float'));
42+
assertType('float', $this->getParameter('app.float'));
43+
assertType("string", $container->getParameter('app.float_as_string'));
44+
assertType("string", $parameterBag->get('app.float_as_string'));
45+
assertType("string", $this->getParameter('app.float_as_string'));
46+
assertType('bool', $container->getParameter('app.boolean'));
47+
assertType('bool', $parameterBag->get('app.boolean'));
48+
assertType('bool', $this->getParameter('app.boolean'));
49+
assertType("string", $container->getParameter('app.boolean_as_string'));
50+
assertType("string", $parameterBag->get('app.boolean_as_string'));
51+
assertType("string", $this->getParameter('app.boolean_as_string'));
52+
assertType("array<int, string>&nonEmpty", $container->getParameter('app.list'));
53+
assertType("array<int, string>&nonEmpty", $parameterBag->get('app.list'));
54+
assertType("array<int, string>&nonEmpty", $this->getParameter('app.list'));
55+
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $container->getParameter('app.list_of_list'));
56+
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $parameterBag->get('app.list_of_list'));
57+
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $this->getParameter('app.list_of_list'));
58+
assertType("array<string, string>&nonEmpty", $container->getParameter('app.map'));
59+
assertType("array<string, string>&nonEmpty", $parameterBag->get('app.map'));
60+
assertType("array<string, string>&nonEmpty", $this->getParameter('app.map'));
61+
assertType("string", $container->getParameter('app.binary'));
62+
assertType("string", $parameterBag->get('app.binary'));
63+
assertType("string", $this->getParameter('app.binary'));
64+
assertType("string", $container->getParameter('app.constant'));
65+
assertType("string", $parameterBag->get('app.constant'));
66+
assertType("string", $this->getParameter('app.constant'));
6767

6868
assertType('false', $container->hasParameter('unknown'));
6969
assertType('false', $parameterBag->has('unknown'));

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

+36-36
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,42 @@ public function parameters(ContainerInterface $container, ParameterBagInterface
2828
assertType('array|bool|float|int|string|null', $container->getParameter('unknown'));
2929
assertType('array|bool|float|int|string|null', $parameterBag->get('unknown'));
3030
assertType('array|bool|float|int|string|null', $this->getParameter('unknown'));
31-
assertType("'abcdef'", $container->getParameter('app.string'));
32-
assertType("'abcdef'", $parameterBag->get('app.string'));
33-
assertType("'abcdef'", $this->getParameter('app.string'));
34-
assertType('123', $container->getParameter('app.int'));
35-
assertType('123', $parameterBag->get('app.int'));
36-
assertType('123', $this->getParameter('app.int'));
37-
assertType("'123'", $container->getParameter('app.int_as_string'));
38-
assertType("'123'", $parameterBag->get('app.int_as_string'));
39-
assertType("'123'", $this->getParameter('app.int_as_string'));
40-
assertType('123.45', $container->getParameter('app.float'));
41-
assertType('123.45', $parameterBag->get('app.float'));
42-
assertType('123.45', $this->getParameter('app.float'));
43-
assertType("'123.45'", $container->getParameter('app.float_as_string'));
44-
assertType("'123.45'", $parameterBag->get('app.float_as_string'));
45-
assertType("'123.45'", $this->getParameter('app.float_as_string'));
46-
assertType('true', $container->getParameter('app.boolean'));
47-
assertType('true', $parameterBag->get('app.boolean'));
48-
assertType('true', $this->getParameter('app.boolean'));
49-
assertType("'true'", $container->getParameter('app.boolean_as_string'));
50-
assertType("'true'", $parameterBag->get('app.boolean_as_string'));
51-
assertType("'true'", $this->getParameter('app.boolean_as_string'));
52-
assertType("array('en', 'es', 'fr')", $container->getParameter('app.list'));
53-
assertType("array('en', 'es', 'fr')", $parameterBag->get('app.list'));
54-
assertType("array('en', 'es', 'fr')", $this->getParameter('app.list'));
55-
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $container->getParameter('app.list_of_list'));
56-
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $parameterBag->get('app.list_of_list'));
57-
assertType("array(array('name' => 'the name', 'value' => 'the value'), array('name' => 'another name', 'value' => 'another value'))", $this->getParameter('app.list_of_list'));
58-
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $container->getParameter('app.map'));
59-
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $parameterBag->get('app.map'));
60-
assertType("array('a' => 'value of a', 'b' => 'value of b', 'c' => 'value of c')", $this->getParameter('app.map'));
61-
assertType("'This is a Bell char '", $container->getParameter('app.binary'));
62-
assertType("'This is a Bell char '", $parameterBag->get('app.binary'));
63-
assertType("'This is a Bell char '", $this->getParameter('app.binary'));
64-
assertType("'Y-m-d\\\\TH:i:sP'", $container->getParameter('app.constant'));
65-
assertType("'Y-m-d\\\\TH:i:sP'", $parameterBag->get('app.constant'));
66-
assertType("'Y-m-d\\\\TH:i:sP'", $this->getParameter('app.constant'));
31+
assertType("string", $container->getParameter('app.string'));
32+
assertType("string", $parameterBag->get('app.string'));
33+
assertType("string", $this->getParameter('app.string'));
34+
assertType('int', $container->getParameter('app.int'));
35+
assertType('int', $parameterBag->get('app.int'));
36+
assertType('int', $this->getParameter('app.int'));
37+
assertType("string", $container->getParameter('app.int_as_string'));
38+
assertType("string", $parameterBag->get('app.int_as_string'));
39+
assertType("string", $this->getParameter('app.int_as_string'));
40+
assertType('float', $container->getParameter('app.float'));
41+
assertType('float', $parameterBag->get('app.float'));
42+
assertType('float', $this->getParameter('app.float'));
43+
assertType("string", $container->getParameter('app.float_as_string'));
44+
assertType("string", $parameterBag->get('app.float_as_string'));
45+
assertType("string", $this->getParameter('app.float_as_string'));
46+
assertType('bool', $container->getParameter('app.boolean'));
47+
assertType('bool', $parameterBag->get('app.boolean'));
48+
assertType('bool', $this->getParameter('app.boolean'));
49+
assertType("string", $container->getParameter('app.boolean_as_string'));
50+
assertType("string", $parameterBag->get('app.boolean_as_string'));
51+
assertType("string", $this->getParameter('app.boolean_as_string'));
52+
assertType("array<int, string>&nonEmpty", $container->getParameter('app.list'));
53+
assertType("array<int, string>&nonEmpty", $parameterBag->get('app.list'));
54+
assertType("array<int, string>&nonEmpty", $this->getParameter('app.list'));
55+
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $container->getParameter('app.list_of_list'));
56+
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $parameterBag->get('app.list_of_list'));
57+
assertType("array<int, array<string, string>&nonEmpty>&nonEmpty", $this->getParameter('app.list_of_list'));
58+
assertType("array<string, string>&nonEmpty", $container->getParameter('app.map'));
59+
assertType("array<string, string>&nonEmpty", $parameterBag->get('app.map'));
60+
assertType("array<string, string>&nonEmpty", $this->getParameter('app.map'));
61+
assertType("string", $container->getParameter('app.binary'));
62+
assertType("string", $parameterBag->get('app.binary'));
63+
assertType("string", $this->getParameter('app.binary'));
64+
assertType("string", $container->getParameter('app.constant'));
65+
assertType("string", $parameterBag->get('app.constant'));
66+
assertType("string", $this->getParameter('app.constant'));
6767

6868
assertType('false', $container->hasParameter('unknown'));
6969
assertType('false', $parameterBag->has('unknown'));

0 commit comments

Comments
 (0)