rector/packages/NodeNestingScope/FlowOfControlLocator.php

63 lines
1.6 KiB
PHP
Raw Normal View History

2019-10-13 07:59:52 +02:00
<?php
declare(strict_types=1);
2019-05-07 14:33:26 +02:00
namespace Rector\NodeNestingScope;
2019-05-07 14:33:26 +02:00
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
2019-05-07 14:33:26 +02:00
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class FlowOfControlLocator
{
public function resolveNestingHashFromFunctionLike(FunctionLike $functionLike, Node $checkedNode): string
{
$nestingHash = spl_object_hash($functionLike) . '__';
2019-05-07 14:33:26 +02:00
$currentNode = $checkedNode;
$previous = $currentNode;
while ($currentNode = $currentNode->getAttribute(AttributeKey::PARENT_NODE)) {
if ($currentNode instanceof Expression) {
2019-05-07 14:33:26 +02:00
continue;
}
if (! $currentNode instanceof Node) {
continue;
}
if ($functionLike === $currentNode) {
// to high
break;
2019-05-07 14:33:26 +02:00
}
$nestingHash .= $this->resolveBinaryOpNestingHash($currentNode, $previous);
$nestingHash .= spl_object_hash($currentNode);
$previous = $currentNode;
2019-05-07 14:33:26 +02:00
}
return $nestingHash;
}
private function resolveBinaryOpNestingHash(Node $currentNode, Node $previous): string
{
if (! $currentNode instanceof BinaryOp) {
return '';
}
// left && right have differnt nesting
if ($currentNode->left === $previous) {
return 'binary_left__';
}
if ($currentNode->right === $previous) {
return 'binary_right__';
}
return '';
}
2019-05-07 14:33:26 +02:00
}