Skip to content
Doctrine extensions for PHPStan
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Prevent crashes for unknown classes Mar 11, 2019
.gitattributes First version Jan 30, 2017
.gitignore First version Jan 30, 2017
.travis.yml Update .travis.yml Feb 13, 2019 Updated README Feb 13, 2019
composer-require-checker.json Fixed build Feb 13, 2019
phpcs.xml Updated coding standard Apr 2, 2018
rules.neon Check non-magic Repository calls like findBy/findOneBy/count Feb 13, 2019

Doctrine extensions for PHPStan

Build Status Latest Stable Version License

This extension provides following features:

  • DQL validation for parse errors, unknown entity classes and unknown persistent fields.
  • Recognizes magic findBy*, findOneBy* and countBy* methods on EntityRepository.
  • Validates entity fields in repository findBy, findBy*, findOneBy, findOneBy*, count and countBy* method calls.
  • Interprets EntityRepository<MyEntity> correctly in phpDocs for further type inference of methods called on the repository.
  • Provides correct return for Doctrine\ORM\EntityManager::getRepository().
  • Provides correct return type for Doctrine\ORM\EntityManager::find, getReference and getPartialReference when Foo::class entity class name is provided as the first argument
  • Adds missing matching method on Doctrine\Common\Collections\Collection. This can be turned off by setting parameters.doctrine.allCollectionsSelectable to false.
  • Also supports Doctrine ODM.


To use this extension, require it in Composer:

composer require --dev phpstan/phpstan-doctrine

Include extension.neon in your project's PHPStan config:

	- vendor/phpstan/phpstan-doctrine/extension.neon

If you're interested in DQL validation, include also rules.neon (you will also need to provide the objectManagerLoader, see below):

	- vendor/phpstan/phpstan-doctrine/rules.neon


If your repositories have a common base class, you can configure it in your phpstan.neon and PHPStan will see additional methods you define in it:

		repositoryClass: MyApp\Doctrine\BetterEntityRepository

You can opt in for more advanced analysis by providing the object manager from your own application. This will allow the correct entity repositoryClass to be inferred when accessing $entityManager->getRepository(). Also, it allows DQL validation when enabled:

		objectManagerLoader: tests/object-manager.php

For example, in a Symfony project, object-manager.php would look something like this:

require dirname(__DIR__).'/../config/bootstrap.php';
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
return $kernel->getContainer()->get('doctrine')->getManager();
You can’t perform that action at this time.