2525use PHPStan \Type \TypeUtils ;
2626use PHPStan \Type \TypeWithClassName ;
2727use PHPStan \Type \VoidType ;
28- use function count ;
2928
3029final class QueryResultDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
3130{
@@ -42,9 +41,6 @@ final class QueryResultDynamicReturnTypeExtension implements DynamicMethodReturn
4241
4342 private const METHOD_HYDRATION_MODE = [
4443 'getArrayResult ' => AbstractQuery::HYDRATE_ARRAY ,
45- 'getScalarResult ' => AbstractQuery::HYDRATE_SCALAR ,
46- 'getSingleColumnResult ' => AbstractQuery::HYDRATE_SCALAR_COLUMN ,
47- 'getSingleScalarResult ' => AbstractQuery::HYDRATE_SINGLE_SCALAR ,
4844 ];
4945
5046 /** @var ObjectMetadataResolver */
@@ -130,26 +126,15 @@ private function getMethodReturnTypeForHydrationMode(
130126 return null ;
131127 }
132128
133- $ singleResult = false ;
134129 switch ($ hydrationMode ->getValue ()) {
135130 case AbstractQuery::HYDRATE_OBJECT :
136131 break ;
137132 case AbstractQuery::HYDRATE_ARRAY :
138133 $ queryResultType = $ this ->getArrayHydratedReturnType ($ queryResultType );
139134 break ;
140- case AbstractQuery::HYDRATE_SCALAR :
141- $ queryResultType = $ this ->getScalarHydratedReturnType ($ queryResultType );
142- break ;
143- case AbstractQuery::HYDRATE_SINGLE_SCALAR :
144- $ singleResult = true ;
145- $ queryResultType = $ this ->getSingleScalarHydratedReturnType ($ queryResultType );
146- break ;
147135 case AbstractQuery::HYDRATE_SIMPLEOBJECT :
148136 $ queryResultType = $ this ->getSimpleObjectHydratedReturnType ($ queryResultType );
149137 break ;
150- case AbstractQuery::HYDRATE_SCALAR_COLUMN :
151- $ queryResultType = $ this ->getScalarColumnHydratedReturnType ($ queryResultType );
152- break ;
153138 default :
154139 return null ;
155140 }
@@ -174,10 +159,6 @@ private function getMethodReturnTypeForHydrationMode(
174159 $ queryResultType
175160 );
176161 default :
177- if ($ singleResult ) {
178- return $ queryResultType ;
179- }
180-
181162 if ($ queryKeyType ->isNull ()->yes ()) {
182163 return AccessoryArrayListType::intersectWith (new ArrayType (
183164 new IntegerType (),
@@ -235,36 +216,6 @@ static function (Type $type, callable $traverse) use ($objectManager, &$mixedFou
235216 return $ mixedFound ? null : $ queryResultType ;
236217 }
237218
238- /**
239- * When we're scalar-hydrating object, we're not sure of the shape of the array.
240- * We could return `new ArrayTyp(new MixedType(), new MixedType())`
241- * but the lack of precision in the array keys/values would give false positive.
242- *
243- * @see https://github.com/phpstan/phpstan-doctrine/pull/453#issuecomment-1895415544
244- */
245- private function getScalarHydratedReturnType (Type $ queryResultType ): ?Type
246- {
247- if (!$ queryResultType ->isArray ()->yes ()) {
248- return null ;
249- }
250-
251- foreach ($ queryResultType ->getArrays () as $ arrayType ) {
252- $ itemType = $ arrayType ->getItemType ();
253-
254- if (
255- !(new ObjectWithoutClassType ())->isSuperTypeOf ($ itemType )->no ()
256- || !$ itemType ->isArray ()->no ()
257- ) {
258- // We could return `new ArrayTyp(new MixedType(), new MixedType())`
259- // but the lack of precision in the array keys/values would give false positive
260- // @see https://github.com/phpstan/phpstan-doctrine/pull/453#issuecomment-1895415544
261- return null ;
262- }
263- }
264-
265- return $ queryResultType ;
266- }
267-
268219 private function getSimpleObjectHydratedReturnType (Type $ queryResultType ): ?Type
269220 {
270221 if ((new ObjectWithoutClassType ())->isSuperTypeOf ($ queryResultType )->yes ()) {
@@ -274,44 +225,4 @@ private function getSimpleObjectHydratedReturnType(Type $queryResultType): ?Type
274225 return null ;
275226 }
276227
277- private function getSingleScalarHydratedReturnType (Type $ queryResultType ): ?Type
278- {
279- $ queryResultType = $ this ->getScalarHydratedReturnType ($ queryResultType );
280- if ($ queryResultType === null || !$ queryResultType ->isConstantArray ()->yes ()) {
281- return null ;
282- }
283-
284- $ types = [];
285- foreach ($ queryResultType ->getConstantArrays () as $ constantArrayType ) {
286- $ values = $ constantArrayType ->getValueTypes ();
287- if (count ($ values ) !== 1 ) {
288- return null ;
289- }
290-
291- $ types [] = $ constantArrayType ->getFirstIterableValueType ();
292- }
293-
294- return TypeCombinator::union (...$ types );
295- }
296-
297- private function getScalarColumnHydratedReturnType (Type $ queryResultType ): ?Type
298- {
299- $ queryResultType = $ this ->getScalarHydratedReturnType ($ queryResultType );
300- if ($ queryResultType === null || !$ queryResultType ->isConstantArray ()->yes ()) {
301- return null ;
302- }
303-
304- $ types = [];
305- foreach ($ queryResultType ->getConstantArrays () as $ constantArrayType ) {
306- $ values = $ constantArrayType ->getValueTypes ();
307- if (count ($ values ) !== 1 ) {
308- return null ;
309- }
310-
311- $ types [] = $ constantArrayType ->getFirstIterableValueType ();
312- }
313-
314- return TypeCombinator::union (...$ types );
315- }
316-
317228}
0 commit comments