Skip to content

Commit

Permalink
[DeadCode] Handle different indirect duplicated on RemoveDuplicatedCa…
Browse files Browse the repository at this point in the history
…seInSwitchRector (#5236)

* [DeadCode] Handle different indirect duplicated on RemoveDuplicatedCaseInSwitchRector

* Fixed 🎉

* add more fixture

* more fixture

* phpstan

* fixed
  • Loading branch information
samsonasik committed Nov 10, 2023
1 parent c61d8ea commit bee1dae
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
@@ -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';
}
}
}

?>
@@ -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';
}
}
}

?>
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

0 comments on commit bee1dae

Please sign in to comment.