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 | 164 | 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 | 11 | 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 | 26 | 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 | 36 | 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 | 24 | 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 |
212 | |||
213 | /** |
||
214 | * Replace this node. |
||
215 | * |
||
216 | * @param string $string |
||
217 | * |
||
218 | * @return SimpleHtmlDomInterface |
||
219 | */ |
||
220 | 8 | protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface |
|
286 | |||
287 | /** |
||
288 | * Replace this node with text |
||
289 | * |
||
290 | * @param string $string |
||
291 | * |
||
292 | * @return SimpleHtmlDomInterface |
||
293 | */ |
||
294 | 1 | View Code Duplication | protected function replaceTextWithString($string): SimpleHtmlDomInterface |
315 | |||
316 | /** |
||
317 | * Set attribute value. |
||
318 | * |
||
319 | * @param string $name <p>The name of the html-attribute.</p> |
||
320 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
321 | * @param bool $strictEmptyValueCheck <p> |
||
322 | * $value must be NULL, to remove the attribute, |
||
323 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
324 | * </p> |
||
325 | * |
||
326 | * @return SimpleHtmlDomInterface |
||
327 | */ |
||
328 | 16 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strictEmptyValueCheck = false): SimpleHtmlDomInterface |
344 | |||
345 | /** |
||
346 | * Get dom node's plain text. |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | 32 | public function text(): string |
|
354 | |||
355 | /** |
||
356 | * Change the name of a tag in a "DOMNode". |
||
357 | * |
||
358 | * @param \DOMNode $node |
||
359 | * @param string $name |
||
360 | * |
||
361 | * @return \DOMElement|false |
||
362 | * <p>DOMElement a new instance of class DOMElement or false |
||
363 | * if an error occured.</p> |
||
364 | */ |
||
365 | 11 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name) |
391 | |||
392 | /** |
||
393 | * Returns children of node. |
||
394 | * |
||
395 | * @param int $idx |
||
396 | * |
||
397 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface|null |
||
398 | */ |
||
399 | 2 | View Code Duplication | public function childNodes(int $idx = -1) |
409 | |||
410 | /** |
||
411 | * Find nodes with a CSS selector. |
||
412 | * |
||
413 | * @param string $selector |
||
414 | * |
||
415 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
416 | */ |
||
417 | 1 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
|
421 | |||
422 | /** |
||
423 | * Find nodes with a CSS selector or false, if no element is found. |
||
424 | * |
||
425 | * @param string $selector |
||
426 | * |
||
427 | * @return false|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
428 | */ |
||
429 | 1 | public function findMultiOrFalse(string $selector) |
|
433 | |||
434 | /** |
||
435 | * Find one node with a CSS selector. |
||
436 | * |
||
437 | * @param string $selector |
||
438 | * |
||
439 | * @return SimpleHtmlDomInterface |
||
440 | */ |
||
441 | 3 | public function findOne(string $selector): SimpleHtmlDomInterface |
|
445 | |||
446 | /** |
||
447 | * Find one node with a CSS selector or false, if no element is found. |
||
448 | * |
||
449 | * @param string $selector |
||
450 | * |
||
451 | * @return false|SimpleHtmlDomInterface |
||
452 | */ |
||
453 | 1 | public function findOneOrFalse(string $selector) |
|
457 | |||
458 | /** |
||
459 | * Returns the first child of node. |
||
460 | * |
||
461 | * @return SimpleHtmlDomInterface|null |
||
462 | */ |
||
463 | 4 | View Code Duplication | public function firstChild() |
474 | |||
475 | /** |
||
476 | * Return elements by ".class". |
||
477 | * |
||
478 | * @param string $class |
||
479 | * |
||
480 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
481 | */ |
||
482 | public function getElementByClass(string $class): SimpleHtmlDomNodeInterface |
||
486 | |||
487 | /** |
||
488 | * Return element by #id. |
||
489 | * |
||
490 | * @param string $id |
||
491 | * |
||
492 | * @return SimpleHtmlDomInterface |
||
493 | */ |
||
494 | 1 | public function getElementById(string $id): SimpleHtmlDomInterface |
|
498 | |||
499 | /** |
||
500 | * Return element by tag name. |
||
501 | * |
||
502 | * @param string $name |
||
503 | * |
||
504 | * @return SimpleHtmlDomInterface |
||
505 | */ |
||
506 | 1 | View Code Duplication | public function getElementByTagName(string $name): SimpleHtmlDomInterface |
520 | |||
521 | /** |
||
522 | * Returns elements by "#id". |
||
523 | * |
||
524 | * @param string $id |
||
525 | * @param int|null $idx |
||
526 | * |
||
527 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
528 | */ |
||
529 | public function getElementsById(string $id, $idx = null) |
||
533 | |||
534 | /** |
||
535 | * Returns elements by tag name. |
||
536 | * |
||
537 | * @param string $name |
||
538 | * @param int|null $idx |
||
539 | * |
||
540 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
541 | */ |
||
542 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
573 | |||
574 | /** |
||
575 | * Create a new "HtmlDomParser"-object from the current context. |
||
576 | * |
||
577 | * @return HtmlDomParser |
||
578 | */ |
||
579 | 105 | public function getHtmlDomParser(): HtmlDomParser |
|
583 | |||
584 | /** |
||
585 | * @return \DOMNode |
||
586 | */ |
||
587 | 106 | public function getNode(): \DOMNode |
|
591 | |||
592 | /** |
||
593 | * Nodes can get partially destroyed in which they're still an |
||
594 | * actual DOM node (such as \DOMElement) but almost their entire |
||
595 | * body is gone, including the `nodeType` attribute. |
||
596 | * |
||
597 | * @return bool true if node has been destroyed |
||
598 | */ |
||
599 | public function isRemoved(): bool |
||
603 | |||
604 | /** |
||
605 | * Returns the last child of node. |
||
606 | * |
||
607 | * @return SimpleHtmlDomInterface|null |
||
608 | */ |
||
609 | 4 | View Code Duplication | public function lastChild() |
620 | |||
621 | /** |
||
622 | * Returns the next sibling of node. |
||
623 | * |
||
624 | * @return SimpleHtmlDomInterface|null |
||
625 | */ |
||
626 | 1 | View Code Duplication | public function nextSibling() |
637 | |||
638 | /** |
||
639 | * Returns the next sibling of node. |
||
640 | * |
||
641 | * @return SimpleHtmlDomInterface|null |
||
642 | */ |
||
643 | 1 | View Code Duplication | public function nextNonWhitespaceSibling() |
659 | |||
660 | /** |
||
661 | * Returns the parent of node. |
||
662 | * |
||
663 | * @return SimpleHtmlDomInterface |
||
664 | */ |
||
665 | 2 | public function parentNode(): SimpleHtmlDomInterface |
|
669 | |||
670 | /** |
||
671 | * Returns the previous sibling of node. |
||
672 | * |
||
673 | * @return SimpleHtmlDomInterface|null |
||
674 | */ |
||
675 | 1 | View Code Duplication | public function previousSibling() |
686 | |||
687 | |||
688 | /** |
||
689 | * Returns the previous sibling of node. |
||
690 | * |
||
691 | * @return SimpleHtmlDomInterface|null |
||
692 | */ |
||
693 | View Code Duplication | public function previousNonWhitespaceSibling() |
|
709 | 1 | ||
710 | /** |
||
711 | * @param string|string[]|null $value <p> |
||
712 | * null === get the current input value |
||
713 | 1 | * text === set a new input value |
|
714 | * </p> |
||
715 | 1 | * |
|
716 | * @return string|string[]|null |
||
717 | 1 | */ |
|
718 | View Code Duplication | public function val($value = null) |
|
795 | |||
796 | 14 | /** |
|
797 | 14 | * @param HtmlDomParser $newDocument |
|
798 | * @param bool $removeExtraHeadTag |
||
799 | 11 | * |
|
800 | * @return HtmlDomParser |
||
801 | */ |
||
802 | 14 | protected function cleanHtmlWrapper( |
|
896 | |||
897 | /** |
||
898 | * Retrieve an external iterator. |
||
899 | * |
||
900 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
901 | * |
||
902 | * @return SimpleHtmlDomNode |
||
903 | * <p> |
||
904 | * An instance of an object implementing <b>Iterator</b> or |
||
905 | * <b>Traversable</b> |
||
906 | * </p> |
||
907 | */ |
||
908 | View Code Duplication | public function getIterator(): SimpleHtmlDomNodeInterface |
|
919 | 14 | ||
920 | /** |
||
921 | 14 | * Get dom node's inner html. |
|
922 | * |
||
923 | 14 | * @param bool $multiDecodeNewHtmlEntity |
|
924 | * |
||
925 | * @return string |
||
926 | 14 | */ |
|
927 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
931 | 14 | ||
932 | 14 | /** |
|
933 | 14 | * Normalize the given input for comparision. |
|
934 | * |
||
935 | 14 | * @param HtmlDomParser|string $input |
|
936 | * |
||
937 | * @return string |
||
938 | */ |
||
939 | private function normalizeStringForComparision($input): string |
||
975 | } |
||
976 |
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.