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:
Complex classes like SimpleXmlDom 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SimpleXmlDom, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class SimpleXmlDom extends AbstractSimpleXmlDom implements \IteratorAggregate, SimpleXmlDomInterface |
||
15 | { |
||
16 | /** |
||
17 | * @param \DOMElement|\DOMNode $node |
||
18 | */ |
||
19 | 1 | public function __construct(\DOMNode $node) |
|
23 | |||
24 | /** |
||
25 | * @param string $name |
||
26 | * @param array $arguments |
||
27 | * |
||
28 | * @throws \BadMethodCallException |
||
29 | * |
||
30 | * @return SimpleXmlDomInterface|string|null |
||
31 | */ |
||
32 | public function __call($name, $arguments) |
||
42 | |||
43 | /** |
||
44 | * Find list of nodes with a CSS selector. |
||
45 | * |
||
46 | * @param string $selector |
||
47 | * @param int|null $idx |
||
48 | * |
||
49 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
|
|||
50 | */ |
||
51 | public function find(string $selector, $idx = null) |
||
55 | |||
56 | /** |
||
57 | * Returns an array of attributes. |
||
58 | * |
||
59 | * @return string[]|null |
||
60 | */ |
||
61 | View Code Duplication | public function getAllAttributes() |
|
78 | |||
79 | /** |
||
80 | * @return bool |
||
81 | */ |
||
82 | public function hasAttributes(): bool |
||
86 | |||
87 | /** |
||
88 | * Return attribute value. |
||
89 | * |
||
90 | * @param string $name |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getAttribute(string $name): string |
||
104 | |||
105 | /** |
||
106 | * Determine if an attribute exists on the element. |
||
107 | * |
||
108 | * @param string $name |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function hasAttribute(string $name): bool |
||
120 | |||
121 | /** |
||
122 | * Get dom node's inner html. |
||
123 | * |
||
124 | * @param bool $multiDecodeNewHtmlEntity |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
132 | |||
133 | /** |
||
134 | * Remove attribute. |
||
135 | * |
||
136 | * @param string $name <p>The name of the html-attribute.</p> |
||
137 | * |
||
138 | * @return SimpleXmlDomInterface |
||
139 | */ |
||
140 | public function removeAttribute(string $name): SimpleXmlDomInterface |
||
148 | |||
149 | /** |
||
150 | * Replace child node. |
||
151 | * |
||
152 | * @param string $string |
||
153 | * |
||
154 | * @return SimpleXmlDomInterface |
||
155 | */ |
||
156 | protected function replaceChildWithString(string $string): SimpleXmlDomInterface |
||
157 | { |
||
158 | if (!empty($string)) { |
||
159 | $newDocument = new XmlDomParser($string); |
||
160 | |||
161 | $tmpDomString = $this->normalizeStringForComparision($newDocument); |
||
162 | $tmpStr = $this->normalizeStringForComparision($string); |
||
163 | if ($tmpDomString !== $tmpStr) { |
||
164 | throw new \RuntimeException( |
||
165 | 'Not valid HTML fragment!' . "\n" . |
||
166 | $tmpDomString . "\n" . |
||
167 | $tmpStr |
||
168 | ); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** @var \DOMNode[] $remove_nodes */ |
||
173 | $remove_nodes = []; |
||
174 | if ($this->node->childNodes->length > 0) { |
||
175 | // INFO: We need to fetch the nodes first, before we can delete them, because of missing references in the dom, |
||
176 | // if we delete the elements on the fly. |
||
177 | foreach ($this->node->childNodes as $node) { |
||
178 | $remove_nodes[] = $node; |
||
179 | } |
||
180 | } |
||
181 | foreach ($remove_nodes as $remove_node) { |
||
182 | $this->node->removeChild($remove_node); |
||
183 | } |
||
184 | |||
185 | if (!empty($newDocument)) { |
||
186 | $ownerDocument = $this->node->ownerDocument; |
||
187 | if ( |
||
188 | $ownerDocument |
||
189 | && |
||
190 | $newDocument->getDocument()->documentElement |
||
191 | ) { |
||
192 | $newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
||
193 | /** @noinspection UnusedFunctionResultInspection */ |
||
194 | $this->node->appendChild($newNode); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | return $this; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Replace this node. |
||
203 | * |
||
204 | * @param string $string |
||
205 | * |
||
206 | * @return SimpleXmlDomInterface |
||
207 | */ |
||
208 | protected function replaceNodeWithString(string $string): SimpleXmlDomInterface |
||
244 | |||
245 | /** |
||
246 | * Replace this node with text |
||
247 | * |
||
248 | * @param string $string |
||
249 | * |
||
250 | * @return SimpleXmlDomInterface |
||
251 | */ |
||
252 | View Code Duplication | protected function replaceTextWithString($string): SimpleXmlDomInterface |
|
270 | |||
271 | /** |
||
272 | * Set attribute value. |
||
273 | * |
||
274 | * @param string $name <p>The name of the html-attribute.</p> |
||
275 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
276 | * @param bool $strict </p> |
||
277 | * $value must be NULL, to remove the attribute, |
||
278 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
279 | * </p> |
||
280 | * |
||
281 | * @return SimpleXmlDomInterface |
||
282 | */ |
||
283 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleXmlDomInterface |
|
299 | |||
300 | /** |
||
301 | * Get dom node's plain text. |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | public function text(): string |
||
309 | |||
310 | /** |
||
311 | * Get dom node's outer html. |
||
312 | * |
||
313 | * @param bool $multiDecodeNewHtmlEntity |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | public function xml(bool $multiDecodeNewHtmlEntity = false): string |
||
321 | |||
322 | /** |
||
323 | * Change the name of a tag in a "DOMNode". |
||
324 | * |
||
325 | * @param \DOMNode $node |
||
326 | * @param string $name |
||
327 | * |
||
328 | * @return \DOMElement|false |
||
329 | * <p>DOMElement a new instance of class DOMElement or false |
||
330 | * if an error occured.</p> |
||
331 | */ |
||
332 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name) |
|
358 | |||
359 | /** |
||
360 | * Returns children of node. |
||
361 | * |
||
362 | * @param int $idx |
||
363 | * |
||
364 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|null |
||
365 | */ |
||
366 | View Code Duplication | public function childNodes(int $idx = -1) |
|
376 | |||
377 | /** |
||
378 | * Find nodes with a CSS selector. |
||
379 | * |
||
380 | * @param string $selector |
||
381 | * |
||
382 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
383 | */ |
||
384 | public function findMulti(string $selector): SimpleXmlDomNodeInterface |
||
388 | |||
389 | /** |
||
390 | * Find nodes with a CSS selector. |
||
391 | * |
||
392 | * @param string $selector |
||
393 | * |
||
394 | * @return false|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
395 | */ |
||
396 | public function findMultiOrFalse(string $selector) |
||
400 | |||
401 | /** |
||
402 | * Find one node with a CSS selector. |
||
403 | * |
||
404 | * @param string $selector |
||
405 | * |
||
406 | * @return SimpleXmlDomInterface |
||
407 | */ |
||
408 | public function findOne(string $selector): SimpleXmlDomInterface |
||
412 | |||
413 | /** |
||
414 | * Find one node with a CSS selector or false, if no element is found. |
||
415 | * |
||
416 | * @param string $selector |
||
417 | * |
||
418 | * @return false|SimpleXmlDomInterface |
||
419 | */ |
||
420 | public function findOneOrFalse(string $selector) |
||
424 | |||
425 | /** |
||
426 | * Returns the first child of node. |
||
427 | * |
||
428 | * @return SimpleXmlDomInterface|null |
||
429 | */ |
||
430 | View Code Duplication | public function firstChild() |
|
441 | |||
442 | /** |
||
443 | * Return elements by ".class". |
||
444 | * |
||
445 | * @param string $class |
||
446 | * |
||
447 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
448 | */ |
||
449 | public function getElementByClass(string $class): SimpleXmlDomNodeInterface |
||
453 | |||
454 | /** |
||
455 | * Return element by #id. |
||
456 | * |
||
457 | * @param string $id |
||
458 | * |
||
459 | * @return SimpleXmlDomInterface |
||
460 | */ |
||
461 | public function getElementById(string $id): SimpleXmlDomInterface |
||
465 | |||
466 | /** |
||
467 | * Return element by tag name. |
||
468 | * |
||
469 | * @param string $name |
||
470 | * |
||
471 | * @return SimpleXmlDomInterface |
||
472 | */ |
||
473 | View Code Duplication | public function getElementByTagName(string $name): SimpleXmlDomInterface |
|
487 | |||
488 | /** |
||
489 | * Returns elements by "#id". |
||
490 | * |
||
491 | * @param string $id |
||
492 | * @param int|null $idx |
||
493 | * |
||
494 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
495 | */ |
||
496 | public function getElementsById(string $id, $idx = null) |
||
500 | |||
501 | /** |
||
502 | * Returns elements by tag name. |
||
503 | * |
||
504 | * @param string $name |
||
505 | * @param int|null $idx |
||
506 | * |
||
507 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
508 | */ |
||
509 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
|
540 | |||
541 | /** |
||
542 | * @return \DOMNode |
||
543 | */ |
||
544 | public function getNode(): \DOMNode |
||
548 | |||
549 | /** |
||
550 | * Create a new "XmlDomParser"-object from the current context. |
||
551 | * |
||
552 | * @return XmlDomParser |
||
553 | */ |
||
554 | public function getXmlDomParser(): XmlDomParser |
||
558 | |||
559 | /** |
||
560 | * Get dom node's inner html. |
||
561 | * |
||
562 | * @param bool $multiDecodeNewHtmlEntity |
||
563 | * |
||
564 | * @return string |
||
565 | */ |
||
566 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
||
570 | |||
571 | /** |
||
572 | * Nodes can get partially destroyed in which they're still an |
||
573 | * actual DOM node (such as \DOMElement) but almost their entire |
||
574 | * body is gone, including the `nodeType` attribute. |
||
575 | * |
||
576 | * @return bool true if node has been destroyed |
||
577 | */ |
||
578 | public function isRemoved(): bool |
||
582 | |||
583 | /** |
||
584 | * Returns the last child of node. |
||
585 | * |
||
586 | * @return SimpleXmlDomInterface|null |
||
587 | */ |
||
588 | View Code Duplication | public function lastChild() |
|
599 | |||
600 | /** |
||
601 | * Returns the next sibling of node. |
||
602 | * |
||
603 | * @return SimpleXmlDomInterface|null |
||
604 | */ |
||
605 | View Code Duplication | public function nextSibling() |
|
616 | |||
617 | /** |
||
618 | * Returns the next sibling of node. |
||
619 | * |
||
620 | * @return SimpleXmlDomInterface|null |
||
621 | */ |
||
622 | View Code Duplication | public function nextNonWhitespaceSibling() |
|
638 | |||
639 | /** |
||
640 | * Returns the parent of node. |
||
641 | * |
||
642 | * @return SimpleXmlDomInterface |
||
643 | */ |
||
644 | public function parentNode(): SimpleXmlDomInterface |
||
648 | |||
649 | /** |
||
650 | * Returns the previous sibling of node. |
||
651 | * |
||
652 | * @return SimpleXmlDomInterface|null |
||
653 | */ |
||
654 | View Code Duplication | public function previousSibling() |
|
665 | |||
666 | /** |
||
667 | * @param string|string[]|null $value <p> |
||
668 | * null === get the current input value |
||
669 | * text === set a new input value |
||
670 | * </p> |
||
671 | * |
||
672 | * @return string|string[]|null |
||
673 | */ |
||
674 | View Code Duplication | public function val($value = null) |
|
752 | |||
753 | /** |
||
754 | * Retrieve an external iterator. |
||
755 | * |
||
756 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
757 | * |
||
758 | * @return SimpleXmlDomNode |
||
759 | * <p> |
||
760 | * An instance of an object implementing <b>Iterator</b> or |
||
761 | * <b>Traversable</b> |
||
762 | * </p> |
||
763 | */ |
||
764 | View Code Duplication | public function getIterator(): SimpleXmlDomNodeInterface |
|
775 | |||
776 | /** |
||
777 | * Normalize the given input for comparision. |
||
778 | * |
||
779 | * @param string|XmlDomParser $input |
||
780 | * |
||
781 | * @return string |
||
782 | */ |
||
783 | private function normalizeStringForComparision($input): string |
||
814 | } |
||
815 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.