This extension provides following features:
- DQL validation for parse errors, unknown entity classes and unknown persistent fields.
- Recognizes magic
findBy*
,findOneBy*
andcountBy*
methods on EntityRepository. - Validates entity fields in repository
findBy
,findBy*
,findOneBy
,findOneBy*
,count
andcountBy*
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
andgetPartialReference
whenFoo::class
entity class name is provided as the first argument - Adds missing
matching
method onDoctrine\Common\Collections\Collection
. This can be turned off by settingparameters.doctrine.allCollectionsSelectable
tofalse
. - 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:
includes:
- 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):
includes:
- 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:
parameters:
doctrine:
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:
parameters:
doctrine:
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']);
$kernel->boot();
return $kernel->getContainer()->get('doctrine')->getManager();