diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 7c5056cb2d..0869b35b8b 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -6226,7 +6226,12 @@ private function getPhpDocReturnType(ResolvedPhpDocBlock $resolvedPhpDoc, Type $ private function getNextUnreachableStatements(array $nodes, bool $earlyBinding): array { $stmts = []; + $isPassedUnreachableStatement = false; foreach ($nodes as $node) { + if ($isPassedUnreachableStatement) { + $stmts[] = $node; + continue; + } if ($node instanceof Node\Stmt\Nop) { continue; } @@ -6237,6 +6242,7 @@ private function getNextUnreachableStatements(array $nodes, bool $earlyBinding): continue; } $stmts[] = $node; + $isPassedUnreachableStatement = true; } return $stmts; } diff --git a/tests/PHPStan/Rules/DeadCode/UnreachableStatementNextStatementsRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnreachableStatementNextStatementsRuleTest.php index 7024b120a6..8b78a4848b 100644 --- a/tests/PHPStan/Rules/DeadCode/UnreachableStatementNextStatementsRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnreachableStatementNextStatementsRuleTest.php @@ -37,7 +37,7 @@ public function processNode(Node $node, Scope $scope): array $totalNextStatements = count($node->getNextStatements()); return [ - RuleErrorBuilder::message(sprintf('It has %d over first unreachable statements', $totalNextStatements)) + RuleErrorBuilder::message(sprintf('It has %d stmts over first unreachable statements', $totalNextStatements)) ->identifier('tests.total.next.unreachable.statement') ->build(), ]; @@ -50,7 +50,7 @@ public function testRule(): void { $this->analyse([__DIR__ . '/data/multiple_unreachable.php'], [ [ - 'It has 2 over first unreachable statements', + 'It has 3 stmts over first unreachable statements', 14, ], ]); diff --git a/tests/PHPStan/Rules/DeadCode/data/multiple_unreachable.php b/tests/PHPStan/Rules/DeadCode/data/multiple_unreachable.php index 383d693dd7..0e9ab15119 100644 --- a/tests/PHPStan/Rules/DeadCode/data/multiple_unreachable.php +++ b/tests/PHPStan/Rules/DeadCode/data/multiple_unreachable.php @@ -13,5 +13,11 @@ function foo($foo) echo 'statement 1'; echo 'statement 2'; - echo 'statement 3'; + + function innerFunction() + { + echo 'statement 3'; + } + + echo innerFunction(); } \ No newline at end of file