diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php index 1494bb16bb2..43ba2aa2bb3 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php @@ -78,6 +78,10 @@ public function refactor(Node $node): ?Node return null; } + if ($this->nodesToAddCollector->isActive()) { + return null; + } + $this->removeNode($node); return $node; diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 4124fa2897a..1ed1161071c 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -122,7 +122,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn */ private $previousAppliedClass; - private NodesToAddCollector $nodesToAddCollector; + protected NodesToAddCollector $nodesToAddCollector; private CurrentFileProvider $currentFileProvider; diff --git a/tests/Issues/Issue6670/Fixture/do_not_remove_used_class_method.php.inc b/tests/Issues/Issue6670/Fixture/do_not_remove_used_class_method.php.inc new file mode 100644 index 00000000000..c423e3acc66 --- /dev/null +++ b/tests/Issues/Issue6670/Fixture/do_not_remove_used_class_method.php.inc @@ -0,0 +1,42 @@ +notUnused(); + } + } + + private function notUnused(): int + { + // This is some code that is very important + } +} +?> +----- +notUnused(); + } + + private function notUnused(): int + { + // This is some code that is very important + } +} +?> diff --git a/tests/Issues/Issue6670/RemoveAlwaysElseAndUnusedPrivateMethodsRectorTest.php b/tests/Issues/Issue6670/RemoveAlwaysElseAndUnusedPrivateMethodsRectorTest.php new file mode 100644 index 00000000000..345ec3f70a8 --- /dev/null +++ b/tests/Issues/Issue6670/RemoveAlwaysElseAndUnusedPrivateMethodsRectorTest.php @@ -0,0 +1,36 @@ +doTestFileInfo($fileInfo); + } + + /** + * @return Iterator + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/Issue6670/config/configured_rule.php b/tests/Issues/Issue6670/config/configured_rule.php new file mode 100644 index 00000000000..57676d915a3 --- /dev/null +++ b/tests/Issues/Issue6670/config/configured_rule.php @@ -0,0 +1,13 @@ +services(); + $services->set(RemoveUnusedPrivateMethodRector::class); + $services->set(RemoveAlwaysElseRector::class); +};