Skip to content
Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,47 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;

class DiffrentIndirectDuplicated
{
public function run($name)
{
switch ($name) {
case 'a':
return 'A';
case 'b':
return 'B';
case 'c';
return 'C';
case 'd':
return 'A';
case 'e':
return 'B';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;

class DiffrentIndirectDuplicated
{
public function run($name)
{
switch ($name) {
case 'a':
case 'd':
return 'A';
case 'b':
case 'e':
return 'B';
case 'c';
return 'C';
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;

class DiffrentIndirectDuplicated2
{
public function run($name)
{
switch ($name) {
case 'a':
return 'A';
case 'b':
return 'B';
case 'c';
return 'C';
case 'f':
return 'C';
case 'd':
return 'A';
case 'e':
return 'B';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;

class DiffrentIndirectDuplicated2
{
public function run($name)
{
switch ($name) {
case 'a':
case 'd':
return 'A';
case 'b':
case 'e':
return 'B';
case 'c':
case 'f':
return 'C';
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
}

Expand Down