Skip to content

Commit

Permalink
Continue analyzing classes and functions after exit
Browse files Browse the repository at this point in the history
  • Loading branch information
takaram committed Jul 11, 2024
1 parent 82673cd commit e64b6d0
Showing 3 changed files with 30 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
@@ -292,24 +292,28 @@ public function processNodes(
callable $nodeCallback,
): void
{
$alreadyTerminated = false;
foreach ($nodes as $i => $node) {
if (!$node instanceof Node\Stmt) {
if (
!$node instanceof Node\Stmt
|| ($alreadyTerminated && !($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassLike))
) {
continue;
}

$statementResult = $this->processStmtNode($node, $scope, $nodeCallback, StatementContext::createTopLevel());
$scope = $statementResult->getScope();
if (!$statementResult->isAlwaysTerminating()) {
if ($alreadyTerminated || !$statementResult->isAlwaysTerminating()) {
continue;
}

$alreadyTerminated = true;
$nextStmt = $this->getFirstUnreachableNode(array_slice($nodes, $i + 1), true);
if (!$nextStmt instanceof Node\Stmt) {
continue;
}

$nodeCallback(new UnreachableStatementNode($nextStmt), $scope);
break;
}
}

10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Debug/DumpTypeRuleTest.php
Original file line number Diff line number Diff line change
@@ -92,4 +92,14 @@ public function testBug11179(): void
]);
}

public function testBug11179NoNamespace(): void
{
$this->analyse([__DIR__ . '/data/bug-11179-no-namespace.php'], [
[
'Dumped type: string',
11,
],
]);
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Debug/data/bug-11179-no-namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

// no namespace

exit(0);

echo 1;

function bug11179Foo(string $p): string
{
\PHPStan\dumpType($p);
return "";
}

0 comments on commit e64b6d0

Please sign in to comment.