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+.
$this/static (relevant in Laravel’s service container and facades).dealerdirect/phpcodesniffer-composer-installer meets this).phpcs-changed (from the same author) to skip unchanged files, reducing CI noise.eval or dynamic includes).| Risk Area | Mitigation Strategy |
|---|---|
| False Positives | Customize via ruleset.xml (e.g., whitelist globals like $_ENV, config()). |
| Performance | PHPCS runs incrementally; cache results with phpcs --cache. |
| PHPCS Version Drift | Pin PHPCS version in composer.json to avoid breaking changes. |
| Template Files | Configure allowUndefinedVariablesInFileScope for Blade templates. |
| Legacy Code | Start with --report=summary to avoid overwhelming teams with warnings. |
noUnusedVars)?$request, app()) to whitelist?allowWordPressPassByRefFunctions?dealerdirect/phpcodesniffer-composer-installer for zero-config installation.phpcs-changed to analyze only modified files (via roave/security-advisories or custom script).allowUndefinedVariablesInFileScope for dynamic globals.$this misuse in facades via validUndefinedVariableNames.composer require --dev sirbrillig/phpcs-variable-analysis
phpcs.xml:
<rule ref="VariableAnalysis" />
--report=summary to assess noise.ruleset.xml (e.g., phpcs-ruleset.xml) to:
$request, config()).allowUnusedFunctionParameters for legacy APIs.<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
<properties>
<property name="validUndefinedVariableNames" value="request config app" />
<property name="allowUnusedFunctionParameters" value="true" />
</properties>
</rule>
.github/workflows/phpcs.yml:
- name: PHP_CodeSniffer
run: phpcs --standard=phpcs-ruleset.xml --report=full src/
| Component | Compatibility Notes |
|---|---|
| PHP 8.x | Fully supported (package requires PHP ≥5.4). |
| Laravel 9/10 | No conflicts; PHPCS operates on abstract syntax. |
| Blade Templates | Requires allowUndefinedVariablesInFileScope for dynamic globals. |
| Composer | Uses phpcodesniffer-composer-installer (no manual PHPCS installation needed). |
| Windows/Linux | Cross-platform (PHPCS is agnostic). |
phpcodesniffer-composer-installer).composer.json (dev dependency).phpcs-ruleset.xml before enabling in CI.phpcs-ruleset.xml.phpcs --generate=ruleset to scaffold updates.sirbrillig/phpcs-variable-analysis and phpcsutils in composer.json to avoid surprises.validUndefinedVariableNames or ignoreUnusedRegexp.--cache in CI.CONTRIBUTING.md section on PHPCS ruleset customization.phpcs-ruleset.xml for Laravel projects.--ignore=* and gradually enable rules.allowUndefinedVariablesInFileScope.phpcs --verbose and --report=emacs for detailed output.eval), open issues in the repo.phpcs src/ app/ tests/ in parallel).phpcs-changed to analyze only modified files.--cache.vendor/ and node_modules/ from analysis.--ignore=* but enforce in CI.phpcs-ruleset.xml via team templates.| Failure Mode | Impact | Mitigation |
|---|---|---|
| False Positives | Developer frustration | Customize ruleset; start with --ignore=* |
| PHPCS Version Conflict | Build failures | Pin PHPCS version in `composer.json |
How can I help you explore Laravel packages today?