Skip to content

Commit b76c21e

Browse files
committed
Fix calling createQueryBuilder() without alias argument
1 parent 815dd47 commit b76c21e

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

src/Type/Doctrine/QueryBuilder/EntityRepositoryCreateQueryBuilderDynamicReturnTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Scalar\String_;
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Reflection\MethodReflection;
11+
use PHPStan\Reflection\ParametersAcceptorSelector;
1112
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1213
use PHPStan\Type\Generic\GenericClassStringType;
1314
use PHPStan\Type\Type;
@@ -39,6 +40,10 @@ public function getTypeFromMethodCall(
3940
$entityNameExpr = new String_($entityNameExprType->getGenericType()->getClassName());
4041
}
4142

43+
if (!isset($methodCall->args[0])) {
44+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
45+
}
46+
4247
$fromArgs = $methodCall->args;
4348
array_unshift($fromArgs, new Arg($entityNameExpr));
4449

tests/DoctrineIntegration/ORM/data/entityRepositoryDynamicReturn-0.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@
88
"message": "Call to method Doctrine\\ORM\\EntityRepository<PHPStan\\DoctrineIntegration\\ORM\\EntityRepositoryDynamicReturn\\MyEntity>::findBy() - entity PHPStan\\DoctrineIntegration\\ORM\\EntityRepositoryDynamicReturn\\MyEntity does not have a field named $blah.",
99
"line": 116,
1010
"ignorable": true
11+
},
12+
{
13+
"message": "Method Doctrine\\ORM\\EntityRepository<mixed>::createQueryBuilder() invoked with 0 parameters, 1-2 required.",
14+
"line": 239,
15+
"ignorable": true
16+
},
17+
{
18+
"message": "Could not analyse QueryBuilder with unknown beginning.",
19+
"line": 241,
20+
"ignorable": true
1121
}
1222
]

tests/DoctrineIntegration/ORM/data/entityRepositoryDynamicReturn-6.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
"message": "Property PHPStan\\DoctrineIntegration\\ORM\\EntityRepositoryDynamicReturn\\Example::$repository with generic class Doctrine\\ORM\\EntityRepository does not specify its types: TEntityClass",
44
"line": 16,
55
"ignorable": true
6+
},
7+
{
8+
"message": "Class PHPStan\\DoctrineIntegration\\ORM\\EntityRepositoryDynamicReturn\\Bug180Repository extends generic class Doctrine\\ORM\\EntityRepository but does not specify its types: TEntityClass",
9+
"line": 232,
10+
"ignorable": true
611
}
712
]

tests/DoctrineIntegration/ORM/data/entityRepositoryDynamicReturn.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,16 @@ public function getRepository(): EntityRepository
228228
abstract public function getEntityManager(): EntityManager;
229229

230230
}
231+
232+
class Bug180Repository extends EntityRepository
233+
{
234+
public const ALIAS = 'o';
235+
236+
237+
public function testingMethod(): int
238+
{
239+
$qb = $this->createQueryBuilder();
240+
241+
return (int) $qb->getQuery()->getSingleScalarResult();
242+
}
243+
}

0 commit comments

Comments
 (0)