Skip to content

Commit

Permalink
Fix empty DQL with QueryBuilder::setParameters()
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque authored and ondrejmirtes committed Nov 26, 2020
1 parent 71bcd7d commit 1d32c42
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\QueryBuilder;

use Doctrine\ORM\QueryBuilder;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
Expand Down Expand Up @@ -79,7 +80,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
if ($returnType instanceof MixedType) {
return false;
}
return $returnType->isSuperTypeOf(new ObjectType($this->getClass()))->yes();
return (new ObjectType(QueryBuilder::class))->isSuperTypeOf($returnType)->yes();
}

public function getTypeFromMethodCall(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\QueryBuilder;

use Doctrine\ORM\QueryBuilder;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function specifyTypes(MethodReflection $methodReflection, MethodCall $nod
if ($returnType instanceof MixedType) {
return new SpecifiedTypes([]);
}
if (!$returnType->isSuperTypeOf(new ObjectType($this->getClass()))->yes()) {
if (!(new ObjectType(QueryBuilder::class))->isSuperTypeOf($returnType)->yes()) {
return new SpecifiedTypes([]);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/DoctrineIntegration/ORM/data/queryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,27 @@ public function dynamicQueryBuilder(string $entityClass): Query
return $queryBuilder->getQuery();
}

public function usingMethodThatReturnStatic(): ?MyEntity
{
$queryBuilder = $this->entityManager->createQueryBuilder();

$queryBuilder
->select('e')
->from(MyEntity::class, 'e')
->where('e.id = :id')
->setParameters([
'id' => 123,
]);

return $queryBuilder->getQuery()->getOneOrNullResult();
}

public function getCustomQueryBuilder(): CustomQueryBuilder
{
return $this->entityManager->createQueryBuilder();
}
}

class CustomQueryBuilder extends \Doctrine\ORM\QueryBuilder
{
}
1 change: 1 addition & 0 deletions tests/DoctrineIntegration/ORM/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ parameters:
doctrine:
objectManagerLoader: entity-manager.php
reportDynamicQueryBuilders: true
queryBuilderClass: PHPStan\DoctrineIntegration\ORM\QueryBuilder\CustomQueryBuilder

0 comments on commit 1d32c42

Please sign in to comment.