Skip to content

Commit cd2d877

Browse files
authored
[CodeQuality] Keep loose comparison in SingularSwitchToIfRector (#9019)
1 parent 6d105a0 commit cd2d877

5 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎rules-tests/CodeQuality/Rector/Switch_/SingularSwitchToIfRector/Fixture/closure_with_break.php.inc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class ClosureWithBreak
2828
{
2929
public function run($value, array $items)
3030
{
31-
if ($value === 100) {
31+
if ($value == 100) {
3232
$callback = function () use ($items) {
3333
foreach ($items as $item) {
3434
break;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SingularSwitchToIfRector\Fixture;
4+
5+
final class IntTypedKeepsStrict
6+
{
7+
public function run(int $value)
8+
{
9+
switch ($value) {
10+
case 100:
11+
return 'hit';
12+
}
13+
14+
return 'miss';
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SingularSwitchToIfRector\Fixture;
23+
24+
final class IntTypedKeepsStrict
25+
{
26+
public function run(int $value)
27+
{
28+
if ($value === 100) {
29+
return 'hit';
30+
}
31+
32+
return 'miss';
33+
}
34+
}
35+
36+
?>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SingularSwitchToIfRector\Fixture;
4+
5+
final class NumericStringKeepsLoose
6+
{
7+
public function run(string $value)
8+
{
9+
switch ($value) {
10+
case '100':
11+
return 'hit';
12+
}
13+
14+
return 'miss';
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SingularSwitchToIfRector\Fixture;
23+
24+
final class NumericStringKeepsLoose
25+
{
26+
public function run(string $value)
27+
{
28+
if ($value == '100') {
29+
return 'hit';
30+
}
31+
32+
return 'miss';
33+
}
34+
}
35+
36+
?>

‎rules-tests/CodeQuality/Rector/Switch_/SingularSwitchToIfRector/Fixture/some_class.php.inc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SomeClass
2727
public function run($value)
2828
{
2929
$result = 1;
30-
if ($value === 100) {
30+
if ($value == 100) {
3131
$result = 1000;
3232
}
3333

‎rules/CodeQuality/Rector/Switch_/SingularSwitchToIfRector.php‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\BinaryOp\Equal;
910
use PhpParser\Node\Expr\BinaryOp\Identical;
1011
use PhpParser\Node\FunctionLike;
1112
use PhpParser\Node\Stmt;
@@ -57,7 +58,7 @@ class SomeObject
5758
public function run($value)
5859
{
5960
$result = 1;
60-
if ($value === 100) {
61+
if ($value == 100) {
6162
$result = 1000;
6263
}
6364
@@ -100,12 +101,27 @@ public function refactor(Node $node): array|If_|null
100101
return array_filter($onlyCase->stmts, static fn (Stmt $stmt): bool => ! $stmt instanceof Break_);
101102
}
102103

103-
$if = new If_(new Identical($node->cond, $onlyCase->cond));
104+
$if = new If_($this->createComparison($node->cond, $onlyCase->cond));
104105
$if->stmts = $this->switchManipulator->removeBreakNodes($onlyCase->stmts);
105106

106107
return $if;
107108
}
108109

110+
/**
111+
* switch compares loosely, so strict comparison is safe only for 2 integers
112+
*/
113+
private function createComparison(Expr $leftExpr, Expr $rightExpr): Equal|Identical
114+
{
115+
$leftType = $this->getType($leftExpr);
116+
$rightType = $this->getType($rightExpr);
117+
118+
if ($leftType->isInteger()->yes() && $rightType->isInteger()->yes()) {
119+
return new Identical($leftExpr, $rightExpr);
120+
}
121+
122+
return new Equal($leftExpr, $rightExpr);
123+
}
124+
109125
/**
110126
* @param Stmt[] $stmts
111127
*/

0 commit comments

Comments
 (0)