File tree Expand file tree Collapse file tree 3 files changed +68
-1
lines changed
rules-tests/DowngradePhp80/Rector/FuncCall/DowngradeSubstrFalsyRector/Fixture
rules/DowngradePhp80/Rector/FuncCall Expand file tree Collapse file tree 3 files changed +68
-1
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \DowngradePhp80 \Rector \FuncCall \DowngradeSubstrFalsyRector \Fixture ;
4+
5+ class ComparedToEmptyString
6+ {
7+ public function run (string $ name )
8+ {
9+ return substr ($ name , 2 ) === '' ;
10+ }
11+
12+ public function run2 (string $ name )
13+ {
14+ return substr ($ name , 2 ) !== '' ;
15+ }
16+
17+ public function run3 (string $ name )
18+ {
19+ return substr ($ name , 2 ) == '' ;
20+ }
21+ }
22+
23+ ?>
24+ -----
25+ <?php
26+
27+ namespace Rector \Tests \DowngradePhp80 \Rector \FuncCall \DowngradeSubstrFalsyRector \Fixture ;
28+
29+ class ComparedToEmptyString
30+ {
31+ public function run (string $ name )
32+ {
33+ return (string ) substr ($ name , 2 ) === '' ;
34+ }
35+
36+ public function run2 (string $ name )
37+ {
38+ return (string ) substr ($ name , 2 ) !== '' ;
39+ }
40+
41+ public function run3 (string $ name )
42+ {
43+ return (string ) substr ($ name , 2 ) == '' ;
44+ }
45+ }
46+
47+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \DowngradePhp80 \Rector \FuncCall \DowngradeSubstrFalsyRector \Fixture ;
4+
5+ class SkipDirectCond
6+ {
7+ public function run (string $ name )
8+ {
9+ if (substr ($ name , 2 )) {
10+ return true ;
11+ }
12+ }
13+ }
Original file line number Diff line number Diff line change 1919use PhpParser \Node \Expr \MethodCall ;
2020use PhpParser \Node \Expr \StaticCall ;
2121use PhpParser \Node \Expr \Ternary ;
22+ use PhpParser \Node \Stmt \If_ ;
2223use PHPStan \Reflection \FunctionReflection ;
2324use PHPStan \Reflection \MethodReflection ;
2425use PHPStan \Type \Constant \ConstantIntegerType ;
@@ -71,12 +72,13 @@ public function getNodeTypes(): array
7172 MethodCall::class,
7273 StaticCall::class,
7374 AssignOp::class,
75+ If_::class,
7476 FuncCall::class,
7577 ];
7678 }
7779
7880 /**
79- * @param Cast|Empty_|BooleanNot|Ternary|Identical|Concat|MethodCall|StaticCall|AssignOp|FuncCall $node
81+ * @param Cast|Empty_|BooleanNot|Ternary|Identical|Concat|MethodCall|StaticCall|AssignOp|If_| FuncCall $node
8082 */
8183 public function refactor (Node $ node ): ?Node
8284 {
@@ -111,6 +113,11 @@ public function refactor(Node $node): ?Node
111113 return null ;
112114 }
113115
116+ if ($ node instanceof If_) {
117+ $ node ->cond ->setAttribute (self ::IS_UNCASTABLE , true );
118+ return null ;
119+ }
120+
114121 if ($ node instanceof CallLike) {
115122 if ($ node ->isFirstClassCallable ()) {
116123 return null ;
You can’t perform that action at this time.
0 commit comments