rector/packages/NodeNestingScope/ParentFinder.php
Tomas Votruba 20dc105d45 Updated Rector to commit 06994b8ff38f65e68a93633d4430ce16b47f506b
06994b8ff3 [Core] Avoid hang on file has template-extends (#1034)
2021-10-22 03:28:32 +00:00

36 lines
1.1 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\NodeNestingScope;
use PhpParser\Node;
use Rector\NodeTypeResolver\Node\AttributeKey;
use RectorPrefix20211022\Webmozart\Assert\Assert;
final class ParentFinder
{
/**
* @template T of \PhpParser\Node
* @param array<class-string<T>> $types
* @return T|null
*/
public function findByTypes(\PhpParser\Node $node, array $types) : ?\PhpParser\Node
{
\RectorPrefix20211022\Webmozart\Assert\Assert::allIsAOf($types, \PhpParser\Node::class);
$parent = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
if (!$parent instanceof \PhpParser\Node) {
return null;
}
do {
foreach ($types as $type) {
if (\is_a($parent, $type, \true)) {
return $parent;
}
}
if ($parent === null) {
return null;
}
} while ($parent = $parent->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE));
return null;
}
}