- How do I install this PHPCS plugin for Laravel projects?
- Run `composer require --dev sirbrillig/phpcs-variable-analysis` after installing `dealerdirect/phpcodesniffer-composer-installer`. Add `<rule ref="VariableAnalysis"/>` to your `phpcs.xml` under `<ruleset>`. No Laravel-specific setup is needed beyond PHPCS configuration.
- Will this work with Laravel Blade templates?
- Yes, but Blade’s dynamic scope may trigger false positives. Add `<property name="allowUndefinedVariablesInFileScope" value="true"/>` to the rule in `phpcs.xml` for Blade files. Alternatively, whitelist known variables like `$request` or `$session` via `validUndefinedVariableNames`.
- Does this replace PHPStan’s unused variable detection?
- No—use both tools. PHPStan catches more dynamic cases, while this PHPCS plugin runs faster in CI/CD. Exclude variables already checked by PHPStan in `phpstan.neon` to avoid redundancy. Example: `excludeChecks: ['VariableAnalysis.UnusedVariable']`.
- What Laravel versions does this support?
- Works with Laravel 8.0+ (PHP 5.4+). No Laravel-specific dependencies exist, but test thoroughly in your CI pipeline. For older Laravel (5.5–7.x), ensure PHPCS ≥3.13.5 is installed via `dealerdirect/phpcodesniffer-composer-installer`.
- How do I ignore false positives for Laravel’s dynamic properties?
- Use `validUndefinedVariableNames` or regex patterns in `phpcs.xml`. Example: `<property name="validUndefinedVariableNames" value="request,input,session"/>` or `<property name="ignoreUnusedRegexp" value="/^_|^laravel_/" />` to skip Laravel-specific variables.
- Can I run this in pre-commit hooks?
- Yes—integrate with tools like `roave/security-advisories` or `php-cs-fixer`. Add this to your `.php-cs-fixer.dist.php`: `$finder->in(__DIR__)->exclude(['vendor', 'node_modules'])->files()->name('*.php')->notName('*.blade.php')`. Run `phpcs --standard=VariableAnalysis` in your hook.
- Will this slow down my CI pipeline?
- Minimal overhead—PHPCSUtils v3.0+ improves speed by 2x. Benchmark with `phpcs --report=summary` to compare against existing sniffs (e.g., SquizLabs). For large codebases, run only on changed files: `phpcs --diff app/`.
- How do I customize rules for Laravel-specific patterns?
- Extend the rule in `phpcs.xml` with Laravel-specific properties. Example: `<rule ref="VariableAnalysis"><properties><property name="allowUnusedCaughtExceptions" value="true"/><property name="ignoreUnusedRegexp" value="/^_/" /></properties></rule>`. See the [README](https://github.com/sirbrillig/phpcs-variable-analysis) for full options.
- Does this work with PHP 8.0+ named arguments or attributes?
- Yes, but some edge cases (e.g., dynamic property access) may need manual whitelisting. Test with `#[AllowDynamicProperties]` classes by adding `<property name="validUndefinedVariableNames" value="#[AllowDynamicProperties]"/>` or regex patterns.
- What are the alternatives to this package?
- For variable analysis, consider PHPStan (`phpstan/extension-installer`) or Psalm. However, this PHPCS plugin is lighter for CI/CD and integrates natively with Laravel’s existing PHPCS setup. Use PHPStan/Psalm for deeper static analysis and PHPCS for quick, rule-based checks.