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 | 139 | 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 | 26 | public function find(string $selector, $idx = null) |
|
55 | |||
56 | /** |
||
57 | * Returns an array of attributes. |
||
58 | * |
||
59 | * @return array|null |
||
60 | */ |
||
61 | 3 | View Code Duplication | public function getAllAttributes() |
74 | |||
75 | /** |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function hasAttributes(): bool |
||
82 | |||
83 | /** |
||
84 | * Return attribute value. |
||
85 | * |
||
86 | * @param string $name |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 24 | public function getAttribute(string $name): string |
|
100 | |||
101 | /** |
||
102 | * Determine if an attribute exists on the element. |
||
103 | * |
||
104 | * @param string $name |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | 2 | public function hasAttribute(string $name): bool |
|
116 | |||
117 | /** |
||
118 | * Get dom node's outer html. |
||
119 | * |
||
120 | * @param bool $multiDecodeNewHtmlEntity |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 32 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
|
128 | |||
129 | /** |
||
130 | * Get dom node's inner html. |
||
131 | * |
||
132 | * @param bool $multiDecodeNewHtmlEntity |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 21 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
|
140 | |||
141 | /** |
||
142 | * Remove attribute. |
||
143 | * |
||
144 | * @param string $name <p>The name of the html-attribute.</p> |
||
145 | * |
||
146 | * @return SimpleHtmlDomInterface |
||
147 | */ |
||
148 | 2 | public function removeAttribute(string $name): SimpleHtmlDomInterface |
|
156 | |||
157 | /** |
||
158 | * Replace child node. |
||
159 | * |
||
160 | * @param string $string |
||
161 | * |
||
162 | * @return SimpleHtmlDomInterface |
||
163 | */ |
||
164 | 8 | View Code Duplication | protected function replaceChildWithString(string $string): SimpleHtmlDomInterface |
209 | |||
210 | /** |
||
211 | * Replace this node. |
||
212 | * |
||
213 | * @param string $string |
||
214 | * |
||
215 | * @return SimpleHtmlDomInterface |
||
216 | */ |
||
217 | 6 | protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface |
|
279 | |||
280 | /** |
||
281 | * Replace this node with text |
||
282 | * |
||
283 | * @param string $string |
||
284 | * |
||
285 | * @return SimpleHtmlDomInterface |
||
286 | */ |
||
287 | 1 | View Code Duplication | protected function replaceTextWithString($string): SimpleHtmlDomInterface |
305 | |||
306 | /** |
||
307 | * Set attribute value. |
||
308 | * |
||
309 | * @param string $name <p>The name of the html-attribute.</p> |
||
310 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
311 | * @param bool $strict </p> |
||
312 | * $value must be NULL, to remove the attribute, |
||
313 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
314 | * </p> |
||
315 | * |
||
316 | * @return SimpleHtmlDomInterface |
||
317 | */ |
||
318 | 14 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface |
334 | |||
335 | /** |
||
336 | * Get dom node's plain text. |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | 18 | public function text(): string |
|
344 | |||
345 | /** |
||
346 | * Change the name of a tag in a "DOMNode". |
||
347 | * |
||
348 | * @param \DOMNode $node |
||
349 | * @param string $name |
||
350 | * |
||
351 | * @return \DOMElement|false |
||
352 | * <p>DOMElement a new instance of class DOMElement or false |
||
353 | * if an error occured.</p> |
||
354 | */ |
||
355 | 9 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name) |
380 | |||
381 | /** |
||
382 | * Returns children of node. |
||
383 | * |
||
384 | * @param int $idx |
||
385 | * |
||
386 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface|null |
||
387 | */ |
||
388 | 2 | View Code Duplication | public function childNodes(int $idx = -1) |
398 | |||
399 | /** |
||
400 | * Find nodes with a CSS selector. |
||
401 | * |
||
402 | * @param string $selector |
||
403 | * |
||
404 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
405 | */ |
||
406 | 1 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
|
410 | |||
411 | /** |
||
412 | * Find nodes with a CSS selector or false, if no element is found. |
||
413 | * |
||
414 | * @param string $selector |
||
415 | * |
||
416 | * @return false|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
417 | */ |
||
418 | 1 | public function findMultiOrFalse(string $selector) |
|
422 | |||
423 | /** |
||
424 | * Find one node with a CSS selector. |
||
425 | * |
||
426 | * @param string $selector |
||
427 | * |
||
428 | * @return SimpleHtmlDomInterface |
||
429 | */ |
||
430 | 2 | public function findOne(string $selector): SimpleHtmlDomInterface |
|
434 | |||
435 | /** |
||
436 | * Find one node with a CSS selector or false, if no element is found. |
||
437 | * |
||
438 | * @param string $selector |
||
439 | * |
||
440 | * @return false|SimpleHtmlDomInterface |
||
441 | */ |
||
442 | 1 | public function findOneOrFalse(string $selector) |
|
446 | |||
447 | /** |
||
448 | * Returns the first child of node. |
||
449 | * |
||
450 | * @return SimpleHtmlDomInterface|null |
||
451 | */ |
||
452 | 4 | View Code Duplication | public function firstChild() |
463 | |||
464 | /** |
||
465 | * Return elements by ".class". |
||
466 | * |
||
467 | * @param string $class |
||
468 | * |
||
469 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
470 | */ |
||
471 | public function getElementByClass(string $class): SimpleHtmlDomNodeInterface |
||
475 | |||
476 | /** |
||
477 | * Return element by #id. |
||
478 | * |
||
479 | * @param string $id |
||
480 | * |
||
481 | * @return SimpleHtmlDomInterface |
||
482 | */ |
||
483 | public function getElementById(string $id): SimpleHtmlDomInterface |
||
487 | |||
488 | /** |
||
489 | * Return element by tag name. |
||
490 | * |
||
491 | * @param string $name |
||
492 | * |
||
493 | * @return SimpleHtmlDomInterface |
||
494 | */ |
||
495 | View Code Duplication | public function getElementByTagName(string $name): SimpleHtmlDomInterface |
|
509 | |||
510 | /** |
||
511 | * Returns elements by "#id". |
||
512 | * |
||
513 | * @param string $id |
||
514 | * @param int|null $idx |
||
515 | * |
||
516 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
517 | */ |
||
518 | public function getElementsById(string $id, $idx = null) |
||
522 | |||
523 | /** |
||
524 | * Returns elements by tag name. |
||
525 | * |
||
526 | * @param string $name |
||
527 | * @param int|null $idx |
||
528 | * |
||
529 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
530 | */ |
||
531 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
|
562 | |||
563 | /** |
||
564 | * Create a new "HtmlDomParser"-object from the current context. |
||
565 | * |
||
566 | * @return HtmlDomParser |
||
567 | */ |
||
568 | public function getHtmlDomParser(): HtmlDomParser |
||
572 | |||
573 | /** |
||
574 | * @return \DOMNode |
||
575 | */ |
||
576 | public function getNode(): \DOMNode |
||
580 | |||
581 | /** |
||
582 | * Nodes can get partially destroyed in which they're still an |
||
583 | * actual DOM node (such as \DOMElement) but almost their entire |
||
584 | * body is gone, including the `nodeType` attribute. |
||
585 | * |
||
586 | * @return bool true if node has been destroyed |
||
587 | */ |
||
588 | public function isRemoved(): bool |
||
592 | |||
593 | /** |
||
594 | * Returns the last child of node. |
||
595 | * |
||
596 | * @return SimpleHtmlDomInterface|null |
||
597 | */ |
||
598 | View Code Duplication | public function lastChild() |
|
609 | |||
610 | /** |
||
611 | * Returns the next sibling of node. |
||
612 | * |
||
613 | * @return SimpleHtmlDomInterface|null |
||
614 | */ |
||
615 | View Code Duplication | public function nextSibling() |
|
626 | |||
627 | /** |
||
628 | * Returns the next sibling of node. |
||
629 | * |
||
630 | * @return SimpleHtmlDomInterface|null |
||
631 | */ |
||
632 | View Code Duplication | public function nextNonWhitespaceSibling() |
|
648 | |||
649 | /** |
||
650 | * Returns the parent of node. |
||
651 | * |
||
652 | * @return SimpleHtmlDomInterface |
||
653 | */ |
||
654 | public function parentNode(): SimpleHtmlDomInterface |
||
658 | |||
659 | /** |
||
660 | * Returns the previous sibling of node. |
||
661 | * |
||
662 | * @return SimpleHtmlDomInterface|null |
||
663 | */ |
||
664 | View Code Duplication | public function previousSibling() |
|
675 | |||
676 | /** |
||
677 | * @param string|string[]|null $value <p> |
||
678 | * null === get the current input value |
||
679 | * text === set a new input value |
||
680 | * </p> |
||
681 | * |
||
682 | * @return string|string[]|null |
||
683 | */ |
||
684 | View Code Duplication | public function val($value = null) |
|
760 | |||
761 | /** |
||
762 | * @param HtmlDomParser $newDocument |
||
763 | * @param bool $removeExtraHeadTag |
||
764 | * |
||
765 | * @return HtmlDomParser |
||
766 | */ |
||
767 | protected function cleanHtmlWrapper( |
||
838 | |||
839 | /** |
||
840 | * Retrieve an external iterator. |
||
841 | * |
||
842 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
843 | * |
||
844 | * @return SimpleHtmlDomNode |
||
845 | * <p> |
||
846 | * An instance of an object implementing <b>Iterator</b> or |
||
847 | * <b>Traversable</b> |
||
848 | * </p> |
||
849 | */ |
||
850 | View Code Duplication | public function getIterator(): SimpleHtmlDomNodeInterface |
|
861 | |||
862 | /** |
||
863 | * Get dom node's inner html. |
||
864 | * |
||
865 | * @param bool $multiDecodeNewHtmlEntity |
||
866 | * |
||
867 | * @return string |
||
868 | */ |
||
869 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
873 | |||
874 | /** |
||
875 | * Normalize the given input for comparision. |
||
876 | * |
||
877 | * @param HtmlDomParser|string $input |
||
878 | * |
||
879 | * @return string |
||
880 | */ |
||
881 | private function normalizeStringForComparision($input): string |
||
917 | } |
||
918 |
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.