PHPStan没有为Doctrine看到正确的ObjectManager

时间:2019-04-29 10:46:14

标签: php symfony doctrine-orm doctrine phpstan

我尝试使用PHPStan改进代码。 我已经安装:

这是我的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

我还要测试调用下一个函数:

$kernel = static::bootKernel();
$doctrine = $kernel->getContainer()->get('doctrine');
$doctrine->getManager()->getConfiguration();
$doctrine->getManager()->getEventManager();

PHPStan对此表示抱怨:

Call to an undefined method Doctrine\Common\Persistence\ObjectManager::getConfiguration().
Call to an undefined method Doctrine\Common\Persistence\ObjectManager::getEventManager().

我研究了$doctrine->getManager()我有Doctrine\ORM\EntityManagerInterface扩展了Doctrine\Common\Persistence\ObjectManager,但PHPStan认为我拥有Doctrine\Common\Persistence\ObjectManager

如何告诉PHPStan $doctrine->getManager()Doctrine\ORM\EntityManagerInterface

1 个答案:

答案 0 :(得分:0)

您可以:

  1. 添加内联PHPDoc(可在任何地方使用):
subnet_ids = flatten([module.vpc.public_subnets, module.vpc.private_subnets])
  1. 使用assert(仅在测试中有效):
/** @var \Doctrine\ORM\EntityManagerInterface */
$doctrine = $kernel->getContainer()->get('doctrine');