sirbrillig/phpcs-variable-analysis
PHPCS plugin that analyzes variable usage: warns on undefined variables (including in unset), unused variables, and use of $this/self/static outside class scope. Works with PHPCS 3.13.5+ and PHP 5.4+.
Installation:
composer require --dev sirbrillig/phpcs-variable-analysis
Add to your phpcs.xml:
<rule ref="VariableAnalysis"/>
First Use Case: Run PHPCS on a file to detect undefined/unused variables:
phpcs app/YourController.php
Example output:
FILE: app/YourController.php
----------------------------------------------------------------------
FOUND 2 ERROR(S) AND 1 WARNING(S) AFFECTING 2 LINE(S)
----------------------------------------------------------------------
10 | ERROR | Undefined variable: $undefinedVar
20 | WARNING | Unused variable: $unusedVar
Key Files:
ruleset.xml.example (for customization)src/VariableAnalysis/CodeAnalysis/VariableAnalysis.php (core logic)Integrate into CI/CD: Add PHPCS to your pipeline (e.g., GitHub Actions):
- name: Run PHPCS
run: phpcs --standard=VariableAnalysis --extensions=php .
Targeted Scans: Focus on critical files:
phpcs app/ --standard=VariableAnalysis --report=full
Custom Rulesets:
Create a project-specific phpcs.xml:
<ruleset>
<rule ref="VariableAnalysis">
<properties>
<property name="allowUnusedFunctionParameters" value="true"/>
<property name="validUnusedVariableNames" value="tmp debug"/>
</properties>
</rule>
</ruleset>
$user vs $users).SquizLabs_PHP_CodeSniffer or PSR12 for broader coverage.VariableAnalysis by subclassing VariableAnalysis and overriding methods like process().--cache flag.False Positives:
allowUndefinedVariablesInFileScope="true" for template files.allowUnusedVariablesBeforeRequire="true" for files with require_once.sitePassByRefFunctions for custom functions like:
<property name="sitePassByRefFunctions" value="my_func:1,2"/>
Scope Issues:
simplify detecting class properties (v2.11.20+) for accurate property analysis.Configuration Conflicts:
phpcs.xml after the base rule:
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
<properties>
<property name="ignoreUnusedRegexp" value="/^_/"/>
</properties>
</rule>
--verbose to see sniff execution details:
phpcs --standard=VariableAnalysis --verbose app/Controller.php
--sniffs flag to test individual sniffs:
phpcs --sniffs=VariableAnalysis app/ --report=json
ignoreUnusedRegexp patterns with PHP’s preg_match() before applying.Custom Sniffs:
Extend the base sniff by copying VariableAnalysis.php and modifying:
init(): Add new properties.process(): Inject custom logic (e.g., ignore variables in specific namespaces).PHPCSUtils Integration:
Leverage phpcsutils (dependency since v3.0.0-beta.9) for advanced AST traversal:
use PHPCSUtils\Token\TokenUtils;
// Example: Check for variable usage in a specific context.
Performance Tuning:
<exclude-pattern>*/tests/*</exclude-pattern>
--parallel for multi-core processing (PHPCS ≥3.6.0).phpcs-changed to avoid false positives in dynamic files.--ignore:
phpcs --ignore=UndefinedVariable app/legacy/
<property name="allowWordPressPassByRefFunctions" value="true"/>
```markdown
### Laravel-Specific Notes
1. **Service Container**:
Ignore container-bound variables (e.g., `$app`) by adding to `validUndefinedVariableNames`:
```xml
<property name="validUndefinedVariableNames" value="app container"/>
Eloquent Models: Exclude model properties from unused checks if dynamically populated:
<property name="ignoreUnusedRegexp" value="/^_|^relation_|^attribute_"/>
Facades:
Mark facade variables (e.g., $cache) as valid to avoid conflicts with global helpers:
<property name="validUndefinedVariableNames" value="cache auth request"/>
Artisan Commands:
Use allowUnusedParametersBeforeUsed="true" for CLI arguments:
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
<properties>
<property name="allowUnusedParametersBeforeUsed" value="true"/>
</properties>
</rule>
How can I help you explore Laravel packages today?