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

README.md

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.

Usage

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

Configuration

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();
You can’t perform that action at this time.