Skip to content

Commit 33fe354

Browse files
committed
allow any expr
1 parent e592082 commit 33fe354

1 file changed

Lines changed: 12 additions & 22 deletions

File tree

rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9-
use PhpParser\Node\Expr\Array_;
109
use PhpParser\Node\Expr\BinaryOp\Equal;
1110
use PhpParser\Node\Expr\BinaryOp\Identical;
1211
use 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'
2930
final class SomeClass
3031
{
3132
public function __construct(array $items)
@@ -39,8 +40,8 @@ public function __construct(array $items)
3940
}
4041
CODE_SAMPLE
4142

42-
,
43-
<<<'CODE_SAMPLE'
43+
,
44+
<<<'CODE_SAMPLE'
4445
final class SomeClass
4546
{
4647
public function __construct(array $items)
@@ -53,8 +54,10 @@ public function __construct(array $items)
5354
}
5455
}
5556
CODE_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

Comments
 (0)