Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to stop rector from changing unaffected files by importNames parameter #8649

Closed
ozahorulia opened this issue May 21, 2024 · 3 comments
Closed

Comments

@ozahorulia
Copy link

Is there an alternative for the removed APPLY_AUTO_IMPORT_NAMES_ON_CHANGED_FILES_ONLY parameter?

Here is my use case. I've got a symfony project with ~5k php files in src folder. I want to use MessageSubscriberInterfaceToAttributeRector rector in order to add an attribute instead of the deprecated interface.

If I just go without any parameters, rector would add a with the full namespace path in the attribute, e.g.

#[\Symfony\Component\Messenger\Attribute\AsMessageHandler]

I don't want this to happen so I'm using:

    $rectorConfig->importNames(true, false);
    $rectorConfig->importShortClasses(false);

Now it works OK, but besides the intended change, it alters hundreds of files, just to change \Namespace\Class calls to an alias. It might be a good idea, but not in a legacy project, when I must only change those things I'm sure about and do not touch hundreds of files, which time has not yet come.

@samsonasik
Copy link
Member

I think you can have 2 kind of configs for that, eg:

  • rector.php without import
  • rector-with-import.php with import

then, in your rector-with-import.php, you can do:

return static function (RectorConfig $rectorConfig): void {
     $rectorConfig->import(__DIR__ . '/rector.php');
     $rectorConfig->importNames();
};

so you can run rector-with-import.php on separate command when you want to change it:

vendor/bin/rector process --config rector-with-import.php

or vice versa so you can review as another individual change.

@ozahorulia
Copy link
Author

@samsonasik Sorry, but I don't understand how exactly it helps in my case.

If I want to go throughout the code applying a rule that changes interface usage to an attribute (in my case - MessageHandlerInterfaceToAttributeRector), it will either

A) [without importNames()] Update only the files I need but use annotation with the full namespace like this:

#[\Symfony\Component\Messenger\Attribute\AsMessageHandler]
class Foo
{
}

B) [with importNames()] It will add correct annotation with the short class name:

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
class Foo
{
}

But it will also change full class name usages throughout all my code, which is huge.

So, there is no option C) at the moment, where I'd get short class for the attribute, and it won't change all other files which I didn't want to affect. I don't understand how having 2 separate config files may help me.

@samsonasik
Copy link
Member

It was removed due to avoid too detailed setup and making ambiguous code, see PR:

You can have as many config as needed for your use case, then run separately with --config argument in CLI.

Thank you for understanding.

@rectorphp rectorphp locked as resolved and limited conversation to collaborators May 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants