Skip to content

Commit 1f0a888

Browse files
authored
[DeadCode] Skip case reached by fall-through in RemoveDuplicatedCaseInSwitchRector (#9020)
1 parent cd2d877 commit 1f0a888

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
4+
5+
final class SkipFallThroughIntoDuplicate
6+
{
7+
public function run($name)
8+
{
9+
switch ($name) {
10+
case 'a':
11+
return 'X';
12+
case 'b':
13+
echo 'log';
14+
case 'c':
15+
return 'X';
16+
case 'd':
17+
return 'Y';
18+
}
19+
}
20+
}

‎rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
namespace Rector\DeadCode\Rector\Switch_;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Throw_;
89
use PhpParser\Node\Stmt;
910
use PhpParser\Node\Stmt\Break_;
1011
use PhpParser\Node\Stmt\Case_;
12+
use PhpParser\Node\Stmt\Continue_;
13+
use PhpParser\Node\Stmt\Expression;
1114
use PhpParser\Node\Stmt\Return_;
1215
use PhpParser\Node\Stmt\Switch_;
1316
use Rector\PhpParser\Printer\BetterStandardPrinter;
@@ -109,6 +112,8 @@ private function removeDuplicatedCases(Switch_ $switch): void
109112
/** @var int[] $processedCasesKeys */
110113
$processedCasesKeys = [];
111114

115+
$fallThroughCasesKeys = $this->resolveFallThroughCasesKeys($switch);
116+
112117
foreach ($switch->cases as $outerCaseKey => $outerCase) {
113118
if (in_array($outerCaseKey, $processedCasesKeys)) {
114119
continue;
@@ -137,6 +142,12 @@ private function removeDuplicatedCases(Switch_ $switch): void
137142
continue;
138143
}
139144

145+
// moving a case reached by fall-through would change what the previous case runs into
146+
if (in_array($innerCaseKey, $fallThroughCasesKeys, true)) {
147+
$casesWithoutStmts = [];
148+
continue;
149+
}
150+
140151
if ($this->areSwitchStmtsEqualsAndWithBreak($outerCase, $innerCase)) {
141152
foreach ($casesWithoutStmts as $caseWithoutStmtsKey => $caseWithoutStmts) {
142153
$equalCases[] = $caseWithoutStmts;
@@ -167,6 +178,38 @@ private function removeDuplicatedCases(Switch_ $switch): void
167178
$switch->cases = $result;
168179
}
169180

181+
/**
182+
* @return int[]
183+
*/
184+
private function resolveFallThroughCasesKeys(Switch_ $switch): array
185+
{
186+
$fallThroughCasesKeys = [];
187+
$isPreviousFallingThrough = false;
188+
189+
foreach ($switch->cases as $key => $case) {
190+
if ($case->stmts === []) {
191+
continue;
192+
}
193+
194+
if ($isPreviousFallingThrough) {
195+
$fallThroughCasesKeys[] = $key;
196+
}
197+
198+
$isPreviousFallingThrough = ! $this->isTerminatingStmt($case->stmts[array_key_last($case->stmts)]);
199+
}
200+
201+
return $fallThroughCasesKeys;
202+
}
203+
204+
private function isTerminatingStmt(Stmt $stmt): bool
205+
{
206+
if ($stmt instanceof Break_ || $stmt instanceof Return_ || $stmt instanceof Continue_) {
207+
return true;
208+
}
209+
210+
return $stmt instanceof Expression && $stmt->expr instanceof Throw_;
211+
}
212+
170213
private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nextCase): bool
171214
{
172215
/**

0 commit comments

Comments
 (0)