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

Php Cs Fixer Laravel Package

fabpot/php-cs-fixer

PHP CS Fixer is a command-line tool for automatically fixing PHP coding standards issues. It formats and modernizes code using configurable rulesets, supports custom rule configurations, and helps keep projects consistent and clean across teams and CI.

Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Code Quality Enforcement: Fits seamlessly into a PHP/Laravel project as a pre-commit hook or CI/CD pipeline tool to enforce PSR-12 (or custom) coding standards.
  • Developer Experience (DX): Reduces manual code review effort by automating formatting, improving team consistency.
  • Toolchain Integration: Complements existing tools like PHPStan, Psalm, or Pest by focusing on fixable issues (vs. static analysis).
  • Laravel-Specific Use Cases:
    • Enforcing standards in custom packages (e.g., vendor:publish templates).
    • Standardizing migrations, factories, or console commands.
    • Aligning with Laravel’s evolving conventions (e.g., use statement ordering).

Integration Feasibility

  • Low Barrier to Entry: PHP-CS-Fixer is a standalone CLI tool with zero Laravel dependencies, making integration trivial.
  • Configuration Flexibility:
    • Supports .php-cs-fixer.dist.php for project-specific rules (e.g., overriding Laravel’s array_syntax to consistent).
    • Can be tied to Laravel’s config/app.php via environment variables (e.g., CS_FIXER_RULES).
  • IDE/Editor Plugins: Works with PHPStorm, VSCode (via extensions) for real-time feedback.

Technical Risk

  • Rule Conflicts: Custom rules (e.g., no_unused_imports) may clash with Laravel’s autoloading or facade usage.
    • Mitigation: Test with php-cs-fixer fix --dry-run before enforcing.
  • Performance Overhead:
    • Large codebases (e.g., Laravel + custom packages) may slow down CI if not cached.
    • Mitigation: Use --cache-file and parallel processing (--parallel).
  • False Positives:
    • Aggressive rules (e.g., ordered_imports) may break dynamic use statements in Laravel’s AppServiceProvider.
    • Mitigation: Whitelist directories (e.g., exclude: [app/Providers/AppServiceProvider.php]).

Key Questions

  1. Rule Set: Should we use PSR-12, Laravel’s default, or a custom config? How will this evolve with Laravel upgrades?
  2. Enforcement Level:
    • Pre-commit (local) vs. CI (blocking) vs. post-merge (non-blocking)?
    • Example: git hooks (Husky) vs. GitHub Actions.
  3. Legacy Code: How will we handle existing violations? Gradual fixes vs. big-bang enforcement?
  4. Toolchain Sync:
    • Should this replace or complement existing tools (e.g., laravel-pint)?
    • Note: pint is Laravel’s official formatter; PHP-CS-Fixer is more granular.
  5. Custom Rules: Do we need project-specific rules (e.g., for Blade templates or Tailwind classes)?

Integration Approach

Stack Fit

  • PHP 8.0+: PHP-CS-Fixer supports modern PHP; aligns with Laravel’s 8.0+ LTS requirements.
  • Composer Dependency: Install via composer require --dev friendsofphp/php-cs-fixer.
  • Laravel-Specific Integrations:
    • Artisan Command: Create a custom command (php artisan cs:fix) to wrap PHP-CS-Fixer.
    • Service Provider: Register a fixer service for dynamic rule loading.
    • Testing: Use php-cs-fixer in phpunit.xml as a test listener to fail builds on violations.

Migration Path

  1. Pilot Phase:
    • Run php-cs-fixer fix --rules=@PSR12 --dry-run on a non-critical branch.
    • Review changes with git diff; adjust rules as needed.
  2. Incremental Rollout:
    • Start with new PRs (CI-only enforcement).
    • Gradually apply to existing code via small, focused commits.
  3. Toolchain Updates:
    • Replace php-cs-fixer with pint if formatting-only needs are sufficient.
    • Use both tools for orthogonal concerns (e.g., pint for files, php-cs-fixer for logic).

Compatibility

  • Laravel Ecosystem:
    • Works with Laravel Mix/Vite, Blade templates (via --path-mode-intersection), and custom packages.
    • Caveat: Blade files require explicit rule configuration (e.g., blade_case_enum).
  • CI/CD Pipelines:
    • GitHub Actions: Cache results between runs for speed.
      - name: PHP-CS-Fixer
        uses: docker://oskarstark/php-cs-fixer-ga
        with:
          args: --rules=@PSR12 --diff
      
    • GitLab CI: Use php-cs-fixer as a pre-job to fail fast.
  • Local Development:
    • VSCode Extension: Real-time feedback via PHP Intelephense + PHP-CS-Fixer.
    • Pre-commit Hook: Use composer-hooks to run fixer automatically.

Sequencing

  1. Configuration:
    • Define .php-cs-fixer.dist.php with Laravel-aware rules (e.g., preserve use Illuminate\Support\Facades\Log;).
    • Example:
      return PhpCsFixerConfig::create()
          ->setRules([
              '@PSR12' => true,
              'array_syntax' => ['syntax' => 'short'],
              'ordered_imports' => ['sort_algorithm' => 'alpha'],
              'no_unused_imports' => true,
          ])
          ->setPathModeIntersection(true);
      
  2. Tooling Setup:
    • Add to composer.json under require-dev.
    • Configure CI/CD (e.g., GitHub Actions).
  3. Enforcement:
    • Start with CI-only (non-blocking).
    • Transition to pre-commit after team adoption.
  4. Monitoring:
    • Track false positives and adjust rules.
    • Measure time saved in code reviews.

Operational Impact

Maintenance

  • Rule Updates:
    • PHP-CS-Fixer releases new rules regularly (e.g., php80_migration).
    • Action: Pin version in composer.json or update annually.
  • Laravel Versioning:
    • If Laravel introduces new coding standards, update .php-cs-fixer.dist.php.
    • Example: Laravel 10’s strict typing may require php81_migration rules.
  • Dependency Management:
    • Monitor for security updates (though PHP-CS-Fixer is low-risk).

Support

  • Onboarding:
    • Document common rule exceptions (e.g., "Why is AppServiceProvider excluded?").
    • Provide a cheat sheet for developers (e.g., "How to fix no_unused_imports errors").
  • Troubleshooting:
    • Debugging: Use --verbose and --diff flags to diagnose issues.
    • Community: Leverage the Symfony ecosystem (PHP-CS-Fixer is maintained by the same team).
  • Tooling Support:
    • Ensure CI/CD admins know how to cache results to avoid flaky builds.

Scaling

  • Performance:
    • Large Codebases: Use --cache-file and --parallel to reduce CI runtime.
    • Distributed Teams: Cache results in GitHub/GitLab caches or Artifact Storage.
  • Parallelization:
    • Split rules by file type (e.g., run blade_case_enum only on .blade.php files).
  • Incremental Adoption:
    • Start with critical paths (e.g., app/Http/Controllers/) before expanding.

Failure Modes

Failure Mode Impact Mitigation
Rule Overkill Breaks legacy code unexpectedly. Use --dry-run and whitelist directories.
CI Timeouts Slow runs block deployments. Cache results, parallelize, or use smaller batches.
Rule Drift Outdated rules cause merge conflicts. Pin versions, review rules annually.
IDE Plugin Issues False positives in VSCode/PHPStorm. Configure IDE to ignore .php-cs-fixer.cache.
Blade Template Conflicts Rules misapply to Blade syntax. Exclude Blade files or use path-mode-intersection.

Ramp-Up

  • Training:
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport