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 SimpleHtmlDom 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 SimpleHtmlDom, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class SimpleHtmlDom extends AbstractSimpleHtmlDom implements \IteratorAggregate, SimpleHtmlDomInterface |
||
15 | { |
||
16 | /** |
||
17 | * @param \DOMElement|\DOMNode $node |
||
18 | */ |
||
19 | 156 | public function __construct(\DOMNode $node) |
|
23 | |||
24 | /** |
||
25 | * @param string $name |
||
26 | * @param array $arguments |
||
27 | * |
||
28 | * @throws \BadMethodCallException |
||
29 | * |
||
30 | * @return SimpleHtmlDomInterface|string|null |
||
31 | */ |
||
32 | 10 | 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 SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
|
|||
50 | */ |
||
51 | 27 | public function find(string $selector, $idx = null) |
|
55 | |||
56 | /** |
||
57 | * Returns an array of attributes. |
||
58 | * |
||
59 | * @return string[]|null |
||
60 | */ |
||
61 | 3 | 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 | 25 | 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 | 2 | public function hasAttribute(string $name): bool |
|
120 | |||
121 | /** |
||
122 | * Get dom node's outer html. |
||
123 | * |
||
124 | * @param bool $multiDecodeNewHtmlEntity |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 34 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
|
132 | |||
133 | /** |
||
134 | * Get dom node's inner html. |
||
135 | * |
||
136 | * @param bool $multiDecodeNewHtmlEntity |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 23 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
|
144 | |||
145 | /** |
||
146 | * Remove attribute. |
||
147 | * |
||
148 | * @param string $name <p>The name of the html-attribute.</p> |
||
149 | * |
||
150 | * @return SimpleHtmlDomInterface |
||
151 | */ |
||
152 | 2 | public function removeAttribute(string $name): SimpleHtmlDomInterface |
|
160 | |||
161 | /** |
||
162 | * Replace child node. |
||
163 | * |
||
164 | * @param string $string |
||
165 | * |
||
166 | * @return SimpleHtmlDomInterface |
||
167 | */ |
||
168 | 9 | View Code Duplication | protected function replaceChildWithString(string $string): SimpleHtmlDomInterface |
213 | |||
214 | /** |
||
215 | * Replace this node. |
||
216 | * |
||
217 | * @param string $string |
||
218 | * |
||
219 | * @return SimpleHtmlDomInterface |
||
220 | */ |
||
221 | 6 | protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface |
|
283 | |||
284 | /** |
||
285 | * Replace this node with text |
||
286 | * |
||
287 | * @param string $string |
||
288 | * |
||
289 | * @return SimpleHtmlDomInterface |
||
290 | */ |
||
291 | 1 | View Code Duplication | protected function replaceTextWithString($string): SimpleHtmlDomInterface |
309 | |||
310 | /** |
||
311 | * Set attribute value. |
||
312 | * |
||
313 | * @param string $name <p>The name of the html-attribute.</p> |
||
314 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
315 | * @param bool $strict </p> |
||
316 | * $value must be NULL, to remove the attribute, |
||
317 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
318 | * </p> |
||
319 | * |
||
320 | * @return SimpleHtmlDomInterface |
||
321 | */ |
||
322 | 15 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface |
338 | |||
339 | /** |
||
340 | * Get dom node's plain text. |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | 28 | public function text(): string |
|
348 | |||
349 | /** |
||
350 | * Change the name of a tag in a "DOMNode". |
||
351 | * |
||
352 | * @param \DOMNode $node |
||
353 | * @param string $name |
||
354 | * |
||
355 | * @return \DOMElement|false |
||
356 | * <p>DOMElement a new instance of class DOMElement or false |
||
357 | * if an error occured.</p> |
||
358 | */ |
||
359 | 10 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name) |
386 | |||
387 | /** |
||
388 | * Returns children of node. |
||
389 | * |
||
390 | * @param int $idx |
||
391 | * |
||
392 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface|null |
||
393 | */ |
||
394 | 2 | View Code Duplication | public function childNodes(int $idx = -1) |
404 | |||
405 | /** |
||
406 | * Find nodes with a CSS selector. |
||
407 | * |
||
408 | * @param string $selector |
||
409 | * |
||
410 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
411 | */ |
||
412 | 1 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
|
416 | |||
417 | /** |
||
418 | * Find nodes with a CSS selector or false, if no element is found. |
||
419 | * |
||
420 | * @param string $selector |
||
421 | * |
||
422 | * @return false|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
423 | */ |
||
424 | 1 | public function findMultiOrFalse(string $selector) |
|
428 | |||
429 | /** |
||
430 | * Find one node with a CSS selector. |
||
431 | * |
||
432 | * @param string $selector |
||
433 | * |
||
434 | * @return SimpleHtmlDomInterface |
||
435 | */ |
||
436 | 3 | public function findOne(string $selector): SimpleHtmlDomInterface |
|
440 | |||
441 | /** |
||
442 | * Find one node with a CSS selector or false, if no element is found. |
||
443 | * |
||
444 | * @param string $selector |
||
445 | * |
||
446 | * @return false|SimpleHtmlDomInterface |
||
447 | */ |
||
448 | 1 | public function findOneOrFalse(string $selector) |
|
452 | |||
453 | /** |
||
454 | * Returns the first child of node. |
||
455 | * |
||
456 | * @return SimpleHtmlDomInterface|null |
||
457 | */ |
||
458 | 4 | View Code Duplication | public function firstChild() |
469 | |||
470 | /** |
||
471 | * Return elements by ".class". |
||
472 | * |
||
473 | * @param string $class |
||
474 | * |
||
475 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
476 | */ |
||
477 | public function getElementByClass(string $class): SimpleHtmlDomNodeInterface |
||
481 | |||
482 | /** |
||
483 | * Return element by #id. |
||
484 | * |
||
485 | * @param string $id |
||
486 | * |
||
487 | * @return SimpleHtmlDomInterface |
||
488 | */ |
||
489 | 1 | public function getElementById(string $id): SimpleHtmlDomInterface |
|
493 | |||
494 | /** |
||
495 | * Return element by tag name. |
||
496 | * |
||
497 | * @param string $name |
||
498 | * |
||
499 | * @return SimpleHtmlDomInterface |
||
500 | */ |
||
501 | 1 | View Code Duplication | public function getElementByTagName(string $name): SimpleHtmlDomInterface |
515 | |||
516 | /** |
||
517 | * Returns elements by "#id". |
||
518 | * |
||
519 | * @param string $id |
||
520 | * @param int|null $idx |
||
521 | * |
||
522 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
523 | */ |
||
524 | public function getElementsById(string $id, $idx = null) |
||
528 | |||
529 | /** |
||
530 | * Returns elements by tag name. |
||
531 | * |
||
532 | * @param string $name |
||
533 | * @param int|null $idx |
||
534 | * |
||
535 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
536 | */ |
||
537 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
568 | |||
569 | /** |
||
570 | * Create a new "HtmlDomParser"-object from the current context. |
||
571 | * |
||
572 | * @return HtmlDomParser |
||
573 | */ |
||
574 | 98 | public function getHtmlDomParser(): HtmlDomParser |
|
578 | |||
579 | /** |
||
580 | * @return \DOMNode |
||
581 | */ |
||
582 | 99 | public function getNode(): \DOMNode |
|
586 | |||
587 | /** |
||
588 | * Nodes can get partially destroyed in which they're still an |
||
589 | * actual DOM node (such as \DOMElement) but almost their entire |
||
590 | * body is gone, including the `nodeType` attribute. |
||
591 | * |
||
592 | * @return bool true if node has been destroyed |
||
593 | */ |
||
594 | public function isRemoved(): bool |
||
598 | |||
599 | /** |
||
600 | * Returns the last child of node. |
||
601 | * |
||
602 | * @return SimpleHtmlDomInterface|null |
||
603 | */ |
||
604 | 4 | View Code Duplication | public function lastChild() |
615 | |||
616 | /** |
||
617 | * Returns the next sibling of node. |
||
618 | * |
||
619 | * @return SimpleHtmlDomInterface|null |
||
620 | */ |
||
621 | 1 | View Code Duplication | public function nextSibling() |
632 | |||
633 | /** |
||
634 | * Returns the next sibling of node. |
||
635 | * |
||
636 | * @return SimpleHtmlDomInterface|null |
||
637 | */ |
||
638 | 1 | View Code Duplication | public function nextNonWhitespaceSibling() |
654 | |||
655 | /** |
||
656 | * Returns the parent of node. |
||
657 | * |
||
658 | * @return SimpleHtmlDomInterface |
||
659 | */ |
||
660 | 2 | public function parentNode(): SimpleHtmlDomInterface |
|
664 | |||
665 | /** |
||
666 | * Returns the previous sibling of node. |
||
667 | * |
||
668 | * @return SimpleHtmlDomInterface|null |
||
669 | */ |
||
670 | 1 | View Code Duplication | public function previousSibling() |
681 | |||
682 | /** |
||
683 | * @param string|string[]|null $value <p> |
||
684 | * null === get the current input value |
||
685 | * text === set a new input value |
||
686 | * </p> |
||
687 | * |
||
688 | * @return string|string[]|null |
||
689 | */ |
||
690 | 1 | View Code Duplication | public function val($value = null) |
768 | |||
769 | /** |
||
770 | * @param HtmlDomParser $newDocument |
||
771 | * @param bool $removeExtraHeadTag |
||
772 | * |
||
773 | * @return HtmlDomParser |
||
774 | */ |
||
775 | 13 | protected function cleanHtmlWrapper( |
|
848 | |||
849 | /** |
||
850 | * Retrieve an external iterator. |
||
851 | * |
||
852 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
853 | * |
||
854 | * @return SimpleHtmlDomNode |
||
855 | * <p> |
||
856 | * An instance of an object implementing <b>Iterator</b> or |
||
857 | * <b>Traversable</b> |
||
858 | * </p> |
||
859 | */ |
||
860 | 3 | View Code Duplication | public function getIterator(): SimpleHtmlDomNodeInterface |
871 | |||
872 | /** |
||
873 | * Get dom node's inner html. |
||
874 | * |
||
875 | * @param bool $multiDecodeNewHtmlEntity |
||
876 | * |
||
877 | * @return string |
||
878 | */ |
||
879 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
883 | |||
884 | /** |
||
885 | * Normalize the given input for comparision. |
||
886 | * |
||
887 | * @param HtmlDomParser|string $input |
||
888 | * |
||
889 | * @return string |
||
890 | */ |
||
891 | 13 | private function normalizeStringForComparision($input): string |
|
927 | } |
||
928 |
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.