Repository navigation
Expand file tree
/
Copy pathNodeComparator.php
More file actions
101 lines (81 loc) · 2.66 KB
/
Copy pathNodeComparator.php
File metadata and controls
101 lines (81 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace Rector\PhpParser\Comparing;
use PhpParser\Node;
use Rector\Comments\CommentRemover;
use Rector\PhpParser\Printer\BetterStandardPrinter;
final readonly class NodeComparator
{
public function __construct(
private CommentRemover $commentRemover,
private BetterStandardPrinter $betterStandardPrinter
) {
}
/**
* Removes all comments from both nodes
* @param Node|Node[]|null $node
*/
public function printWithoutComments(Node | array | null $node): string
{
$node = $this->commentRemover->removeFromNode($node);
$content = $this->betterStandardPrinter->print($node);
return trim($content);
}
/**
* @param Node|Node[]|null $firstNode
* @param Node|Node[]|null $secondNode
*/
public function areNodesEqual(Node | array | null $firstNode, Node | array | null $secondNode): bool
{
if ($firstNode instanceof Node && ! $secondNode instanceof Node) {
return false;
}
if (! $firstNode instanceof Node && $secondNode instanceof Node) {
return false;
}
if (is_array($firstNode) && ! is_array($secondNode)) {
return false;
}
if (! is_array($secondNode)) {
return $this->printWithoutComments($firstNode) === $this->printWithoutComments($secondNode);
}
if (is_array($firstNode)) {
return $this->printWithoutComments($firstNode) === $this->printWithoutComments($secondNode);
}
return false;
}
/**
* @api
* @param Node[] $availableNodes
*/
public function isNodeEqual(Node $singleNode, array $availableNodes): bool
{
return array_any(
$availableNodes,
fn (Node $availableNode): bool => $this->areNodesEqual($singleNode, $availableNode)
);
}
/**
* Checks even clone nodes
*/
public function areSameNode(Node $firstNode, Node $secondNode): bool
{
if ($firstNode === $secondNode) {
return true;
}
$firstClass = $firstNode::class;
$secondClass = $secondNode::class;
if ($firstClass !== $secondClass) {
return false;
}
if ($firstNode->getStartTokenPos() !== $secondNode->getStartTokenPos()) {
return false;
}
if ($firstNode->getEndTokenPos() !== $secondNode->getEndTokenPos()) {
return false;
}
$printFirstNode = $this->betterStandardPrinter->print($firstNode);
$printSecondNode = $this->betterStandardPrinter->print($secondNode);
return $printFirstNode === $printSecondNode;
}
}