Skip to content

Commit

Permalink
[Core] Add BetterNodeFinder::findFirstInlinedPrevious() (#2238)
Browse files Browse the repository at this point in the history
* [Core] Add BetterNodeFinder::findFirstInlinedPrevious()

* phpstan

* add comment

* add comment
  • Loading branch information
samsonasik committed May 5, 2022
1 parent aa6524e commit 7f5acb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
5 changes: 2 additions & 3 deletions rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,12 @@ private function processReturn(Match_ $match): ?Return_

private function changeToAssign(Switch_ $switch, Match_ $match, Expr $assignExpr): Assign
{
$prevInitializedAssign = $this->betterNodeFinder->findFirstPrevious(
$prevInitializedAssign = $this->betterNodeFinder->findFirstInlinedPrevious(
$switch,
fn (Node $node): bool => $node instanceof Assign && $this->nodeComparator->areNodesEqual(
$node->var,
$assignExpr
),
false
)
);

$assign = new Assign($assignExpr, $match);
Expand Down
38 changes: 26 additions & 12 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,25 +270,39 @@ public function findPreviousAssignToExpr(Expr $expr): ?Node
}

/**
* Only search in previous Node/Stmt
*
* @param callable(Node $node): bool $filter
*/
public function findFirstPrevious(Node $node, callable $filter, bool $lookupParent = true): ?Node
public function findFirstInlinedPrevious(Node $node, callable $filter): ?Node
{
// move to previous Node
$previousNode = $node->getAttribute(AttributeKey::PREVIOUS_NODE);
if ($previousNode instanceof Node) {
$foundNode = $this->findFirst($previousNode, $filter);
if (! $previousNode instanceof Node) {
return null;
}

// we found what we need
if ($foundNode instanceof Node) {
return $foundNode;
}
$foundNode = $this->findFirst($previousNode, $filter);

return $this->findFirstPrevious($previousNode, $filter, $lookupParent);
// we found what we need
if ($foundNode instanceof Node) {
return $foundNode;
}

if (! $lookupParent) {
return null;
return $this->findFirstInlinedPrevious($previousNode, $filter);
}

/**
* Search in previous Node/Stmt, when no Node found, lookup previous Stmt of Parent Node
*
* @param callable(Node $node): bool $filter
*/
public function findFirstPrevious(Node $node, callable $filter): ?Node
{
$foundNode = $this->findFirstInlinedPrevious($node, $filter);

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

$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
Expand All @@ -297,7 +311,7 @@ public function findFirstPrevious(Node $node, callable $filter, bool $lookupPare
}

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

return null;
Expand Down

0 comments on commit 7f5acb6

Please sign in to comment.