From 752eeb73348a88db6f92fa5536ca602bf4a1438d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 4 Aug 2026 08:59:50 +0200 Subject: [PATCH] [CodeQuality] Skip event and session params in ControllerMethodInjectionToConstructorRector --- .../skip_event_subscriber_method.php.inc | 28 +++++++++++++++++++ .../Fixture/skip_session_interface.php.inc | 18 ++++++++++++ ...llerMethodInjectionToConstructorRector.php | 4 +++ src/Enum/SymfonyClass.php | 2 ++ 4 files changed, 52 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_event_subscriber_method.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_session_interface.php.inc diff --git a/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_event_subscriber_method.php.inc b/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_event_subscriber_method.php.inc new file mode 100644 index 00000000..2c607feb --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_event_subscriber_method.php.inc @@ -0,0 +1,28 @@ +getRequest()->attributes->get('_controller'); + } + + /** + * @return array + */ + public static function getSubscribedEvents(): array + { + return [ + KernelEvents::REQUEST => 'onRequest', + ]; + } +} diff --git a/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_session_interface.php.inc b/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_session_interface.php.inc new file mode 100644 index 00000000..6db13968 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_session_interface.php.inc @@ -0,0 +1,18 @@ +invalidate(); + } +} diff --git a/rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php b/rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php index b19b091f..e8e826a6 100644 --- a/rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php +++ b/rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php @@ -183,6 +183,10 @@ public function refactor(Node $node): ?Node new ObjectType(SymfonyClass::USER_INTERFACE), new ObjectType('DateTimeInterface'), new ObjectType(SymfonyClass::UUID), + // event listener method, not a controller action + new ObjectType(SymfonyClass::EVENT), + // request-scoped, must stay in the action method + new ObjectType(SymfonyClass::SESSION_INTERFACRE), ] )) { continue; diff --git a/src/Enum/SymfonyClass.php b/src/Enum/SymfonyClass.php index b1c5d9df..b9ec898c 100644 --- a/src/Enum/SymfonyClass.php +++ b/src/Enum/SymfonyClass.php @@ -20,6 +20,8 @@ final class SymfonyClass public const string EVENT_DISPATCHER_INTERFACE = 'Symfony\Contracts\EventDispatcher\EventDispatcherInterface'; + public const string EVENT = 'Symfony\Contracts\EventDispatcher\Event'; + public const string VALIDATOR_INTERFACE = 'Symfony\Component\Validator\Validator\ValidatorInterface'; public const string LOGGER_INTERFACE = 'Psr\Log\LoggerInterface';