Skip to content

Commit

Permalink
[Core] Make consistent BetterNodeFinder findParent and findParentByTy…
Browse files Browse the repository at this point in the history
…pes (#3258)

* [Core] Make consistent BetterNodeFinder findParent and findParentByTypes

* [Core] Make consistent BetterNodeFinder findParent and findParentByTypes

* while will stop when not a Node

* using faster instanceof

* update with MultiInstanceofChecker

* Revert update with MultiInstanceofChecker

This reverts commit 3d4ff4a.
  • Loading branch information
samsonasik committed Dec 31, 2022
1 parent eadd540 commit e61c177
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,20 @@ public function __construct(
* @param array<class-string<TNode>> $types
* @return TNode|null
*/
public function findParentByTypes(Node $currentNode, array $types): ?Node
public function findParentByTypes(Node $node, array $types): ?Node
{
Assert::allIsAOf($types, Node::class);

while ($currentNode = $currentNode->getAttribute(AttributeKey::PARENT_NODE)) {
/** @var Node|null $currentNode */
if (! $currentNode instanceof Node) {
return null;
}
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);

while ($parentNode instanceof Node) {
foreach ($types as $type) {
if (is_a($currentNode, $type, true)) {
return $currentNode;
if ($parentNode instanceof $type) {
return $parentNode;
}
}

$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
}

return null;
Expand All @@ -78,17 +77,14 @@ public function findParentType(Node $node, string $type): ?Node
Assert::isAOf($type, Node::class);

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Node) {
return null;
}

do {
if (is_a($parentNode, $type, true)) {
while ($parentNode instanceof Node) {
if ($parentNode instanceof $type) {
return $parentNode;
}

$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
} while ($parentNode instanceof Node);
}

return null;
}
Expand Down

0 comments on commit e61c177

Please sign in to comment.