Rector Rules for Laravel

repository·main·Indexed 22 days ago

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

A Rector extension that automates Laravel upgrades and improves code quality through specialized rules for Laravel, Cashier, and Livewire. It provides version-based upgrade sets via LaravelLevelSetList, improvement sets via LaravelSetList, and a variety of specific refactoring rules for Eloquent, migrations, and service containers. It also includes a scaffolding command, `make:rule`, to create new custom Rector rules.

Tokens
26.1K
Snippets
131
Records
151
Agent score
78%

What's inside rector-laravel

  1. Use Laravel improvement sets

    main

    Use LaravelSetList to apply groups of rules designed to improve specific aspects of your Laravel codebase, such as code quality, collection usage, or testing.

    <?php declare(strict_types=1);
    
    use Rector\Config\RectorConfig;
    use RectorLaravel\Set\LaravelSetList;
    
    return RectorConfig::configure()
        ->withSets([
            LaravelSetList::LARAVEL_CODE_QUALITY,
            LaravelSetList::LARAVEL_COLLECTION,
            ...
        ]);
  2. Automate Laravel Upgrades using Composer detection

    main

    To automatically apply the correct rules based on the Laravel version detected in your composer.json, use the LaravelSetProvider and the withComposerBased method in your rector.php configuration.

    <?php declare(strict_types=1);
    
    use Rector\Config\RectorConfig;
    use RectorLaravel\Set\LaravelSetProvider;
    
    return RectorConfig::configure()
        ->withSetProviders(LaravelSetProvider::class)
        ->withComposerBased(laravel: true, /** other options */);
  3. Use opinionated Rector rules

    main

    Some rules in rector-laravel are considered opinionated and are not included in standard rule sets by default. To use them, you must manually add them to your rector.php configuration file using withRules().

    Example of enabling the ResponseHelperCallToJsonResponseRector rule:

    <?php declare(strict_types=1);
    
    use Rector\Config\RectorConfig;
    use RectorLaravel\Rector\MethodCall\ResponseHelperCallToJsonResponseRector;
    
    return RectorConfig::configure()
        ->withRules([
            ResponseHelperCallToJsonResponseRector::class,
        ]);
  4. Install Rector Rules for Laravel

    main

    Install driftingly/rector-laravel as a development dependency using Composer. This package provides Rector rules specifically for Laravel, including support for first-party packages like Cashier and Livewire.

    composer require --dev driftingly/rector-laravel
  5. Create a new Rector rule

    main

    You can scaffold a new rule using the provided Composer script. This generates the rule class in src/Rector/ along with its corresponding test files.

    Important: Always use -- before your arguments to ensure they are passed to the script and not interpreted as Composer arguments.

    To create a rule with a specific directory structure, include slashes in the name. For example, If_/ConvertIfToWhen will create the rule in src/Rector/If_/ with the namespace RectorLaravel\Rector\If_.

  6. Manually configure Laravel version sets

    main

    If you want to manually specify a target version upgrade, use LaravelLevelSetList.

    Note:

    • LaravelLevelSetList constants (e.g., UP_TO_LARAVEL_130) include sets for all lower versions.
    • LaravelSetList constants (e.g., LARAVEL_130) contain only the changes specific to that version upgrade (e.g., upgrading from 12 to 13).
    <?php declare(strict_types=1);
    
    use Rector\Config\RectorConfig;
    use RectorLaravel\Set\LaravelLevelSetList;
    
    return RectorConfig::configure()
        ->withSets([
            LaravelLevelSetList::UP_TO_LARAVEL_130,
        ]);
  7. Configure rule scaffolding type

    main

    When scaffolding a rule, you can specify whether the rule is configurable or non-configurable. This choice determines which templates are used for the rule class and the test configuration file.

    • Non-configurable (Default): Uses non-configurable-rule.php.template and non-configurable-config.php.template.
    • Configurable: Uses configurable-rule.php.template and configurable-config.php.template.

    This ensures that the generated boilerplate includes the necessary logic for handling configuration parameters if your rule requires them.

  8. Make Model attributes and scopes protected

    main

    Uses MakeModelAttributesAndScopesProtectedRector to change public Model attributes and scopes to protected visibility.

     class User extends Model
     {
    -    public function foo(): Attribute
    +    protected function foo(): Attribute
         {
             return Attribute::get(fn () => $this->bar);
         }
    
         #[Scope]
    -    public function active(Builder $query): Builder
    +    protected function active(Builder $query): Builder
         {
             return $query->where('active', true);
         }
     }