Skip to content

Commit fa08709

Browse files
committed
Do not infer getArrayResult for now
1 parent 66d6943 commit fa08709

File tree

3 files changed

+15
-74
lines changed

3 files changed

+15
-74
lines changed

src/Type/Doctrine/HydrationModeReturnTypeResolver.php

-48
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
use PHPStan\Type\IntegerRangeType;
1111
use PHPStan\Type\IntegerType;
1212
use PHPStan\Type\IterableType;
13-
use PHPStan\Type\MixedType;
1413
use PHPStan\Type\ObjectWithoutClassType;
1514
use PHPStan\Type\Type;
1615
use PHPStan\Type\TypeCombinator;
17-
use PHPStan\Type\TypeTraverser;
1816
use PHPStan\Type\TypeUtils;
19-
use PHPStan\Type\TypeWithClassName;
2017
use PHPStan\Type\VoidType;
2118

2219
class HydrationModeReturnTypeResolver
@@ -47,9 +44,6 @@ public function getMethodReturnTypeForHydrationMode(
4744
switch ($hydrationMode) {
4845
case AbstractQuery::HYDRATE_OBJECT:
4946
break;
50-
case AbstractQuery::HYDRATE_ARRAY:
51-
$queryResultType = $this->getArrayHydratedReturnType($queryResultType, $objectManager);
52-
break;
5347
case AbstractQuery::HYDRATE_SIMPLEOBJECT:
5448
$queryResultType = $this->getSimpleObjectHydratedReturnType($queryResultType);
5549
break;
@@ -90,48 +84,6 @@ public function getMethodReturnTypeForHydrationMode(
9084
}
9185
}
9286

93-
/**
94-
* When we're array-hydrating object, we're not sure of the shape of the array.
95-
* We could return `new ArrayType(new MixedType(), new MixedType())`
96-
* but the lack of precision in the array keys/values would give false positive.
97-
*
98-
* @see https://github.com/phpstan/phpstan-doctrine/pull/412#issuecomment-1497092934
99-
*/
100-
private function getArrayHydratedReturnType(Type $queryResultType, ?ObjectManager $objectManager): ?Type
101-
{
102-
$mixedFound = false;
103-
$queryResultType = TypeTraverser::map(
104-
$queryResultType,
105-
static function (Type $type, callable $traverse) use ($objectManager, &$mixedFound): Type {
106-
$isObject = (new ObjectWithoutClassType())->isSuperTypeOf($type);
107-
if ($isObject->no()) {
108-
return $traverse($type);
109-
}
110-
if (
111-
$isObject->maybe()
112-
|| !$type instanceof TypeWithClassName
113-
|| $objectManager === null
114-
) {
115-
$mixedFound = true;
116-
117-
return new MixedType();
118-
}
119-
120-
/** @var class-string $className */
121-
$className = $type->getClassName();
122-
if (!$objectManager->getMetadataFactory()->hasMetadataFor($className)) {
123-
return $traverse($type);
124-
}
125-
126-
$mixedFound = true;
127-
128-
return new MixedType();
129-
}
130-
);
131-
132-
return $mixedFound ? null : $queryResultType;
133-
}
134-
13587
private function getSimpleObjectHydratedReturnType(Type $queryResultType): ?Type
13688
{
13789
if ((new ObjectWithoutClassType())->isSuperTypeOf($queryResultType)->yes()) {

src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php

+15-22
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ final class QueryResultDynamicReturnTypeExtension implements DynamicMethodReturn
2828
'getSingleResult' => 0,
2929
];
3030

31-
private const METHOD_HYDRATION_MODE = [
32-
'getArrayResult' => AbstractQuery::HYDRATE_ARRAY,
33-
];
34-
3531
/** @var ObjectMetadataResolver */
3632
private $objectMetadataResolver;
3733

@@ -54,8 +50,7 @@ public function getClass(): string
5450

5551
public function isMethodSupported(MethodReflection $methodReflection): bool
5652
{
57-
return isset(self::METHOD_HYDRATION_MODE_ARG[$methodReflection->getName()])
58-
|| isset(self::METHOD_HYDRATION_MODE[$methodReflection->getName()]);
53+
return isset(self::METHOD_HYDRATION_MODE_ARG[$methodReflection->getName()]);
5954
}
6055

6156
public function getTypeFromMethodCall(
@@ -66,25 +61,23 @@ public function getTypeFromMethodCall(
6661
{
6762
$methodName = $methodReflection->getName();
6863

69-
if (isset(self::METHOD_HYDRATION_MODE[$methodName])) {
70-
$hydrationMode = new ConstantIntegerType(self::METHOD_HYDRATION_MODE[$methodName]);
71-
} elseif (isset(self::METHOD_HYDRATION_MODE_ARG[$methodName])) {
72-
$argIndex = self::METHOD_HYDRATION_MODE_ARG[$methodName];
73-
$args = $methodCall->getArgs();
74-
75-
if (isset($args[$argIndex])) {
76-
$hydrationMode = $scope->getType($args[$argIndex]->value);
77-
} else {
78-
$parametersAcceptor = ParametersAcceptorSelector::selectSingle(
79-
$methodReflection->getVariants()
80-
);
81-
$parameter = $parametersAcceptor->getParameters()[$argIndex];
82-
$hydrationMode = $parameter->getDefaultValue() ?? new NullType();
83-
}
84-
} else {
64+
if (!isset(self::METHOD_HYDRATION_MODE_ARG[$methodName])) {
8565
throw new ShouldNotHappenException();
8666
}
8767

68+
$argIndex = self::METHOD_HYDRATION_MODE_ARG[$methodName];
69+
$args = $methodCall->getArgs();
70+
71+
if (isset($args[$argIndex])) {
72+
$hydrationMode = $scope->getType($args[$argIndex]->value);
73+
} else {
74+
$parametersAcceptor = ParametersAcceptorSelector::selectSingle(
75+
$methodReflection->getVariants()
76+
);
77+
$parameter = $parametersAcceptor->getParameters()[$argIndex];
78+
$hydrationMode = $parameter->getDefaultValue() ?? new NullType();
79+
}
80+
8881
$queryType = $scope->getType($methodCall->var);
8982

9083
if (!$hydrationMode instanceof ConstantIntegerType) {

tests/Type/Doctrine/data/QueryResult/queryResult.php

-4
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,6 @@ public function testReturnTypeOfQueryMethodsWithExplicitArrayHydrationMode(Entit
199199
'list<array{intColumn: int, stringNullColumn: string|null, datetimeColumn: DateTime}>',
200200
$query->getResult(AbstractQuery::HYDRATE_ARRAY)
201201
);
202-
assertType(
203-
'list<array{intColumn: int, stringNullColumn: string|null, datetimeColumn: DateTime}>',
204-
$query->getArrayResult()
205-
);
206202
assertType(
207203
'list<array{intColumn: int, stringNullColumn: string|null, datetimeColumn: DateTime}>',
208204
$query->execute(null, AbstractQuery::HYDRATE_ARRAY)

0 commit comments

Comments
 (0)