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.
## Getting Started
### Minimal Steps
1. **Installation** (updated for Rector 2.4.1 compatibility):
```bash
composer require --dev driftingly/rector-laravel:^2.3.0
// rector.php
use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelSetProvider;
return RectorConfig::configure()
->withSetProviders(LaravelSetProvider::class)
->withComposerBased(laravel: true);
vendor/bin/rector process src
# 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
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
]);
Validation Rule Improvements:
// rector.php
return RectorConfig::configure()
->withSets([
LaravelSetList::LARAVEL_VALIDATION_RULES, // Fixes closure handling in rules()
]);
Livewire 4.0 Migration:
// rector.php
return RectorConfig::configure()
->withSets([
LaravelSetList::LIVEWIRE_40, // Fixed associative array handling
]);
CSRF Middleware Update (Laravel 13):
// rector.php
return RectorConfig::configure()
->withSets([
LaravelSetList::LARAVEL_13_MIDDLEWARE, // Renames PreventRequestsForgery
]);
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]);
Laravel 13 Model Attributes:
UP_TO_LARAVEL_130 may incorrectly generate Table attribute arguments.LARAVEL_13_MODEL_ATTRIBUTES and verify generated code.Validation Rule Closures:
ValidationRuleArrayStringValueToArrayRector may fail on complex rules() method closures.--dry-run first or exclude problematic files.Middleware Renaming:
PreventRequestsForgery middleware may not be found after upgrade.LARAVEL_13_MIDDLEWARE set or manually update imports.Rector 2.4.1 Migration:
getFile() method changes may break custom rules.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',
],
]);
Custom Laravel 13 Rules:
composer require rector/rector:^2.4.1
composer make:rule -- SingleQueueableTraitRector
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
);
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
]);
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
Laravel 13 Support:
$attributes property)$queueable trait)PreventRequestsForgery)Validation Improvements:
rules() methodLivewire 4.0 Fixes:
RenameClassRectorPerformance:
NO_UPDATE_NEEDED would not apply here as this release introduces significant new features and fixes that warrant updating the assessment.
How can I help you explore Laravel packages today?