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 | 162 | 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 | 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 | 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 | 8 | protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface  | 
            |
| 288 | |||
| 289 | /**  | 
            ||
| 290 | * Replace this node with text  | 
            ||
| 291 | *  | 
            ||
| 292 | * @param string $string  | 
            ||
| 293 | *  | 
            ||
| 294 | * @return SimpleHtmlDomInterface  | 
            ||
| 295 | */  | 
            ||
| 296 | 1 | View Code Duplication | protected function replaceTextWithString($string): SimpleHtmlDomInterface  | 
            
| 317 | |||
| 318 | /**  | 
            ||
| 319 | * Set attribute value.  | 
            ||
| 320 | *  | 
            ||
| 321 | * @param string $name <p>The name of the html-attribute.</p>  | 
            ||
| 322 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p>  | 
            ||
| 323 | * @param bool $strict </p>  | 
            ||
| 324 | * $value must be NULL, to remove the attribute,  | 
            ||
| 325 | * so that you can set an empty string as attribute-value e.g. autofocus=""  | 
            ||
| 326 | * </p>  | 
            ||
| 327 | *  | 
            ||
| 328 | * @return SimpleHtmlDomInterface  | 
            ||
| 329 | */  | 
            ||
| 330 | 15 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface  | 
            
| 346 | |||
| 347 | /**  | 
            ||
| 348 | * Get dom node's plain text.  | 
            ||
| 349 | *  | 
            ||
| 350 | * @return string  | 
            ||
| 351 | */  | 
            ||
| 352 | 32 | public function text(): string  | 
            |
| 356 | |||
| 357 | /**  | 
            ||
| 358 | * Change the name of a tag in a "DOMNode".  | 
            ||
| 359 | *  | 
            ||
| 360 | * @param \DOMNode $node  | 
            ||
| 361 | * @param string $name  | 
            ||
| 362 | *  | 
            ||
| 363 | * @return \DOMElement|false  | 
            ||
| 364 | * <p>DOMElement a new instance of class DOMElement or false  | 
            ||
| 365 | * if an error occured.</p>  | 
            ||
| 366 | */  | 
            ||
| 367 | 11 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name)  | 
            
| 393 | |||
| 394 | /**  | 
            ||
| 395 | * Returns children of node.  | 
            ||
| 396 | *  | 
            ||
| 397 | * @param int $idx  | 
            ||
| 398 | *  | 
            ||
| 399 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface|null  | 
            ||
| 400 | */  | 
            ||
| 401 | 2 | View Code Duplication | public function childNodes(int $idx = -1)  | 
            
| 411 | |||
| 412 | /**  | 
            ||
| 413 | * Find nodes with a CSS selector.  | 
            ||
| 414 | *  | 
            ||
| 415 | * @param string $selector  | 
            ||
| 416 | *  | 
            ||
| 417 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>  | 
            ||
| 418 | */  | 
            ||
| 419 | 1 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface  | 
            |
| 423 | |||
| 424 | /**  | 
            ||
| 425 | * Find nodes with a CSS selector or false, if no element is found.  | 
            ||
| 426 | *  | 
            ||
| 427 | * @param string $selector  | 
            ||
| 428 | *  | 
            ||
| 429 | * @return false|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>  | 
            ||
| 430 | */  | 
            ||
| 431 | 1 | public function findMultiOrFalse(string $selector)  | 
            |
| 435 | |||
| 436 | /**  | 
            ||
| 437 | * Find one node with a CSS selector.  | 
            ||
| 438 | *  | 
            ||
| 439 | * @param string $selector  | 
            ||
| 440 | *  | 
            ||
| 441 | * @return SimpleHtmlDomInterface  | 
            ||
| 442 | */  | 
            ||
| 443 | 3 | public function findOne(string $selector): SimpleHtmlDomInterface  | 
            |
| 447 | |||
| 448 | /**  | 
            ||
| 449 | * Find one node with a CSS selector or false, if no element is found.  | 
            ||
| 450 | *  | 
            ||
| 451 | * @param string $selector  | 
            ||
| 452 | *  | 
            ||
| 453 | * @return false|SimpleHtmlDomInterface  | 
            ||
| 454 | */  | 
            ||
| 455 | 1 | public function findOneOrFalse(string $selector)  | 
            |
| 459 | |||
| 460 | /**  | 
            ||
| 461 | * Returns the first child of node.  | 
            ||
| 462 | *  | 
            ||
| 463 | * @return SimpleHtmlDomInterface|null  | 
            ||
| 464 | */  | 
            ||
| 465 | 4 | View Code Duplication | public function firstChild()  | 
            
| 476 | |||
| 477 | /**  | 
            ||
| 478 | * Return elements by ".class".  | 
            ||
| 479 | *  | 
            ||
| 480 | * @param string $class  | 
            ||
| 481 | *  | 
            ||
| 482 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>  | 
            ||
| 483 | */  | 
            ||
| 484 | public function getElementByClass(string $class): SimpleHtmlDomNodeInterface  | 
            ||
| 488 | |||
| 489 | /**  | 
            ||
| 490 | * Return element by #id.  | 
            ||
| 491 | *  | 
            ||
| 492 | * @param string $id  | 
            ||
| 493 | *  | 
            ||
| 494 | * @return SimpleHtmlDomInterface  | 
            ||
| 495 | */  | 
            ||
| 496 | 1 | public function getElementById(string $id): SimpleHtmlDomInterface  | 
            |
| 500 | |||
| 501 | /**  | 
            ||
| 502 | * Return element by tag name.  | 
            ||
| 503 | *  | 
            ||
| 504 | * @param string $name  | 
            ||
| 505 | *  | 
            ||
| 506 | * @return SimpleHtmlDomInterface  | 
            ||
| 507 | */  | 
            ||
| 508 | 1 | View Code Duplication | public function getElementByTagName(string $name): SimpleHtmlDomInterface  | 
            
| 522 | |||
| 523 | /**  | 
            ||
| 524 | * Returns elements by "#id".  | 
            ||
| 525 | *  | 
            ||
| 526 | * @param string $id  | 
            ||
| 527 | * @param int|null $idx  | 
            ||
| 528 | *  | 
            ||
| 529 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>  | 
            ||
| 530 | */  | 
            ||
| 531 | public function getElementsById(string $id, $idx = null)  | 
            ||
| 535 | |||
| 536 | /**  | 
            ||
| 537 | * Returns elements by tag name.  | 
            ||
| 538 | *  | 
            ||
| 539 | * @param string $name  | 
            ||
| 540 | * @param int|null $idx  | 
            ||
| 541 | *  | 
            ||
| 542 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>  | 
            ||
| 543 | */  | 
            ||
| 544 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null)  | 
            
| 575 | |||
| 576 | /**  | 
            ||
| 577 | * Create a new "HtmlDomParser"-object from the current context.  | 
            ||
| 578 | *  | 
            ||
| 579 | * @return HtmlDomParser  | 
            ||
| 580 | */  | 
            ||
| 581 | 104 | public function getHtmlDomParser(): HtmlDomParser  | 
            |
| 585 | |||
| 586 | /**  | 
            ||
| 587 | * @return \DOMNode  | 
            ||
| 588 | */  | 
            ||
| 589 | 105 | public function getNode(): \DOMNode  | 
            |
| 593 | |||
| 594 | /**  | 
            ||
| 595 | * Nodes can get partially destroyed in which they're still an  | 
            ||
| 596 | * actual DOM node (such as \DOMElement) but almost their entire  | 
            ||
| 597 | * body is gone, including the `nodeType` attribute.  | 
            ||
| 598 | *  | 
            ||
| 599 | * @return bool true if node has been destroyed  | 
            ||
| 600 | */  | 
            ||
| 601 | public function isRemoved(): bool  | 
            ||
| 605 | |||
| 606 | /**  | 
            ||
| 607 | * Returns the last child of node.  | 
            ||
| 608 | *  | 
            ||
| 609 | * @return SimpleHtmlDomInterface|null  | 
            ||
| 610 | */  | 
            ||
| 611 | 4 | View Code Duplication | public function lastChild()  | 
            
| 622 | |||
| 623 | /**  | 
            ||
| 624 | * Returns the next sibling of node.  | 
            ||
| 625 | *  | 
            ||
| 626 | * @return SimpleHtmlDomInterface|null  | 
            ||
| 627 | */  | 
            ||
| 628 | 1 | View Code Duplication | public function nextSibling()  | 
            
| 639 | |||
| 640 | /**  | 
            ||
| 641 | * Returns the next sibling of node.  | 
            ||
| 642 | *  | 
            ||
| 643 | * @return SimpleHtmlDomInterface|null  | 
            ||
| 644 | */  | 
            ||
| 645 | 1 | View Code Duplication | public function nextNonWhitespaceSibling()  | 
            
| 661 | |||
| 662 | /**  | 
            ||
| 663 | * Returns the parent of node.  | 
            ||
| 664 | *  | 
            ||
| 665 | * @return SimpleHtmlDomInterface  | 
            ||
| 666 | */  | 
            ||
| 667 | 2 | public function parentNode(): SimpleHtmlDomInterface  | 
            |
| 671 | |||
| 672 | /**  | 
            ||
| 673 | * Returns the previous sibling of node.  | 
            ||
| 674 | *  | 
            ||
| 675 | * @return SimpleHtmlDomInterface|null  | 
            ||
| 676 | */  | 
            ||
| 677 | 1 | View Code Duplication | public function previousSibling()  | 
            
| 688 | |||
| 689 | /**  | 
            ||
| 690 | * @param string|string[]|null $value <p>  | 
            ||
| 691 | * null === get the current input value  | 
            ||
| 692 | * text === set a new input value  | 
            ||
| 693 | * </p>  | 
            ||
| 694 | *  | 
            ||
| 695 | * @return string|string[]|null  | 
            ||
| 696 | */  | 
            ||
| 697 | 1 | View Code Duplication | public function val($value = null)  | 
            
| 775 | |||
| 776 | /**  | 
            ||
| 777 | * @param HtmlDomParser $newDocument  | 
            ||
| 778 | * @param bool $removeExtraHeadTag  | 
            ||
| 779 | *  | 
            ||
| 780 | * @return HtmlDomParser  | 
            ||
| 781 | */  | 
            ||
| 782 | 14 | protected function cleanHtmlWrapper(  | 
            |
| 880 | |||
| 881 | /**  | 
            ||
| 882 | * Retrieve an external iterator.  | 
            ||
| 883 | *  | 
            ||
| 884 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php  | 
            ||
| 885 | *  | 
            ||
| 886 | * @return SimpleHtmlDomNode  | 
            ||
| 887 | * <p>  | 
            ||
| 888 | * An instance of an object implementing <b>Iterator</b> or  | 
            ||
| 889 | * <b>Traversable</b>  | 
            ||
| 890 | * </p>  | 
            ||
| 891 | */  | 
            ||
| 892 | 3 | View Code Duplication | public function getIterator(): SimpleHtmlDomNodeInterface  | 
            
| 903 | |||
| 904 | /**  | 
            ||
| 905 | * Get dom node's inner html.  | 
            ||
| 906 | *  | 
            ||
| 907 | * @param bool $multiDecodeNewHtmlEntity  | 
            ||
| 908 | *  | 
            ||
| 909 | * @return string  | 
            ||
| 910 | */  | 
            ||
| 911 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string  | 
            ||
| 915 | |||
| 916 | /**  | 
            ||
| 917 | * Normalize the given input for comparision.  | 
            ||
| 918 | *  | 
            ||
| 919 | * @param HtmlDomParser|string $input  | 
            ||
| 920 | *  | 
            ||
| 921 | * @return string  | 
            ||
| 922 | */  | 
            ||
| 923 | 14 | private function normalizeStringForComparision($input): string  | 
            |
| 959 | }  | 
            ||
| 960 | 
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.