rectorphp / rector-symfony

Rector upgrade rules for Symfony
http://getrector.com
MIT License
180 stars 86 forks source link

Add removal of AsEventListener attribute when converting to EventSubscriber #588

Closed stefantalen closed 2 months ago

stefantalen commented 6 months ago

Current behaviour results in a mixed result when the #AsEventListener attribute has been used:

 namespace App\EventListener;

+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 use App\Exception\Http\RestHttpExceptionInterface;
 use FOS\RestBundle\View\View;
 use FOS\RestBundle\View\ViewHandlerInterface;
@@ @@
 use Symfony\Component\HttpKernel\KernelEvents;

 #[AsEventListener(KernelEvents::EXCEPTION, method: 'onKernelException', priority: 10)]
-class RestExceptionListener
+class RestExceptionEventSubscriber implements EventSubscriberInterface
 {
     public function __construct(
         protected ViewHandlerInterface $viewHandler,
@@ @@
             $response = $this->viewHandler->handle($view);
             $event->setResponse($response);
         }
+    }
+    /**
+     * @return array<string, mixed>
+     */
+    public static function getSubscribedEvents(): array
+    {
+        return [KernelEvents::EXCEPTION => ['onKernelException', 10]];
     }
 }

Since the rule is there to convert a Listener to a Subscriber it makes sense to remove the attribute right?

Todo:

stefantalen commented 2 months ago

A solution has been provided in #616, closing this

TomasVotruba commented 2 months ago

Thanks for checking :+1: