Weave 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 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

Getting Started

Minimal Steps

  1. Installation:

    composer require --dev dealerdirect/phpcodesniffer-composer-installer
    composer require --dev sirbrillig/phpcs-variable-analysis
    

    Add to your phpcs.xml:

    <rule ref="VariableAnalysis"/>
    
  2. First Use Case: Run PHPCS on a Laravel controller or service:

    phpcs app/Http/Controllers/ --standard=VariableAnalysis
    

    Focus on files with undefined variables (e.g., typos) or unused variables (e.g., leftover debug code).


Implementation Patterns

Workflows

  1. CI/CD Integration: Add to .github/workflows/phpcs.yml:

    - name: Run PHPCS
      run: phpcs --standard=VariableAnalysis --extensions=php app/
    

    Fail builds on warnings (adjust severity in phpcs.xml):

    <arg name="severity" value="10"/>
    
  2. Laravel-Specific Customization: Override defaults in phpcs.xml for Laravel patterns:

    <rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
        <properties>
            <property name="allowUnusedFunctionParameters" value="true"/>
            <property name="allowWordPressPassByRefFunctions" value="true"/>
            <property name="sitePassByRefFunctions" value="Laravel\Helper\str_replace:1"/>
        </properties>
    </rule>
    
  3. Targeted Analysis: Use --ignore to focus on critical files:

    phpcs --standard=VariableAnalysis --ignore="*,tests/*" app/
    
  4. Pre-Commit Hooks: Add to .git/hooks/pre-commit:

    #!/bin/sh
    phpcs --standard=VariableAnalysis --extensions=php --warning-severity=5 "$@"
    

Gotchas and Tips

Pitfalls

  1. False Positives in Templates: Disable for Blade templates:

    <file>resources/views/*.blade.php</file>
    <rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
        <properties>
            <property name="allowUndefinedVariablesInFileScope" value="true"/>
        </properties>
    </rule>
    
  2. Constructor Promotion Confusion: Ensure sitePassByRefFunctions includes Laravel’s promoted properties:

    <property name="sitePassByRefFunctions" value="__construct:1,2,3"/>
    
  3. Arrow Functions: Nested arrow functions may trigger false negatives. Use:

    <property name="allowUnusedForeachVariables" value="false"/>
    
  4. Global Variables: Exclude globals like $_ENV or $_SERVER:

    <property name="validUndefinedVariableRegexp" value="/^_|^GLOBALS\["/>
    

Debugging

  • Verbose Output:
    phpcs -v --standard=VariableAnalysis app/
    
  • Isolate Rules: Test individual sniffs:
    phpcs --standard=VariableAnalysis --sniffs=UndefinedVariable app/
    

Extension Points

  1. Custom Regex: Ignore magic methods (e.g., __get):

    <property name="ignoreUnusedRegexp" value="/^__\w+$|^_"/>
    
  2. Framework-Specific Rules: Add Laravel’s app() helper:

    <property name="sitePassByRefFunctions" value="app():1"/>
    
  3. Performance: Cache results in CI:

    phpcs --standard=VariableAnalysis --cache-file=.phpcs-cache app/
    
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.
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo
stevenay/myanfont
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc