From 2f74b02ef6c754c68abcdd246837583967e24678 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 11 Nov 2023 02:09:43 +0700 Subject: [PATCH] [DeadCode] Handle repetitive jump equal case stmts on RemoveDuplicatedCaseInSwitchRector (#5239) * [DeadCode] Handle repetitive jump equal case stmts on RemoveDuplicatedCaseInSwitchRector * Fixed :tada: * fix missing-in-set config * fix @return * clean up --- .../different_indirect_duplicated3.php.inc | 59 +++++++++++++++++++ .../RemoveDuplicatedCaseInSwitchRector.php | 54 +++++------------ utils/Command/MissingInSetCommand.php | 4 ++ 3 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated3.php.inc diff --git a/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated3.php.inc b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated3.php.inc new file mode 100644 index 00000000000..756769d47bc --- /dev/null +++ b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated3.php.inc @@ -0,0 +1,59 @@ + +----- + \ No newline at end of file diff --git a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php index b71216a8f3f..9b45274b8c4 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -86,9 +86,7 @@ public function refactor(Node $node): ?Node $this->hasChanged = false; - $insertByKeys = $this->resolveInsertedByKeys($node); - $this->insertCaseByKeys($node, $insertByKeys); - + $this->removeDuplicatedCases($node); if (! $this->hasChanged) { return null; } @@ -96,31 +94,17 @@ public function refactor(Node $node): ?Node return $node; } - /** - * @return array> - */ - private function resolveInsertedByKeys(Switch_ $switch): array + private function removeDuplicatedCases(Switch_ $switch): void { $totalKeys = count($switch->cases); - $insertByKeys = []; - $appendKey = 0; - /** @var Case_|null $previousCase */ - $previousCase = null; - - foreach ($switch->cases as $key => $case) { - if ($previousCase instanceof Case_ && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) { - $previousCase->stmts = []; - $this->hasChanged = true; - } - - $previousCase = $case; - - for ($jumpToKey = $key + 2; $jumpToKey < $totalKeys; ++$jumpToKey) { + foreach (array_keys($switch->cases) as $key) { + $nextCases = []; + for ($jumpToKey = $key + 1; $jumpToKey < $totalKeys; ++$jumpToKey) { if (! isset($switch->cases[$jumpToKey])) { continue; } - if (! $this->areSwitchStmtsEqualsAndWithBreak($case, $switch->cases[$jumpToKey])) { + if (! $this->areSwitchStmtsEqualsAndWithBreak($switch->cases[$key], $switch->cases[$jumpToKey])) { continue; } @@ -128,30 +112,22 @@ private function resolveInsertedByKeys(Switch_ $switch): array unset($switch->cases[$jumpToKey]); - $insertByKeys[$key + $appendKey][] = $nextCase; + $nextCases[] = $nextCase; $this->hasChanged = true; } - $appendKey = isset($insertByKeys[$key]) ? count($insertByKeys[$key]) : 0; - } - - return $insertByKeys; - } - - /** - * @param array> $insertByKeys - */ - private function insertCaseByKeys(Switch_ $switch, array $insertByKeys): void - { - foreach ($insertByKeys as $key => $insertByKey) { - $nextKey = $key + 1; + if ($nextCases === []) { + continue; + } - array_splice($switch->cases, $nextKey, 0, $insertByKey); + array_splice($switch->cases, $key + 1, 0, $nextCases); - $switch->cases[$key]->stmts = []; + for ($jumpToKey = $key; $jumpToKey < $key + count($nextCases); ++$jumpToKey) { + $switch->cases[$jumpToKey]->stmts = []; + } - $this->hasChanged = true; + $key += count($nextCases); } } diff --git a/utils/Command/MissingInSetCommand.php b/utils/Command/MissingInSetCommand.php index 623fedfe0d8..f10c42927a7 100644 --- a/utils/Command/MissingInSetCommand.php +++ b/utils/Command/MissingInSetCommand.php @@ -8,6 +8,7 @@ use Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector; use Rector\Core\Contract\Rector\ConfigurableRectorInterface; use Rector\Php71\Rector\FuncCall\CountOnNullRector; +use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveSetMethodsMethodCallRector; use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenCollectorRector; use Rector\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector; use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorReadonlyClassRector; @@ -38,6 +39,9 @@ final class MissingInSetCommand extends Command TypedPropertyFromStrictConstructorReadonlyClassRector::class, BinarySwitchToIfElseRector::class, CountOnNullRector::class, + + // namespace to be moved from CodeQuality to PHPUnit100 + RemoveSetMethodsMethodCallRector::class, ]; public function __construct(