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 from Laravel 10 → 11:
vendor/bin/rector process src --dry-run
vendor/bin/rector process src
withComposerBased(laravel: true) to auto-select rules based on composer.json.->withSets([LaravelLevelSetList::UP_TO_LARAVEL_130])
Collection Improvements:
->withSets([LaravelSetList::LARAVEL_COLLECTION])
Converts collect($array)->toArray() → $array (if redundant).
Facade to DI:
->withSets([LaravelSetList::LARAVEL_STATIC_TO_INJECTION])
Replaces Cache::get() → inject Cache into constructor.
Testing Upgrades:
->withSets([LaravelSetList::LARAVEL_TESTING])
Converts assertSee() → assertStringContains().
php-cs-fixer workflows for pre-commit checks:
# .github/workflows/rector.yml
- name: Run Rector
run: vendor/bin/rector process src --dry-run
vendor/bin/rector process app/Http/Controllers --dry-run
--parallel for faster execution:
vendor/bin/rector process src --parallel
Dry Runs First:
--dry-run before applying changes to avoid breaking builds.vendor/bin/rector process src --dry-run --format=diff
Facade Aliases:
LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES may break tests using aliases like Auth::user().Route Callables:
RouteActionCallableRector requires NAMESPACE config for custom controller paths:
->withConfiguredRule(RouteActionCallableRector::class, [
'NAMESPACE' => 'App\\Http\\Controllers\\Admin\\',
])
Type Declarations:
LaravelSetList::LARAVEL_TYPE_DECLARATIONS may fail on dynamic properties (e.g., __get()).->withExcludedPaths([__DIR__.'/app/Helpers/DynamicProperty.php'])
vendor/bin/rector process src --verbose
->withRules([]) // Start empty, then add rules one by one
PostgreSQL vs MySQL:
WhereToWhereLikeRector requires USING_POSTGRES_DRIVER for PostgreSQL:
->withConfiguredRule(WhereToWhereLikeRector::class, [
'USING_POSTGRES_DRIVER' => true,
])
Excluding Files:
withExcludedPaths() or withExcludedNodes():
->withExcludedPaths([__DIR__.'/vendor/', __DIR__.'/tests/'])
Custom Rules:
vendor/bin/rector process src --rules=YourRuleName
Create Custom Rules:
composer make:rule -- YourCustomRuleName
Str::of($str)->upper() → Str::upper($str).Override Default Sets:
->withSets([
LaravelSetList::LARAVEL_CODE_QUALITY,
// Override a specific rule
new class implements RectorSet {
public function getRules(RectorConfig $config): array {
return [new YourCustomRector()];
}
},
])
Parallel Processing:
vendor/bin/rector process src --parallel --max-workers=4
echo 'vendor/bin/rector process src --dry-run' >> .git/hooks/pre-commit
--format=diff to review changes in Git:
vendor/bin/rector process src --dry-run --format=diff | git apply --check
File > Invalidate Caches).How can I help you explore Laravel packages today?