2020-04-13 23:44:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-14 10:49:58 +02:00
|
|
|
namespace Rector\NodeNestingScope;
|
2020-04-13 23:44:44 +02:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
2020-04-01 03:55:44 +02:00
|
|
|
use Rector\NodeNestingScope\ValueObject\ControlStructure;
|
2020-04-13 23:44:44 +02:00
|
|
|
|
|
|
|
final class ScopeNestingComparator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var BetterNodeFinder
|
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
|
|
|
|
|
|
|
public function __construct(BetterNodeFinder $betterNodeFinder)
|
|
|
|
{
|
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function areScopeNestingEqual(Node $firstNode, Node $secondNode): bool
|
|
|
|
{
|
|
|
|
$firstNodeScopeNode = $this->findParentControlStructure($firstNode);
|
|
|
|
$secondNodeScopeNode = $this->findParentControlStructure($secondNode);
|
|
|
|
|
|
|
|
return $firstNodeScopeNode === $secondNodeScopeNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findParentControlStructure(Node $node): ?Node
|
|
|
|
{
|
2020-05-09 21:50:52 +02:00
|
|
|
return $this->betterNodeFinder->findFirstParentInstanceOf($node, ControlStructure::BREAKING_SCOPE_NODE_TYPES);
|
2020-04-13 23:44:44 +02:00
|
|
|
}
|
|
|
|
}
|