Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Phpcs Variable Analysis

Phpcs Variable Analysis Laravel Package

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+.

View on GitHub
Deep Wiki
Context7

A PHPCS sniff to detect problems with variables.

Frequently asked questions about Phpcs Variable Analysis
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai