Skip to content

Commit

Permalink
[DeadCode] Reduce loop on RemoveDuplicatedCaseInSwitchRector (#5238)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik authored Nov 10, 2023
1 parent b0d4b15 commit eac58be
Showing 1 changed file with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,17 @@ private function resolveInsertedByKeys(Switch_ $switch): array
$totalKeys = count($switch->cases);
$insertByKeys = [];
$appendKey = 0;
/** @var Case_|null $previousCase */
$previousCase = null;

foreach ($switch->cases as $key => $case) {
if ($case->stmts === []) {
continue;
if ($previousCase instanceof Case_ && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) {
$previousCase->stmts = [];
$this->hasChanged = true;
}

$previousCase = $case;

for ($jumpToKey = $key + 2; $jumpToKey < $totalKeys; ++$jumpToKey) {
if (! isset($switch->cases[$jumpToKey])) {
continue;
Expand Down Expand Up @@ -143,23 +148,16 @@ private function insertCaseByKeys(Switch_ $switch, array $insertByKeys): void
$nextKey = $key + 1;

array_splice($switch->cases, $nextKey, 0, $insertByKey);
}

/** @var Case_|null $previousCase */
$previousCase = null;
foreach ($switch->cases as $case) {
if ($previousCase instanceof Case_ && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) {
$previousCase->stmts = [];
$this->hasChanged = true;
}
$switch->cases[$key]->stmts = [];

$previousCase = $case;
$this->hasChanged = true;
}
}

private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nextCase): bool
private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nextOrPrevCase): bool
{
if (! $this->nodeComparator->areNodesEqual($currentCase->stmts, $nextCase->stmts)) {
if (! $this->nodeComparator->areNodesEqual($currentCase->stmts, $nextOrPrevCase->stmts)) {
return false;
}

Expand Down

0 comments on commit eac58be

Please sign in to comment.