From 24f8610224663ef71ca2e4007ef89997303f53b9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 19 May 2023 01:51:10 +0700 Subject: [PATCH] [NodeTypeResolver] Remove next node on PHPStanNodeScopeResolver (#3888) --- .../Scope/PHPStanNodeScopeResolver.php | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 0fcf4ef13b2..e84670846ab 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -44,6 +44,7 @@ use PHPStan\Type\TypeCombinator; use Rector\Caching\Detector\ChangedFilesDetector; use Rector\Caching\FileSystem\DependencyResolver; +use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\NodeAnalyzer\ClassAnalyzer; use Rector\Core\PhpParser\Node\BetterNodeFinder; @@ -325,12 +326,22 @@ private function processUnreachableStatementNode( $this->processNodes([$originalStmt], $filePath, $mutatingScope); - $nextNode = $originalStmt->getAttribute(AttributeKey::NEXT_NODE); - while ($nextNode instanceof Stmt) { - $nextNode->setAttribute(AttributeKey::IS_UNREACHABLE, true); + $parentNode = $unreachableStatementNode->getAttribute(AttributeKey::PARENT_NODE); - $this->processNodes([$nextNode], $filePath, $mutatingScope); - $nextNode = $nextNode->getAttribute(AttributeKey::NEXT_NODE); + if (! $parentNode instanceof StmtsAwareInterface) { + return; + } + + $stmtKey = $unreachableStatementNode->getAttribute(AttributeKey::STMT_KEY); + $totalKeys = $parentNode->stmts === null ? 0 : count($parentNode->stmts); + + for ($key = $stmtKey + 1; $key < $totalKeys; ++$key) { + if (! isset($parentNode->stmts[$key])) { + continue; + } + + $parentNode->stmts[$key]->setAttribute(AttributeKey::IS_UNREACHABLE, true); + $this->processNodes([$parentNode->stmts[$key]], $filePath, $mutatingScope); } }