Skip to content

Commit 4058fdc

Browse files
janedbalondrejmirtes
authored andcommittedMay 15, 2024·
Return null from dynamic return type extension instead of copying default type
1 parent 4b66f5c commit 4058fdc

6 files changed

+25
-51
lines changed
 

‎src/Type/Doctrine/Query/QueryGetDqlDynamicReturnTypeExtension.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8-
use PHPStan\Reflection\ParametersAcceptorSelector;
98
use PHPStan\Type\Constant\ConstantStringType;
109
use PHPStan\Type\Doctrine\DoctrineTypeUtils;
1110
use PHPStan\Type\DynamicMethodReturnTypeExtension;
@@ -30,16 +29,12 @@ public function getTypeFromMethodCall(
3029
MethodReflection $methodReflection,
3130
MethodCall $methodCall,
3231
Scope $scope
33-
): Type
32+
): ?Type
3433
{
3534
$calledOnType = $scope->getType($methodCall->var);
3635
$queryTypes = DoctrineTypeUtils::getQueryTypes($calledOnType);
3736
if (count($queryTypes) === 0) {
38-
return ParametersAcceptorSelector::selectFromArgs(
39-
$scope,
40-
$methodCall->getArgs(),
41-
$methodReflection->getVariants()
42-
)->getReturnType();
37+
return null;
4338
}
4439

4540
$dqls = [];

‎src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php

+4-13
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getTypeFromMethodCall(
4646
MethodReflection $methodReflection,
4747
MethodCall $methodCall,
4848
Scope $scope
49-
): Type
49+
): ?Type
5050
{
5151
$methodName = $methodReflection->getName();
5252

@@ -82,7 +82,7 @@ private function getMethodReturnTypeForHydrationMode(
8282
Type $hydrationMode,
8383
Type $queryKeyType,
8484
Type $queryResultType
85-
): Type
85+
): ?Type
8686
{
8787
$isVoidType = (new VoidType())->isSuperTypeOf($queryResultType);
8888

@@ -95,13 +95,13 @@ private function getMethodReturnTypeForHydrationMode(
9595
if ($isVoidType->maybe()) {
9696
// We can't be sure what the query type is, so we return the
9797
// declared return type of the method.
98-
return $this->originalReturnType($methodReflection);
98+
return null;
9999
}
100100

101101
if (!$this->isObjectHydrationMode($hydrationMode)) {
102102
// We support only HYDRATE_OBJECT. For other hydration modes, we
103103
// return the declared return type of the method.
104-
return $this->originalReturnType($methodReflection);
104+
return null;
105105
}
106106

107107
switch ($methodReflection->getName()) {
@@ -137,13 +137,4 @@ private function isObjectHydrationMode(Type $type): bool
137137
return $type->getValue() === AbstractQuery::HYDRATE_OBJECT;
138138
}
139139

140-
private function originalReturnType(MethodReflection $methodReflection): Type
141-
{
142-
$parametersAcceptor = ParametersAcceptorSelector::selectSingle(
143-
$methodReflection->getVariants()
144-
);
145-
146-
return $parametersAcceptor->getReturnType();
147-
}
148-
149140
}

‎src/Type/Doctrine/QueryBuilder/EntityRepositoryCreateQueryBuilderDynamicReturnTypeExtension.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\Scalar\String_;
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Reflection\MethodReflection;
11-
use PHPStan\Reflection\ParametersAcceptorSelector;
1211
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1312
use PHPStan\Type\Type;
1413
use function array_unshift;
@@ -31,7 +30,7 @@ public function getTypeFromMethodCall(
3130
MethodReflection $methodReflection,
3231
MethodCall $methodCall,
3332
Scope $scope
34-
): Type
33+
): ?Type
3534
{
3635
$entityNameExpr = new MethodCall($methodCall->var, new Identifier('getEntityName'));
3736

@@ -41,7 +40,7 @@ public function getTypeFromMethodCall(
4140
}
4241

4342
if (!isset($methodCall->getArgs()[0])) {
44-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
43+
return null;
4544
}
4645

4746
$fromArgs = $methodCall->getArgs();

‎src/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtension.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8-
use PHPStan\Reflection\ParametersAcceptorSelector;
98
use PHPStan\Rules\Doctrine\ORM\DynamicQueryBuilderArgumentException;
109
use PHPStan\Type\Doctrine\ArgumentsProcessor;
1110
use PHPStan\Type\DynamicMethodReturnTypeExtension;
@@ -38,25 +37,23 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3837
return in_array($methodReflection->getName(), ['add', 'addMultiple'], true);
3938
}
4039

41-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
40+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4241
{
43-
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
44-
4542
try {
4643
$args = $this->argumentsProcessor->processArgs($scope, $methodReflection->getName(), $methodCall->getArgs());
4744
} catch (DynamicQueryBuilderArgumentException $e) {
48-
return $defaultReturnType;
45+
return null;
4946
}
5047

5148
$calledOnType = $scope->getType($methodCall->var);
5249
if (!$calledOnType instanceof ExprType) {
53-
return $defaultReturnType;
50+
return null;
5451
}
5552

5653
$expr = $calledOnType->getExprObject();
5754

5855
if (!method_exists($expr, $methodReflection->getName())) {
59-
return $defaultReturnType;
56+
return null;
6057
}
6158

6259
$exprValue = $expr->{$methodReflection->getName()}(...$args);

‎src/Type/Doctrine/QueryBuilder/Expr/ExpressionBuilderDynamicReturnTypeExtension.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node\Expr\MethodCall;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Reflection\MethodReflection;
9-
use PHPStan\Reflection\ParametersAcceptorSelector;
109
use PHPStan\Rules\Doctrine\ORM\DynamicQueryBuilderArgumentException;
1110
use PHPStan\Type\Doctrine\ArgumentsProcessor;
1211
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
@@ -44,17 +43,15 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4443
return true;
4544
}
4645

47-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
46+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4847
{
49-
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
50-
5148
$objectManager = $this->objectMetadataResolver->getObjectManager();
5249
if ($objectManager === null) {
53-
return $defaultReturnType;
50+
return null;
5451
}
5552
$entityManagerInterface = 'Doctrine\ORM\EntityManagerInterface';
5653
if (!$objectManager instanceof $entityManagerInterface) {
57-
return $defaultReturnType;
54+
return null;
5855
}
5956

6057
/** @var EntityManagerInterface $objectManager */
@@ -65,7 +62,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6562
try {
6663
$args = $this->argumentsProcessor->processArgs($scope, $methodReflection->getName(), $methodCall->getArgs());
6764
} catch (DynamicQueryBuilderArgumentException $e) {
68-
return $defaultReturnType;
65+
return null;
6966
}
7067

7168
$calledOnType = $scope->getType($methodCall->var);
@@ -76,7 +73,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
7673
}
7774

7875
if (!method_exists($expr, $methodReflection->getName())) {
79-
return $defaultReturnType;
76+
return null;
8077
}
8178

8279
$exprValue = $expr->{$methodReflection->getName()}(...$args);

‎src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php

+8-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PhpParser\Node\Identifier;
1313
use PHPStan\Analyser\Scope;
1414
use PHPStan\Reflection\MethodReflection;
15-
use PHPStan\Reflection\ParametersAcceptorSelector;
1615
use PHPStan\Rules\Doctrine\ORM\DynamicQueryBuilderArgumentException;
1716
use PHPStan\Type\Doctrine\ArgumentsProcessor;
1817
use PHPStan\Type\Doctrine\DescriptorRegistry;
@@ -93,26 +92,22 @@ public function getTypeFromMethodCall(
9392
MethodReflection $methodReflection,
9493
MethodCall $methodCall,
9594
Scope $scope
96-
): Type
95+
): ?Type
9796
{
9897
$calledOnType = $scope->getType($methodCall->var);
99-
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
100-
$scope,
101-
$methodCall->getArgs(),
102-
$methodReflection->getVariants()
103-
)->getReturnType();
98+
10499
$queryBuilderTypes = DoctrineTypeUtils::getQueryBuilderTypes($calledOnType);
105100
if (count($queryBuilderTypes) === 0) {
106-
return $defaultReturnType;
101+
return null;
107102
}
108103

109104
$objectManager = $this->objectMetadataResolver->getObjectManager();
110105
if ($objectManager === null) {
111-
return $defaultReturnType;
106+
return null;
112107
}
113108
$entityManagerInterface = 'Doctrine\ORM\EntityManagerInterface';
114109
if (!$objectManager instanceof $entityManagerInterface) {
115-
return $defaultReturnType;
110+
return null;
116111
}
117112

118113
/** @var EntityManagerInterface $objectManager */
@@ -150,7 +145,7 @@ public function getTypeFromMethodCall(
150145
try {
151146
$args = $this->argumentsProcessor->processArgs($scope, $methodName, array_slice($calledMethodCall->getArgs(), 0, 1));
152147
} catch (DynamicQueryBuilderArgumentException $e) {
153-
return $defaultReturnType;
148+
return null;
154149
}
155150
if (count($args) === 1) {
156151
$queryBuilder->set($args[0], $args[0]);
@@ -168,13 +163,13 @@ public function getTypeFromMethodCall(
168163
if (in_array($lowerMethodName, self::METHODS_NOT_AFFECTING_RESULT_TYPE, true)) {
169164
continue;
170165
}
171-
return $defaultReturnType;
166+
return null;
172167
}
173168

174169
try {
175170
$queryBuilder->{$methodName}(...$args);
176171
} catch (Throwable $e) {
177-
return $defaultReturnType;
172+
return null;
178173
}
179174
}
180175

0 commit comments

Comments
 (0)
Please sign in to comment.