Skip to content

Commit

Permalink
[Parser] Allow resolve next/prev stmt inside ClassLike or Declare_ on…
Browse files Browse the repository at this point in the history
… BetterNodeFinder (#3905)

* [Parser] Allow resolve next/prev stmt inside ClassLike or Declare_

* add var

* Fix
  • Loading branch information
samsonasik committed May 20, 2023
1 parent 974a1be commit 20fce3b
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\For_;
Expand Down Expand Up @@ -534,8 +535,16 @@ public function resolveCurrentStatement(Node $node): ?Stmt
return null;
}

private function resolveNeighborNextStmt(StmtsAwareInterface $stmtsAware, Stmt $stmt, int $key): ?Node
private function isAllowedParentNode(?Node $node): bool
{
return $node instanceof StmtsAwareInterface || $node instanceof ClassLike || $node instanceof Declare_;
}

private function resolveNeighborNextStmt(
StmtsAwareInterface|ClassLike|Declare_ $stmtsAware,
Stmt $stmt,
int $key
): ?Node {
if (! isset($stmtsAware->stmts[$key - 1])) {
return $stmtsAware->stmts[$key + 1] ?? null;
}
Expand Down Expand Up @@ -567,11 +576,12 @@ private function findFirstInlinedNext(Node $node, callable $filter, array $newSt
if (! $parentNode instanceof Node) {
$nextNode = $this->resolveNextNodeFromFile($newStmts, $node);
} elseif ($node instanceof Stmt) {
if (! $parentNode instanceof StmtsAwareInterface) {
if (! $this->isAllowedParentNode($parentNode)) {
return null;
}

$currentStmtKey = $node->getAttribute(AttributeKey::STMT_KEY);
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
$nextNode = $this->resolveNeighborNextStmt($parentNode, $node, $currentStmtKey);
} else {
$nextNode = $this->resolveNextNodeFromOtherNode($node);
Expand Down Expand Up @@ -610,8 +620,10 @@ private function resolveNewStmts(?Node $parentNode): array
/**
* @param callable(Node $node): bool $filter
*/
private function findFirstInTopLevelStmtsAware(StmtsAwareInterface $stmtsAware, callable $filter): ?Node
{
private function findFirstInTopLevelStmtsAware(
StmtsAwareInterface|ClassLike|Declare_ $stmtsAware,
callable $filter
): ?Node {
$nodes = [];

if ($stmtsAware instanceof Foreach_) {
Expand Down Expand Up @@ -721,18 +733,18 @@ private function resolvePreviousNodeFromOtherNode(Node $node): ?Node
return null;
}

$nodes = $this->find(
$currentStmt,
static fn (Node $subNode): bool => $subNode->getEndTokenPos() < $startTokenPos
);
$nodes = $node instanceof Stmt
? []
: $this->find($currentStmt, static fn (Node $subNode): bool => $subNode->getEndTokenPos() < $startTokenPos);

if ($nodes === []) {
$parentNode = $currentStmt->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof StmtsAwareInterface) {
if (! $this->isAllowedParentNode($parentNode)) {
return null;
}

$currentStmtKey = $currentStmt->getAttribute(AttributeKey::STMT_KEY);
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
return $parentNode->stmts[$currentStmtKey - 1] ?? null;
}

Expand All @@ -757,18 +769,18 @@ private function resolveNextNodeFromOtherNode(Node $node): ?Node
return null;
}

$nextNode = $this->findFirst(
$currentStmt,
static fn (Node $subNode): bool => $subNode->getStartTokenPos() > $endTokenPos
);
$nextNode = $node instanceof Stmt
? null
: $this->findFirst($currentStmt, static fn (Node $subNode): bool => $subNode->getStartTokenPos() > $endTokenPos);

if (! $nextNode instanceof Node) {
$parentNode = $currentStmt->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof StmtsAwareInterface) {
if (! $this->isAllowedParentNode($parentNode)) {
return null;
}

$currentStmtKey = $currentStmt->getAttribute(AttributeKey::STMT_KEY);
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
return $this->resolveNeighborNextStmt($parentNode, $currentStmt, $currentStmtKey);
}

Expand All @@ -786,11 +798,12 @@ private function findFirstInlinedPrevious(Node $node, callable $filter, array $n
if (! $parentNode instanceof Node) {
$previousNode = $this->resolvePreviousNodeFromFile($newStmts, $node);
} elseif ($node instanceof Stmt) {
if (! $parentNode instanceof StmtsAwareInterface) {
if (! $this->isAllowedParentNode($parentNode)) {
return null;
}

$currentStmtKey = $node->getAttribute(AttributeKey::STMT_KEY);
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
if (! isset($parentNode->stmts[$currentStmtKey - 1])) {
return $this->findFirstInTopLevelStmtsAware($parentNode, $filter);
}
Expand Down

0 comments on commit 20fce3b

Please sign in to comment.