Skip to content

Commit

Permalink
[Parser] Handle just removed stmt on BetterNodeFinder::resolveNodeFro…
Browse files Browse the repository at this point in the history
…mFile() as fall for get next stmt (#3897)

* [Parser] Handle just removed stmt on BetterNodeFinder::resolveNodeFromFile() as fall for get next stmt

* clean up
  • Loading branch information
samsonasik committed May 19, 2023
1 parent 5b990ff commit 11b9411
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,28 @@ private function resolveNodeFromFile(array $newStmts, Node $node, bool $isPrevio
$currentStmtKey = $node->getAttribute(AttributeKey::STMT_KEY);
$stmtKey = $isPrevious ? $currentStmtKey - 1 : $currentStmtKey + 1;

return $newStmts[$stmtKey] ?? null;
if ($isPrevious) {
return $newStmts[$stmtKey] ?? null;
}

if (! isset($newStmts[$currentStmtKey - 1])) {
return $newStmts[$stmtKey] ?? null;
}

$startTokenPos = $node->getStartTokenPos();
if ($newStmts[$currentStmtKey - 1]->getStartTokenPos() !== $startTokenPos) {
return $newStmts[$stmtKey] ?? null;
}

if (! isset($newStmts[$currentStmtKey])) {
return null;
}

if ($newStmts[$currentStmtKey]->getStartTokenPos() === $startTokenPos) {
return null;
}

return $newStmts[$currentStmtKey];
}

/**
Expand Down

0 comments on commit 11b9411

Please sign in to comment.