diff --git a/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/no_stmts_with_equal_comments.php.inc b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/no_stmts_with_equal_comments.php.inc new file mode 100644 index 00000000000..0ee4d1fd19e --- /dev/null +++ b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/no_stmts_with_equal_comments.php.inc @@ -0,0 +1,53 @@ +cases as $outerCaseKey => $outerCase) { - if (in_array($outerCaseKey, $processedCasesNumbers)) { + if (in_array($outerCaseKey, $processedCasesKeys)) { continue; } - $processedCasesNumbers[] = $outerCaseKey; + $processedCasesKeys[] = $outerCaseKey; if ($outerCase->stmts === []) { $result[] = $outerCase; @@ -127,7 +127,7 @@ private function removeDuplicatedCases(Switch_ $switch): void $equalCases = []; foreach ($switch->cases as $innerCaseKey => $innerCase) { - if (in_array($innerCaseKey, $processedCasesNumbers)) { + if (in_array($innerCaseKey, $processedCasesKeys)) { continue; } @@ -137,23 +137,17 @@ private function removeDuplicatedCases(Switch_ $switch): void } if ($this->areSwitchStmtsEqualsAndWithBreak($outerCase, $innerCase)) { - if ($casesWithoutStmts !== []) { - foreach ($casesWithoutStmts as $caseWithoutStmtsKey => $caseWithoutStmts) { - $equalCases[] = $caseWithoutStmts; - $processedCasesNumbers[] = $caseWithoutStmtsKey; - } - - $casesWithoutStmts = []; + foreach ($casesWithoutStmts as $caseWithoutStmtsKey => $caseWithoutStmts) { + $equalCases[] = $caseWithoutStmts; + $processedCasesKeys[] = $caseWithoutStmtsKey; } $innerCase->stmts = []; $equalCases[] = $innerCase; - $processedCasesNumbers[] = $innerCaseKey; - - $this->hasChanged = true; - } else { - $casesWithoutStmts = []; + $processedCasesKeys[] = $innerCaseKey; } + + $casesWithoutStmts = []; } if ($equalCases === []) { @@ -161,6 +155,8 @@ private function removeDuplicatedCases(Switch_ $switch): void continue; } + $this->hasChanged = true; + $equalCases[array_key_last($equalCases)]->stmts = $outerCase->stmts; $outerCase->stmts = []; @@ -185,11 +181,7 @@ private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nex } foreach ($currentCase->stmts as $stmt) { - if ($stmt instanceof Break_) { - return true; - } - - if ($stmt instanceof Return_) { + if ($stmt instanceof Break_ || $stmt instanceof Return_) { return true; } } @@ -199,6 +191,9 @@ private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nex private function areSwitchStmtsEqualsConsideringComments(Case_ $currentCase, Case_ $nextCase): bool { - return $this->betterStandardPrinter->print($currentCase->stmts) === $this->betterStandardPrinter->print($nextCase->stmts); + $currentCasePrintResult = $this->betterStandardPrinter->print($currentCase->stmts); + $nextCasePrintResult = $this->betterStandardPrinter->print($nextCase->stmts); + + return $currentCasePrintResult === $nextCasePrintResult; } }