rector icon indicating copy to clipboard operation
rector copied to clipboard

EventListenerToEventSubscriberRector - Do I have to skip it, when #[AsEventListener] Attribute is used?

Open CMH-Benny opened this issue 1 year ago • 2 comments

Question

Hey, I am updating a symfony project with rector and I so much love it! Thanks for this amazing tool :) I am almost done, only SYMFONY_CODE_QUALITY is missing.

Rector always trys to convert EventListeners to EventSubscribers even if I use the Attribute and I wondered why. I found this blog about it: https://tomasvotruba.com/blog/2019/07/22/how-to-convert-listeners-to-subscribers-and-reduce-your-configs/

But in Symfony 6.4 upwards we also have the #[AsEventListener] Attribute, which not only makes the "no config" argument obsolete, but also allows to target methods, so you can add multiple Listeners to the same method for different events, or have multiple handler methods in one class etc.

There is really no reason anymore to change to EventSubscriber then, or am I wrong? Wouldn't it be better and more modern to have a EventSubscriberToAsEventListenerAttributeRector and a EventListenerToAsEventListenerAttributeRector instead? Or at least make it aware of that attribute and don't run that rector in that case?

Thanks in advance for your answers <3

CMH-Benny avatar Mar 08 '24 03:03 CMH-Benny

Could you create failing fixture PR at https://github.com/rectorphp/rector-symfony/tree/5ce789ddc9f145d4d93496db165ef9daa9be4424/rules-tests/CodeQuality/Rector/Class_/EventListenerToEventSubscriberRector/Fixture , eg: you can name it: skip_when_as_event_listener_attribute_exists.php.inc there

thank you.

samsonasik avatar Mar 11 '24 10:03 samsonasik

Hey, I hope this is correct: https://github.com/rectorphp/rector-symfony/pull/589

Wasn't sure if that will work, since IDE is complaining about same class names in the same file, but apparently we don't want Rector to change them in those cases, correct? :)

Thanks in Advance

CMH-Benny avatar Mar 11 '24 12:03 CMH-Benny

I'm looking into this...

TomasVotruba avatar Jun 22 '24 01:06 TomasVotruba

Fixed in https://github.com/rectorphp/rector-symfony/pull/616 :+1:

TomasVotruba avatar Jun 22 '24 02:06 TomasVotruba

Thank you so much <3

CMH-Benny avatar Jun 27 '24 14:06 CMH-Benny