File tree Expand file tree Collapse file tree
rules-tests/CodeQuality/Rector/Switch_/SingularSwitchToIfRector/Fixture
rules/CodeQuality/Rector/Switch_ Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 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+ ?>
Original file line number Diff line number Diff line change 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+ ?>
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 66
77use PhpParser \Node ;
88use PhpParser \Node \Expr ;
9+ use PhpParser \Node \Expr \BinaryOp \Equal ;
910use PhpParser \Node \Expr \BinaryOp \Identical ;
1011use PhpParser \Node \FunctionLike ;
1112use 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 */
You can’t perform that action at this time.
0 commit comments