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

Dynamic return type extension for EntityRepository::createQueryBuilder() not working?

Open Wrongusername opened this issue 2 years ago • 0 comments

Regarding https://github.com/phpstan/phpstan-doctrine/issues/66 and solution for it https://github.com/phpstan/phpstan-doctrine/pull/140

Issue description obviously produces expectation that solution would let you validate query builders created using (1)RepositoryClass::createQueryBuilder as a common use case, and not just ones from (2) ->getEntityManager()->createQueryBuilder()

But PR "solving" it is less clear on what it does. In practice all i get with (1) is 'Could not analyse QueryBuilder with dynamic arguments.'. RepositoryClass::createQueryBuilder naturally accepts alias and optional indexBy as opposed to ->getEntityManager()->createQueryBuilder(), so i guess it's "dynamic"

Even if that is intended to (not) work like that it's normally not even possible to realize something is wrong, as reportDynamicQueryBuilders is false by default, so for average user with repo-based queries using createQueryBuilder extension just silently fails to do any validation, which is a very weird decision and takes long time to debug.

class UserRepository extends EntityRepository
{
    public function findWorks(): User
    {
        return $this
            ->getEntityManager()
            ->createQueryBuilder()
            ->select('u')
            ->from(User::class, 'u')
            ->andWhere('u.dsdsf = :id')
            ->setParameter('id', 1)
            ->getQuery()
            ->getResult();
    }

    public function findFailDynamic(): User
    {
        return $this->createQueryBuilder('u')
            ->select('u')
            ->andWhere('u.asdfd = :id')
            ->setParameter('id', 1)
            ->getQuery()
            ->getResult();
    }

Wrongusername avatar Jul 31 '23 16:07 Wrongusername