- How do I install Psalm for a Laravel project?
- Run `composer require vimeo/psalm --dev` to install Psalm as a development dependency. Initialize its configuration with `./vendor/bin/psalm --init`, which generates a `psalm.json` file. Adjust paths in this file to match your Laravel project’s structure (e.g., `src`, `app`, `database`).
- Does Psalm work with Laravel’s Eloquent ORM?
- Psalm supports Eloquent but may flag dynamic queries (e.g., `Model::where()`) as type errors. Mitigate this by using custom stubs like `vimeo/psalm-plugin-laravel` or adding ignore rules in `psalm.json` for specific methods. Pre-built stubs help reduce false positives.
- Can Psalm analyze Blade templates (.blade.php files)?
- Psalm ignores Blade files by default. To analyze them, explicitly include Blade paths in `psalm.json` under `files` or use `exclude_patterns` to filter out non-PHP files. However, dynamic Blade content (e.g., `@php`) may still require stubs or manual tuning.
- What Laravel versions does Psalm support?
- Psalm works with Laravel 5.8+ but integrates best with Laravel 9+ (PHP 8.1+), where native type hints reduce noise. For older Laravel versions (e.g., PHP 7.4), use docblock annotations (`@var`, `@return`) and expect more manual configuration to avoid false positives.
- How do I integrate Psalm into GitHub Actions for Laravel?
- Add a step to your workflow after `composer install` to run Psalm with strict checks. Example: `./vendor/bin/psalm --no-cache --threads=4`. Use exit codes (0=clean, 2=errors) to block merges. Cache results between runs to improve performance for large codebases.
- Will Psalm break my legacy Laravel codebase?
- Psalm is configurable to avoid breaking changes. Start with `--init` and gradually increase strictness. Use `psalm-refactor` for safe automated fixes (e.g., namespace changes) and `psalter` with `--safe-types` to preview risky changes before applying them.
- How do I handle false positives in Laravel-specific code?
- False positives often occur with dynamic Laravel features (e.g., `Facade::class`, magic methods). Create custom stubs for third-party packages or use `stub_files` in `psalm.json` to point to community stubs like `laravel-stubs`. Ignore specific errors with `ignore_errors` or `@psalm-suppress` annotations.
- Can Psalm improve performance for large Laravel apps?
- Psalm’s analysis is CPU-intensive. For large apps, use `--threads=4` (or higher) and `--diff` in CI to analyze only changed files. Cache results between runs with tools like `psalm --cache-results` or integrate with GitHub Actions caching to reduce runtime.
- Are there alternatives to Psalm for Laravel static analysis?
- Yes, alternatives include PHPStan (more strict but slower) and PhpMD (less type-focused). Psalm stands out for its balance of performance and Laravel integration, especially with plugins like `vimeo/psalm-plugin-laravel`. Choose based on your need for type safety vs. runtime speed.
- How do I refactor Laravel code safely using Psalm?
- Use `psalm-refactor` for bulk changes (e.g., renaming classes, moving namespaces) and `psalter` to preview fixes. For Laravel, ensure stubs are up-to-date to avoid errors in dynamic code. Test refactored code locally before applying changes to the full project.