Skip to content

Commit bda2d3c

Browse files
authored
Fix RemoveDuplicatedCaseInSwitchRector behavior (#7173)
* Fix `RemoveDuplicatedCaseInSwitchRector` behavior * fix cs * fix and test case
1 parent dd7d554 commit bda2d3c

File tree

7 files changed

+315
-34
lines changed

7 files changed

+315
-34
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ composer.lock
44
# PHPStorm meta files
55
.idea/
66

7+
# VS Code files
8+
.vscode
9+
710
.phpunit.result.cache
811
# since PHPUnit 10
912
.phpunit.cache
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
4+
5+
class ConsecutiveEqualCaseStmts4
6+
{
7+
public function run($name)
8+
{
9+
switch ($name) {
10+
case 'a':
11+
return 'A';
12+
case 'b':
13+
return 'C';
14+
case 'd':
15+
case 'c':
16+
case 'f':
17+
default:
18+
return 'C';
19+
}
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
28+
29+
class ConsecutiveEqualCaseStmts4
30+
{
31+
public function run($name)
32+
{
33+
switch ($name) {
34+
case 'a':
35+
return 'A';
36+
case 'b':
37+
case 'd':
38+
case 'c':
39+
case 'f':
40+
default:
41+
return 'C';
42+
}
43+
}
44+
}
45+
46+
?>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
4+
5+
class ConsecutiveEqualCaseStmts5
6+
{
7+
public function run($name)
8+
{
9+
switch ($name) {
10+
case 'a':
11+
return 'A';
12+
case 'b':
13+
return 'C';
14+
default:
15+
case 'd':
16+
case 'c':
17+
case 'f':
18+
return 'C';
19+
}
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
28+
29+
class ConsecutiveEqualCaseStmts5
30+
{
31+
public function run($name)
32+
{
33+
switch ($name) {
34+
case 'a':
35+
return 'A';
36+
case 'b':
37+
default:
38+
case 'd':
39+
case 'c':
40+
case 'f':
41+
return 'C';
42+
}
43+
}
44+
}
45+
46+
?>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
4+
5+
class DifferentIndirectDuplicated4
6+
{
7+
public function run()
8+
{
9+
switch ($item) {
10+
case 'a':
11+
case 'b':
12+
return 'val1';
13+
case 'c':
14+
return 'val2';
15+
case 'd':
16+
case 'e':
17+
return 'val5';
18+
case 'f':
19+
case 'g':
20+
case 'h':
21+
return 'val2';
22+
case 'i':
23+
return 'val3';
24+
}
25+
}
26+
}
27+
28+
?>
29+
-----
30+
<?php
31+
32+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
33+
34+
class DifferentIndirectDuplicated4
35+
{
36+
public function run()
37+
{
38+
switch ($item) {
39+
case 'a':
40+
case 'b':
41+
return 'val1';
42+
case 'c':
43+
case 'f':
44+
case 'g':
45+
case 'h':
46+
return 'val2';
47+
case 'd':
48+
case 'e':
49+
return 'val5';
50+
case 'i':
51+
return 'val3';
52+
}
53+
}
54+
}
55+
56+
?>
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 DifferentIndirectDuplicated5
6+
{
7+
public function run($name)
8+
{
9+
switch ($name) {
10+
case 'a':
11+
return 'A';
12+
case 'b':
13+
return 'C';
14+
case 'd':
15+
case 'c':
16+
case 'f':
17+
return 'C';
18+
case 'e':
19+
return 'A';
20+
default:
21+
return 'C';
22+
case 'i':
23+
return 'A';
24+
}
25+
}
26+
}
27+
28+
?>
29+
-----
30+
<?php
31+
32+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
33+
34+
class DifferentIndirectDuplicated5
35+
{
36+
public function run($name)
37+
{
38+
switch ($name) {
39+
case 'a':
40+
case 'e':
41+
case 'i':
42+
return 'A';
43+
case 'b':
44+
case 'd':
45+
case 'c':
46+
case 'f':
47+
default:
48+
return 'C';
49+
}
50+
}
51+
}
52+
53+
?>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
4+
5+
class NoStmtsWithoutComments
6+
{
7+
public function run($foo)
8+
{
9+
switch ($foo) {
10+
case 'A':
11+
break;
12+
case 'B':
13+
case 'C':
14+
break;
15+
case 'D':
16+
$type = 'BAR';
17+
break;
18+
case 'E':
19+
case 'F':
20+
default:
21+
break;
22+
}
23+
}
24+
}
25+
-----
26+
<?php
27+
28+
namespace Rector\Tests\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;
29+
30+
class NoStmtsWithoutComments
31+
{
32+
public function run($foo)
33+
{
34+
switch ($foo) {
35+
case 'A':
36+
case 'B':
37+
case 'C':
38+
case 'E':
39+
case 'F':
40+
default:
41+
break;
42+
case 'D':
43+
$type = 'BAR';
44+
break;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)