rector/packages/NodeNestingScope/ParentFinder.php
Tomas Votruba c94867257b Updated Rector to commit b8a6c976b4a477fad3dd6fa7e8564cf82a2aa2b3
b8a6c976b4 [TypeDeclaration] Handle UnitEnum not exists check on ParamTypeByParentCallTypeRector (#841)
2021-09-07 06:01:30 +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 RectorPrefix20210907\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
{
\RectorPrefix20210907\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;
}
}