Skip to content

Commit 8c6332e

Browse files
Introduce default hydration mode
1 parent 31a924b commit 8c6332e

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

src/Type/Doctrine/HydrationModeReturnTypeResolver.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Type\Doctrine;
44

55
use Doctrine\ORM\AbstractQuery;
6-
use Doctrine\Persistence\ObjectManager;
76
use PHPStan\Type\Accessory\AccessoryArrayListType;
87
use PHPStan\Type\ArrayType;
98
use PHPStan\Type\BenevolentUnionType;
@@ -25,7 +24,7 @@ public function getMethodReturnTypeForHydrationMode(
2524
Type $hydrationMode,
2625
Type $queryKeyType,
2726
Type $queryResultType,
28-
?ObjectManager $objectManager
27+
?Type $defaultHydrationModeType = null
2928
): ?Type
3029
{
3130
$isVoidType = (new VoidType())->isSuperTypeOf($queryResultType);
@@ -42,6 +41,10 @@ public function getMethodReturnTypeForHydrationMode(
4241
return null;
4342
}
4443

44+
if ($defaultHydrationModeType !== null && $hydrationMode->isNull()->yes()) {
45+
$hydrationMode = $defaultHydrationModeType;
46+
}
47+
4548
if (!$hydrationMode instanceof ConstantIntegerType) {
4649
return null;
4750
}

src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getTypeFromMethodCall(
8484
$hydrationMode,
8585
$queryType->getTemplateType(AbstractQuery::class, 'TKey'),
8686
$queryType->getTemplateType(AbstractQuery::class, 'TResult'),
87-
$this->objectMetadataResolver->getObjectManager(),
87+
$queryType->getTemplateType(AbstractQuery::class, 'THydrationMode'),
8888
);
8989
}
9090

stubs/ORM/AbstractQuery.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use Doctrine\ORM\NoResultException;
99
/**
1010
* @template-covariant TKey The type of column used in indexBy
1111
* @template-covariant TResult The type of results returned by this query in HYDRATE_OBJECT mode
12+
* @template-covariant THydrationMode of string|AbstractQuery::HYDRATE_* = AbstractQuery::HYDRATE_OBJECT The default hydration mode when none is provided
1213
*/
1314
abstract class AbstractQuery
1415
{
@@ -84,4 +85,13 @@ abstract class AbstractQuery
8485
{
8586
}
8687

88+
/**
89+
* @template TNewHydrationMode of string|AbstractQuery::HYDRATE_*
90+
* @param TNewHydrationMode $hydrationMode
91+
* @phpstan-self-out self<TKey, TResult, TNewHydrationMode>
92+
*/
93+
public function setHydrationMode(string|int $hydrationMode): static
94+
{
95+
}
96+
8797
}

stubs/ORM/Query.stub

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ namespace Doctrine\ORM;
55
/**
66
* @template-covariant TKey The type of column used in indexBy
77
* @template-covariant TResult The type of results returned by this query in HYDRATE_OBJECT mode
8+
* @template-covariant THydrationMode of string|AbstractQuery::HYDRATE_* = AbstractQuery::HYDRATE_OBJECT The default hydration mode when none is provided
89
*
9-
* @extends AbstractQuery<TKey, TResult>
10+
* @extends AbstractQuery<TKey, TResult, THydrationMode>
1011
*/
1112
final class Query extends AbstractQuery
1213
{

tests/Type/Doctrine/Query/QueryResultTypeWalkerHydrationModeTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ public function test(Type $expectedType, string $dql, string $methodName, ?int $
9191
new ConstantIntegerType($this->getRealHydrationMode($methodName, $hydrationMode)),
9292
$typeBuilder->getIndexType(),
9393
$typeBuilder->getResultType(),
94-
$entityManager,
9594
) ?? new MixedType();
9695

9796
self::assertSame(

0 commit comments

Comments
 (0)