66
77use PhpParser \Node ;
88use PhpParser \Node \Expr ;
9- use PhpParser \Node \Expr \Array_ ;
109use PhpParser \Node \Expr \BinaryOp \Equal ;
1110use PhpParser \Node \Expr \BinaryOp \Identical ;
1211use PhpParser \Node \Stmt \If_ ;
@@ -23,9 +22,11 @@ final class RemoveConditionExactReturnRector extends AbstractRector
2322{
2423 public function getRuleDefinition (): RuleDefinition
2524 {
26- return new RuleDefinition ('Remove unused empty array condition and return value directly ' , [
27- new CodeSample (
28- <<<'CODE_SAMPLE'
25+ return new RuleDefinition (
26+ 'Remove if with condition and return with same expr, followed by compared expr return ' ,
27+ [
28+ new CodeSample (
29+ <<<'CODE_SAMPLE'
2930final class SomeClass
3031{
3132 public function __construct(array $items)
@@ -39,8 +40,8 @@ public function __construct(array $items)
3940}
4041CODE_SAMPLE
4142
42- ,
43- <<<'CODE_SAMPLE'
43+ ,
44+ <<<'CODE_SAMPLE'
4445final class SomeClass
4546{
4647 public function __construct(array $items)
@@ -53,8 +54,10 @@ public function __construct(array $items)
5354 }
5455}
5556CODE_SAMPLE
56- ),
57- ]);
57+ ),
58+
59+ ]
60+ );
5861 }
5962
6063 /**
@@ -95,16 +98,12 @@ public function refactor(Node $node): ?Node
9598 $ identicalOrEqual = $ stmt ->cond ;
9699 $ return = $ soleIfStmt ;
97100
98- if (! $ this ->isEmptyArray ($ identicalOrEqual ->right )) {
101+ if (! $ this ->nodeComparator -> areNodesEqual ($ identicalOrEqual ->right , $ return -> expr )) {
99102 continue ;
100103 }
101104
102105 $ comparedVariable = $ identicalOrEqual ->left ;
103106
104- if (! $ this ->isEmptyArray ($ return ->expr )) {
105- continue ;
106- }
107-
108107 // next stmt must be return of the same var
109108 $ nextStmt = $ node ->stmts [$ key + 1 ] ?? null ;
110109 if (! $ nextStmt instanceof Return_) {
@@ -130,13 +129,4 @@ public function refactor(Node $node): ?Node
130129
131130 return null ;
132131 }
133-
134- private function isEmptyArray (?Expr $ expr ): bool
135- {
136- if (! $ expr instanceof Array_) {
137- return false ;
138- }
139-
140- return $ expr ->items === [];
141- }
142132}
0 commit comments