66
77use PhpParser \Node ;
88use PhpParser \Node \Expr ;
9+ use PhpParser \Node \Expr \BinaryOp \Equal ;
10+ use PhpParser \Node \Expr \BinaryOp \Identical ;
911use PhpParser \Node \Expr \BooleanNot ;
1012use PhpParser \Node \Expr \Cast ;
1113use PhpParser \Node \Expr \Cast \String_ ;
1214use PhpParser \Node \Expr \Empty_ ;
1315use PhpParser \Node \Expr \FuncCall ;
1416use PhpParser \Node \Expr \Ternary ;
17+ use Rector \PhpParser \Node \Value \ValueResolver ;
1518use Rector \Rector \AbstractRector ;
1619use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1720use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
@@ -28,6 +31,12 @@ final class DowngradeSubstrFalsyRector extends AbstractRector
2831 */
2932 private const IS_UNCASTABLE = 'is_uncastable ' ;
3033
34+ public function __construct (
35+ private readonly ValueResolver $ valueResolver
36+ ) {
37+
38+ }
39+
3140 public function getRuleDefinition (): RuleDefinition
3241 {
3342 return new RuleDefinition ('Downgrade substr() with cast string on possibly falsy result ' , [
@@ -40,11 +49,11 @@ public function getRuleDefinition(): RuleDefinition
4049 */
4150 public function getNodeTypes (): array
4251 {
43- return [Cast::class, Empty_::class, BooleanNot::class, Ternary::class, FuncCall::class];
52+ return [Cast::class, Empty_::class, BooleanNot::class, Ternary::class, Equal::class, Identical::class, FuncCall::class];
4453 }
4554
4655 /**
47- * @param Cast|Empty_|BooleanNot|Ternary|FuncCall $node
56+ * @param Cast|Empty_|BooleanNot|Ternary|Equal|Identical| FuncCall $node
4857 */
4958 public function refactor (Node $ node ): ?Node
5059 {
@@ -61,6 +70,18 @@ public function refactor(Node $node): ?Node
6170 return null ;
6271 }
6372
73+ if ($ node instanceof Equal || $ node instanceof Identical) {
74+ if ($ this ->valueResolver ->isFalse ($ node ->left )) {
75+ $ node ->right ->setAttribute (self ::IS_UNCASTABLE , true );
76+ }
77+
78+ if ($ this ->valueResolver ->isFalse ($ node ->right )) {
79+ $ node ->left ->setAttribute (self ::IS_UNCASTABLE , true );
80+ }
81+
82+ return null ;
83+ }
84+
6485 if (! $ this ->isName ($ node , 'substr ' )) {
6586 return null ;
6687 }
0 commit comments