Skip to content

Commit cb298f3

Browse files
committed
reuse duplicated process unreachable statement logic
1 parent ebc0855 commit cb298f3

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -309,29 +309,37 @@ public function processNodes(
309309

310310
$alreadyTerminated = true;
311311
$nextStmts = $this->getNextUnreachableStatements(array_slice($nodes, $i + 1), true);
312+
$this->processUnreachableStatement($nextStmts, $scope, $nodeCallback);
313+
}
314+
}
312315

313-
if ($nextStmts === []) {
314-
continue;
315-
}
316-
317-
$unreachableStatement = null;
318-
$nextStatements = [];
319-
320-
foreach ($nextStmts as $key => $nextStmt) {
321-
if ($key === 0) {
322-
$unreachableStatement = $nextStmt;
323-
continue;
324-
}
316+
/**
317+
* @param Node\Stmt[] $nextStmts
318+
* @param callable(Node $node, Scope $scope): void $nodeCallback
319+
*/
320+
private function processUnreachableStatement(array $nextStmts, MutatingScope $scope, callable $nodeCallback): void
321+
{
322+
if ($nextStmts === []) {
323+
return;
324+
}
325325

326-
$nextStatements[] = $nextStmt;
327-
}
326+
$unreachableStatement = null;
327+
$nextStatements = [];
328328

329-
if (!$unreachableStatement instanceof Node\Stmt) {
329+
foreach ($nextStmts as $key => $nextStmt) {
330+
if ($key === 0) {
331+
$unreachableStatement = $nextStmt;
330332
continue;
331333
}
332334

333-
$nodeCallback(new UnreachableStatementNode($unreachableStatement, $nextStatements), $scope);
335+
$nextStatements[] = $nextStmt;
336+
}
337+
338+
if (!$unreachableStatement instanceof Node\Stmt) {
339+
return;
334340
}
341+
342+
$nodeCallback(new UnreachableStatementNode($unreachableStatement, $nextStatements), $scope);
335343
}
336344

337345
/**
@@ -418,28 +426,7 @@ public function processStmtNodes(
418426

419427
$alreadyTerminated = true;
420428
$nextStmts = $this->getNextUnreachableStatements(array_slice($stmts, $i + 1), $parentNode instanceof Node\Stmt\Namespace_);
421-
422-
if ($nextStmts === []) {
423-
continue;
424-
}
425-
426-
$unreachableStatement = null;
427-
$nextStatements = [];
428-
429-
foreach ($nextStmts as $key => $nextStmt) {
430-
if ($key === 0) {
431-
$unreachableStatement = $nextStmt;
432-
continue;
433-
}
434-
435-
$nextStatements[] = $nextStmt;
436-
}
437-
438-
if (!$unreachableStatement instanceof Node\Stmt) {
439-
continue;
440-
}
441-
442-
$nodeCallback(new UnreachableStatementNode($unreachableStatement, $nextStatements), $scope);
429+
$this->processUnreachableStatement($nextStmts, $scope, $nodeCallback);
443430
}
444431

445432
$statementResult = new StatementResult($scope, $hasYield, $alreadyTerminated, $exitPoints, $throwPoints, $impurePoints);

0 commit comments

Comments
 (0)