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

lcobucci/coding-standard

PHP_CodeSniffer ruleset based on Doctrine’s coding standard with small tweaks. Install and use it in your projects to enforce consistent formatting and code style via phpcs in CI and local development.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the package via Composer:
    composer require --dev lcobucci/coding-standard
    
  2. Integrate with PHP_CodeSniffer (PHPCS) by adding this to your composer.json under scripts:
    "scripts": {
        "cs-check": "phpcs --standard=vendor/lcobucci/coding-standard",
        "cs-fix": "php-cs-fixer fix --rules=@lcobucci"
    }
    
  3. Run a quick check on your project:
    composer cs-check
    

First Use Case: CI/CD Enforcement

Add a GitHub Actions workflow (.github/workflows/coding-standard.yml) to block PR merges on violations:

name: Coding Standard
on: [push, pull_request]
jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.4'
      - run: composer install
      - run: composer cs-check

Implementation Patterns

Workflow: Local Development

  1. Pre-commit Hook (using php-cs-fixer):
    composer cs-fix  # Auto-fix violations before committing
    
  2. Manual Checks:
    composer cs-check app/Http/Controllers/  # Scope to a directory
    

Workflow: CI/CD

  • Fail Fast: Configure CI to exit with non-zero status on violations (default PHPCS behavior).
  • Parallelize: Run PHPCS on multiple directories in parallel (e.g., app/, tests/) to speed up CI.

Integration with Laravel

  1. Laravel Valet/Laravel Sail: Add a custom command to app/Console/Kernel.php:

    protected function commands()
    {
        $this->load(__DIR__.'/Commands/CodingStandardCheck.php');
    }
    

    Then run:

    php artisan coding-standard:check
    
  2. Laravel Forge/Envoyer: Add a deploy hook to run composer cs-check before code is merged to production.

Customization

  • Override Rules: Extend the standard by creating a custom .php-cs-fixer.dist.php:
    return (new PhpCsFixer\Config())
        ->setRules([
            '@lcobucci' => true,
            'array_syntax' => ['syntax' => 'short'], // Override array syntax
        ]);
    
  • Ignore Files: Use PHPCS’s --ignore flag or .phpcsignore:
    composer cs-check --ignore=vendor/
    

Gotchas and Tips

Pitfalls

  1. PHP Version Mismatch:

    • The package requires PHP 8.4+. If your project uses an older version, either upgrade or use doctrine/coding-standard directly.
    • Fix: Update php.ini or use a .php-version file in your repo.
  2. PHPCS 4.0+ Dependency:

    • Older PHPCS versions (<4.0) may cause errors. Ensure your php-cs-fixer and phpcodesniffer are updated:
      composer require --dev php-cs-fixer:^3.30 phpcodesniffer:^4.0
      
  3. False Positives in Legacy Code:

    • Strict rules (e.g., DisallowImplicitArrayCreation) may flag legacy code. Use --ignore or .phpcsignore to exclude problematic files temporarily.
  4. Attribute Formatting:

    • The standard enforces consistent attribute formatting (e.g., [Route('/')]). If your team uses a different style, override it in php-cs-fixer:
      'attributes' => ['order' => 'attributes'],
      

Debugging Tips

  • Verbose Output: Run PHPCS with -v for detailed error messages:
    composer cs-check -v
    
  • Fix Specific Issues: Use php-cs-fixer to auto-fix common violations:
    composer cs-fix --dry-run  # Preview changes
    composer cs-fix            # Apply fixes
    
  • Custom Error Groups: PHPCS groups errors by rule. Use --report=full to see all violations:
    composer cs-check --report=full
    

Extension Points

  1. Add Custom Rules: Extend the standard by creating a custom PHPCS rule and merging it with lcobucci/coding-standard:

    composer require --dev your/custom-phpcs-rule
    

    Then update your PHPCS config to include it.

  2. Modify Rule Severity: Adjust severity levels (e.g., treat warnings as errors) in your PHPCS config:

    composer cs-check --warning-severity=0
    
  3. Parallel Execution: Speed up CI by running PHPCS on multiple files in parallel (requires PHPCS 4.0+):

    composer cs-check --parallel=8
    

Pro Tips

  • Pair with phpstan: Combine with phpstan/extension-installer for static analysis:

    composer require --dev phpstan/phpstan
    

    Then add to composer.json:

    "scripts": {
        "test": "phpstan analyse --level=5"
    }
    
  • Template for New Projects: Use this composer.json template for new Laravel projects:

    {
        "require-dev": {
            "lcobucci/coding-standard": "^12.0",
            "php-cs-fixer": "^3.30",
            "phpcodesniffer": "^4.0"
        },
        "scripts": {
            "cs-check": "phpcs --standard=vendor/lcobucci/coding-standard",
            "cs-fix": "php-cs-fixer fix --rules=@lcobucci"
        }
    }
    
  • Git Hooks: Automate local checks with a pre-commit hook (e.g., using Husky):

    composer require --dev husky
    npx husky add .husky/pre-commit "composer cs-check"
    
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.
hamzi/corewatch
minionfactory/raw-hydrator
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