- What is rector/rector-laravel used for?
- rector/rector-laravel is a community Rector extension that automates Laravel upgrades and code modernization. It adds Laravel-specific rules plus rules for first-party packages such as Cashier and Livewire. You use it to apply version-based refactors instead of manually updating legacy Laravel patterns.
- How do I install rector-laravel in a Laravel project?
- Install it as a development dependency with composer require --dev driftingly/rector-laravel. Then add the Laravel sets to your Rector configuration file. The package is a Rector extension, so your project also needs Rector installed and configured.
- Does rector-laravel support automatic Laravel version detection?
- Yes. You can call withComposerBased(laravel: true) in your RectorConfig to register LaravelSetList::COMPOSER_BASED. This applies version sets based on the Laravel version detected in composer.json, and it can also apply sets for Faker and Livewire when those packages are installed.
- How do I manually configure Laravel upgrade sets?
- Use RectorLaravel\Set\LaravelLevelSetList and choose the constant for your target version, such as UP_TO_LARAVEL_130. Higher-version sets include the lower-version sets, so you can target a specific upgrade level. Add the chosen set to withSets() in your Rector configuration.
- Which Laravel versions does rector-laravel support?
- The package provides version-based level sets, including constants such as UP_TO_LARAVEL_130, and higher sets include lower-version rules. The composer-based option applies sets up to the Laravel version installed in your project. Check the package documentation and rule overview for the current version coverage and available rules.
- Can rector-laravel refactor first-party packages like Cashier and Livewire?
- Yes. The package includes rules for additional first-party packages, with Cashier and Livewire called out in the README. Composer-based configuration can also register Livewire version sets when Livewire is installed. This helps you upgrade package-specific code alongside your Laravel application code.
- How do I test Rector Laravel rules safely before changing code?
- Run Rector in dry-run mode first and review the diffs before applying changes. Test the rules in a staging environment or a dedicated branch, especially for stateful refactors that may interact with existing migrations or custom logic. Applying smaller rule sets incrementally reduces risk.
- Is rector-laravel suitable for CI/CD and large codebases?
- Yes, Rector rules can run in pre-commit hooks, pull request checks, or scheduled pipelines. The repository includes GitHub Actions workflows for tests and code analysis. For large codebases, use Rector options such as --parallel and --exclude-paths to manage performance.
- Can I write custom Rector rules for Laravel-specific patterns?
- Yes. Rector supports custom rules, and the package is designed to be extensible for project-specific naming conventions or legacy patterns. You can scaffold a custom rule with Rector's make:rule command and add it to your configuration. This lets you tailor automated refactoring beyond the built-in Laravel sets.
- What are alternatives to rector-laravel for Laravel upgrades?
- Alternatives include manually following Laravel's upgrade guide, using Laravel Shift, or writing custom Rector rules with Rector core. rector-laravel is focused on automated, version-based Laravel refactors and first-party package rules. Teams often combine automated Rector runs with tests and manual review for safe upgrades.