Hello!

Doctrine entities contain an attribute that specifies the repository for the entity:

#[ORM\Entity(repositoryClass: UserRepository::class)]

Right now we need this @var declaration to let the PHP Tools to know which repository to use.

/** @var UserRepository */
$userRepository = $entityManager->getRepository(User::class);

It would be nice if the extension would be able to get the repository type from the entity class. If we don't specify the repository type, the extension doesn't pick up any custom methods from the repository.

Why this is an issue for me:

https://github.com/phpstan/phpstan-doctrine/issues/630

phpstan is moving forward to cut down on phpdoc usage.

I'm not sure actually how it works behind the scenes.

It's possible phpstan gets the actual repository class from #[ORM\Entity(repositoryClass: UserRepository::class)] in the entity.

Right now getting the repository from entity manager with
$userRepository = $entityManager->getRepository(User::class); results in this info:

(local variable) EntityRepository<User> $userRepository

# Doctrine\ORM\EntityManager::getRepository
function EntityManager::getRepository<T>(
    class-string<T> $className
): Doctrine\ORM\EntityRepository<T>

So it's missing out on methods in the actual UserRepository retrieved.

Of course /** @var UserRepository */ fixes this, but causes phpstan to give an error.

Really shouldn't be using getRepository in the first place, but working on converting 20 years old code to newer standards. :)

    18 days later

    reio Ok, I understand how it works now;

    I'll try to "teach" IntelliSense to return the correct "UserRepository" class by getRepository(string $className) method :) No PHPDoc annotation would satisfactorily achieve that.

      Thanks, that would be wonderful!

      Implemented, so

      $userRepository = $entityManager->getRepository(User::class);

      gets UserRepository (or anything specified in #[ORM\Entity(repositoryClass)] instead of EntityRepository<T>.

      The feature will be available in the upcoming pre-release and release.

        Write a Reply...