Total Complexity | 40 |
Total Lines | 151 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like NodeComparator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NodeComparator, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
24 | class NodeComparator |
||
25 | { |
||
26 | // https://dom.spec.whatwg.org/#concept-node-equals |
||
27 | // https://github.com/php/php-src/blob/master/ext/dom/node.c |
||
28 | public static function isEqualNode(?DOMNode $node, ?DOMNode $otherNode): bool |
||
29 | { |
||
30 | if (!isset($node, $otherNode) || $node->nodeType !== $otherNode->nodeType) |
||
31 | { |
||
32 | return false; |
||
33 | } |
||
34 | $classes = [ |
||
35 | 'DOMElement', |
||
36 | 'DOMCharacterData', |
||
37 | 'DOMProcessingInstruction', |
||
38 | 'DOMAttr', |
||
39 | 'DOMDocument', |
||
40 | 'DOMDocumentFragment', |
||
41 | 'DOMDocumentType', |
||
42 | 'DOMEntityReference', |
||
43 | 'DOMEntity', |
||
44 | 'DOMNotation' |
||
45 | ]; |
||
46 | foreach ($classes as $className) |
||
47 | { |
||
48 | if ($node instanceof $className && $otherNode instanceof $className) |
||
49 | { |
||
50 | $methodName = 'isEqual' . substr($className, 3); |
||
51 | |||
52 | return static::$methodName($node, $otherNode); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | // @codeCoverageIgnoreStart |
||
57 | return $node->isSameNode($otherNode); |
||
58 | // @codeCoverageIgnoreEnd |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return array<string, string> |
||
63 | */ |
||
64 | protected static function getNamespaceDeclarations(DOMElement $element): array |
||
65 | { |
||
66 | $namespaces = []; |
||
67 | $xpath = new DOMXPath($element->ownerDocument); |
||
68 | foreach ($xpath->query('namespace::*', $element) as $node) |
||
69 | { |
||
70 | if ($element->hasAttribute($node->nodeName)) |
||
71 | { |
||
72 | $namespaces[$node->nodeName] = $node->nodeValue; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | return $namespaces; |
||
77 | } |
||
78 | |||
79 | protected static function hasEqualNamespaceDeclarations(DOMElement $element, DOMElement $otherElement): bool |
||
80 | { |
||
81 | return static::getNamespaceDeclarations($element) == static::getNamespaceDeclarations($otherElement); |
||
82 | } |
||
83 | |||
84 | protected static function isEqualAttr(DOMAttr $node, DOMAttr $otherNode): bool |
||
85 | { |
||
86 | return $node->namespaceURI === $otherNode->namespaceURI |
||
87 | && $node->localName === $otherNode->localName |
||
88 | && $node->value === $otherNode->value; |
||
89 | } |
||
90 | |||
91 | protected static function isEqualCharacterData(DOMCharacterData $node, DOMCharacterData $otherNode): bool |
||
92 | { |
||
93 | // Covers DOMCdataSection, DOMComment, and DOMText |
||
94 | return $node->data === $otherNode->data; |
||
95 | } |
||
96 | |||
97 | protected static function isEqualDocument(DOMDocument $node, DOMDocument $otherNode): bool |
||
98 | { |
||
99 | return static::isEqualNodeList($node->childNodes, $otherNode->childNodes); |
||
100 | } |
||
101 | |||
102 | protected static function isEqualDocumentFragment(DOMDocumentFragment $node, DOMDocumentFragment $otherNode): bool |
||
103 | { |
||
104 | return static::isEqualNodeList($node->childNodes, $otherNode->childNodes); |
||
105 | } |
||
106 | |||
107 | protected static function isEqualDocumentType(DOMDocumentType $node, DOMDocumentType $otherNode): bool |
||
108 | { |
||
109 | return $node->name === $otherNode->name |
||
110 | && $node->publicId === $otherNode->publicId |
||
111 | && $node->systemId === $otherNode->systemId; |
||
112 | } |
||
113 | |||
114 | protected static function isEqualElement(DOMElement $element, DOMElement $otherElement): bool |
||
134 | } |
||
135 | |||
136 | protected static function isEqualEntity(DOMEntity $node, DOMEntity $otherNode): bool |
||
137 | { |
||
138 | return $node->nodeName === $otherNode->nodeName |
||
139 | && $node->publicId === $otherNode->publicId |
||
140 | && $node->systemId === $otherNode->systemId; |
||
141 | } |
||
142 | |||
143 | protected static function isEqualEntityReference(DOMEntityReference $node, DOMEntityReference $otherNode): bool |
||
144 | { |
||
145 | return $node->nodeName === $otherNode->nodeName; |
||
146 | } |
||
147 | |||
148 | protected static function isEqualNodeList(DOMNodeList $list, DOMNodeList $otherList): bool |
||
149 | { |
||
150 | if ($list->length !== $otherList->length) |
||
151 | { |
||
152 | return false; |
||
153 | } |
||
154 | foreach ($list as $i => $node) |
||
155 | { |
||
156 | if (!static::isEqualNode($node, $otherList->item($i))) |
||
157 | { |
||
158 | return false; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return true; |
||
163 | } |
||
164 | |||
165 | protected static function isEqualNotation(DOMNotation $node, DOMNotation $otherNode): bool |
||
170 | } |
||
171 | |||
172 | protected static function isEqualProcessingInstruction(DOMProcessingInstruction $node, DOMProcessingInstruction $otherNode): bool |
||
173 | { |
||
174 | return $node->target === $otherNode->target && $node->data === $otherNode->data; |
||
175 | } |
||
176 | } |