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.
CommandPropertyToAsCommandAttributeRector (property-based $signature/$description → #[AsCommand] attribute), which is directly on the critical path for teams moving to attribute-driven Laravel.blank()/filled() over empty(), CollectionCountComparisonToIsEmptyRector (count($c) === 0 → isEmpty()), type declarations, and collection optimizations. Aligns with PSR-12 and Laravel's evolving best practices.RelationTableStringToPivotClassRector and TablePropertyToTableAttributeRector push stringly-typed relational metadata into typed pivot classes and #[Table] attributes — a concrete, measurable step toward typed models and better static analysis. Treat this as a technical-debt reduction workstream, not just cosmetic cleanup.LARAVEL_CODE_QUALITY, LARAVEL_STATIC_TO_INJECTION) to clean up legacy codebases without full rewrites. Prioritize high-impact areas like container bindings (now resolving named binding arguments), event dispatching (broadcast support added), and Eloquent magic methods.dd() removal, unlink() → File::delete()). Pairs cleanly with PHPStan/Psalm for post-refactor type safety.--dev on require, reducing the chance the tool ships to production. Combined with export-ignore for .github, this is a small but real operational win for teams with strict dependency governance.TablePropertyToTableAttributeRector — a reminder that model-touching rules can produce valid-but-wrong code that tests won't necessarily catch. Mandate: pin the exact version, always run --dry-run first on a branch, review diffs on app/Models and migrations with extra scrutiny, and hold a DB-level regression pass after any Eloquent-related set. Prefer the newest patch over older pins, since correctness fixes are landing continuously."This tool automates most of the manual work in upgrading Laravel — saving 2–4 weeks per major version for a team of 5. Migrating from Laravel 8 to 13 typically requires 10–15 hours of manual refactoring per developer; Rector cuts that to under 2 hours. The 2.7.0 release adds five new rules (130 total) covering modern Laravel conventions like attribute-based commands and typed pivot relations, and fixes several correctness bugs — evidence the project is actively maintained rather than a stalled side project. MIT license, low-risk, high-reward for technical debt reduction."
ROI Hook:
*"Rector/Laravel lets us treat code upgrades like infrastructure updates — repeatable, auditable, scalable. Key wins:
--dry-run previews changes before merging; export-ignore and dev-only keywords keep it out of production builds.composer make:rule).Example Workflow:
# Upgrade Laravel 11 → 12 in one command:
rector process src --config=rector.php --set=LaravelLevelSetList::UP_TO_LARAVEL_120
Blockers to Address:
dd() removal). Start with --dry-run.*"Stop manually fixing Laravel deprecations! This tool writes the boring refactoring code for you:
Cache::get() → constructor injection.$signature/$description properties → #[AsCommand].count($c) === 0 → isEmpty(); where() chains → filter()/reject().unlink() → File::delete(), dd() removal.Pro Tip:
Use LaravelSetList::LARAVEL_CODE_QUALITY to auto-fix common anti-patterns:
// Before
if (count($items) === 0) { ... }
// After (auto-applied)
if ($items->isEmpty()) { ... }
Try It:
composer require --dev driftingly/rector-laravel
rector init --with-config --with-set=LaravelSetList::LARAVEL_TYPE_DECLARATIONS
Run --dry-run first; never let it touch app/Models in a PR you haven't eyeballed."
How can I help you explore Laravel packages today?