Skip to content

Commit ade5d19

Browse files
authored
Refactor RemoveDuplicatedCaseInSwitchRector and adding test cases (#7175)
1 parent 5bbc5ed commit ade5d19

File tree

3 files changed

+84
-22
lines changed

3 files changed

+84
-22
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
4+
5+
class NoStmtsWithEqualComments
6+
{
7+
public function run($foo)
8+
{
9+
switch ($foo) {
10+
case 'A':
11+
// Equal comment
12+
break;
13+
case 'B':
14+
case 'C':
15+
// Some comment
16+
break;
17+
case 'D':
18+
$type = 'BAR';
19+
break;
20+
case 'E':
21+
case 'F':
22+
default:
23+
// Equal comment
24+
break;
25+
}
26+
}
27+
}
28+
-----
29+
<?php
30+
31+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
32+
33+
class NoStmtsWithEqualComments
34+
{
35+
public function run($foo)
36+
{
37+
switch ($foo) {
38+
case 'A':
39+
case 'E':
40+
case 'F':
41+
default:
42+
// Equal comment
43+
break;
44+
case 'B':
45+
case 'C':
46+
// Some comment
47+
break;
48+
case 'D':
49+
$type = 'BAR';
50+
break;
51+
}
52+
}
53+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
4+
5+
class SkipWithOneCase
6+
{
7+
public function run($foo)
8+
{
9+
switch ($foo) {
10+
case 'A':
11+
return 'a';
12+
}
13+
}
14+
}

rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ private function removeDuplicatedCases(Switch_ $switch): void
106106
$result = [];
107107

108108
/** @var int[] */
109-
$processedCasesNumbers = [];
109+
$processedCasesKeys = [];
110110

111111
foreach ($switch->cases as $outerCaseKey => $outerCase) {
112-
if (in_array($outerCaseKey, $processedCasesNumbers)) {
112+
if (in_array($outerCaseKey, $processedCasesKeys)) {
113113
continue;
114114
}
115115

116-
$processedCasesNumbers[] = $outerCaseKey;
116+
$processedCasesKeys[] = $outerCaseKey;
117117

118118
if ($outerCase->stmts === []) {
119119
$result[] = $outerCase;
@@ -127,7 +127,7 @@ private function removeDuplicatedCases(Switch_ $switch): void
127127
$equalCases = [];
128128

129129
foreach ($switch->cases as $innerCaseKey => $innerCase) {
130-
if (in_array($innerCaseKey, $processedCasesNumbers)) {
130+
if (in_array($innerCaseKey, $processedCasesKeys)) {
131131
continue;
132132
}
133133

@@ -137,30 +137,26 @@ private function removeDuplicatedCases(Switch_ $switch): void
137137
}
138138

139139
if ($this->areSwitchStmtsEqualsAndWithBreak($outerCase, $innerCase)) {
140-
if ($casesWithoutStmts !== []) {
141-
foreach ($casesWithoutStmts as $caseWithoutStmtsKey => $caseWithoutStmts) {
142-
$equalCases[] = $caseWithoutStmts;
143-
$processedCasesNumbers[] = $caseWithoutStmtsKey;
144-
}
145-
146-
$casesWithoutStmts = [];
140+
foreach ($casesWithoutStmts as $caseWithoutStmtsKey => $caseWithoutStmts) {
141+
$equalCases[] = $caseWithoutStmts;
142+
$processedCasesKeys[] = $caseWithoutStmtsKey;
147143
}
148144

149145
$innerCase->stmts = [];
150146
$equalCases[] = $innerCase;
151-
$processedCasesNumbers[] = $innerCaseKey;
152-
153-
$this->hasChanged = true;
154-
} else {
155-
$casesWithoutStmts = [];
147+
$processedCasesKeys[] = $innerCaseKey;
156148
}
149+
150+
$casesWithoutStmts = [];
157151
}
158152

159153
if ($equalCases === []) {
160154
$result[] = $outerCase;
161155
continue;
162156
}
163157

158+
$this->hasChanged = true;
159+
164160
$equalCases[array_key_last($equalCases)]->stmts = $outerCase->stmts;
165161
$outerCase->stmts = [];
166162

@@ -185,11 +181,7 @@ private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nex
185181
}
186182

187183
foreach ($currentCase->stmts as $stmt) {
188-
if ($stmt instanceof Break_) {
189-
return true;
190-
}
191-
192-
if ($stmt instanceof Return_) {
184+
if ($stmt instanceof Break_ || $stmt instanceof Return_) {
193185
return true;
194186
}
195187
}
@@ -199,6 +191,9 @@ private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nex
199191

200192
private function areSwitchStmtsEqualsConsideringComments(Case_ $currentCase, Case_ $nextCase): bool
201193
{
202-
return $this->betterStandardPrinter->print($currentCase->stmts) === $this->betterStandardPrinter->print($nextCase->stmts);
194+
$currentCasePrintResult = $this->betterStandardPrinter->print($currentCase->stmts);
195+
$nextCasePrintResult = $this->betterStandardPrinter->print($nextCase->stmts);
196+
197+
return $currentCasePrintResult === $nextCasePrintResult;
203198
}
204199
}

0 commit comments

Comments
 (0)