Axel Libori Roch
Axel Libori Roch
  • 10 Jan, 2026
  • 2 min read

Refactor your Laravel project with Rector

Keeping a Laravel project up to date and maintaining clean code can become costly as it grows. This is where Rector becomes a key tool.

Rector is an automation tool that analyzes and refactors your PHP code safely. It allows you to apply modern improvements, adapt your code to new PHP or Laravel versions, and remove deprecated patterns automatically.

Why use Rector in Laravel?

In a Laravel project, Rector can help you:

- Upgrade legacy code to newer PHP or Laravel versions

- Apply best practices consistently

- Reduce human error during large refactors

All of this without changing your application’s behavior.

Basic steps to get started with Rector in Laravel

1. Install Rector and the Laravel extension as a development dependency

- https://getrector.com/documentation

- https://github.com/driftingly/rector-laravel

1 
2 
3composer require rector/rector --dev
4 
5composer require --dev driftingly/rector-laravel

Code highlighting powered by torchlight.dev.

2. Create the configuration file (rector.php) at project's root

Define the PHP version, Laravel rules, and the directories to analyze.

1 
2 
3declare(strict_types=1);
4 
5use Rector\Config\RectorConfig;
6use RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector;
7use RectorLaravel\Rector\StaticCall\DispatchToHelperFunctionsRector;
8use RectorLaravel\Set\LaravelLevelSetList;
9use RectorLaravel\Set\LaravelSetList;
10 
11return RectorConfig::configure()
12 ->withPaths([
13 __DIR__.'/app',
14 __DIR__.'/tests',
15 ])
16 ->withPhpSets(php84: true)
17 ->withSets([
18 LaravelLevelSetList::UP_TO_LARAVEL_120,
19 LaravelSetList::LARAVEL_CODE_QUALITY,
20 LaravelSetList::LARAVEL_COLLECTION,
21 ])
22 ->withConfiguredRule(RemoveDumpDataDeadCodeRector::class, ['dd', 'dump', 'var_dump'])
23 ->withSkip([DispatchToHelperFunctionsRector::class]);

Code highlighting powered by torchlight.dev.

3. Run Rector in dry-run mode (--dry-run)

Review the proposed changes before applying them.

1 
2./vendor/bin/rector process --dry-run

Code highlighting powered by torchlight.dev.

4. Apply changes

Start with safe rules and avoid doing everything at once on large projects.

1 
2./vendor/bin/rector process

Code highlighting powered by torchlight.dev.

5. Run your tests

Make sure the application behavior remains unchanged.

1 
2php artisan test

Code highlighting powered by torchlight.dev.

A day-to-day ally

Rector doesn’t replace code reviews or tests, but it’s a great ally for keeping your codebase healthy over time. You can run it occasionally or integrate it into your regular workflow.

If you work with Laravel and want to evolve your project with confidence, Rector is well worth it.