Skip to content

Commit

Permalink
[Core] Make BetterNodeFinder::findFirstPrevious() consistent with fin…
Browse files Browse the repository at this point in the history
…dFirstNext() (#2215)

* [Core] Make BetterNodeFinder::findFirstPrevious() consistent with findFirstNext()

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* clean up

* clean up

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed May 2, 2022
1 parent 6bb8973 commit 4c3bf6f
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,36 +272,32 @@ public function findPreviousAssignToExpr(Expr $expr): ?Node
/**
* @param callable(Node $node): bool $filter
*/
public function findFirstPrevious(
Node $node,
callable $filter,
bool $lookupParent = true,
bool $stopOnFunctionLike = true
): ?Node {
public function findFirstPrevious(Node $node, callable $filter, bool $lookupParent = true): ?Node
{
// move to previous Node
$previousStatement = $node->getAttribute(AttributeKey::PREVIOUS_NODE);
if ($previousStatement instanceof Node) {
$foundNode = $this->findFirst([$previousStatement], $filter);
$previousNode = $node->getAttribute(AttributeKey::PREVIOUS_NODE);
if ($previousNode instanceof Node) {
$foundNode = $this->findFirst($previousNode, $filter);

// we found what we need
if ($foundNode instanceof Node) {
return $foundNode;
}

return $this->findFirstPrevious($previousStatement, $filter);
return $this->findFirstPrevious($previousNode, $filter, $lookupParent);
}

if (! $lookupParent) {
return null;
}

$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($stopOnFunctionLike && $parent instanceof FunctionLike) {
if ($parent instanceof FunctionLike) {
return null;
}

if ($parent instanceof Node) {
return $this->findFirstPrevious($parent, $filter);
return $this->findFirstPrevious($parent, $filter, $lookupParent);
}

return null;
Expand Down

0 comments on commit 4c3bf6f

Please sign in to comment.