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

Coding Standard Laravel Package

phpcq/coding-standard

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Install the Package Require the package in your Laravel project:

    composer require --dev phpcq/coding-standard
    
  2. First Run with PHP_CodeSniffer Use the provided ruleset directly with phpcs:

    vendor/bin/phpcs --standard=vendor/phpcq/coding-standard/phpcs/PhpCodeQuality/ruleset.xml src/
    
  3. First Run with PHPMD Run static analysis checks:

    vendor/bin/phpmd src/ text vendor/phpcq/coding-standard/phpmd/ruleset.xml
    
  4. Quick Integration with Laravel Add a Composer script for easy access:

    "scripts": {
        "cs-fix": "phpcs --standard=vendor/phpcq/coding-standard/phpcs/PhpCodeQuality/ruleset.xml --report=full src/",
        "cs-fix:fix": "phpcbf --standard=vendor/phpcq/coding-standard/phpcs/PhpCodeQuality/ruleset.xml src/"
    }
    

    Run with:

    composer cs-fix
    
  5. Where to Look First

    • Ruleset XML: vendor/phpcq/coding-standard/phpcs/PhpCodeQuality/ruleset.xml
    • Documentation: Check the PHP_CodeSniffer docs for rule explanations.
    • Laravel-Specific Adjustments: Focus on app/ and routes/ directories initially.

Implementation Patterns

Daily Developer Workflows

  1. Local Development

    • Run checks before committing:
      composer cs-fix
      
    • Use IDE integration (e.g., PHPStorm’s PHP_CodeSniffer plugin) for real-time feedback.
  2. Pull Requests

    • Add a CI check in .github/workflows/phpcs.yml:
      - name: PHP_CodeSniffer
        run: composer cs-fix
      
    • Require approval only if checks pass.
  3. Team Standards

    • Document exceptions in CONTRIBUTING.md:
      ## Coding Standards
      - Run `composer cs-fix` before PRs.
      - Exceptions: `app/Providers/` may have dynamic properties.
      
  4. Automated Fixes

    • Use phpcbf (PHP_CodeSniffer’s fixer) for auto-correction:
      composer cs-fix:fix
      

Laravel-Specific Patterns

  1. Excluding Laravel Directories Override the ruleset to exclude Laravel-specific paths:

    <!-- phpcs.ruleset.xml -->
    <rule ref="PhpCodeQuality">
        <exclude name="App\Providers" />
        <exclude name="routes" />
        <exclude name="bootstrap/cache" />
    </rule>
    
  2. Laravel Naming Conventions Enforce Laravel’s App\ namespace prefix:

    <rule ref="PhpCodeQuality">
        <arg name="namespacePrefix" value="App\" />
    </rule>
    
  3. Integration with Laravel Mix Add checks to postcss or webpack.mix.js build scripts:

    mix.postCss('resources/css/app.css', 'public/css', [
        require('postcss-import'),
        require('tailwindcss')
    ])
    .then(() => {
        require('child_process').execSync('composer cs-fix');
    });
    
  4. Testing Workflows Skip checks for tests/ in CI but enforce in local development:

    # .github/workflows/phpcs.yml
    jobs:
      phpcs:
        runs-on: ubuntu-latest
        steps:
          - run: composer cs-fix --ignore=tests/
    

CI/CD Patterns

  1. GitHub Actions

    name: PHP_CodeSniffer
    on: [push, pull_request]
    jobs:
      phpcs:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - run: composer install --dev
          - run: vendor/bin/phpcs --standard=vendor/phpcq/coding-standard/phpcs/PhpCodeQuality/ruleset.xml src/
    
  2. GitLab CI

    phpcs:
      stage: test
      script:
        - composer cs-fix
      only:
        - merge_requests
    
  3. Parallel Checks Split checks across files/directories for faster CI:

    - name: PHPCS (App)
      run: vendor/bin/phpcs src/App/
    - name: PHPCS (Routes)
      run: vendor/bin/phpcs src/routes/
    

Gotchas and Tips

Common Pitfalls

  1. False Positives in Laravel

    • Dynamic Properties: Laravel uses dynamic properties (e.g., Model::$relation). Exclude or suppress:
      <rule ref="Generic.Files.LineEndings" />
      <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
          <exclude name="App\Models" />
      </rule>
      
    • Magic Methods: Laravel’s __call, __get, etc., may trigger rules. Add exceptions:
      <rule ref="Squiz.Commenting.InlineComment">
          <exclude name="App\Concerns" />
      </rule>
      
  2. Performance in CI

    • Exclude Large Files: Skip tests/, storage/, and vendor/:
      vendor/bin/phpcs --ignore=tests/,storage/,vendor/ src/
      
    • Cache Results: Use tools like phpcs-cache to avoid rescanning unchanged files.
  3. Rule Conflicts

    • PSR-12 vs. PhpCodeQuality: Some rules may conflict. Audit with:
      vendor/bin/phpcs --standard=PSR12 src/ | head -n 20
      vendor/bin/phpcs --standard=PhpCodeQuality src/ | head -n 20
      
    • Merge Rulesets: Combine with PSR-12 if needed:
      <config name="standard" value="PSR12,PhpCodeQuality" />
      
  4. IDE Integration Issues

    • PHPStorm: Ensure the PHP_CodeSniffer plugin is configured to use the correct ruleset path:
      vendor/phpcq/coding-standard/phpcs/PhpCodeQuality/ruleset.xml
      
    • VSCode: Use the PHP Intelephense extension with phpcs configured in settings.json:
      "intelephense.phpcsStandard": "vendor/phpcq/coding-standard/phpcs/PhpCodeQuality/ruleset.xml"
      

Debugging Tips

  1. Verbose Output Run with --report=full for detailed violations:

    vendor/bin/phpcs --standard=PhpCodeQuality --report=full src/
    
  2. Isolate Violations Check specific files or directories:

    vendor/bin/phpcs src/App/Http/Controllers/
    
  3. Temporary Exclusions Use comments to suppress specific violations:

    // phpcs:ignore Generic.Files.LineEndings
    
  4. Update Rulesets Monitor for updates and test before applying:

    composer update phpcq/coding-standard --dev
    

Extension Points

  1. Custom Rules Extend the ruleset by adding new rules:

    <rule ref="PhpCodeQuality">
        <file>src/App/Rules/LaravelSpecificRule.php</file>
    </rule>
    
  2. PHPMD Custom Rules Add PHPMD rules for Laravel-specific smells:

    <rule ref="phpmd/rules/naming.xml">
        <exclude name="src/App/Providers" />
    </rule>
    
  3. Composer Scripts Create reusable scripts in composer.json:

    "scripts": {
        "cs-check": "vendor/bin/phpcs --standard=PhpCodeQuality src/",
        "cs-check:fix": "vendor/bin/phpcbf --standard=PhpCodeQuality src/",
        "cs-check:ci": "vendor/bin/phpcs --standard=PhpCodeQuality --ignore=tests/ src/"
    }
    
  4. Environment-Specific Configs Use different rulesets for local vs. CI:

    # Local (strict)
    vendor/bin/phpcs --standard=PhpCodeQuality src/
    
    # CI (lenient)
    vendor/bin/phpcs --standard=PhpCodeQuality --ignore=tests/ src/
    

Laravel-Specific Quirks

  1. Facade Usage Laravel’s facades (e.g., Route::get()) may trigger unused-function calls. Exclude:

    <rule ref="UnusedFunction">
        <exclude name="src/routes" />
    </rule>
    
  2. Blade Templates Exclude Blade files from phpcs

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.
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php
trappistes/laravel-custom-fields
splash/sonata-admin
splash/metadata