I'm implementing PHPStan into my application and also testing Psalm for automatic fixes, however I'm unable to make them read entity class strings (with colon) fe:
$repository = $this->em->getRepository('MyApp:EntityClass');
turns into Psalm error:
ERROR: UndefinedClass - src/Controller/MyController.php:229:48 - Class, interface or enum named MyApp:EntityClass does not exist (see https://psalm.dev/019)
and in PHP Stan
Line src/Controller/MyController.php
------ ---------------------------------------------------------------------------------------------------------------------------------------------------
229 Parameter #1 $className of method Doctrine\ORM\EntityManagerInterface::getRepository() expects class-string<MyApp:EntityClass>, string given.
------ ---------------------------------------------------------------------------------------------------------------------------------------------------
Easiest way to fix this is to use \EntityClass::class instead of a string 'MyApp:EntityClass' however I would like to avoid that. Probable I need to use some annotation to make both tools interprete string correctly, but I have problems figuring out which one. It would also be nice to have it avaialble globaly via entire app, and don't use annotation each time I call a class via string. Currently I've just added this error to ignored ones but would like to know how to fix that.
You already wrote the recommended answer. Using shortname alias is deprecated since
doctrine/ormv2.10. It'll probably be removed in v3.0. You should consider upgrading.That said, this is the only sustainable solution.
Install phpstan/phpstan-doctrine
Using phpstan without phpstan-doctrine is not recommended (while interpreting types of a doctrine entity). Install the phpstan extension
phpstan/phpstan-doctrineand configure it. It interpretes types ofQueryBuilderandRepositoryinside your project; likeEntityManager::getReposiotry(EntityClass::class):Optional: automatic refactoring
That said, you really should update your code base. To ease that process, you could use rector using the
EntityAliasToClassConstantReferenceRector. Correctly configured, you're done in seconds.