- How do I install this package in a Laravel project?
- Run `composer require --dev dealerdirect/phpcodesniffer-composer-installer` first, then `composer require --dev sirbrillig/phpcs-variable-analysis`. Add `<rule ref="VariableAnalysis"/>` to your `phpcs.xml` file to enable the sniffs. No additional Laravel-specific setup is needed.
- Will this conflict with PHPStan or Psalm in Laravel?
- No, but they may overlap on unused variables. Use PHPCS for syntax-level checks (e.g., undefined variables) and PHPStan/Psalm for semantic analysis. Configure one tool to handle unused variables to avoid redundancy. Example: Disable PHPStan’s unused-variable rule if using this package.
- Does this work with Laravel’s Blade templates?
- Yes, but Blade variables (e.g., `@foreach`) may trigger false positives. Customize the ruleset in `phpcs.xml` to ignore Blade-specific variables or use `<property name="allowUnusedVariablesInFileScope" value="true"/>` for template files.
- What Laravel versions does this package support?
- The package itself requires PHP 5.4+, but Laravel’s default PHPCS setup (via `dealerdirect/phpcodesniffer-composer-installer`) meets the PHPCS 3.13.5+ requirement. Works with Laravel 5.8+ (PHP 7.2+) and older projects with PHP 5.4+ constraints.
- How do I ignore Laravel-specific variables like `$request` or `$app`?
- Add a `validUndefinedVariableNames` property in your `phpcs.xml` ruleset. Example: `<property name="validUndefinedVariableNames" value="request app loop"/>` to skip warnings for these globals. This avoids false positives in Laravel’s dependency injection context.
- Can I run this in CI/CD for Laravel projects?
- Yes, integrate it into your CI pipeline (e.g., GitHub Actions) with `phpcs --standard=VariableAnalysis`. For pre-commit checks, use tools like `husky` or `pre-commit` with PHPCS. Start non-blocking in CI, then enforce it later if needed.
- What if this package misses dynamic properties or magic methods in Laravel?
- Complex PHP constructs (e.g., `__get()`, `__set()`) may evade detection. Test against Laravel’s magic methods and adjust rulesets. For dynamic properties, consider combining this with PHPStan’s `@property` annotations or Psalm’s type hints.
- How do I handle false positives for unset() or foreach loops in Laravel?
- False positives often occur with `unset($undefinedVar)` or `foreach` loops. Use `<property name="ignoreUnsetUsage" value="true"/>` or whitelist variables in the ruleset. For Laravel’s `collect()` or `foreach` patterns, test edge cases and refine the ruleset.
- Is this package maintained for future PHPCS versions?
- The package is actively maintained, but PHPCS major updates (e.g., v4.x) may require adjustments. Monitor the [GitHub repo](https://github.com/sirbrillig/phpcs-variable-analysis) for compatibility notes. Pin PHPCS versions in `composer.json` to avoid breakages.
- What’s the performance impact on large Laravel apps?
- The package is optimized for speed (2x faster in v3.0 via `phpcsutils`). For monorepos or 10K+ files, use PHPCS’s `--parallel` flag. Benchmark in your CI pipeline—most Laravel projects see minimal runtime impact during static analysis.