PHPStan doesn't use custom entity repository - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Tuesday, March 22, 2022

PHPStan doesn't use custom entity repository

I am using PHPStan with its Doctrine extension.

I have a custom entity repository called App\Repository\Doctrine\UserRepository with the @extends doc block:

/**
 * @extends \Doctrine\ORM\EntityRepository<\App\Entity\User>
 */
class UserRepository extends EntityRepository implements IUserRepository
{
    public function customRepositoryMethod()
    {
        // ...
    }
}

In a controller, this code:

    public function getUserMatches(EntityManager $em)
    {
        $userRepo = $em->getRepository(\App\Entity\User::class);
        $userRepo->customRepositoryMethod();
    }

...results in this PHPStan error:

Call to an undefined method Doctrine\ORM\EntityRepository<meQ\Entity\User>::customRepositoryMethod().

Thanks to phpstan-doctrine, static analysis knows that $em->getRepository(User::class) returns a EntityRepository<User>.

However, it does not know to consider the custom repository class UserRepository as the implementation of that generic type.

How do I DocBlock the UserRepository class, or otherwise configure PHPStan, so that it interprets UserRepository as the implementation of EntityRepository<User>?

What else I've tried

I've also tried this DocBlock on UserRepository to no avail:

/**
 * @implements \Doctrine\ORM\EntityRepository<\App\Entity\User>
 */


source https://stackoverflow.com/questions/71560316/phpstan-doesnt-use-custom-entity-repository

No comments:

Post a Comment