From 0f5bfcb7712b2d4989531639c031b7893635a4fb Mon Sep 17 00:00:00 2001 From: Johan MARTIN Date: Tue, 25 Nov 2025 09:19:42 +0100 Subject: [PATCH 1/2] Fix rule when method is __invoke --- .../Fixture/with_invoke_method.php.inc | 40 +++++++++++++++++++ .../Class_/InlineClassRoutePrefixRector.php | 17 +++++++- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector/Fixture/with_invoke_method.php.inc diff --git a/rules-tests/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector/Fixture/with_invoke_method.php.inc b/rules-tests/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector/Fixture/with_invoke_method.php.inc new file mode 100644 index 00000000..35d54ac5 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector/Fixture/with_invoke_method.php.inc @@ -0,0 +1,40 @@ + + ----- + diff --git a/rules/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector.php b/rules/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector.php index e12c9b4a..285aff72 100644 --- a/rules/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector.php +++ b/rules/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector.php @@ -134,7 +134,7 @@ public function refactor(Node $node): ?Class_ $hasChanged = false; foreach ($node->getMethods() as $classMethod) { - if (! $classMethod->isPublic() || $classMethod->isMagic()) { + if ($this->shouldSkipMethod($classMethod)) { continue; } @@ -255,6 +255,19 @@ public function refactor(Node $node): ?Class_ return $node; } + private function shouldSkipMethod(Node\Stmt\ClassMethod $classMethod): bool + { + if (!$classMethod->isPublic()) { + return true; + } + + if ($classMethod->isMagic() && $classMethod->name->toLowerString() !== '__invoke') { + return true; + } + + return false; + } + private function shouldSkipClass(Class_ $class): bool { if (! $this->controllerAnalyzer->isController($class)) { @@ -262,7 +275,7 @@ private function shouldSkipClass(Class_ $class): bool } foreach ($class->getMethods() as $classMethod) { - if (! $classMethod->isPublic() || $classMethod->isMagic()) { + if ($this->shouldSkipMethod($classMethod)) { continue; } From cd23578dc7e0aa568db9f01d94f36916afb47b2d Mon Sep 17 00:00:00 2001 From: Johan MARTIN Date: Tue, 25 Nov 2025 16:55:17 +0100 Subject: [PATCH 2/2] Use method isUnsafeOverridden and fix TU --- .../Fixture/with_invoke_method.php.inc | 2 +- .../Class_/InlineClassRoutePrefixRector.php | 16 ++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/rules-tests/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector/Fixture/with_invoke_method.php.inc b/rules-tests/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector/Fixture/with_invoke_method.php.inc index 35d54ac5..0eb3ccc6 100644 --- a/rules-tests/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector/Fixture/with_invoke_method.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector/Fixture/with_invoke_method.php.inc @@ -19,7 +19,7 @@ final class WithInvokeMethod extends Controller } ?> - ----- +----- isPublic()) { - return true; - } - - if ($classMethod->isMagic() && $classMethod->name->toLowerString() !== '__invoke') { - return true; - } - - return false; + return + !$classMethod->isPublic() + || $this->magicClassMethodAnalyzer->isUnsafeOverridden($classMethod); } private function shouldSkipClass(Class_ $class): bool