From b1d573e8b56a92a0f3dcd0f11a49a41d2c2f72f6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 1 Sep 2021 14:17:01 +0700 Subject: [PATCH] [DeadCode][EarlyReturn] Handle RemoveUnusedPrivateMethodRector + RemoveAlwaysElseRector (#807) * added failing test fixture * update * update * Closes #801 Fixes https://github.com/rectorphp/rector/issues/6670 Co-authored-by: Bl00D4NGEL --- .../RemoveUnusedPrivateMethodRector.php | 4 ++ src/Rector/AbstractRector.php | 2 +- .../do_not_remove_used_class_method.php.inc | 42 +++++++++++++++++++ ...sElseAndUnusedPrivateMethodsRectorTest.php | 36 ++++++++++++++++ .../Issue6670/config/configured_rule.php | 13 ++++++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/Issues/Issue6670/Fixture/do_not_remove_used_class_method.php.inc create mode 100644 tests/Issues/Issue6670/RemoveAlwaysElseAndUnusedPrivateMethodsRectorTest.php create mode 100644 tests/Issues/Issue6670/config/configured_rule.php 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); +};