Skip to content

Commit 1e97742

Browse files
authored
Fix type errors in AbstractQuery and QueryBuilder (doctrine#9275)
1 parent 4117ca3 commit 1e97742

File tree

8 files changed

+81
-59
lines changed

8 files changed

+81
-59
lines changed

lib/Doctrine/ORM/AbstractQuery.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Doctrine\ORM\Query\QueryException;
2323
use Doctrine\ORM\Query\ResultSetMapping;
2424
use Doctrine\Persistence\Mapping\MappingException;
25+
use LogicException;
2526
use Psr\Cache\CacheItemPoolInterface;
2627
use Traversable;
2728

@@ -91,7 +92,7 @@ abstract class AbstractQuery
9192
/**
9293
* The user-specified ResultSetMapping to use.
9394
*
94-
* @var ResultSetMapping
95+
* @var ResultSetMapping|null
9596
*/
9697
protected $_resultSetMapping;
9798

@@ -113,6 +114,7 @@ abstract class AbstractQuery
113114
* The hydration mode.
114115
*
115116
* @var string|int
117+
* @psalm-var string|AbstractQuery::HYDRATE_*
116118
*/
117119
protected $_hydrationMode = self::HYDRATE_OBJECT;
118120

@@ -150,6 +152,7 @@ abstract class AbstractQuery
150152
* Second level query cache mode.
151153
*
152154
* @var int|null
155+
* @psalm-var Cache::MODE_*|null
153156
*/
154157
protected $cacheMode;
155158

@@ -251,7 +254,8 @@ public function setLifetime($lifetime)
251254
}
252255

253256
/**
254-
* @return int
257+
* @return int|null
258+
* @psalm-return Cache::MODE_*|null
255259
*/
256260
public function getCacheMode()
257261
{
@@ -260,6 +264,7 @@ public function getCacheMode()
260264

261265
/**
262266
* @param int $cacheMode
267+
* @psalm-param Cache::MODE_* $cacheMode
263268
*
264269
* @return $this
265270
*/
@@ -492,7 +497,7 @@ public function setResultSetMapping(Query\ResultSetMapping $rsm)
492497
/**
493498
* Gets the ResultSetMapping used for hydration.
494499
*
495-
* @return ResultSetMapping
500+
* @return ResultSetMapping|null
496501
*/
497502
protected function getResultSetMapping()
498503
{
@@ -829,6 +834,7 @@ public function setFetchMode($class, $assocName, $fetchMode)
829834
*
830835
* @param string|int $hydrationMode Doctrine processing mode to be used during hydration process.
831836
* One of the Query::HYDRATE_* constants.
837+
* @psalm-param string|AbstractQuery::HYDRATE_* $hydrationMode
832838
*
833839
* @return $this
834840
*/
@@ -843,6 +849,7 @@ public function setHydrationMode($hydrationMode)
843849
* Gets the hydration mode currently used by the query.
844850
*
845851
* @return string|int
852+
* @psalm-return string|AbstractQuery::HYDRATE_*
846853
*/
847854
public function getHydrationMode()
848855
{
@@ -855,6 +862,7 @@ public function getHydrationMode()
855862
* Alias for execute(null, $hydrationMode = HYDRATE_OBJECT).
856863
*
857864
* @param string|int $hydrationMode
865+
* @psalm-param string|AbstractQuery::HYDRATE_* $hydrationMode
858866
*
859867
* @return mixed
860868
*/
@@ -902,7 +910,8 @@ public function getScalarResult()
902910
/**
903911
* Get exactly one result or null.
904912
*
905-
* @param string|int $hydrationMode
913+
* @param string|int|null $hydrationMode
914+
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
906915
*
907916
* @return mixed
908917
*
@@ -939,7 +948,8 @@ public function getOneOrNullResult($hydrationMode = null)
939948
* If the result is not unique, a NonUniqueResultException is thrown.
940949
* If there is no result, a NoResultException is thrown.
941950
*
942-
* @param string|int $hydrationMode
951+
* @param string|int|null $hydrationMode
952+
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
943953
*
944954
* @return mixed
945955
*
@@ -1037,6 +1047,7 @@ public function getHints()
10371047
*
10381048
* @param ArrayCollection|mixed[]|null $parameters The query parameters.
10391049
* @param string|int|null $hydrationMode The hydration mode to use.
1050+
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode The hydration mode to use.
10401051
*
10411052
* @return IterableResult
10421053
*/
@@ -1057,7 +1068,11 @@ public function iterate($parameters = null, $hydrationMode = null)
10571068
$this->setParameters($parameters);
10581069
}
10591070

1060-
$rsm = $this->getResultSetMapping();
1071+
$rsm = $this->getResultSetMapping();
1072+
if ($rsm === null) {
1073+
throw new LogicException('Uninitialized result set mapping.');
1074+
}
1075+
10611076
$stmt = $this->_doExecute();
10621077

10631078
return $this->_em->newHydrator($this->_hydrationMode)->iterate($stmt, $rsm, $this->_hints);
@@ -1070,6 +1085,7 @@ public function iterate($parameters = null, $hydrationMode = null)
10701085
* @param ArrayCollection|array|mixed[] $parameters The query parameters.
10711086
* @param string|int|null $hydrationMode The hydration mode to use.
10721087
* @psalm-param ArrayCollection<int, Parameter>|mixed[] $parameters
1088+
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
10731089
*
10741090
* @return iterable<mixed>
10751091
*/
@@ -1087,6 +1103,9 @@ public function toIterable(iterable $parameters = [], $hydrationMode = null): it
10871103
}
10881104

10891105
$rsm = $this->getResultSetMapping();
1106+
if ($rsm === null) {
1107+
throw new LogicException('Uninitialized result set mapping.');
1108+
}
10901109

10911110
if ($rsm->isMixed && count($rsm->scalarMappings) > 0) {
10921111
throw QueryException::iterateWithMixedResultNotAllowed();
@@ -1103,6 +1122,7 @@ public function toIterable(iterable $parameters = [], $hydrationMode = null): it
11031122
* @param ArrayCollection|mixed[]|null $parameters Query parameters.
11041123
* @param string|int|null $hydrationMode Processing mode to be used during the hydration process.
11051124
* @psalm-param ArrayCollection<int, Parameter>|mixed[]|null $parameters
1125+
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
11061126
*
11071127
* @return mixed
11081128
*/
@@ -1121,6 +1141,7 @@ public function execute($parameters = null, $hydrationMode = null)
11211141
* @param ArrayCollection|mixed[]|null $parameters
11221142
* @param string|int|null $hydrationMode
11231143
* @psalm-param ArrayCollection<int, Parameter>|mixed[]|null $parameters
1144+
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
11241145
*
11251146
* @return mixed
11261147
*/
@@ -1165,7 +1186,11 @@ private function executeIgnoreQueryCache($parameters = null, $hydrationMode = nu
11651186
return $stmt;
11661187
}
11671188

1168-
$rsm = $this->getResultSetMapping();
1189+
$rsm = $this->getResultSetMapping();
1190+
if ($rsm === null) {
1191+
throw new LogicException('Uninitialized result set mapping.');
1192+
}
1193+
11691194
$data = $this->_em->newHydrator($this->_hydrationMode)->hydrateAll($stmt, $rsm, $this->_hints);
11701195

11711196
$setCacheEntry($data);
@@ -1197,12 +1222,17 @@ private function getHydrationCache(): CacheItemPoolInterface
11971222
* @param ArrayCollection|mixed[]|null $parameters
11981223
* @param string|int|null $hydrationMode
11991224
* @psalm-param ArrayCollection<int, Parameter>|mixed[]|null $parameters
1225+
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
12001226
*
12011227
* @return mixed
12021228
*/
12031229
private function executeUsingQueryCache($parameters = null, $hydrationMode = null)
12041230
{
1205-
$rsm = $this->getResultSetMapping();
1231+
$rsm = $this->getResultSetMapping();
1232+
if ($rsm === null) {
1233+
throw new LogicException('Uninitialized result set mapping.');
1234+
}
1235+
12061236
$queryCache = $this->_em->getCache()->getQueryCache($this->cacheRegion);
12071237
$queryKey = new QueryCacheKey(
12081238
$this->getHash(),
@@ -1237,6 +1267,7 @@ private function executeUsingQueryCache($parameters = null, $hydrationMode = nul
12371267

12381268
private function getTimestampKey(): ?TimestampCacheKey
12391269
{
1270+
assert($this->_resultSetMapping !== null);
12401271
$entityName = reset($this->_resultSetMapping->aliasMap);
12411272

12421273
if (empty($entityName)) {

lib/Doctrine/ORM/Cache/QueryCacheKey.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ class QueryCacheKey extends CacheKey
1919
public $lifetime;
2020

2121
/**
22+
* Cache mode
23+
*
2224
* READ-ONLY: Public only for performance reasons, it should be considered immutable.
2325
*
24-
* @var int Cache mode (Doctrine\ORM\Cache::MODE_*)
26+
* @var int
27+
* @psalm-var Cache::MODE_*
2528
*/
2629
public $cacheMode;
2730

@@ -32,6 +35,9 @@ class QueryCacheKey extends CacheKey
3235
*/
3336
public $timestampKey;
3437

38+
/**
39+
* @psalm-param Cache::MODE_* $cacheMode
40+
*/
3541
public function __construct(
3642
string $cacheId,
3743
int $lifetime = 0,

lib/Doctrine/ORM/Configuration.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Doctrine\ORM\Exception\NamedQueryNotFound;
2626
use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating;
2727
use Doctrine\ORM\Exception\UnknownEntityNamespace;
28+
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
2829
use Doctrine\ORM\Mapping\ClassMetadataFactory;
2930
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
3031
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
@@ -712,7 +713,7 @@ public function setCustomDatetimeFunctions(array $functions)
712713
/**
713714
* Sets the custom hydrator modes in one pass.
714715
*
715-
* @param array<string, class-string> $modes An array of ($modeName => $hydrator).
716+
* @param array<string, class-string<AbstractHydrator>> $modes An array of ($modeName => $hydrator).
716717
*
717718
* @return void
718719
*/
@@ -731,7 +732,7 @@ public function setCustomHydrationModes($modes)
731732
* @param string $modeName The hydration mode name.
732733
*
733734
* @return string|null The hydrator class name.
734-
* @psalm-return ?class-string
735+
* @psalm-return class-string<AbstractHydrator>|null
735736
*/
736737
public function getCustomHydrationMode($modeName)
737738
{
@@ -743,7 +744,7 @@ public function getCustomHydrationMode($modeName)
743744
*
744745
* @param string $modeName The hydration mode name.
745746
* @param string $hydrator The hydrator class name.
746-
* @psalm-param class-string $hydrator
747+
* @psalm-param class-string<AbstractHydrator> $hydrator
747748
*
748749
* @return void
749750
*/

lib/Doctrine/ORM/EntityManagerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ public function getUnitOfWork();
284284
* @deprecated
285285
*
286286
* @param string|int $hydrationMode
287+
* @psalm-param string|AbstractQuery::HYDRATE_* $hydrationMode
287288
*
288289
* @return AbstractHydrator
289290
*/
@@ -293,6 +294,7 @@ public function getHydrator($hydrationMode);
293294
* Create a new instance for the given hydration mode.
294295
*
295296
* @param string|int $hydrationMode
297+
* @psalm-param string|AbstractQuery::HYDRATE_* $hydrationMode
296298
*
297299
* @return AbstractHydrator
298300
*

lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ final protected function resultSetMapping(): ResultSetMapping
228228
* Hydrates all rows returned by the passed statement instance at once.
229229
*
230230
* @param Result|ResultStatement $stmt
231-
* @param object $resultSetMapping
231+
* @param ResultSetMapping $resultSetMapping
232232
* @psalm-param array<string, string> $hints
233233
*
234234
* @return mixed[]

lib/Doctrine/ORM/Query.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Doctrine\ORM\Query\Parser;
2424
use Doctrine\ORM\Query\ParserResult;
2525
use Doctrine\ORM\Query\QueryException;
26+
use Doctrine\ORM\Query\ResultSetMapping;
2627
use Doctrine\ORM\Utility\HierarchyDiscriminatorResolver;
2728
use Psr\Cache\CacheItemPoolInterface;
2829

@@ -210,6 +211,8 @@ public function getAST()
210211

211212
/**
212213
* {@inheritdoc}
214+
*
215+
* @return ResultSetMapping
213216
*/
214217
protected function getResultSetMapping()
215218
{
@@ -442,8 +445,6 @@ private function resolveParameterValue(Parameter $parameter): array
442445
$value = $originalValue;
443446
$rsm = $this->getResultSetMapping();
444447

445-
assert($rsm !== null);
446-
447448
if ($value instanceof ClassMetadata && isset($rsm->metadataParameterMapping[$key])) {
448449
$value = $value->getMetadataValue($rsm->metadataParameterMapping[$key]);
449450
}
@@ -709,6 +710,7 @@ public function getMaxResults(): ?int
709710
* @param ArrayCollection|mixed[]|null $parameters The query parameters.
710711
* @param string|int $hydrationMode The hydration mode to use.
711712
* @psalm-param ArrayCollection<int, Parameter>|array<string, mixed>|null $parameters
713+
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
712714
*/
713715
public function iterate($parameters = null, $hydrationMode = self::HYDRATE_OBJECT): IterableResult
714716
{

lib/Doctrine/ORM/QueryBuilder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,22 @@ class QueryBuilder
7474
* The type of query this is. Can be select, update or delete.
7575
*
7676
* @var int
77+
* @psalm-var self::SELECT|self::DELETE|self::UPDATE
7778
*/
7879
private $_type = self::SELECT;
7980

8081
/**
8182
* The state of the query object. Can be dirty or clean.
8283
*
8384
* @var int
85+
* @psalm-var self::STATE_*
8486
*/
8587
private $_state = self::STATE_CLEAN;
8688

8789
/**
8890
* The complete DQL string for this query.
8991
*
90-
* @var string
92+
* @var string|null
9193
*/
9294
private $_dql;
9395

@@ -138,6 +140,7 @@ class QueryBuilder
138140
* Second level query cache mode.
139141
*
140142
* @var int|null
143+
* @psalm-var Cache::MODE_*|null
141144
*/
142145
protected $cacheMode;
143146

@@ -244,7 +247,8 @@ public function setLifetime($lifetime)
244247
}
245248

246249
/**
247-
* @return int
250+
* @return int|null
251+
* @psalm-return Cache::MODE_*|null
248252
*/
249253
public function getCacheMode()
250254
{
@@ -253,6 +257,7 @@ public function getCacheMode()
253257

254258
/**
255259
* @param int $cacheMode
260+
* @psalm-param Cache::MODE_* $cacheMode
256261
*
257262
* @return $this
258263
*/
@@ -267,6 +272,7 @@ public function setCacheMode($cacheMode)
267272
* Gets the type of the currently built query.
268273
*
269274
* @return int
275+
* @psalm-return self::SELECT|self::DELETE|self::UPDATE
270276
*/
271277
public function getType()
272278
{
@@ -287,6 +293,7 @@ public function getEntityManager()
287293
* Gets the state of this query builder instance.
288294
*
289295
* @return int Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN.
296+
* @psalm-return self::STATE_*
290297
*/
291298
public function getState()
292299
{

0 commit comments

Comments
 (0)