phpstan-doctrine
phpstan-doctrine copied to clipboard
QueryBuilder return in anonymous function with if-condition not recognized
Since Version 1.3 I get the error Anonymous function never returns Doctrine\ORM\QueryBuilder so it can be removed from the return type.
I use the anonymous functions within the buildForm method for my Symfony forms. The following code is working:
'query_builder' => static function (EntityRepository $er): QueryBuilder {
return $er->createQueryBuilder('p')
->orderBy('p.sort', 'ASC');
},
But the following code returns the mentioned error:
'query_builder' => static function (EntityRepository $er) use ($country): QueryBuilder {
$qb = $er->createQueryBuilder('addr');
if ($country instanceof Country) {
$qb->where('addr.country = :country');
$qb->setParameter('country', $country->getCountryKey());
}
return $qb;
},
I also tried different approaches like
'query_builder' => static function (EntityRepository $er) use ($country): QueryBuilder {
if ($country instanceof Country) {
return $er->createQueryBuilder('addr')
->where('addr.country = :country')
->setParameter('country', $country->getCountryKey());
}
return $er->createQueryBuilder('addr');
},
But this still doesn't work.
If I set queryBuilderFastAlgorithm: true, I don't get this error, but I'm not sure, if this behavious is intended.
This is a regression from https://github.com/phpstan/phpstan-doctrine/commit/88a26a2dabf5f47e7fd9b1c74a2537eca48c0578
@ondrejmirtes do you think you could provide another way to fix #309 that doesn't cause this regression?
This is still a problem
Somehow this just got fixed somewhere in the last ~5 releases
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.