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

Codestyler Laravel Package

dragon-code/codestyler

Codestyler by The Dragon Code automatically fixes coding style issues in PHP/Laravel projects using Rector and Laravel Pint. Installs via Composer and can publish pint.json and .editorconfig via CLI for consistent formatting and linting.

View on GitHub
Deep Wiki
Context7
## Getting Started

### Minimal Setup
1. **Install the package** in your Laravel project:
   ```bash
   composer require dragon-code/codestyler --dev
  1. Add to composer.json under scripts.post-update-cmd:
    "scripts": {
        "post-update-cmd": [
            "vendor/bin/codestyle pint 8.5",
            "vendor/bin/codestyle rector laravel",
            "vendor/bin/codestyle editorconfig"
        ]
    }
    
  2. Run once to generate config files:
    composer update
    

First Use Case: Quick Style Fix

Run the style command to auto-fix PHP, JS, CSS, and JSON:

composer style

This executes:

  • Pint (PHP formatter)
  • Rector (PHP refactoring)
  • Biome (JS/CSS/JSON formatter/linter)

Implementation Patterns

1. PHP-Only Workflow

For PHP-only projects, simplify composer.json:

"scripts": {
    "style": [
        "vendor/bin/rector",
        "vendor/bin/pint --parallel"
    ]
}

Run with:

composer style

2. Custom Rector Presets

Override default Rector rules by publishing its config:

vendor/bin/codestyle rector laravel --publish-config

Edit config/rector.php to add custom rules (e.g., SetListSyntax::TO_SHORT).

3. Excluding Files/Directories

Skip specific paths in .editorconfig or pint.json:

// pint.json
{
    "paths": [
        "app/**/*.php",
        "!app/Excluded/**/*.php"
    ]
}

4. Git Hook Integration

Add to .git/hooks/pre-commit:

#!/bin/sh
composer style

Ensure it runs only on staged PHP/JS files for performance.

5. CI/CD Pipeline

Use the provided GitHub Actions template:

# .github/workflows/code-style.yml
jobs:
    Checks:
        uses: TheDragonCode/.github/.github/workflows/code-style.yml@main
        with:
            php: true
            node: false  # Disable if not needed

6. Team Onboarding

  1. Generate configs once:
    composer update
    
  2. Share .editorconfig, pint.json, and biome.json via Git.
  3. Document the composer style command in your CONTRIBUTING.md.

Gotchas and Tips

Pitfalls

  1. PHP Version Mismatch

    • Codestyler defaults to PHP 8.5. If your project uses an older version (e.g., 8.2), specify it explicitly:
      vendor/bin/codestyle pint 8.2
      
    • Fix: Update composer.json scripts to match your PHP version.
  2. Biome Not Running

    • If package.json is missing, Biome commands fail silently.
    • Fix: Set CODESTYLER_SKIP_NODE=true in CI or ensure package.json exists.
  3. Rector Breaking Changes

    • Rector’s laravel preset includes risky rules (e.g., modernize_strpos).
    • Fix: Review Rector’s docs before enabling or disable risky rules in rector.php:
      return [
          'rules' => [
              // Disable risky rules
              \Rector\Modernize\Rector\String_\ModernizeStrposRector::class => false,
          ],
      ];
      
  4. EditorConfig Conflicts

    • IDEs may ignore .editorconfig if not properly configured.
    • Fix: Verify IDE settings (e.g., PhpStorm: File | Settings | Editor | Code Style | Scheme: Project).
  5. Composer Normalize Overwriting

    • composer normalize may reformat composer.json unexpectedly.
    • Fix: Exclude it from post-update-cmd if your team prefers manual formatting.

Debugging Tips

  • Dry Run: Test changes without writing to files:
    vendor/bin/pint --dry-run
    vendor/bin/rector --dry-run
    
  • Verbose Output: Enable debug logs for Codestyler:
    vendor/bin/codestyle --verbose
    
  • Isolated Testing: Run Codestyler on a single file:
    vendor/bin/pint app/Http/Controllers/ExampleController.php
    

Extension Points

  1. Custom Pint Presets Extend pint.json with project-specific rules:

    {
        "preset": "laravel",
        "rules": {
            "@PHPUnit": true,
            "no_unused_imports": true
        }
    }
    
  2. Rector Custom Rules Add project-specific Rector rules:

    // config/rector.php
    return [
        'rules' => [
            \Rector\CodeQuality\Rector\Class_\CompletePhpdocByShortVarRector::class,
            \App\Custom\Rector\MyCustomRector::class, // Your custom rector
        ],
    ];
    
  3. Biome Custom Config Override Biome’s biome.json to exclude analytics/metrics files:

    {
        "linter": {
            "ignores": ["analytics/**", "metrics/**"]
        }
    }
    
  4. Post-Style Hooks Add cleanup tasks after composer style:

    "scripts": {
        "style": [
            "vendor/bin/rector",
            "vendor/bin/pint --parallel",
            "@post-style" // Custom script
        ],
        "post-style": [
            "php artisan optimize:clear"
        ]
    }
    

Performance Optimizations

  • Parallel Processing: Enable Pint’s parallel mode for large codebases:
    vendor/bin/pint --parallel
    
  • Exclude Tests: Skip formatting tests in pint.json:
    "paths": ["app/**/*.php", "!tests/**"]
    
  • Cache Rector: Use Rector’s cache for faster repeated runs:
    vendor/bin/rector --cache-dir=var/rector_cache
    

Config Quirks

  • Symfony 8 Compatibility: Ensure symfony/* packages are updated to avoid conflicts.
  • Global Installation: If using globally (composer global require), replace vendor/bin/codestyle with codestyle in scripts.
  • Self-Styling: Codestyler auto-styles its own code. To disable:
    vendor/bin/codestyle --skip-self
    

---
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
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
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation