66
77use PhpParser \Node ;
88use PhpParser \Node \Expr ;
9+ use PhpParser \Node \Expr \ConstFetch ;
910use PhpParser \Node \Expr \Exit_ ;
1011use PhpParser \Node \Expr \Throw_ ;
1112use PhpParser \Node \FunctionLike ;
13+ use PhpParser \Node \Scalar \Int_ ;
1214use PhpParser \Node \Stmt ;
1315use PhpParser \Node \Stmt \Break_ ;
1416use PhpParser \Node \Stmt \ClassLike ;
1517use PhpParser \Node \Stmt \Continue_ ;
18+ use PhpParser \Node \Stmt \Do_ ;
1619use PhpParser \Node \Stmt \Else_ ;
1720use PhpParser \Node \Stmt \Expression ;
1821use PhpParser \Node \Stmt \Finally_ ;
22+ use PhpParser \Node \Stmt \For_ ;
1923use PhpParser \Node \Stmt \Function_ ;
2024use PhpParser \Node \Stmt \Goto_ ;
2125use PhpParser \Node \Stmt \If_ ;
2630use PhpParser \Node \Stmt \Return_ ;
2731use PhpParser \Node \Stmt \Switch_ ;
2832use PhpParser \Node \Stmt \TryCatch ;
33+ use PhpParser \Node \Stmt \While_ ;
2934use PhpParser \NodeVisitor ;
3035use Rector \PhpDocParser \NodeTraverser \SimpleCallableNodeTraverser ;
3136use 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