Skip to content

Commit ec49b7a

Browse files
committed
Query generic type fixes
1 parent 636f9dc commit ec49b7a

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
use PHPStan\Type\ArrayType;
1212
use PHPStan\Type\Constant\ConstantIntegerType;
1313
use PHPStan\Type\DynamicMethodReturnTypeExtension;
14-
use PHPStan\Type\Generic\GenericObjectType;
14+
use PHPStan\Type\GenericTypeVariableResolver;
1515
use PHPStan\Type\IntegerType;
1616
use PHPStan\Type\IterableType;
1717
use PHPStan\Type\MixedType;
1818
use PHPStan\Type\NullType;
1919
use PHPStan\Type\Type;
2020
use PHPStan\Type\TypeCombinator;
21+
use PHPStan\Type\TypeWithClassName;
2122
use PHPStan\Type\VoidType;
2223

2324
final class QueryResultDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -80,13 +81,16 @@ public function getTypeFromMethodCall(
8081

8182
private function getQueryResultType(Type $queryType): Type
8283
{
83-
if (!$queryType instanceof GenericObjectType) {
84+
if (!$queryType instanceof TypeWithClassName) {
8485
return new MixedType();
8586
}
8687

87-
$types = $queryType->getTypes();
88+
$resultType = GenericTypeVariableResolver::getType($queryType, AbstractQuery::class, 'TResult');
89+
if ($resultType === null) {
90+
return new MixedType();
91+
}
8892

89-
return $types[0] ?? new MixedType();
93+
return $resultType;
9094
}
9195

9296
private function getMethodReturnTypeForHydrationMode(

stubs/ORM/AbstractQuery.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Doctrine\ORM;
55
use Doctrine\Common\Collections\ArrayCollection;
66

77
/**
8-
* @template TResult The type of results returned by this query in HYDRATE_OBJECT mode
8+
* @template-covariant TResult The type of results returned by this query in HYDRATE_OBJECT mode
99
*/
1010
abstract class AbstractQuery
1111
{

stubs/ORM/Query.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Doctrine\ORM;
44

55
/**
6-
* @template TResult The type of results returned by this query in HYDRATE_OBJECT mode
6+
* @template-covariant TResult The type of results returned by this query in HYDRATE_OBJECT mode
77
*
88
* @extends AbstractQuery<TResult>
99
*/

0 commit comments

Comments
 (0)