vimeo/psalm
Psalm is a PHP static analysis tool that finds type issues, bugs, and dead code before runtime. It supports gradual typing via annotations, powerful checks, and configurable rules to improve code quality in applications and libraries.
Psalm is a static analysis tool designed to identify errors, type inconsistencies, and security vulnerabilities in PHP applications. For a Laravel-based product, it aligns well with:
TaintedLlmPrompt in v7.0) detects injection risks (SQLi, XSS) and unsafe operations (e.g., eval, unserialize).@var, @param, and @return annotations.Misalignment Risks:
--no-cache for CI, cached results locally).Model::find(), collect()) may trigger noise. Psalm’s --ignore-missing and custom stubs (e.g., for Illuminate\Support) can mitigate this.| Component | Feasibility | Notes |
|---|---|---|
| Laravel Core | High | Psalm supports Laravel’s autoloading (Composer) and PSR-4 namespaces. |
| Eloquent ORM | Medium | Requires custom stubs for Illuminate\Database\Eloquent\Model. |
| Blade Templates | Low | Psalm does not natively parse Blade; workarounds include: |
| - Treat Blade as plain PHP (lose template-specific checks). | ||
- Pre-process Blade to PHP (e.g., laravel-blade-compiler). |
||
| Service Providers | High | Psalm analyzes container bindings if stubs for Illuminate\Container exist. |
| Queues/Jobs | High | Supports serialized job payloads (with unserialize callmap limits). |
| API Routes | High | Validates route parameters and request validation (if typed). |
Key Dependencies:
dev dependency (require-dev vimeo/psalm:^7.0).psalm-ide-helper), and CLI.| Risk Area | Severity | Mitigation |
|---|---|---|
| Analysis Time | High | - Run in CI with --no-cache (faster but noisier). |
- Use --threads=N for parallel scans. |
||
| False Positives | Medium | - Configure psalm.xml to ignore known issues (e.g., MissingReturnType). |
- Use @psalm-suppress for Laravel-specific edge cases. |
||
| Stub Maintenance | High | - Leverage community stubs (e.g., vimeo/psalm-plugin-laravel). |
- Generate stubs for custom packages using psalm-plugin. |
||
| Plugin Stability | Medium | - Test plugins in isolated environments before full integration. |
| Breaking Changes | High | - Psalm 7.0 introduces mutability annotations (backward-incompatible). |
- Gradually adopt via --alter and MissingPureAnnotation. |
Critical Questions for TPM:
--changed-files) be implemented in CI?| Layer | Psalm Integration | Tools/Workarounds |
|---|---|---|
| Codebase | Analyzes PHP files (ignores Blade by default). | psalm.xml configuration. |
| CI/CD | Runs in GitHub Actions, GitLab CI, or CircleCI as a pre-merge check. | Cache results between runs. |
| IDE | Integrates with PhpStorm, VSCode (via psalm-ide-helper). |
Real-time feedback during development. |
| Monorepo | Supports multiple projects (e.g., --project-paths). |
Parallel scanning with --threads. |
| Legacy PHP | Works with PHP 7.4+ (Laravel 8+) via type inference. | Gradual annotation with @var. |
Phase 1: Pilot (2-4 Weeks)
auth or api).composer require-dev vimeo/psalm).psalm.xml with basic rules (e.g., NoUnresolvableReferences).--no-cache and monitor false positives.Phase 2: Gradual Rollout (4-8 Weeks)
users, payments).Illuminate\Auth).@psalm-pure, @psalm-mutation-free) via --alter.Nullsafe violations).Phase 3: Full Integration (8-12 Weeks)
TaintedLlmPrompt).@psalm-param, @psalm-return).Phase 4: Optimization (Ongoing)
--threads/--memory-limit.| Laravel Feature | Psalm Support | Workaround |
|---|---|---|
| Eloquent Models | Partial (requires stubs). | Use vimeo/psalm-plugin-laravel. |
| Blade Templates | None (treated as PHP). | Pre-process with laravel-blade-compiler. |
| Service Container | Limited (needs Illuminate\Container stubs). |
Annotate bindings manually. |
How can I help you explore Laravel packages today?