Home

Awesome

Rector Rules for Laravel

Tests Code Analysis Packagist Downloads Packagist Version

See available Laravel rules

Install

This package is a Rector extension developed by the Laravel community.

Rules for additional first party packages are included as well e.g. Cashier and Livewire.

Install as a dev dependency:

composer require --dev driftingly/rector-laravel

Automate Laravel Upgrades

To add a set to your config, use RectorLaravel\Set\LaravelLevelSetList and pick the constant that matches your target version. Sets for higher versions include sets for lower versions.

<?php declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelLevelSetList;

return RectorConfig::configure()
    ->withSets([
        LaravelLevelSetList::UP_TO_LARAVEL_110,
    ]);

The sets in RectorLaravel\Set\LaravelSetList only contain changes related to a specific version upgrade. For example, the rules in LaravelSetList::LARAVEL_110 apply when upgrading from Laravel 10 to Laravel 11.

Additional Sets

To improve different aspects of your code, use the sets in RectorLaravel\Set\LaravelSetList.

<?php declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelSetList;

return RectorConfig::configure()
    ->withSets([
        LaravelSetList::LARAVEL_CODE_QUALITY,
        LaravelSetList::LARAVEL_COLLECTION,
        ...
    ]);
SetPurpose
LaravelSetList::LARAVEL_ARRAYACCESS_TO_METHOD_CALLConverts uses of things like $app['config'] to $app->make('config').
LaravelSetList::LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALLConverts most string and array helpers into Str and Arr Facades' static calls.<br/>https://laravel.com/docs/11.x/facades#facades-vs-helper-functions
LaravelSetList::LARAVEL_CODE_QUALITYReplaces magical call on $this->app["something"] to standalone variable with PHPDocs.
LaravelSetList::LARAVEL_COLLECTIONImproves the usage of Laravel Collections by using simpler, more efficient, or more readable methods.
LaravelSetList::LARAVEL_CONTAINER_STRING_TO_FULLY_QUALIFIED_NAMEChanges the string or class const used for a service container make call.<br/>https://laravel.com/docs/11.x/container#the-make-method
LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDERTransforms magic method calls on Eloquent Models into corresponding Query Builder method calls.<br/>https://laravel.com/docs/11.x/eloquent
LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMESReplaces Facade aliases with full Facade names.<br/>https://laravel.com/docs/11.x/facades#facade-class-reference
LaravelSetList::LARAVEL_IF_HELPERSReplaces abort(), report(), throw statements inside conditions with abort_if(), report_if(), throw_if() function calls.<br/>https://laravel.com/docs/11.x/helpers#method-abort-if
LaravelSetList::LARAVEL_LEGACY_FACTORIES_TO_CLASSESMigrates Eloquent legacy model factories (with closures) into class based factories.<br/>https://laravel.com/docs/8.x/releases#model-factory-classes
LaravelSetList::LARAVEL_STATIC_TO_INJECTIONReplaces Laravel's Facades with Dependency Injection.<br/>https://tomasvotruba.com/blog/2019/03/04/how-to-turn-laravel-from-static-to-dependency-injection-in-one-day/<br/>https://laravel.com/docs/11.x/facades#facades-vs-dependency-injection

Contributors

Thank you everyone who works so hard on improving this package:

Hire The Rector Team

Rector is a tool that we develop and share for free, so anyone can automate their refactoring. But not everyone has dozens of hours to understand complexity of abstract-syntax-tree in their own time. That's why we provide commercial support - to save your time.

Would you like to apply Rector on your code base but don't have time for the struggle with your project? Hire the Rector team to get there faster.

Learn Rector Faster

Not everyone has time to understand Rector and AST complexity. You can speed up the process by reading the book The Power of Automated Refactoring. Not only will it help you learn and understand Rector but it supports the project as well.