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

Rector Laravel Laravel Package

driftingly/rector-laravel

Community-maintained Rector extension for Laravel. Apply automated refactoring rules to upgrade Laravel (and first-party packages like Cashier/Livewire) via composer-based detection or manual version sets, helping modernize codebases safely and consistently.

View on GitHub
Deep Wiki
Context7
## Getting Started

### Minimal Steps
1. **Installation** (updated for Rector 2.4.1 compatibility):
   ```bash
   composer require --dev driftingly/rector-laravel:^2.3.0
  1. Basic Configuration (auto-detects Laravel version):
    // rector.php
    use Rector\Config\RectorConfig;
    use RectorLaravel\Set\LaravelSetProvider;
    
    return RectorConfig::configure()
        ->withSetProviders(LaravelSetProvider::class)
        ->withComposerBased(laravel: true);
    
  2. Run Rector (updated for Rector 2.4.1):
    vendor/bin/rector process src
    

First Use Case: Laravel 13 Migration

# Dry-run to preview Laravel 13 changes
vendor/bin/rector process src --set=LaravelLevelSetList::UP_TO_LARAVEL_130 --dry-run

# Apply Laravel 13-specific rules
vendor/bin/rector process src --set=LaravelLevelSetList::LARAVEL_130

Implementation Patterns

Workflows

  1. Laravel 13-Specific Upgrades:

    // rector.php
    return RectorConfig::configure()
        ->withSets([
            LaravelLevelSetList::UP_TO_LARAVEL_130, // Includes 11→12→13 rules
            LaravelSetList::LARAVEL_13_MODEL_ATTRIBUTES, // New in 2.3.0
            LaravelSetList::LARAVEL_13_QUEUE_JOBS, // New in 2.3.0
        ]);
    
  2. Validation Rule Improvements:

    // rector.php
    return RectorConfig::configure()
        ->withSets([
            LaravelSetList::LARAVEL_VALIDATION_RULES, // Fixes closure handling in rules()
        ]);
    
  3. Livewire 4.0 Migration:

    // rector.php
    return RectorConfig::configure()
        ->withSets([
            LaravelSetList::LIVEWIRE_40, // Fixed associative array handling
        ]);
    
  4. CSRF Middleware Update (Laravel 13):

    // rector.php
    return RectorConfig::configure()
        ->withSets([
            LaravelSetList::LARAVEL_13_MIDDLEWARE, // Renames PreventRequestsForgery
        ]);
    

Integration Tips

  • CI/CD Pipeline (updated for Rector 2.4.1):

    # .github/workflows/rector.yml
    jobs:
      rector:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - run: composer require driftingly/rector-laravel:^2.3.0
          - run: vendor/bin/rector process src --set=LaravelLevelSetList::UP_TO_LARAVEL_130 --dry-run
    
  • Pre-Commit Hook (with parallel processing):

    // rector.php
    return RectorConfig::configure()
        ->withAutoloadPaths([__DIR__.'/app'])
        ->withParallel()
        ->withSets([LaravelSetList::LARAVEL_CODE_QUALITY]);
    

Gotchas and Tips

Pitfalls

  1. Laravel 13 Model Attributes:

    • Issue: UP_TO_LARAVEL_130 may incorrectly generate Table attribute arguments.
    • Fix: Use explicit LARAVEL_13_MODEL_ATTRIBUTES and verify generated code.
  2. Validation Rule Closures:

    • Issue: ValidationRuleArrayStringValueToArrayRector may fail on complex rules() method closures.
    • Fix: Test with --dry-run first or exclude problematic files.
  3. Middleware Renaming:

    • Issue: PreventRequestsForgery middleware may not be found after upgrade.
    • Fix: Use LARAVEL_13_MIDDLEWARE set or manually update imports.
  4. Rector 2.4.1 Migration:

    • Issue: getFile() method changes may break custom rules.
    • Fix: Update custom rules to use Rector 2.4.1's file handling API.

Debugging

  • Verbose Output (with rule details):

    vendor/bin/rector process src --verbose --set=LaravelLevelSetList::LARAVEL_130
    
  • Rule-Specific Debugging (updated for Rector 2.4.1):

    // rector.php
    return RectorConfig::configure()
        ->withRules([
            \RectorLaravel\Rector\MethodCall\WhereToWhereLikeRector::class,
            \RectorLaravel\Rector\Class_\RenameClassRector::class, // Fixed in 2.3.0
        ])
        ->withRuleSkip([
            \RectorLaravel\Rector\Class_\RenameClassRector::class => [
                __DIR__.'/app/Models/User.php:100',
            ],
        ]);
    

Extension Points

  1. Custom Laravel 13 Rules:

    composer require rector/rector:^2.4.1
    composer make:rule -- SingleQueueableTraitRector
    
  2. Configurable Rules (updated examples):

    // rector.php
    return RectorConfig::configure()
        ->withConfiguredRule(
            \RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector::class,
            ['dd', 'dump', 'log', 'info'] // Extended debug functions
        )
        ->withConfiguredRule(
            \RectorLaravel\Rector\Class_\AddTableAttributeRector::class,
            ['connection' => 'mysql'] // Custom table attribute config
        );
    
  3. Post-Rector Hooks (with type declarations):

    // rector.php
    return RectorConfig::configure()
        ->withPostProcessors([
            \RectorLaravel\PostProcessor\AddMissingUsesPostProcessor::class,
            \Rector\PostProcessor\TypeDeclarationPostProcessor::class, // New in 2.4.1
        ]);
    

Pro Tips

  • Selective Laravel 13 Application:

    vendor/bin/rector process app/Models --set=LaravelSetList::LARAVEL_13_MODEL_ATTRIBUTES
    vendor/bin/rector process app/Jobs --set=LaravelSetList::LARAVEL_13_QUEUE_JOBS
    
  • IDE-Friendly Setup:

    // rector.php
    return RectorConfig::configure()
        ->withSets([
            LaravelSetList::LARAVEL_TYPE_DECLARATIONS, // Enhanced in 2.4.1
            LaravelSetList::LARAVEL_13_MODEL_ATTRIBUTES,
        ]);
    
  • Backup Strategy (with git):

    git stash --keep-index && \
    vendor/bin/rector process src --set=LaravelLevelSetList::UP_TO_LARAVEL_130 && \
    git diff --cached
    
  • Livewire 4.0 Migration:

    vendor/bin/rector process resources/views --set=LaravelSetList::LIVEWIRE_40
    

New in 2.3.0

  1. Laravel 13 Support:

    • Model attributes ($attributes property)
    • Queue job attributes ($queueable trait)
    • Middleware renaming (PreventRequestsForgery)
  2. Validation Improvements:

    • Better handling of closures in rules() method
  3. Livewire 4.0 Fixes:

    • Proper associative array handling in RenameClassRector
  4. Performance:

    • Parallel processing improvements
    • Rector 2.4.1 compatibility

NO_UPDATE_NEEDED would not apply here as this release introduces significant new features and fixes that warrant updating the assessment.
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