diff --git a/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/re_use_existing_service.php.inc b/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/re_use_existing_service.php.inc index 78ff55ab1..11379084b 100644 --- a/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/re_use_existing_service.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/re_use_existing_service.php.inc @@ -37,7 +37,7 @@ use Symfony\Component\Routing\Annotation\Route; final class ReUseExistingService extends AbstractController { - public function __construct(private readonly \Psr\Log\LoggerInterface $logger) + public function __construct(private LoggerInterface $logger) { } diff --git a/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_constructor.php.inc b/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_constructor.php.inc new file mode 100644 index 000000000..e9ba1ac40 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/skip_constructor.php.inc @@ -0,0 +1,26 @@ +logger = $logger; + } + + #[Route('/some-action', name: 'some_action')] + public function someAction( + \Symfony\Component\HttpFoundation\Request $request, + ) { + $this->logger->log('level', 'value'); + } +} diff --git a/rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php b/rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php index 12c82f0ca..e53f05dc9 100644 --- a/rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php +++ b/rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php @@ -17,6 +17,7 @@ use Rector\Symfony\Bridge\NodeAnalyzer\ControllerMethodAnalyzer; use Rector\Symfony\Enum\SymfonyClass; use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer; +use Rector\ValueObject\MethodName; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -101,6 +102,10 @@ public function refactor(Node $node): ?Node $propertyMetadatas = []; foreach ($node->getMethods() as $classMethod) { + if ($classMethod->isMagic() && ! $this->isName($classMethod->name, MethodName::INVOKE)) { + continue; + } + if (! $this->controllerMethodAnalyzer->isAction($classMethod)) { continue; }