2222use Doctrine \ORM \Query \QueryException ;
2323use Doctrine \ORM \Query \ResultSetMapping ;
2424use Doctrine \Persistence \Mapping \MappingException ;
25+ use LogicException ;
2526use Psr \Cache \CacheItemPoolInterface ;
2627use 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 )) {
0 commit comments