Skip to content

Commit c2d3f79

Browse files
committed
fix: prefer create the default type manually instead
1 parent 8a43848 commit c2d3f79

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
"phpstan/phpstan-phpunit": "^0.12.16",
2727
"phpstan/phpstan-strict-rules": "^0.12.5",
2828
"phpunit/phpunit": "^7.5.20",
29-
"symfony/console": "^4.0 || ^5.0",
3029
"symfony/config": "^4.2 || ^5.0",
31-
"symfony/framework-bundle": "^4.0 || ^5.0",
30+
"symfony/console": "^4.0 || ^5.0",
31+
"symfony/framework-bundle": "^4.4 || ^5.0",
3232
"symfony/http-foundation": "^4.0 || ^5.0",
3333
"symfony/messenger": "^4.2 || ^5.0",
3434
"symfony/serializer": "^4.0 || ^5.0"

src/Type/Symfony/ParameterDynamicReturnTypeExtension.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
use PHPStan\Reflection\ParametersAcceptorSelector;
99
use PHPStan\ShouldNotHappenException;
1010
use PHPStan\Symfony\ParameterMap;
11+
use PHPStan\Type\ArrayType;
12+
use PHPStan\Type\BooleanType;
1113
use PHPStan\Type\Constant\ConstantBooleanType;
1214
use PHPStan\Type\DynamicMethodReturnTypeExtension;
15+
use PHPStan\Type\FloatType;
16+
use PHPStan\Type\IntegerType;
17+
use PHPStan\Type\MixedType;
18+
use PHPStan\Type\NullType;
19+
use PHPStan\Type\StringType;
1320
use PHPStan\Type\Type;
21+
use PHPStan\Type\UnionType;
1422
use function in_array;
1523

1624
final class ParameterDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -71,7 +79,16 @@ private function getGetTypeFromMethodCall(
7179
Scope $scope
7280
): Type
7381
{
74-
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
82+
// We don't use the method's return type because this won't work properly with lowest and
83+
// highest versions of Symfony ("mixed" for lowest, "array|bool|float|integer|string|null" for highest).
84+
$returnType = new UnionType([
85+
new ArrayType(new MixedType(), new MixedType()),
86+
new BooleanType(),
87+
new FloatType(),
88+
new IntegerType(),
89+
new StringType(),
90+
new NullType(),
91+
]);
7592
if (!isset($methodCall->args[0])) {
7693
return $returnType;
7794
}

tests/Type/Symfony/data/ExampleAbstractController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function services(): void
2626
public function parameters(ContainerInterface $container, ParameterBagInterface $parameterBag): void
2727
{
2828
assertType('array|bool|float|int|string|null', $container->getParameter('unknown'));
29-
assertType('mixed', $parameterBag->get('unknown'));
29+
assertType('array|bool|float|int|string|null', $parameterBag->get('unknown'));
3030
assertType('array|bool|float|int|string|null', $this->getParameter('unknown'));
3131
assertType("'abcdef'", $container->getParameter('app.string'));
3232
assertType("'abcdef'", $parameterBag->get('app.string'));

0 commit comments

Comments
 (0)