vimeo/psalm
Psalm is a powerful PHP static analysis tool that finds type errors and bugs before runtime. Install via Composer, configure for your codebase, and run it locally or try the live demo at psalm.dev. Docs and integrations available for teams and CI.
Psalm is a static analysis tool designed to identify errors, type inconsistencies, and potential bugs in PHP codebases. For a Laravel-based application, it aligns well with:
0=clean, 2=issues) fit naturally into CI pipelines (e.g., GitHub Actions, GitLab CI) to block merges on type errors.psalm-refactor and psalter tools enable safe, automated codebase improvements (e.g., namespace moves, unused property removal), critical for large Laravel monoliths.Key Misalignments:
--threads). For large Laravel apps (10K+ files), initial runs may require dedicated CI workers or caching strategies.Illuminate\Support\Facades) may trigger noise. Custom stubs or configuration tweaks (e.g., stub_files) are needed to reduce false positives.| Component | Feasibility | Notes |
|---|---|---|
| Laravel Core | High | Psalm works with vanilla PHP; Laravel’s dependency injection and service containers are analyzable. |
| Eloquent ORM | Medium | Requires custom stubs for dynamic queries (e.g., Model::where()). |
| Blade Templates | Low | Psalm ignores .blade.php by default; exclude patterns needed. |
| Artisan Commands | High | Static methods and closures are fully supported. |
| Third-Party Pkgs | Medium | Some packages (e.g., legacy ones) lack type info; stubs or ignore rules required. |
Critical Paths:
psalm.json (e.g., src, app, database paths).laravel-stubs) are essential to avoid false positives.composer install) to ensure dependencies are resolved.| Risk Area | Severity | Mitigation |
|---|---|---|
| False Positives | High | Invest time in custom stubs and psalm.json tuning (e.g., ignore_errors). |
| Performance | Medium | Use --threads=4 and --diff in CI; cache results between runs. |
| Backward Incompatibility | Low | Psalter’s --safe-types flag prevents risky auto-fixes. |
| Tooling Ecosystem | Medium | Limited Laravel-specific plugins; rely on community stubs (e.g., vimeo/psalm-plugin-laravel). |
| Maintainer Dependence | Low | MIT license; active community, but commercial support (Daniil Gentili) is an option. |
Key Questions for the Team:
Psalm integrates seamlessly with:
composer require vimeo/psalm --dev).Stack Limitations:
@return instead of : string).| Phase | Action | Tools/Config |
|---|---|---|
| 1. Pilot Project | Run Psalm on a single module (e.g., app/Http/Controllers). |
./vendor/bin/psalm --init → psalm.json |
| 2. Configuration | Tune psalm.json for Laravel: add stubs, exclude Blade files. |
stub_files, exclude_patterns |
| 3. CI Integration | Add Psalm to CI; start with --no-cache to avoid flaky runs. |
GitHub Actions: psalm --diff |
| 4. Fix Critical Issues | Address high-severity errors (e.g., TypeError, UndefinedMethod). |
psalter --issues=MissingReturnType |
| 5. Refactoring | Use psalm-refactor for namespace moves or class renames. |
--move, --rename flags |
| 6. Full Enforcement | Set Psalm to fail builds on errors; gradually tighten rules. | exit_code: 2 in psalm.json |
Recommended Order:
psalter for low-risk auto-fixes (e.g., MissingReturnType).psalm-refactor for large-scale changes.| Laravel Feature | Psalm Support | Workaround |
|---|---|---|
| Service Container | High (static analysis) | Stub Illuminate\Container\Container. |
| Eloquent Models | Medium (dynamic queries) | Stub Illuminate\Database\Eloquent\Model. |
| Blade Templates | Low (ignored by default) | Exclude via exclude_patterns. |
| Facades | Medium (e.g., Route::get()) |
Stub Illuminate\Support\Facades\Route. |
| Magic Methods | High (e.g., __call, __get) |
No action needed. |
| Traits | High | Fully supported. |
Critical Compatibility Checks:
./vendor/bin/psalm --init to auto-generate a baseline psalm.json../vendor/bin/psalm --no-cache --stats to validate coverage../vendor/bin/psalm --shepherd to benchmark against similar Laravel projects.composer install).composer require vimeo/psalm --dev../vendor/bin/psalm --init.vimeo/psalm-plugin-laravel)."exclude_patterns": ["resources/views/**"]../vendor/bin/psalm app/Http/Controllers..github/workflows/psalm.yml:
- name: Psalm
run: vendor/bin/psalm --diff --threads=4
How can I help you explore Laravel packages today?