rector/rector-laravel
Community Rector extension to automate Laravel upgrades. Adds Laravel and first-party package rules (e.g., Cashier, Livewire), with version-based set providers that detect your composer.json or manual level sets to apply the right refactors for your target Laravel version.
composer require --dev driftingly/rector-laravel
composer.json):
// rector.php
use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelSetProvider;
return RectorConfig::configure()
->withSetProviders(LaravelSetProvider::class)
->withComposerBased(laravel: true);
vendor/bin/rector process src
Upgrade Laravel 12 → 13:
composer require --dev driftingly/rector-laravel
vendor/bin/rector process src --dry-run # Preview changes
vendor/bin/rector process src # Apply changes
Version-Specific Upgrades:
Use LaravelSetProvider for automatic version detection or manually specify sets:
->withSets([
LaravelLevelSetList::UP_TO_LARAVEL_130, // Covers 12→13
LaravelLevelSetList::LARAVEL_130, // Only 12→13 changes
])
Code Quality Improvements: Combine version upgrades with quality sets:
->withSets([
LaravelLevelSetList::UP_TO_LARAVEL_130,
LaravelSetList::LARAVEL_CODE_QUALITY,
LaravelSetList::LARAVEL_COLLECTION,
])
Testing-Focused Refinements:
->withSets([
LaravelSetList::LARAVEL_TESTING,
LaravelSetList::LARAVEL_TYPE_DECLARATIONS,
])
php-cs-fixer/rector step in GitHub Actions:
- name: Run Rector
run: vendor/bin/rector process src --dry-run
rector with php-cs-fixer via roave/security-advisories or custom script.vendor/bin/rector process app/Http/Controllers --set LaravelSetList::LARAVEL_STATIC_TO_INJECTION
Dry-Run First:
Always use --dry-run to preview changes before applying:
vendor/bin/rector process src --dry-run
dd() removals in tests).Facade → DI Migration:
LaravelSetList::LARAVEL_STATIC_TO_INJECTION may break:
Cache → Illuminate\Contracts\Cache\Repository).Collection Method Conflicts:
LaravelSetList::LARAVEL_COLLECTION may clash with custom methods:
->withExcludedPaths([__DIR__.'/app/Helpers/Collection.php'])
Route Action Strings:
RouteActionCallableRector requires namespace config:
->withConfiguredRule(RouteActionCallableRector::class, [
'NAMESPACE' => 'App\\Http\\Controllers\\',
'ROUTES' => [
'admin' => 'App\\Http\\Controllers\\Admin\\',
],
])
vendor/bin/rector process src --verbose
->withSkip([
\RectorLaravel\Rector\MethodCall\WhereToWhereLikeRector::class,
])
Custom Rules: Generate and extend rules:
composer make:rule -- If_/ConvertLegacyAuthToSanctum
Auth::user() → auth()->user() in legacy code.Conditional Rules:
Use Rector\Contract\Rector\ConfigurableRectorInterface for dynamic behavior:
class CustomCacheRector implements ConfigurableRectorInterface {
public function __construct(public bool $useSeconds) {}
// ...
}
Post-Rector Hooks:
Chain with php-cs-fixer or pestphp/pest for final formatting:
vendor/bin/rector process src && vendor/bin/php-cs-fixer fix
->withExcludedPaths([__DIR__.'/tests'])
dev-main branch for unreleased rules:
composer require driftingly/rector-laravel:dev-main --dev
rector.log for skipped files/rules:
->withAutoloadPaths([__DIR__.'/vendor'])
How can I help you explore Laravel packages today?