Skip to content

Commit ce03c55

Browse files
authored
Fix false positive for aliases matching PHP class names
1 parent b6c65f0 commit ce03c55

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/Type/Doctrine/ArgumentsProcessor.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Rules\Doctrine\ORM\DynamicQueryBuilderArgumentException;
88
use PHPStan\Type\Doctrine\QueryBuilder\Expr\ExprType;
99
use function count;
10+
use function in_array;
1011
use function strpos;
1112

1213
/** @api */
@@ -32,7 +33,7 @@ public function processArgs(
3233
): array
3334
{
3435
$args = [];
35-
foreach ($methodCallArgs as $arg) {
36+
foreach ($methodCallArgs as $argIndex => $arg) {
3637
if ($arg->unpack) {
3738
throw new DynamicQueryBuilderArgumentException();
3839
}
@@ -61,12 +62,14 @@ public function processArgs(
6162
if ($value->isClassString()->yes() && count($value->getClassStringObjectType()->getObjectClassNames()) === 1) {
6263
/** @var class-string $className */
6364
$className = $value->getClassStringObjectType()->getObjectClassNames()[0];
64-
if ($this->objectMetadataResolver->isTransient($className)) {
65-
throw new DynamicQueryBuilderArgumentException();
65+
$isEntityClassArgument = $argIndex === 0 && in_array($methodName, ['from', 'join', 'innerJoin', 'leftJoin'], true);
66+
if ($isEntityClassArgument) {
67+
if ($this->objectMetadataResolver->isTransient($className)) {
68+
throw new DynamicQueryBuilderArgumentException();
69+
}
70+
$args[] = $className;
71+
continue;
6672
}
67-
68-
$args[] = $className;
69-
continue;
7073
}
7174

7275
if (count($value->getConstantScalarValues()) !== 1) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,14 @@ public function testNonEntityClassString(EntityManagerInterface $em, string $cla
310310
assertType('mixed', $result);
311311
}
312312

313+
public function testEventAlias(EntityManagerInterface $em): void
314+
{
315+
$query = $em->createQueryBuilder()
316+
->select('event')
317+
->from(Many::class, 'event')
318+
->getQuery();
319+
320+
assertType('Doctrine\ORM\Query<null, QueryResult\Entities\Many>', $query);
321+
}
322+
313323
}

0 commit comments

Comments
 (0)