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 |
||
12 | class SimpleXmlDom extends AbstractSimpleXmlDom implements \IteratorAggregate, SimpleXmlDomInterface |
||
13 | { |
||
14 | /** |
||
15 | * @param \DOMElement|\DOMNode $node |
||
16 | */ |
||
17 | 1 | public function __construct(\DOMNode $node) |
|
21 | |||
22 | /** |
||
23 | * @param string $name |
||
24 | * @param array $arguments |
||
25 | * |
||
26 | * @throws \BadMethodCallException |
||
27 | * |
||
28 | * @return SimpleXmlDomInterface|string|null |
||
29 | */ |
||
30 | public function __call($name, $arguments) |
||
40 | |||
41 | /** |
||
42 | * Find list of nodes with a CSS selector. |
||
43 | * |
||
44 | * @param string $selector |
||
45 | * @param int|null $idx |
||
46 | * |
||
47 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface |
||
48 | */ |
||
49 | public function find(string $selector, $idx = null) |
||
53 | |||
54 | /** |
||
55 | * Returns an array of attributes. |
||
56 | * |
||
57 | * @return array|null |
||
58 | */ |
||
59 | View Code Duplication | public function getAllAttributes() |
|
72 | |||
73 | /** |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function hasAttributes(): bool |
||
80 | |||
81 | /** |
||
82 | * Return attribute value. |
||
83 | * |
||
84 | * @param string $name |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getAttribute(string $name): string |
||
98 | |||
99 | /** |
||
100 | * Determine if an attribute exists on the element. |
||
101 | * |
||
102 | * @param string $name |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function hasAttribute(string $name): bool |
||
114 | |||
115 | /** |
||
116 | * Get dom node's inner html. |
||
117 | * |
||
118 | * @param bool $multiDecodeNewHtmlEntity |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
126 | |||
127 | /** |
||
128 | * Remove attribute. |
||
129 | * |
||
130 | * @param string $name <p>The name of the html-attribute.</p> |
||
131 | * |
||
132 | * @return SimpleXmlDomInterface |
||
133 | */ |
||
134 | public function removeAttribute(string $name): SimpleXmlDomInterface |
||
142 | |||
143 | /** |
||
144 | * Replace child node. |
||
145 | * |
||
146 | * @param string $string |
||
147 | * |
||
148 | * @return SimpleXmlDomInterface |
||
149 | */ |
||
150 | View Code Duplication | protected function replaceChildWithString(string $string): SimpleXmlDomInterface |
|
198 | |||
199 | /** |
||
200 | * Replace this node. |
||
201 | * |
||
202 | * @param string $string |
||
203 | * |
||
204 | * @return SimpleXmlDomInterface |
||
205 | */ |
||
206 | protected function replaceNodeWithString(string $string): SimpleXmlDomInterface |
||
242 | |||
243 | /** |
||
244 | * Replace this node with text |
||
245 | * |
||
246 | * @param string $string |
||
247 | * |
||
248 | * @return SimpleXmlDomInterface |
||
249 | */ |
||
250 | View Code Duplication | protected function replaceTextWithString($string): SimpleXmlDomInterface |
|
268 | |||
269 | /** |
||
270 | * Set attribute value. |
||
271 | * |
||
272 | * @param string $name <p>The name of the html-attribute.</p> |
||
273 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
274 | * @param bool $strict </p> |
||
275 | * $value must be NULL, to remove the attribute, |
||
276 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
277 | * </p> |
||
278 | * |
||
279 | * @return SimpleXmlDomInterface |
||
280 | */ |
||
281 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleXmlDomInterface |
|
297 | |||
298 | /** |
||
299 | * Get dom node's plain text. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function text(): string |
||
307 | |||
308 | /** |
||
309 | * Get dom node's outer html. |
||
310 | * |
||
311 | * @param bool $multiDecodeNewHtmlEntity |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function xml(bool $multiDecodeNewHtmlEntity = false): string |
||
319 | |||
320 | /** |
||
321 | * Change the name of a tag in a "DOMNode". |
||
322 | * |
||
323 | * @param \DOMNode $node |
||
324 | * @param string $name |
||
325 | * |
||
326 | * @return \DOMElement|false |
||
327 | * <p>DOMElement a new instance of class DOMElement or false |
||
328 | * if an error occured.</p> |
||
329 | */ |
||
330 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name) |
|
355 | |||
356 | /** |
||
357 | * Returns children of node. |
||
358 | * |
||
359 | * @param int $idx |
||
360 | * |
||
361 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface|null |
||
362 | */ |
||
363 | View Code Duplication | public function childNodes(int $idx = -1) |
|
373 | |||
374 | /** |
||
375 | * Find nodes with a CSS selector. |
||
376 | * |
||
377 | * @param string $selector |
||
378 | * |
||
379 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface |
||
380 | */ |
||
381 | public function findMulti(string $selector): SimpleXmlDomNodeInterface |
||
385 | |||
386 | /** |
||
387 | * Find nodes with a CSS selector. |
||
388 | * |
||
389 | * @param string $selector |
||
390 | * |
||
391 | * @return false|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface |
||
392 | */ |
||
393 | public function findMultiOrFalse(string $selector) |
||
397 | |||
398 | /** |
||
399 | * Find one node with a CSS selector. |
||
400 | * |
||
401 | * @param string $selector |
||
402 | * |
||
403 | * @return SimpleXmlDomInterface |
||
404 | */ |
||
405 | public function findOne(string $selector): SimpleXmlDomInterface |
||
409 | |||
410 | /** |
||
411 | * Find one node with a CSS selector or false, if no element is found. |
||
412 | * |
||
413 | * @param string $selector |
||
414 | * |
||
415 | * @return false|SimpleXmlDomInterface |
||
416 | */ |
||
417 | public function findOneOrFalse(string $selector) |
||
421 | |||
422 | /** |
||
423 | * Returns the first child of node. |
||
424 | * |
||
425 | * @return SimpleXmlDomInterface|null |
||
426 | */ |
||
427 | View Code Duplication | public function firstChild() |
|
438 | |||
439 | /** |
||
440 | * Return elements by ".class". |
||
441 | * |
||
442 | * @param string $class |
||
443 | * |
||
444 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface |
||
445 | */ |
||
446 | public function getElementByClass(string $class): SimpleXmlDomNodeInterface |
||
450 | |||
451 | /** |
||
452 | * Return element by #id. |
||
453 | * |
||
454 | * @param string $id |
||
455 | * |
||
456 | * @return SimpleXmlDomInterface |
||
457 | */ |
||
458 | public function getElementById(string $id): SimpleXmlDomInterface |
||
462 | |||
463 | /** |
||
464 | * Return element by tag name. |
||
465 | * |
||
466 | * @param string $name |
||
467 | * |
||
468 | * @return SimpleXmlDomInterface |
||
469 | */ |
||
470 | View Code Duplication | public function getElementByTagName(string $name): SimpleXmlDomInterface |
|
484 | |||
485 | /** |
||
486 | * Returns elements by "#id". |
||
487 | * |
||
488 | * @param string $id |
||
489 | * @param int|null $idx |
||
490 | * |
||
491 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface |
||
492 | */ |
||
493 | public function getElementsById(string $id, $idx = null) |
||
497 | |||
498 | /** |
||
499 | * Returns elements by tag name. |
||
500 | * |
||
501 | * @param string $name |
||
502 | * @param int|null $idx |
||
503 | * |
||
504 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface |
||
505 | */ |
||
506 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
|
537 | |||
538 | /** |
||
539 | * @return \DOMNode |
||
540 | */ |
||
541 | 1 | public function getNode(): \DOMNode |
|
545 | |||
546 | /** |
||
547 | * Create a new "XmlDomParser"-object from the current context. |
||
548 | * |
||
549 | * @return XmlDomParser |
||
550 | */ |
||
551 | public function getXmlDomParser(): XmlDomParser |
||
555 | |||
556 | /** |
||
557 | * Get dom node's inner html. |
||
558 | * |
||
559 | * @param bool $multiDecodeNewHtmlEntity |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
||
567 | |||
568 | /** |
||
569 | * Nodes can get partially destroyed in which they're still an |
||
570 | * actual DOM node (such as \DOMElement) but almost their entire |
||
571 | * body is gone, including the `nodeType` attribute. |
||
572 | * |
||
573 | * @return bool true if node has been destroyed |
||
574 | */ |
||
575 | public function isRemoved(): bool |
||
579 | |||
580 | /** |
||
581 | * Returns the last child of node. |
||
582 | * |
||
583 | * @return SimpleXmlDomInterface|null |
||
584 | */ |
||
585 | View Code Duplication | public function lastChild() |
|
596 | |||
597 | /** |
||
598 | * Returns the next sibling of node. |
||
599 | * |
||
600 | * @return SimpleXmlDomInterface|null |
||
601 | */ |
||
602 | View Code Duplication | public function nextSibling() |
|
613 | |||
614 | /** |
||
615 | * Returns the parent of node. |
||
616 | * |
||
617 | * @return SimpleXmlDomInterface |
||
618 | */ |
||
619 | public function parentNode(): SimpleXmlDomInterface |
||
623 | |||
624 | /** |
||
625 | * Returns the previous sibling of node. |
||
626 | * |
||
627 | * @return SimpleXmlDomInterface|null |
||
628 | */ |
||
629 | View Code Duplication | public function previousSibling() |
|
640 | |||
641 | /** |
||
642 | * @param string|string[]|null $value <p> |
||
643 | * null === get the current input value |
||
644 | * text === set a new input value |
||
645 | * </p> |
||
646 | * |
||
647 | * @return string|string[]|null |
||
648 | */ |
||
649 | View Code Duplication | public function val($value = null) |
|
725 | |||
726 | /** |
||
727 | * Retrieve an external iterator. |
||
728 | * |
||
729 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
730 | * |
||
731 | * @return SimpleXmlDomNode |
||
732 | * <p> |
||
733 | * An instance of an object implementing <b>Iterator</b> or |
||
734 | * <b>Traversable</b> |
||
735 | * </p> |
||
736 | */ |
||
737 | View Code Duplication | public function getIterator(): SimpleXmlDomNodeInterface |
|
748 | |||
749 | /** |
||
750 | * Normalize the given input for comparision. |
||
751 | * |
||
752 | * @param string|XmlDomParser $input |
||
753 | * |
||
754 | * @return string |
||
755 | */ |
||
756 | private function normalizeStringForComparision($input): string |
||
787 | } |
||
788 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.