55namespace Rector \Doctrine \TypedCollections \Rector \If_ ;
66
77use PhpParser \Node ;
8+ use PhpParser \Node \Expr ;
9+ use PhpParser \Node \Expr \BinaryOp \BooleanAnd ;
810use PhpParser \Node \Expr \BinaryOp \BooleanOr ;
911use PhpParser \Node \Expr \BinaryOp \Identical ;
10- use PhpParser \Node \Expr \ConstFetch ;
12+ use PhpParser \Node \Expr \BinaryOp \ NotIdentical ;
1113use PhpParser \Node \Stmt \If_ ;
1214use Rector \Doctrine \TypedCollections \TypeAnalyzer \CollectionTypeDetector ;
15+ use Rector \PhpParser \Node \Value \ValueResolver ;
1316use Rector \Rector \AbstractRector ;
1417use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1518use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
@@ -21,6 +24,7 @@ final class RemoveIfCollectionIdenticalToNullRector extends AbstractRector
2124{
2225 public function __construct (
2326 private readonly CollectionTypeDetector $ collectionTypeDetector ,
27+ private readonly ValueResolver $ valueResolver ,
2428 ) {
2529
2630 }
@@ -35,31 +39,25 @@ public function getNodeTypes(): array
3539 */
3640 public function refactor (Node $ node ): ?If_
3741 {
38- if (! $ node ->cond instanceof BooleanOr) {
39- return null ;
40- }
42+ if ($ node ->cond instanceof BooleanOr) {
43+ $ changedCond = $ this ->refactorBooleanOr ($ node ->cond );
44+ if ($ changedCond instanceof Expr) {
45+ $ node ->cond = $ changedCond ;
4146
42- $ leftCondition = $ node ->cond ->left ;
43- if (! $ leftCondition instanceof Identical) {
44- return null ;
47+ return $ node ;
48+ }
4549 }
4650
47- if (! $ leftCondition ->right instanceof ConstFetch || ! $ this ->isName (
48- $ leftCondition ->right ->name ,
49- 'null '
50- )) {
51- return null ;
52- }
51+ if ($ node ->cond instanceof BooleanAnd) {
52+ $ changedCond = $ this ->refactorBooleanAnd ($ node ->cond );
53+ if ($ changedCond instanceof Expr) {
54+ $ node ->cond = $ changedCond ;
5355
54- if (! $ this -> collectionTypeDetector -> isCollectionType ( $ leftCondition -> left )) {
55- return null ;
56+ return $ node ;
57+ }
5658 }
5759
58- $ rightCondition = $ node ->cond ->right ;
59-
60- $ node ->cond = $ rightCondition ;
61-
62- return $ node ;
60+ return null ;
6361 }
6462
6563 public function getRuleDefinition (): RuleDefinition
@@ -103,4 +101,40 @@ public function someMethod()
103101CODE_SAMPLE
104102 )]);
105103 }
104+
105+ private function refactorBooleanOr (BooleanOr $ booleanOr ): ?Expr
106+ {
107+ $ leftCondition = $ booleanOr ->left ;
108+ if (! $ leftCondition instanceof Identical) {
109+ return null ;
110+ }
111+
112+ if (! $ this ->valueResolver ->isNull (($ leftCondition ->right ))) {
113+ return null ;
114+ }
115+
116+ if (! $ this ->collectionTypeDetector ->isCollectionType ($ leftCondition ->left )) {
117+ return null ;
118+ }
119+
120+ return $ booleanOr ->right ;
121+ }
122+
123+ private function refactorBooleanAnd (BooleanAnd $ booleanAnd ): ?Expr
124+ {
125+ $ leftCondition = $ booleanAnd ->left ;
126+ if (! $ leftCondition instanceof NotIdentical) {
127+ return null ;
128+ }
129+
130+ if (! $ this ->valueResolver ->isNull ($ leftCondition ->right )) {
131+ return null ;
132+ }
133+
134+ if (! $ this ->collectionTypeDetector ->isCollectionType ($ leftCondition ->left )) {
135+ return null ;
136+ }
137+
138+ return $ booleanAnd ->right ;
139+ }
106140}
0 commit comments