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. :)