Reputation: 498
I try improve my code with PHPStan. I already install:
Here my phpstan.neon:
includes:
- vendor/phpstan/phpstan/conf/config.levelmax.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-doctrine/extension.neon
- vendor/phpstan/phpstan-doctrine/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
parameters:
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests
symfony:
container_xml_path: '%currentWorkingDirectory%/var/cache/dev/srcApp_KernelDevDebugContainer.xml'
autoload_directories:
- %currentWorkingDirectory%/tests
Also I have test with calling next functions:
$kernel = static::bootKernel();
$doctrine = $kernel->getContainer()->get('doctrine');
$doctrine->getManager()->getConfiguration();
$doctrine->getManager()->getEventManager();
And PHPStan complain on that:
Call to an undefined method Doctrine\Common\Persistence\ObjectManager::getConfiguration().
Call to an undefined method Doctrine\Common\Persistence\ObjectManager::getEventManager().
I researched that $doctrine->getManager()
I have Doctrine\ORM\EntityManagerInterface
that extend Doctrine\Common\Persistence\ObjectManager
but PHPStan think that I have Doctrine\Common\Persistence\ObjectManager
How to tell to PHPStan that $doctrine->getManager()
is Doctrine\ORM\EntityManagerInterface
?
Upvotes: 2
Views: 2908
Reputation: 168
You can either:
/** @var \Doctrine\ORM\EntityManagerInterface */
$doctrine = $kernel->getContainer()->get('doctrine');
$doctrine = $kernel->getContainer()->get('doctrine');
self::assertInstanceOf(EntityManagerInterface::class, $doctrine);
Upvotes: 2