- How do I install Psalm for Laravel projects via Composer?
- Run `composer require vimeo/psalm` to install Psalm. Configure it by creating a `psalm.xml` file in your project root, then run `./vendor/bin/psalm` to analyze your codebase. Psalm works globally across PHP projects, including Laravel.
- Does Psalm support Laravel’s dynamic features like facades or service containers?
- Psalm can analyze Laravel’s dynamic features but may flag them as errors. Use `@psalm-suppress` annotations or configure `psalm.xml` to ignore specific patterns (e.g., `ignore_missing_docblock_return_type`). For facades, generate stubs with `laravel-stubs` or manually suppress warnings.
- Which Laravel versions work best with Psalm?
- Psalm works with Laravel 5.8+ but excels with Laravel 8+ due to PHP 8’s stricter typing. For older Laravel versions, enable gradual typing with `declare(strict_types=1)` in files or use Psalm’s `--no-strict-types` flag for partial analysis.
- Can Psalm replace PHPStan in a Laravel project?
- Psalm and PHPStan serve similar purposes but have different strengths. Psalm is faster and better at detecting type inconsistencies, while PHPStan excels at deeper static analysis. Many teams run both in CI for comprehensive coverage. Choose based on your project’s needs.
- How do I configure Psalm to ignore false positives in Laravel?
- Edit `psalm.xml` to whitelist Laravel-specific patterns. For example, add `<ignore_missing_docblock_return_type>true</ignore_missing_docblock_return_type>` or suppress specific issues with `@psalm-suppress`. Test configurations incrementally to avoid missing real errors.
- Will Psalm slow down my CI/CD pipeline?
- Psalm can be optimized for CI with flags like `--diff` (analyze only changed files), `--threads`, and caching. For large Laravel apps, exclude non-critical paths (e.g., `tests/`) and run in parallel with other tools like PHP-CS-Fixer. GitHub Actions caching further reduces runtime.
- How does Psalm handle Laravel’s IDE Helper-generated stubs?
- Psalm relies on type information from stubs or docblocks. Use `laravel-ide-helper` to generate stubs for Psalm to analyze Laravel’s core classes. Ensure stubs are up-to-date and configure Psalm to include vendor stubs with `<projectFiles>` in `psalm.xml`.
- Can Psalm automatically fix type issues in Laravel code?
- Psalm includes `psalter`, a tool to automatically fix some type issues. Run `./vendor/bin/psalter` with `--dry-run` first to preview changes, then apply fixes in small batches. Avoid using it blindly—review changes in PRs to prevent breaking Laravel-specific logic.
- Does Psalm work with Laravel’s dynamic properties or magic methods?
- Psalm may flag dynamic properties (`__get`, `__set`) or magic methods as errors. Configure `psalm.xml` to ignore them or use `@psalm-suppress PropertyNotSetInConstructor` for known safe cases. For complex cases, contribute Laravel-specific stubs to Psalm’s plugin ecosystem.
- How do I enforce Psalm checks in a Laravel team workflow?
- Integrate Psalm into your CI pipeline to fail builds on warnings (exit code 2). Use GitHub Actions or GitLab CI templates like `psalm/psalm-github-actions`. For local development, add a pre-commit hook or use IDE plugins (e.g., Intelephense for VSCode) for real-time feedback.