Skip to content

Commit 03bbc04

Browse files
committed
reuse duplicated process unreachable statement logic
1 parent 54c68ac commit 03bbc04

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
@@ -317,29 +317,37 @@ public function processNodes(
317317

318318
$alreadyTerminated = true;
319319
$nextStmts = $this->getNextUnreachableStatements(array_slice($nodes, $i + 1), true);
320+
$this->processUnreachableStatement($nextStmts, $scope, $nodeCallback);
321+
}
322+
}
320323

321-
if ($nextStmts === []) {
322-
continue;
323-
}
324-
325-
$unreachableStatement = null;
326-
$nextStatements = [];
327-
328-
foreach ($nextStmts as $key => $nextStmt) {
329-
if ($key === 0) {
330-
$unreachableStatement = $nextStmt;
331-
continue;
332-
}
324+
/**
325+
* @param Node\Stmt[] $nextStmts
326+
* @param callable(Node $node, Scope $scope): void $nodeCallback
327+
*/
328+
private function processUnreachableStatement(array $nextStmts, MutatingScope $scope, callable $nodeCallback): void
329+
{
330+
if ($nextStmts === []) {
331+
return;
332+
}
333333

334-
$nextStatements[] = $nextStmt;
335-
}
334+
$unreachableStatement = null;
335+
$nextStatements = [];
336336

337-
if (!$unreachableStatement instanceof Node\Stmt) {
337+
foreach ($nextStmts as $key => $nextStmt) {
338+
if ($key === 0) {
339+
$unreachableStatement = $nextStmt;
338340
continue;
339341
}
340342

341-
$nodeCallback(new UnreachableStatementNode($unreachableStatement, $nextStatements), $scope);
343+
$nextStatements[] = $nextStmt;
344+
}
345+
346+
if (!$unreachableStatement instanceof Node\Stmt) {
347+
return;
342348
}
349+
350+
$nodeCallback(new UnreachableStatementNode($unreachableStatement, $nextStatements), $scope);
343351
}
344352

345353
/**
@@ -427,28 +435,7 @@ public function processStmtNodes(
427435

428436
$alreadyTerminated = true;
429437
$nextStmts = $this->getNextUnreachableStatements(array_slice($stmts, $i + 1), $parentNode instanceof Node\Stmt\Namespace_);
430-
431-
if ($nextStmts === []) {
432-
continue;
433-
}
434-
435-
$unreachableStatement = null;
436-
$nextStatements = [];
437-
438-
foreach ($nextStmts as $key => $nextStmt) {
439-
if ($key === 0) {
440-
$unreachableStatement = $nextStmt;
441-
continue;
442-
}
443-
444-
$nextStatements[] = $nextStmt;
445-
}
446-
447-
if (!$unreachableStatement instanceof Node\Stmt) {
448-
continue;
449-
}
450-
451-
$nodeCallback(new UnreachableStatementNode($unreachableStatement, $nextStatements), $scope);
438+
$this->processUnreachableStatement($nextStmts, $scope, $nodeCallback);
452439
}
453440

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

0 commit comments

Comments
 (0)