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

Phpcsfixer Configs Php Laravel Package

drupol/phpcsfixer-configs-php

Ready-made PHP-CS-Fixer config objects for common standards and PHP versions (PSR-12, PHP 5.6–7.3). Implements ConfigInterface and supports combining rules via withRulesFrom() for easy reuse across projects.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the package in your Laravel project:
    composer require --dev drupol/phpcsfixer-configs-php
    
  2. Integrate with PHP-CS-Fixer in your phpcs.xml or phpcsfixer.dist.php:
    use Drupol\PhpCsFixerConfigsPhp\Config\PSR12;
    
    $finder = PhpCsFixer\Finder::create()
        ->in(__DIR__.'/src')
        ->name('*.php');
    
    return (new PhpCsFixer\Config())
        ->setRules(new PSR12())
        ->setFinder($finder);
    
  3. Run PHP-CS-Fixer via Artisan (if using Laravel Mix or custom scripts):
    ./vendor/bin/php-cs-fixer fix
    

First Use Case

Use the PSR12 config for modern Laravel projects (recommended for Laravel 8+). For legacy PHP 5.6 projects, use Php56.


Implementation Patterns

Workflow Integration

  1. Laravel CI/CD Pipelines Add PHP-CS-Fixer to your GitHub Actions workflow:

    - name: Run PHP-CS-Fixer
      run: ./vendor/bin/php-cs-fixer fix --config=phpcsfixer.dist.php --dry-run --diff
    

    Use --dry-run for pre-commit checks.

  2. Pre-Commit Hooks Integrate with Laravel Pint (if using) or Husky:

    composer require --dev laravel/pint
    ./vendor/bin/pint --test  # Test before commit
    
  3. Custom Rule Overrides Extend base configs (e.g., PSR12) with project-specific rules:

    $config = (new PSR12())
        ->withRulesFrom([
            '@PSR12',
            'no_unused_imports' => true,
            'ordered_imports' => ['sort_algorithm' => 'alpha'],
        ]);
    
  4. Multi-PHP Version Projects Use version-specific configs (e.g., Php71 for legacy code):

    $config = (new Php71())
        ->withRulesFrom(new PSR12()); // Merge PSR12 rules
    

Laravel-Specific Tips

  • Laravel Mix: Add PHP-CS-Fixer to postcss or webpack.mix.js for build-time checks.
  • Laravel Forge: Use Deployer to run PHP-CS-Fixer on server deployments:
    task('phpcsfixer', function () {
        run('./vendor/bin/php-cs-fixer fix');
    });
    

Gotchas and Tips

Common Pitfalls

  1. Rule Conflicts

    • Avoid mixing Php56 and PSR12 rules directly; use withRulesFrom() to merge.
    • Example of conflict: array_push is enabled in Php56 but disabled in PSR12.
  2. Deprecated Rules

    • The package drops deprecated PHP-CS-Fixer rules (e.g., escape_implicit_backslashesstring_implicit_backslashes).
    • Fix: Update your config to use the new rule name.
  3. Performance

    • Exclude vendor/ and node_modules/ from PHP-CS-Fixer scans:
      $finder->exclude(['vendor', 'node_modules']);
      
  4. PHP-CS-Fixer Version Locking

    • The package pins PHP-CS-Fixer versions to avoid breaking changes (e.g., ^3.0 but excludes 3.0.13.0.3).
    • Fix: Update composer.json constraints if needed:
      "require-dev": {
          "php-cs-fixer": "^3.10"
      }
      

Debugging Tips

  • Dry Runs: Always use --dry-run in CI to preview changes:
    ./vendor/bin/php-cs-fixer fix --dry-run
    
  • Rule Validation: Test individual rules with:
    ./vendor/bin/php-cs-fixer fix --rules=@PSR12 --path-mode=intersection
    
  • Custom Cache: Speed up repeated runs with:
    $config->setCacheFile(__DIR__.'/phpcsfixer.cache');
    

Extension Points

  1. Custom Configs Create a Config class extending Drupol\PhpCsFixerConfigsPhp\Config\BaseConfig for reusable rules:

    namespace App\Configs;
    
    use Drupol\PhpCsFixerConfigsPhp\Config\PSR12;
    
    class LaravelConfig extends PSR12 {
        public function __construct() {
            $this->withRules([
                'no_unused_imports' => true,
                'native_function_invocation' => ['include' => ['@compiler_optimized']],
            ]);
        }
    }
    
  2. Dynamic Rule Loading Load rules from environment variables (e.g., .env):

    $rules = explode(',', env('PHP_CS_FIXER_RULES', ''));
    $config = (new PSR12())->withRulesFrom($rules);
    
  3. Symfony Integration For Symfony projects, use the symfony:risky preset as a base:

    $config = (new PSR12())->withRulesFrom(['@Symfony', '@Symfony:risky']);
    
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.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
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