Error Identifier: doctrine.queryBuilderDynamicArgument
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;
function buildQuery(EntityManagerInterface $em, string $field): void
{
$qb = $em->createQueryBuilder()
->select('u')
->from('App\Entity\User', 'u')
->where("u.$field = :value")
->setParameter('value', 'test');
$qb->getQuery();
}
Why is it reported? #
This error is reported by the phpstan-doctrine extension.
PHPStan could not statically analyse the QueryBuilder because it contains dynamic (non-constant) arguments. In the example above, the $field variable makes the DQL string dynamic, preventing PHPStan from validating the query at analysis time.
How to fix it #
Use constant string values in QueryBuilder method calls so that PHPStan can statically determine the resulting DQL:
<?php declare(strict_types = 1);
use Doctrine\ORM\EntityManagerInterface;
-function buildQuery(EntityManagerInterface $em, string $field): void
+function buildQuery(EntityManagerInterface $em): void
{
$qb = $em->createQueryBuilder()
->select('u')
->from('App\Entity\User', 'u')
- ->where("u.$field = :value")
+ ->where('u.email = :value')
->setParameter('value', 'test');
$qb->getQuery();
}
How to ignore this error #
You can use the identifier doctrine.queryBuilderDynamicArgument to ignore this error using a comment:
// @phpstan-ignore doctrine.queryBuilderDynamicArgument
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.queryBuilderDynamicArgument
Rules that report this error #
- PHPStan\Rules\Doctrine\ORM\QueryBuilderDqlRule [1] phpstan/phpstan-doctrine