Skip to content

Commit 6046f59

Browse files
committed
skip method/static calls
1 parent 33fe354 commit 6046f59

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveConditionExactReturnRector\Fixture;
4+
5+
final class SkipSideEffectCall
6+
{
7+
public function run($result)
8+
{
9+
if ($result === $this->isMissing(100)) {
10+
return $this->isMissing(100);
11+
}
12+
13+
return $result;
14+
}
15+
16+
private function isMissing(int $int): int
17+
{
18+
return mt_rand(0, 1) * $int;
19+
}
20+
}

rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\BinaryOp\Equal;
1010
use PhpParser\Node\Expr\BinaryOp\Identical;
11+
use PhpParser\Node\Expr\MethodCall;
12+
use PhpParser\Node\Expr\StaticCall;
1113
use PhpParser\Node\Stmt\If_;
1214
use PhpParser\Node\Stmt\Return_;
1315
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
@@ -82,12 +84,8 @@ public function refactor(Node $node): ?Node
8284
continue;
8385
}
8486

85-
if (count($stmt->stmts) !== 1) {
86-
continue;
87-
}
88-
89-
$soleIfStmt = $stmt->stmts[0];
90-
if (! $soleIfStmt instanceof Return_) {
87+
$soleIfReturn = $this->matchSoleIfReturn($stmt);
88+
if (! $soleIfReturn instanceof Return_) {
9189
continue;
9290
}
9391

@@ -96,15 +94,19 @@ public function refactor(Node $node): ?Node
9694
}
9795

9896
$identicalOrEqual = $stmt->cond;
99-
$return = $soleIfStmt;
10097

101-
if (! $this->nodeComparator->areNodesEqual($identicalOrEqual->right, $return->expr)) {
98+
// skip obvious complexity
99+
if ($identicalOrEqual->right instanceof MethodCall || $identicalOrEqual->right instanceof StaticCall) {
100+
continue;
101+
}
102+
103+
if (! $this->nodeComparator->areNodesEqual($identicalOrEqual->right, $soleIfReturn->expr)) {
102104
continue;
103105
}
104106

105107
$comparedVariable = $identicalOrEqual->left;
106108

107-
// next stmt must be return of the same var
109+
// next if must be return of the same var
108110
$nextStmt = $node->stmts[$key + 1] ?? null;
109111
if (! $nextStmt instanceof Return_) {
110112
continue;
@@ -118,7 +120,7 @@ public function refactor(Node $node): ?Node
118120
continue;
119121
}
120122

121-
// remove next stmt
123+
// remove next if
122124
unset($node->stmts[$key + 1]);
123125

124126
// replace if with return
@@ -129,4 +131,18 @@ public function refactor(Node $node): ?Node
129131

130132
return null;
131133
}
134+
135+
private function matchSoleIfReturn(If_ $if): ?Return_
136+
{
137+
if (count($if->stmts) !== 1) {
138+
return null;
139+
}
140+
141+
$soleIfStmt = $if->stmts[0];
142+
if (! $soleIfStmt instanceof Return_) {
143+
return null;
144+
}
145+
146+
return $soleIfStmt;
147+
}
132148
}

0 commit comments

Comments
 (0)