weirdan/doctrine-psalm-plugin
Psalm plugin for Doctrine ORM projects. Adds smarter type inference for EntityManager, repositories, proxies and collections, reducing false positives and improving static analysis of Doctrine entities and queries in PHP applications.
doctrine/dbal and illuminate/database wrappers), making it a near-perfect fit for Laravel applications leveraging Eloquent’s underlying ORM patterns. The plugin’s ability to infer types for entities, repositories, and proxies aligns with Laravel’s repository pattern (via custom repositories) and Eloquent model abstractions.EntityManager::find(), repository methods) directly addresses Laravel’s pain points around runtime type safety in domain logic, especially in complex queries or polymorphic relationships.weirdan/doctrine-psalm-plugin).plugins array in psalm.xml to include the plugin.Doctrine\ORM\EntityRepository.DBAL or EntityManager injections).Illuminate\Database\Eloquent\Concerns\HasDynamicAttributes or Illuminate\Database\Eloquent\Relations\Relation proxies.doctrine/dbal and doctrine/orm). Older Laravel versions may need Doctrine ORM installed separately (e.g., doctrine/orm:^2.10).| Risk Area | Mitigation Strategy |
|---|---|
| False Positives/Negatives | Start with a baseline Psalm config (e.g., --strict-typing) and gradually enable plugin features. Use psalm --init to auto-generate a config. |
| Performance Overhead | Psalm plugins add ~10–30% runtime to static analysis. Mitigate by: |
--cache-dir).--jobs=4).hasManyThrough, accessors) beyond raw Doctrine ORM?return_type annotations).psalm.xml for plugin configuration.psalm --no-cache for pre-merge checks.psalm.xml) for Doctrine-related rules (e.g., @mixin, @template).composer require doctrine/orm
composer require --dev weirdan/doctrine-psalm-plugin
Update psalm.xml:
<plugin-classes>
<plugin-class>WeirdAn\DoctrinePsalmPlugin\Plugin</plugin-class>
</plugin-classes>
<param name="entityPaths" type="string[]" description="Paths to Doctrine entities">
<value>app/Models</value>
<value>src/Entity</value>
</param>
--level=5 for strict typing).--no-cache to test in CI before full adoption.| Component | Compatibility Notes |
|---|---|
| Laravel Versions | Works with Laravel 10+ (Doctrine ORM 2.10+). Older versions may need manual Doctrine setup. |
| Psalm Versions | Tested with Psalm 5.x. Check for ^4.0 support if using older Psalm. |
| Doctrine ORM | Requires 2.10+ (Laravel 10+ includes this via doctrine/dbal). |
| Eloquent | Limited native support; treat Eloquent models as opaque objects unless using custom Doctrine repositories. |
| Custom Repositories | Full support if extending Doctrine\ORM\EntityRepository. |
psalm --no-cache --init.@ORM\Column(type="json")).Proxy\__CG__App\Models\User).php-cs-fixer + Psalm).weirdan/doctrine-psalm-plugin alongside Psalm (e.g., via composer update --with-dependencies).psalm.xml template in the repo to standardize across environments.PSALM_RULES.md.psalm --log-errors-to=psalm.log to diagnose plugin-specific issues.How can I help you explore Laravel packages today?