phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Query analyser gets confused when a join alias is equal to the relation name

Open DCoderLT opened this issue 1 year ago • 0 comments

Hi,

this code fragment triggers a “Could not analyse QueryBuilder with dynamic arguments” error.

$builder = $entityManager->createQueryBuilder();
$builder->from(MyEntity::class, 'rr');
$builder->addSelect('rr');
$builder->leftJoin('rr.user', 'user');
$builder->addSelect('user');
$builder->leftJoin('user.role', 'r');
$builder->addSelect('r');

// Error here: Could not analyse QueryBuilder with dynamic arguments.
$query = $builder->getQuery();

But as soon as I change the alias to be different from the relation name, the analyser stops raising the error:

$builder = $entityManager->createQueryBuilder();
$builder->from(MyEntity::class, 'rr');
$builder->addSelect('rr');
$builder->leftJoin('rr.user', 'u');
$builder->addSelect('u');
$builder->leftJoin('u.role', 'r');
$builder->addSelect('r');

// no error
$query = $builder->getQuery();

I have no problem changing the alias name, but I had no idea that this error could be triggered by that . I had to resort to putting $builder->getQuery(); after every line to see which line set it off :)

I’m using the latest versions of phpstan with 2.x versions of doctrine packages:

phpstan/extension-installer 1.3.1
phpstan/phpdoc-parser       1.27.0
phpstan/phpstan             1.10.64
phpstan/phpstan-doctrine    1.3.64
phpstan/phpstan-symfony     1.3.9
doctrine/dbal               2.13.9
doctrine/orm                2.11.3

DCoderLT avatar Mar 22 '24 07:03 DCoderLT