- Can I use phpstan/phpstan-symfony in a Laravel project that integrates Symfony components (e.g., Messenger, HttpClient)?
- Yes, this extension works seamlessly with Laravel when Symfony components are used. It enhances static analysis for Symfony’s DI container, Messenger handlers, and other services, even if your Laravel app doesn’t use full Symfony bundles. Just install it as a dev dependency and configure PHPStan to include the extension.
- How do I install and configure phpstan/phpstan-symfony for Laravel?
- Run `composer require --dev phpstan/phpstan-symfony` and ensure `phpstan/extension-installer` is installed. If not, manually add the extension in `phpstan.neon` under `extensions:` with the path to the extension’s `phpstan-symfony.neon`. No Laravel-specific config is needed beyond standard PHPStan setup.
- Will this extension work with Laravel’s service container (e.g., `app()->make()`) or only Symfony’s `ContainerInterface`?
- This extension is *Symfony-specific* and won’t directly analyze Laravel’s container methods like `app()->make()`. However, if your Laravel app uses Symfony’s `ContainerInterface` (e.g., via Symfony Messenger or HttpClient), it will provide precise return types for those calls. For Laravel’s native container, stick to PHPStan’s core rules.
- Does phpstan/phpstan-symfony support Symfony 6.x and Laravel 9+ projects?
- Yes, the extension supports Symfony 4.x–6.x+ and is fully compatible with Laravel 9+ if Symfony components are used. It automatically adapts to Symfony’s container XML naming conventions (e.g., `var/cache/dev/projectContainer.xml` for Symfony 5/6). Test it in your CI to confirm compatibility with your Symfony version.
- How do I handle false positives for Symfony’s dynamic features (e.g., optional services, runtime config)?
- Use `constantHassers: false` in your `phpstan.neon` to suppress `::has()` checks if they trigger false positives. For runtime config, disable specific rules via `rules.neon` or suppress errors with inline comments like `// @phpstan-ignore-next-line`. Always validate configurations in CI to catch regressions.
- Can I use this extension to analyze Symfony Messenger handlers in a Laravel app?
- Absolutely. The extension provides precise return types for `HandleTrait::handle()` and Messenger-related methods. Configure `HandleTrait` wrappers in `phpstan.neon` to map message handlers to their concrete types. This is especially useful if your Laravel app uses Symfony Messenger for CQRS or background jobs.
- What’s the performance impact of running phpstan/phpstan-symfony in CI?
- The extension adds CPU/memory overhead due to container XML parsing and Messenger analysis. Mitigate this by running PHPStan in parallel (`--parallel`) and limiting analysis to critical paths. Since it’s a dev dependency, exclude it from production builds. Warm the Symfony cache (`cache:warm`) in CI before analysis to avoid stale container data.
- Are there alternatives to phpstan/phpstan-symfony for Symfony static analysis?
- For Symfony-specific analysis, this is the most comprehensive PHPStan extension. Alternatives include generic PHPStan rules or Psalm’s Symfony plugin, but neither offers the same depth of integration for DI, Messenger, or Forms. If you need lightweight checks, PHPStan’s core rules may suffice, but you’ll miss Symfony’s dynamic type inference.
- How do I configure custom error messages for Symfony-specific PHPStan warnings?
- Extend your `phpstan.neon` with custom error messages under `errorLevel:` or `messages:`. For example, add `messages: { 'Symfony\Component\HttpFoundation\Request::getContent()': 'Use precise return types for request content.' }` to clarify Symfony-related issues. Document these in your team’s runbook for consistency.
- What should I do if the extension fails due to missing `srcDevDebugProjectContainer.xml`?
- Regenerate the Symfony cache in your CI/CD pipeline by running `php bin/console cache:warm`. If the file is still missing, ensure your environment is properly configured (e.g., `APP_ENV=dev`). As a temporary workaround, set `constantHassers: false` in `phpstan.neon`, but address the root cause to avoid analysis gaps.