Skip to content

opensalt/phpstan-doctrine

 
 

Repository files navigation

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();

About

Doctrine extensions for PHPStan

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%