php-standard-library/psalm-plugin
Psalm plugin for PHP Standard Library (PSL) that improves type inference for PSL Type specifications (e.g., shape/optional), producing more precise array shapes and safer analysis. Install via Composer and enable with psalm-plugin.
Type, Iter, Str, and Regex components, which are increasingly used in Laravel for data validation, transformations, and collections. This reduces the need for custom type logic in business layers.Validator::make() with PSL shapes where possible).Collection class could leverage PSL’s Iter and Type for statically typed operations, reducing runtime errors in loops/transformations.composer require --dev) and activation is a one-liner (psalm-plugin enable). No Laravel-specific configuration is required.5.x) and Laravel’s PHP version (e.g., 8.2+).- name: Psalm + PSL Plugin
run: vendor/bin/psalm --init --plugins=Psl\Psalm\Plugin
| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Psalm Version Drift | Plugin supports Psalm 4/5, but Laravel may upgrade to Psalm 6+. | Monitor Psalm 6 compatibility; contribute backports if needed. |
| PSL Adoption Curve | Developers may resist adopting PSL shapes over Laravel’s Validator. |
Pilot in high-risk modules (e.g., payments) first; demonstrate ROI (e.g., fewer runtime errors). |
| Performance Overhead | Psalm analysis adds CPU/memory usage in CI. | Cache Psalm results (e.g., psalm --cache); run only on changed files. |
| False Positives | PSL types may conflict with Laravel’s dynamic data (e.g., $_GET). |
Use @psalm-suppress sparingly; document exceptions in a PSL_RULES.md. |
| Dependency Bloat | Adding Psalm + PSL plugin increases composer.json complexity. |
Scope to PSL-heavy modules initially; avoid global enforcement. |
Strategic Alignment:
Validator with PSL shapes in new features, or use them side-by-side?Adoption Path:
Toolchain Integration:
Long-Term Vision:
Carbon, Illuminate\Support) upstream?Validator::make() with PSL shapes where possible (e.g., API payloads).protected $casts = ['email' => Type\email_address()]).Iter for statically typed Collection operations.| Phase | Actionable Steps | Tools/Artifacts | Success Metrics |
|---|---|---|---|
| Assessment | Audit codebase for PSL usage; identify high-impact modules (e.g., API, payments). | composer why-not php-standard-library/psl |
List of candidate modules. |
| Pilot | Enable Psalm + plugin in CI for 1–2 modules; document findings. | GitHub Actions workflow, psalm.json |
Zero runtime validation errors in pilot. |
| Enforcement | Add Psalm to pre-commit hooks (e.g., Laravel Pint + Psalm). | php-cs-fixer, roave/security-advisories |
90% CI pass rate for Psalm. |
| Replacement | Replace Validator::make() with PSL shapes in new features. |
Custom PsalmValidator facade |
30% reduction in runtime validation. |
| Optimization | Cache Psalm results; parallelize analysis. | psalm --cache, parallel-lint |
CI runtime < 5 mins. |
$_GET/$_POST. Use @psalm-suppress or runtime coercion.--no-cache).Validator::make() in favor of PSL where Psalm can verify types.psalm.json with:
{
"plugins": ["Psl\\Psalm\\Plugin"],
"typeCoverageAnalysis": true,
"suppressInvalidPsr4": true
}
php-standard-library/psl and vimeo/psalm to specific versions in composer.json.PSL_GUIDE.md.@psalm-var, @psalm-suppress).@psalm-suppress judiciously.psalm --cache).psalm --cache and parallelize analysis.composer require --dev php-standard-library/psalm-plugin as part of setup.| Failure Mode
How can I help you explore Laravel packages today?