Skip to content

Commit 0f41093

Browse files
Avoid false positive about array result
1 parent f3d776c commit 0f41093

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,14 @@ static function (Type $type, callable $traverse) use ($objectManager): Type {
199199
return new MixedType();
200200
}
201201

202-
return $objectManager->getMetadataFactory()->hasMetadataFor($type->getClassName())
203-
? new ArrayType(new MixedType(), new MixedType())
204-
: $traverse($type);
202+
if (!$objectManager->getMetadataFactory()->hasMetadataFor($type->getClassName())) {
203+
return $traverse($type);
204+
}
205+
206+
// We could return `new ArrayTyp(new MixedType(), new MixedType())`
207+
// but the lack of precision in the array keys/values would give false positive
208+
// @see https://github.com/phpstan/phpstan-doctrine/pull/412#issuecomment-1497092934
209+
return new MixedType();
205210
}
206211
);
207212
}

tests/Type/Doctrine/data/QueryResult/queryResult.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,35 +156,35 @@ public function testReturnTypeOfQueryMethodsWithExplicitArrayHydrationMode(Entit
156156
');
157157

158158
assertType(
159-
'list<array>',
159+
'list<mixed>',
160160
$query->getResult(AbstractQuery::HYDRATE_ARRAY)
161161
);
162162
assertType(
163-
'list<array>',
163+
'list<mixed>',
164164
$query->getArrayResult()
165165
);
166166
assertType(
167-
'iterable<int, array>',
167+
'iterable<int, mixed>',
168168
$query->toIterable([], AbstractQuery::HYDRATE_ARRAY)
169169
);
170170
assertType(
171-
'list<array>',
171+
'list<mixed>',
172172
$query->execute(null, AbstractQuery::HYDRATE_ARRAY)
173173
);
174174
assertType(
175-
'list<array>',
175+
'list<mixed>',
176176
$query->executeIgnoreQueryCache(null, AbstractQuery::HYDRATE_ARRAY)
177177
);
178178
assertType(
179-
'list<array>',
179+
'list<mixed>',
180180
$query->executeUsingQueryCache(null, AbstractQuery::HYDRATE_ARRAY)
181181
);
182182
assertType(
183-
'array',
183+
'mixed',
184184
$query->getSingleResult(AbstractQuery::HYDRATE_ARRAY)
185185
);
186186
assertType(
187-
'array|null',
187+
'mixed',
188188
$query->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY)
189189
);
190190

0 commit comments

Comments
 (0)