Timur
Timur

Reputation: 498

PHPStan don't see proper ObjectManager for Doctrine

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

Answers (1)

Martin Hujer
Martin Hujer

Reputation: 168

You can either:

  1. add a inline PHPDoc (works anywhere):
/** @var \Doctrine\ORM\EntityManagerInterface */
$doctrine = $kernel->getContainer()->get('doctrine');
  1. use assert (works only in tests):
$doctrine = $kernel->getContainer()->get('doctrine');
self::assertInstanceOf(EntityManagerInterface::class, $doctrine);

Upvotes: 2

Related Questions