rector/packages/NodeNestingScope/ParentFinder.php
Tomas Votruba 1257466608 Updated Rector to commit 00e0f49b9d0a07bf903b9c1b2d1be5a92608c1d5
00e0f49b9d [Php80] Skip Ternary indirect usage with null else on NullsafeOperatorRector (#891)
2021-09-16 10:24:17 +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 RectorPrefix20210916\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
{
\RectorPrefix20210916\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;
}
}