# phpstan-doctrine **Repository Path**: mirrors_simPod/phpstan-doctrine ## Basic Information - **Project Name**: phpstan-doctrine - **Description**: Doctrine extensions for PHPStan - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-29 - **Last Updated**: 2025-09-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Doctrine extensions for PHPStan [![Build](https://github.com/phpstan/phpstan-doctrine/workflows/Build/badge.svg)](https://github.com/phpstan/phpstan-doctrine/actions) [![Latest Stable Version](https://poser.pugx.org/phpstan/phpstan-doctrine/v/stable)](https://packagist.org/packages/phpstan/phpstan-doctrine) [![License](https://poser.pugx.org/phpstan/phpstan-doctrine/license)](https://packagist.org/packages/phpstan/phpstan-doctrine) * [PHPStan](https://phpstan.org/) * [Doctrine](https://www.doctrine-project.org/) This extension provides following features: * DQL validation for parse errors, unknown entity classes and unknown persistent fields. QueryBuilder validation is also supported. * 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` 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. * Analysis of discrepancies between entity column types and property field types. ## Installation To use this extension, require it in [Composer](https://getcomposer.org/): ```bash composer require --dev phpstan/phpstan-doctrine ``` If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!
Manual installation If you don't want to use `phpstan/extension-installer`, include extension.neon in your project's PHPStan config: ```neon includes: - vendor/phpstan/phpstan-doctrine/extension.neon ``` If you're interested in DQL/QueryBuilder validation, include also `rules.neon` (you will also need to provide the `objectManagerLoader`, see below): ```neon 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: ```neon parameters: doctrine: ormRepositoryClass: MyApp\Doctrine\BetterEntityRepository odmRepositoryClass: MyApp\Doctrine\BetterDocumentRepository ``` You can opt in for more advanced analysis by providing the object manager from your own application. This will enable DQL validation: ```neon parameters: doctrine: objectManagerLoader: tests/object-manager.php ``` Example for Symfony 4: ```php // tests/object-manager.php use App\Kernel; require __DIR__ . '/../config/bootstrap.php'; $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); $kernel->boot(); return $kernel->getContainer()->get('doctrine')->getManager(); ``` Example for Symfony 5: ```php // tests/object-manager.php use App\Kernel; use Symfony\Component\Dotenv\Dotenv; require __DIR__ . '/../vendor/autoload.php'; (new Dotenv())->bootEnv(__DIR__ . '/../.env'); $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); $kernel->boot(); return $kernel->getContainer()->get('doctrine')->getManager(); ``` ## Custom types If your application uses custom Doctrine types, you can write your own type descriptors to analyse them properly. Type descriptors implement the interface `PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor` which looks like this: ```php