driftingly / rector-laravel

Rector upgrades rules for Laravel
http://getrector.org
MIT License
500 stars 49 forks source link

Add method exclusions for `EloquentMagicMethodToQueryBuilderRector` #171

Closed tanerkay closed 7 months ago

tanerkay commented 7 months ago

Add ability to exclude methods from the rector EloquentMagicMethodToQueryBuilderRector

I found this beneficial for find() and findOrFail(), as these are often used in simple statements without chaining, and subjectively appear overly verbose when ::query()-> is added to them.

e.g.

<?php

declare(strict_types=1);

use RectorLaravel\Rector\MethodCall\EloquentOrderByToLatestOrOldestRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $containerConfigurator->extension('rectorConfig', [
        [
            'class' => EloquentMagicMethodToQueryBuilderRector::class,
            'configuration' => [
                'exclude_methods' => [
                    'find',
                    'findOrFail',
                ],
            ],
        ],
    ]);
};

 use App\Models\User;

$user = User::find(1); // no change

-$user = User::where('x', 1);
+$user = User::query()->where('x', 1);