File tree Expand file tree Collapse file tree 2 files changed +33
-5
lines changed
rules-tests/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector/Fixture
rules/CodeQuality/Rector/Equal Expand file tree Collapse file tree 2 files changed +33
-5
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \CodeQuality \Rector \Equal \UseIdenticalOverEqualWithSameTypeRector \Fixture ;
4+
5+ /**
6+ * @see https://3v4l.org/i3oCg
7+ */
8+ final class SkipBoolEqualInteger
9+ {
10+ public function equal ()
11+ {
12+ // maybe compare to numeric string
13+ $ value = rand (0 , 1 ) ? 1 : null ;
14+
15+ var_dump (true == $ value );
16+ var_dump ($ value == true );
17+ }
18+ }
Original file line number Diff line number Diff line change @@ -74,7 +74,7 @@ public function refactor(Node $node): ?Node
7474 $ leftStaticType = $ this ->nodeTypeResolver ->getNativeType ($ node ->left );
7575 $ rightStaticType = $ this ->nodeTypeResolver ->getNativeType ($ node ->right );
7676
77- if ($ this ->shouldSkipCompareNumericString ($ leftStaticType , $ rightStaticType )) {
77+ if ($ this ->shouldSkipCompareBoolToNumeric ($ leftStaticType , $ rightStaticType )) {
7878 return null ;
7979 }
8080
@@ -101,15 +101,25 @@ public function refactor(Node $node): ?Node
101101 return $ this ->processIdenticalOrNotIdentical ($ node );
102102 }
103103
104- private function shouldSkipCompareNumericString (Type $ leftStaticType , Type $ rightStaticType ): bool
104+ private function shouldSkipCompareBoolToNumeric (Type $ leftStaticType , Type $ rightStaticType ): bool
105105 {
106- // use ! ->no() as to support both yes and maybe
106+ // use ! ->no() as to verify both yes and maybe
107107 if ($ leftStaticType instanceof BooleanType) {
108- return ! $ rightStaticType ->isNumericString ()->no ();
108+ if (! $ rightStaticType ->isNumericString ()->no ()) {
109+ return true ;
110+ }
111+
112+ return ! $ rightStaticType ->isInteger ()
113+ ->no ();
109114 }
110115
111116 if ($ rightStaticType instanceof BooleanType) {
112- return ! $ leftStaticType ->isNumericString ()->no ();
117+ if (! $ leftStaticType ->isNumericString ()->no ()) {
118+ return true ;
119+ }
120+
121+ return ! $ leftStaticType ->isInteger ()
122+ ->no ();
113123 }
114124
115125 return false ;
You can’t perform that action at this time.
0 commit comments