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

QueryBuilder return in anonymous function with if-condition not recognized

Open av3 opened this issue 3 years ago • 1 comments
trafficstars

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.

av3 avatar Mar 31 '22 07:03 av3

This is a regression from https://github.com/phpstan/phpstan-doctrine/commit/88a26a2dabf5f47e7fd9b1c74a2537eca48c0578

b1rdex avatar Apr 05 '22 04:04 b1rdex

@ondrejmirtes do you think you could provide another way to fix #309 that doesn't cause this regression?

fbastien avatar Oct 18 '23 14:10 fbastien

This is still a problem

mmarton avatar Nov 21 '23 08:11 mmarton

Somehow this just got fixed somewhere in the last ~5 releases

mmarton avatar Jan 15 '24 12:01 mmarton

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.

github-actions[bot] avatar Feb 16 '24 00:02 github-actions[bot]