Error Identifier: doctrine.queryBuilderDynamic
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
class UserRepository
{
public function __construct(private EntityManagerInterface $em)
{
}
public function findUsers(QueryBuilder $qb): array
{
return $qb->getQuery()->getResult();
}
}
This error is reported by phpstan/phpstan-doctrine.
Why is it reported? #
PHPStan’s Doctrine extension validates DQL queries built with QueryBuilder at analysis time. To do this, it needs to trace the query builder from its creation (e.g. $em->createQueryBuilder()) through the chain of method calls to getQuery(). When the query builder is received as a parameter or its origin cannot be determined, PHPStan cannot reconstruct the DQL being built and reports this error.
This error is only reported when the reportDynamicQueryBuilders option is enabled in the phpstan-doctrine configuration.
How to fix it #
Build the query builder in the same method scope so PHPStan can trace it from creation to execution:
<?php declare(strict_types = 1);
use Doctrine\ORM\EntityManagerInterface;
class UserRepository
{
public function __construct(private EntityManagerInterface $em)
{
}
- public function findUsers(QueryBuilder $qb): array
+ public function findUsers(): array
{
- return $qb->getQuery()->getResult();
+ return $this->em->createQueryBuilder()
+ ->select('u')
+ ->from(User::class, 'u')
+ ->getQuery()
+ ->getResult();
}
}
If the dynamic query builder is intentional and cannot be avoided, the error can be ignored with a PHPStan ignore comment referencing this identifier.
How to ignore this error #
You can use the identifier doctrine.queryBuilderDynamic to ignore this error using a comment:
// @phpstan-ignore doctrine.queryBuilderDynamic
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: doctrine.queryBuilderDynamic
Rules that report this error #
- PHPStan\Rules\Doctrine\ORM\QueryBuilderDqlRule [1] phpstan/phpstan-doctrine