diff --git a/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated.php.inc b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated.php.inc new file mode 100644 index 00000000000..ff07ba331ab --- /dev/null +++ b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated.php.inc @@ -0,0 +1,47 @@ + +----- + \ No newline at end of file diff --git a/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc new file mode 100644 index 00000000000..e636eab2eb3 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc @@ -0,0 +1,50 @@ + +----- + \ No newline at end of file diff --git a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php index 4658772ce96..78ec5a2fe40 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -103,13 +103,14 @@ private function resolveInsertedByKeys(Switch_ $switch): array { $totalKeys = count($switch->cases); $insertByKeys = []; + $appendKey = 0; foreach ($switch->cases as $key => $case) { if ($case->stmts === []) { continue; } - for ($jumpToKey = $key + 1; $jumpToKey < $totalKeys; ++$jumpToKey) { + for ($jumpToKey = $key + 2; $jumpToKey < $totalKeys; ++$jumpToKey) { if (! isset($switch->cases[$jumpToKey])) { continue; } @@ -122,10 +123,12 @@ private function resolveInsertedByKeys(Switch_ $switch): array unset($switch->cases[$jumpToKey]); - $insertByKeys[$key][] = $nextCase; + $insertByKeys[$key + $appendKey][] = $nextCase; $this->hasChanged = true; } + + $appendKey = isset($insertByKeys[$key]) ? count($insertByKeys[$key]) : 0; } return $insertByKeys; @@ -140,10 +143,16 @@ private function insertCaseByKeys(Switch_ $switch, array $insertByKeys): void $nextKey = $key + 1; array_splice($switch->cases, $nextKey, 0, $insertByKey); + } - for ($jumpToKey = $key; $jumpToKey < $key + count($insertByKey); ++$jumpToKey) { - $switch->cases[$jumpToKey]->stmts = []; + /** @var Case_|null $previousCase */ + $previousCase = null; + foreach ($switch->cases as $case) { + if ($previousCase instanceof Case_ && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) { + $previousCase->stmts = []; } + + $previousCase = $case; } }