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+.
php-cs-fixer, pest, or custom PHPCS rulesets).$request->input() vs. $request->nonexistent).$query or $builder vars may slip through).$this outside classes, relevant for Laravel’s service containers or facades).phpcsutils) claims 2x speed improvements, mitigating concerns about runtime overhead in large codebases (e.g., Laravel’s monolithic apps or packages).dealerdirect/phpcodesniffer-composer-installer meets this).laravel-pint or custom phpcs scripts (e.g., in package.json or composer.json scripts).PHP_CodeSniffer extension).phpcsutils: New dependency in 3.0.x may require composer updates, but it’s a lightweight utility library.#[AllowDynamicProperties] or magic __get() may trigger false "undefined variable" warnings. Mitigation: Use validUndefinedVariableNames or validUndefinedVariableRegexp to whitelist dynamic properties (e.g., $model->dynamic_*).@php blocks or global variables (e.g., $_ENV) may need allowUndefinedVariablesInFileScope.$app->make() or app()->bind() might confuse static analysis. Mitigation: Exclude vendor files or use ignoreUnusedRegexp for container-related vars.sitePassByRefFunctions) require XML tweaks. Risk: Overly permissive configs (e.g., allowUnusedVariablesInFileScope) could hide real issues.phpcsutils dependency may need composer.lock updates.app/ and src/?spatie/laravel-*)? Exclude via PHPCS --exclude or whitelist known vars?--report=summary first.phpcs.xml) to avoid drift?dealerdirect/phpcodesniffer-composer-installer. This package drops in without disrupting existing tooling.php artisan phpcs command for consistency.Settings > PHP > Quality Tools.PHP_CodeSniffer extension with phpcs.xml config.phpunit.xml or pest.php as a pre-test hook.feature/phpcs-variable-analysis).UndefinedVariable and UnusedVariable).app/Http/Controllers) before full rollout.--report=summary to avoid noise.--standard=VariableAnalysis alongside existing standards (e.g., PSR12).phpcs.xml snippet:
<config name="installed_paths" value="./vendor/sirbrillig/phpcs-variable-analysis"/>
<rule ref="VariableAnalysis">
<properties>
<property name="validUndefinedVariableNames" value="request response"/>
<property name="ignoreUnusedRegexp" value="/^_|^temp/"/>
</properties>
</rule>
tests/Feature/BladeTest.php) via:
<file>./tests</file>
<exclude-pattern>.*/BladeTest\.php</exclude-pattern>
validUndefinedVariableNames to whitelist magic properties (e.g., $model->attributes).$this->app->make() vars with ignoreUnusedRegexp=/^app|facade/.allowUndefinedVariablesInFileScope for global vars (e.g., $_ENV, $config).phpcodesniffer-composer-installer is installed:
composer require --dev dealerdirect/phpcodesniffer-composer-installer
composer.json to allow plugins:
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
composer require --dev sirbrillig/phpcs-variable-analysis
phpcs.xml (see Phase 2 above).vendor/bin/phpcs --standard=VariableAnalysis app/Http/Controllers/
.github/workflows/phpcs.yml:
- name: PHPCS Variable Analysis
run: vendor/bin/phpcs --standard=VariableAnalysis --report=summary .
phpcs.xml locally, leading to inconsistent enforcement..phpcs.dist.xml (committed to repo).husky + simple-phpcs) to enforce standards.phpcsutils).How can I help you explore Laravel packages today?