.phpstorm.meta.php: Map all parameter values to a specific return type

I'm using Doctrine which has a class called EntityManager that has a method EntityManager::getRepository, which by default returns an EntityRepository object. However, the return type can be changed using Configuration::setDefaultRepositoryClassName(string $repositoryClassName).

<?php

use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;

class MyEntityRepository extends EntityRepository { /* ... */ }

$config = new Configuration;
$config->setDefaultRepositoryClassName(MyEntityRepository::class);
$em = EntityManager::create($databaseConnection, $config);
$repository = $em->getRepository($whatever);

Now $repository is a MyEntityRepository object, but it is still assumed to be only an EntityRepository. Thus, I'd like to use .phpstorm.meta.php to change the return type. I tried

<?php
namespace PHPSTORM_META {
override(Doctrine\Common\Persistence\ObjectManager::getRepository(0), \MyEntityRepository::class);
}

and

<?php
namespace PHPSTORM_META {
override(Doctrine\Common\Persistence\ObjectManager::getRepository(0),
map([
'' => \MyEntityRepository::class
]));
}

but neither worked. What is the correct way to map all parameter values to a specific return type?

2 comments

It seems to be a bug. A workaround is to use a string instead of the class constant:

<?php
namespace PHPSTORM_META {
override(Doctrine\Common\Persistence\ObjectManager::getRepository(0),
map([
'' => '\MyEntityRepository'
]));
}
1

Thanks for a report. Reproduced on my end also

0

Please sign in to leave a comment.