Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Reduce double loop on RemoveDuplicatedCaseInSwitchRector #5235

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public function refactor(Node $node): ?Node

$insertByKeys = $this->resolveInsertedByKeys($node);
$this->insertCaseByKeys($node, $insertByKeys);
$this->cleanUpEqualCaseStmts($node);

if (! $this->hasChanged) {
return null;
Expand All @@ -110,7 +109,6 @@ private function resolveInsertedByKeys(Switch_ $switch): array
continue;
}

$nextKey = $key + 1;
for ($jumpToKey = $key + 1; $jumpToKey < $totalKeys; ++$jumpToKey) {
if (! isset($switch->cases[$jumpToKey])) {
continue;
Expand All @@ -120,10 +118,6 @@ private function resolveInsertedByKeys(Switch_ $switch): array
continue;
}

if ($nextKey === $jumpToKey) {
continue 2;
}

$nextCase = $switch->cases[$jumpToKey];

unset($switch->cases[$jumpToKey]);
Expand All @@ -143,25 +137,13 @@ private function resolveInsertedByKeys(Switch_ $switch): array
private function insertCaseByKeys(Switch_ $switch, array $insertByKeys): void
{
foreach ($insertByKeys as $key => $insertByKey) {
$switch->cases[$key]->stmts = [];
$nextKey = $key + 1;

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

private function cleanUpEqualCaseStmts(Switch_ $switch): void
{
/** @var Case_|null $previousCase */
$previousCase = null;
foreach ($switch->cases as $case) {
if ($previousCase instanceof Case_ && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) {
$previousCase->stmts = [];

$this->hasChanged = true;
for ($jumpToKey = $key; $jumpToKey < $key + count($insertByKey); ++$jumpToKey) {
$switch->cases[$jumpToKey]->stmts = [];
}

$previousCase = $case;
}
}

Expand Down