driftingly / rector-laravel

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

EloquentMagicMethodToQueryBuilderRector transforms class to include namespace #149

Closed phh closed 8 months ago

phh commented 10 months ago

Example from EloquentMagicMethodToQueryBuilderRector prepends query to the Eloquent magic methods.

Example:

 use App\Models\User;

-$user = User::find(1);
+$user = User::query()->find(1);

After trying it:

 use App\Models\User;

-$user = User::find(1);
+$user = \App\Models\User::query()->find(1);

Is there a way for the rule to leave the class at the current class name and only prepend the query() method?

Ruleset is very simple:

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/app',
    ]);
    $rectorConfig->rule(EloquentMagicMethodToQueryBuilderRector::class);
};
phh commented 10 months ago

And I know I can just $rectorConfig->importNames(); but I would rather not include that rule at this point.

clementbirkle commented 10 months ago

Try with this config:

    $rectorConfig->importNames(importDocBlockNames: false);
    $rectorConfig->importShortClasses(false);

It should works.