| Total Complexity | 46 |
| Total Lines | 117 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 | if ($node instanceof DOMElement && $otherNode instanceof DOMElement) |
||
| 35 | { |
||
| 36 | return static::isEqualElementNode($node, $otherNode); |
||
| 37 | } |
||
| 38 | if ($node instanceof DOMCharacterData && $otherNode instanceof DOMCharacterData) |
||
| 39 | { |
||
| 40 | // Covers DOMCdataSection, DOMComment, and DOMText |
||
| 41 | return $node->data === $otherNode->data; |
||
| 42 | } |
||
| 43 | if ($node instanceof DOMProcessingInstruction && $otherNode instanceof DOMProcessingInstruction) |
||
| 44 | { |
||
| 45 | return $node->target === $otherNode->target && $node->data === $otherNode->data; |
||
| 46 | } |
||
| 47 | if ($node instanceof DOMAttr && $otherNode instanceof DOMAttr) |
||
| 48 | { |
||
| 49 | return $node->namespaceURI === $otherNode->namespaceURI |
||
| 50 | && $node->localName === $otherNode->localName |
||
| 51 | && $node->value === $otherNode->value; |
||
| 52 | } |
||
| 53 | if (($node instanceof DOMDocument && $otherNode instanceof DOMDocument) |
||
| 54 | || ($node instanceof DOMDocumentFragment && $otherNode instanceof DOMDocumentFragment)) |
||
| 55 | { |
||
| 56 | return static::isEqualNodeList($node->childNodes, $otherNode->childNodes); |
||
| 57 | } |
||
| 58 | if ($node instanceof DOMDocumentType && $otherNode instanceof DOMDocumentType) |
||
| 59 | { |
||
| 60 | return $node->name === $otherNode->name |
||
| 61 | && $node->publicId === $otherNode->publicId |
||
| 62 | && $node->systemId === $otherNode->systemId; |
||
| 63 | } |
||
| 64 | if ($node instanceof DOMEntityReference && $otherNode instanceof DOMEntityReference) |
||
| 65 | { |
||
| 66 | return $node->nodeName === $otherNode->nodeName; |
||
| 67 | } |
||
| 68 | if (($node instanceof DOMEntity && $otherNode instanceof DOMEntity) |
||
| 69 | || ($node instanceof DOMNotation && $otherNode instanceof DOMNotation)) |
||
| 70 | { |
||
| 71 | return $node->nodeName === $otherNode->nodeName |
||
| 72 | && $node->publicId === $otherNode->publicId |
||
| 73 | && $node->systemId === $otherNode->systemId; |
||
| 74 | } |
||
| 75 | |||
| 76 | // @codeCoverageIgnoreStart |
||
| 77 | return $node->isSameNode($otherNode); |
||
| 78 | // @codeCoverageIgnoreEnd |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return array<string, string> |
||
| 83 | */ |
||
| 84 | protected static function getNamespaceDeclarations(DOMElement $element): array |
||
| 85 | { |
||
| 86 | $namespaces = []; |
||
| 87 | $xpath = new DOMXPath($element->ownerDocument); |
||
|
1 ignored issue
–
show
|
|||
| 88 | foreach ($xpath->query('namespace::*', $element) as $node) |
||
| 89 | { |
||
| 90 | if ($element->hasAttribute($node->nodeName)) |
||
| 91 | { |
||
| 92 | $namespaces[$node->nodeName] = $node->nodeValue; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | return $namespaces; |
||
| 97 | } |
||
| 98 | |||
| 99 | protected static function hasEqualNamespaceDeclarations(DOMElement $element, DOMElement $otherElement): bool |
||
| 100 | { |
||
| 101 | return static::getNamespaceDeclarations($element) == static::getNamespaceDeclarations($otherElement); |
||
| 102 | } |
||
| 103 | |||
| 104 | protected static function isEqualElementNode(DOMElement $element, DOMElement $otherElement): bool |
||
| 124 | } |
||
| 125 | |||
| 126 | protected static function isEqualNodeList(DOMNodeList $list, DOMNodeList $otherList): bool |
||
| 127 | { |
||
| 141 | } |
||
| 142 | } |