Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 7 | final class DomDocXmlReader implements XmlReader |
||
| 8 | { |
||
| 9 | private \DOMXPath $domXpath; |
||
|
|
|||
| 10 | |||
| 11 | private function __construct(\DOMDocument $domDocument, array $namespaces = []) |
||
| 12 | { |
||
| 13 | $this->domXpath = new \DOMXPath($domDocument); |
||
| 14 | |||
| 15 | foreach ($namespaces as $prefix => $namespace) { |
||
| 16 | if (false === $this->domXpath->registerNamespace($prefix, $namespace)) { |
||
| 17 | throw new \RuntimeException("Error while trying to register namespace [$namespace] with prefix [$prefix]"); |
||
| 18 | } |
||
| 19 | } |
||
| 20 | } |
||
| 21 | |||
| 22 | public static function fromFile(string $filePath, array $namespaces = []): self |
||
| 23 | { |
||
| 24 | $domDocument = new \DOMDocument(); |
||
| 25 | |||
| 26 | try { |
||
| 27 | if (false === \file_exists($filePath) || false === $domDocument->load($filePath)) { |
||
| 28 | throw new \RuntimeException("Could not load xml file [$filePath]."); |
||
| 29 | } |
||
| 30 | } catch (\Exception $e) { |
||
| 31 | throw new \RuntimeException($e->getMessage()); |
||
| 32 | } |
||
| 33 | |||
| 34 | return new self($domDocument, $namespaces); |
||
| 35 | } |
||
| 36 | |||
| 37 | public static function fromString(string $xmlStr, array $namespaces = []): self |
||
| 38 | { |
||
| 39 | $domDocument = new \DOMDocument(); |
||
| 40 | |||
| 41 | try { |
||
| 42 | if (false === $domDocument->loadXML($xmlStr)) { |
||
| 43 | throw new \RuntimeException("Could not load XML from string [$xmlStr]"); |
||
| 44 | } |
||
| 45 | } catch (\Exception $e) { |
||
| 46 | throw new \RuntimeException($e->getMessage()); |
||
| 47 | } |
||
| 48 | |||
| 49 | return new self($domDocument, $namespaces); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function getList(string $xpath): \Generator |
||
| 53 | { |
||
| 54 | if (null === $nodeList = $this->getNodeList($xpath)) { |
||
| 55 | return []; |
||
| 56 | } |
||
| 57 | |||
| 58 | foreach ($nodeList as $node) { |
||
| 59 | yield $this->getNodeValue($node); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | public function getNodeList(string $xpath, \DOMNode $contextNode = null): \DOMNodeList |
||
| 64 | { |
||
| 65 | try { |
||
| 66 | $nodeList = $this->domXpath->query($xpath, $contextNode); |
||
| 67 | } catch (\Throwable $e) { |
||
| 68 | return new \DOMNodeList(); |
||
| 69 | } |
||
| 70 | |||
| 71 | if (false === $nodeList) { |
||
| 72 | return new \DOMNodeList(); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $nodeList; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getFirstNode(string $xpath, \DOMNode $contextNode = null): ?\DOMElement |
||
| 79 | { |
||
| 80 | $nodeList = $this->getNodeList($xpath, $contextNode); |
||
| 81 | |||
| 82 | if (0 !== $nodeList->length) { |
||
| 83 | return $nodeList->item(0); |
||
| 84 | } |
||
| 85 | |||
| 86 | return null; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function getLastNode(string $xpath, \DOMNode $contextNode = null): ?\DOMElement |
||
| 90 | { |
||
| 91 | $nodeList = $this->getNodeList($xpath, $contextNode); |
||
| 92 | |||
| 93 | if (0 !== $nodeList->length) { |
||
| 94 | $lastIndex = $nodeList->length - 1; |
||
| 95 | |||
| 96 | return $nodeList->item($lastIndex); |
||
| 97 | } |
||
| 98 | |||
| 99 | return null; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function getNodeAttribute(string $att, \DOMElement $node = null): ?string |
||
| 103 | { |
||
| 104 | if (null !== $node) { |
||
| 105 | return $node->getAttribute($att); |
||
| 106 | } |
||
| 107 | |||
| 108 | return null; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function getNodeValue(\DOMElement $node = null): ?string |
||
| 112 | { |
||
| 113 | if (null !== $node) { |
||
| 114 | return $node->nodeValue; |
||
| 115 | } |
||
| 116 | |||
| 117 | return null; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function getValue(string $xpath, \DOMNode $contextNode = null): ?string |
||
| 121 | { |
||
| 122 | return $this->getNodeValue($this->getFirstNode($xpath, $contextNode)); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function getLastValue(string $xpath, \DOMNode $contextNode = null): ?string |
||
| 126 | { |
||
| 127 | return $this->getNodeValue($this->getLastNode($xpath, $contextNode)); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getNeighborNodeValue(string $neighborNodeName, \DOMElement $node = null): ?string |
||
| 131 | { |
||
| 132 | if (null !== $node) { |
||
| 133 | return $this->getNodeValue( |
||
| 134 | $node->parentNode->getElementsByTagName($neighborNodeName)->item(0) |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | return null; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function getValues(\DOMNodeList $contextNodes, string $keyNodeName, array $valNodes = [], \Closure $fn = null, array $fnParams = []): array |
||
| 142 | { |
||
| 143 | $values = []; |
||
| 144 | |||
| 145 | foreach ($contextNodes as $node) { |
||
| 146 | $keyNodeValue = $this->getValue($keyNodeName, $node); |
||
| 147 | |||
| 148 | if (!\array_key_exists($keyNodeValue, $values)) { |
||
| 149 | $values[$keyNodeValue] = []; |
||
| 150 | } |
||
| 151 | |||
| 152 | foreach ($valNodes as $valNodeName) { |
||
| 153 | if (null !== $fn) { |
||
| 154 | $params = $fnParams; |
||
| 155 | \array_unshift($params, $this->getValue($valNodeName, $node)); |
||
| 156 | $values[$keyNodeValue] = $fn($params); |
||
| 157 | } else { |
||
| 158 | $values[$keyNodeValue] = $this->getValue($valNodeName, $node); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | \ksort($values); |
||
| 164 | |||
| 165 | return $values; |
||
| 166 | } |
||
| 167 | } |
||
| 168 |