Skip to content

Commit 975c46e

Browse files
authored
[NodeAnalyzer] Treat infinite loop without break as always terminated (#8348)
1 parent 1813324 commit 975c46e

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;
4+
5+
class AlwaysTerminatedInfiniteWhile
6+
{
7+
public function run($a)
8+
{
9+
while (true) {
10+
if ($a) {
11+
return 'A';
12+
}
13+
14+
$a = doSomething();
15+
}
16+
17+
echo 'never executed';
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;
26+
27+
class AlwaysTerminatedInfiniteWhile
28+
{
29+
public function run($a)
30+
{
31+
while (true) {
32+
if ($a) {
33+
return 'A';
34+
}
35+
36+
$a = doSomething();
37+
}
38+
}
39+
}
40+
41+
?>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;
4+
5+
class SkipWhileTrueWithBreak
6+
{
7+
public function run($a)
8+
{
9+
while (true) {
10+
if ($a) {
11+
break;
12+
}
13+
14+
$a = doSomething();
15+
}
16+
17+
echo 'still reachable after break';
18+
}
19+
}

‎src/NodeAnalyzer/TerminatedNodeAnalyzer.php‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\ConstFetch;
910
use PhpParser\Node\Expr\Exit_;
1011
use PhpParser\Node\Expr\Throw_;
1112
use PhpParser\Node\FunctionLike;
13+
use PhpParser\Node\Scalar\Int_;
1214
use PhpParser\Node\Stmt;
1315
use PhpParser\Node\Stmt\Break_;
1416
use PhpParser\Node\Stmt\ClassLike;
1517
use PhpParser\Node\Stmt\Continue_;
18+
use PhpParser\Node\Stmt\Do_;
1619
use PhpParser\Node\Stmt\Else_;
1720
use PhpParser\Node\Stmt\Expression;
1821
use PhpParser\Node\Stmt\Finally_;
22+
use PhpParser\Node\Stmt\For_;
1923
use PhpParser\Node\Stmt\Function_;
2024
use PhpParser\Node\Stmt\Goto_;
2125
use PhpParser\Node\Stmt\If_;
@@ -26,6 +30,7 @@
2630
use PhpParser\Node\Stmt\Return_;
2731
use PhpParser\Node\Stmt\Switch_;
2832
use PhpParser\Node\Stmt\TryCatch;
33+
use PhpParser\Node\Stmt\While_;
2934
use PhpParser\NodeVisitor;
3035
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
3136
use Rector\PhpParser\Node\FileNode;
@@ -65,6 +70,11 @@ public function isAlwaysTerminated(Node $stmtsAware, Stmt $node, Stmt $currentSt
6570
return false;
6671
}
6772

73+
// an infinite loop with no break never falls through to the next stmt
74+
if ($node instanceof While_ || $node instanceof Do_ || $node instanceof For_) {
75+
return $this->isTerminatedInfiniteLoop($node);
76+
}
77+
6878
if (! in_array($node::class, self::TERMINABLE_NODES_BY_ITS_STMTS, true)) {
6979
return $this->isTerminatedNode($node, $currentStmt);
7080
}
@@ -81,6 +91,68 @@ public function isAlwaysTerminated(Node $stmtsAware, Stmt $node, Stmt $currentSt
8191
return $this->isTerminatedInLastStmtsSwitch($node);
8292
}
8393

94+
private function isTerminatedInfiniteLoop(While_|Do_|For_ $loop): bool
95+
{
96+
if (! $this->isInfiniteLoopCondition($loop)) {
97+
return false;
98+
}
99+
100+
// a break/goto escaping the loop makes the following stmt reachable again;
101+
// stay conservative and treat any break/goto in the body as escaping
102+
return ! $this->hasBreakOrGoto($loop->stmts);
103+
}
104+
105+
private function isInfiniteLoopCondition(While_|Do_|For_ $loop): bool
106+
{
107+
if ($loop instanceof For_) {
108+
// "for (;;)" has no condition and loops forever
109+
if ($loop->cond === []) {
110+
return true;
111+
}
112+
113+
$lastCond = end($loop->cond);
114+
return $lastCond instanceof Expr && $this->isAlwaysTrue($lastCond);
115+
}
116+
117+
return $this->isAlwaysTrue($loop->cond);
118+
}
119+
120+
private function isAlwaysTrue(Expr $expr): bool
121+
{
122+
if ($expr instanceof ConstFetch) {
123+
return $expr->name->toLowerString() === 'true';
124+
}
125+
126+
return $expr instanceof Int_ && $expr->value !== 0;
127+
}
128+
129+
/**
130+
* @param Stmt[] $stmts
131+
*/
132+
private function hasBreakOrGoto(array $stmts): bool
133+
{
134+
$hasBreakOrGoto = false;
135+
136+
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
137+
$stmts,
138+
static function (Node $node) use (&$hasBreakOrGoto): ?int {
139+
// nested scopes bring their own jump targets
140+
if ($node instanceof FunctionLike || $node instanceof ClassLike) {
141+
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
142+
}
143+
144+
if (! $node instanceof Break_ && ! $node instanceof Goto_) {
145+
return null;
146+
}
147+
148+
$hasBreakOrGoto = true;
149+
return NodeVisitor::STOP_TRAVERSAL;
150+
}
151+
);
152+
153+
return $hasBreakOrGoto;
154+
}
155+
84156
private function isTerminatedNode(Stmt $previousNode, Stmt $currentStmt): bool
85157
{
86158
if (in_array($previousNode::class, self::TERMINABLE_NODES, true)) {

0 commit comments

Comments
 (0)