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 | 160 | 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 | protected function replaceChildWithString(string $string): SimpleHtmlDomInterface |
|
| 169 | { |
||
| 170 | 9 | if (!empty($string)) { |
|
| 171 | 8 | $newDocument = new HtmlDomParser($string); |
|
| 172 | |||
| 173 | 8 | $tmpDomString = $this->normalizeStringForComparision($newDocument); |
|
| 174 | 8 | $tmpStr = $this->normalizeStringForComparision($string); |
|
| 175 | 8 | if ($tmpDomString !== $tmpStr) { |
|
| 176 | throw new \RuntimeException( |
||
| 177 | 'Not valid HTML fragment!' . "\n" . |
||
| 178 | $tmpDomString . "\n" . |
||
| 179 | $tmpStr |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** @var \DOMNode[] $remove_nodes */ |
||
| 185 | 9 | $remove_nodes = []; |
|
| 186 | 9 | if ($this->node->childNodes->length > 0) { |
|
| 187 | // INFO: We need to fetch the nodes first, before we can delete them, because of missing references in the dom, |
||
| 188 | // if we delete the elements on the fly. |
||
| 189 | 9 | foreach ($this->node->childNodes as $node) { |
|
| 190 | 9 | $remove_nodes[] = $node; |
|
| 191 | } |
||
| 192 | } |
||
| 193 | 9 | foreach ($remove_nodes as $remove_node) { |
|
| 194 | 9 | $this->node->removeChild($remove_node); |
|
| 195 | } |
||
| 196 | |||
| 197 | 9 | if (!empty($newDocument)) { |
|
| 198 | 8 | $newDocument = $this->cleanHtmlWrapper($newDocument); |
|
| 199 | 8 | $ownerDocument = $this->node->ownerDocument; |
|
| 200 | if ( |
||
| 201 | 8 | $ownerDocument |
|
| 202 | && |
||
| 203 | 8 | $newDocument->getDocument()->documentElement |
|
| 204 | ) { |
||
| 205 | 8 | $newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
|
| 206 | /** @noinspection UnusedFunctionResultInspection */ |
||
| 207 | 8 | $this->node->appendChild($newNode); |
|
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | 9 | return $this; |
|
| 212 | } |
||
| 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 | 32 | 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) |
| 385 | |||
| 386 | /** |
||
| 387 | * Returns children of node. |
||
| 388 | * |
||
| 389 | * @param int $idx |
||
| 390 | * |
||
| 391 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface|null |
||
| 392 | */ |
||
| 393 | 2 | View Code Duplication | public function childNodes(int $idx = -1) |
| 403 | |||
| 404 | /** |
||
| 405 | * Find nodes with a CSS selector. |
||
| 406 | * |
||
| 407 | * @param string $selector |
||
| 408 | * |
||
| 409 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
| 410 | */ |
||
| 411 | 1 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Find nodes with a CSS selector or false, if no element is found. |
||
| 418 | * |
||
| 419 | * @param string $selector |
||
| 420 | * |
||
| 421 | * @return false|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
| 422 | */ |
||
| 423 | 1 | public function findMultiOrFalse(string $selector) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Find one node with a CSS selector. |
||
| 430 | * |
||
| 431 | * @param string $selector |
||
| 432 | * |
||
| 433 | * @return SimpleHtmlDomInterface |
||
| 434 | */ |
||
| 435 | 3 | public function findOne(string $selector): SimpleHtmlDomInterface |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Find one node with a CSS selector or false, if no element is found. |
||
| 442 | * |
||
| 443 | * @param string $selector |
||
| 444 | * |
||
| 445 | * @return false|SimpleHtmlDomInterface |
||
| 446 | */ |
||
| 447 | 1 | public function findOneOrFalse(string $selector) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Returns the first child of node. |
||
| 454 | * |
||
| 455 | * @return SimpleHtmlDomInterface|null |
||
| 456 | */ |
||
| 457 | 4 | View Code Duplication | public function firstChild() |
| 468 | |||
| 469 | /** |
||
| 470 | * Return elements by ".class". |
||
| 471 | * |
||
| 472 | * @param string $class |
||
| 473 | * |
||
| 474 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
| 475 | */ |
||
| 476 | public function getElementByClass(string $class): SimpleHtmlDomNodeInterface |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Return element by #id. |
||
| 483 | * |
||
| 484 | * @param string $id |
||
| 485 | * |
||
| 486 | * @return SimpleHtmlDomInterface |
||
| 487 | */ |
||
| 488 | 1 | public function getElementById(string $id): SimpleHtmlDomInterface |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Return element by tag name. |
||
| 495 | * |
||
| 496 | * @param string $name |
||
| 497 | * |
||
| 498 | * @return SimpleHtmlDomInterface |
||
| 499 | */ |
||
| 500 | 1 | View Code Duplication | public function getElementByTagName(string $name): SimpleHtmlDomInterface |
| 514 | |||
| 515 | /** |
||
| 516 | * Returns elements by "#id". |
||
| 517 | * |
||
| 518 | * @param string $id |
||
| 519 | * @param int|null $idx |
||
| 520 | * |
||
| 521 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
| 522 | */ |
||
| 523 | public function getElementsById(string $id, $idx = null) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Returns elements by tag name. |
||
| 530 | * |
||
| 531 | * @param string $name |
||
| 532 | * @param int|null $idx |
||
| 533 | * |
||
| 534 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
||
| 535 | */ |
||
| 536 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
| 567 | |||
| 568 | /** |
||
| 569 | * Create a new "HtmlDomParser"-object from the current context. |
||
| 570 | * |
||
| 571 | * @return HtmlDomParser |
||
| 572 | */ |
||
| 573 | 102 | public function getHtmlDomParser(): HtmlDomParser |
|
| 577 | |||
| 578 | /** |
||
| 579 | * @return \DOMNode |
||
| 580 | */ |
||
| 581 | 103 | public function getNode(): \DOMNode |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Nodes can get partially destroyed in which they're still an |
||
| 588 | * actual DOM node (such as \DOMElement) but almost their entire |
||
| 589 | * body is gone, including the `nodeType` attribute. |
||
| 590 | * |
||
| 591 | * @return bool true if node has been destroyed |
||
| 592 | */ |
||
| 593 | public function isRemoved(): bool |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Returns the last child of node. |
||
| 600 | * |
||
| 601 | * @return SimpleHtmlDomInterface|null |
||
| 602 | */ |
||
| 603 | 4 | View Code Duplication | public function lastChild() |
| 614 | |||
| 615 | /** |
||
| 616 | * Returns the next sibling of node. |
||
| 617 | * |
||
| 618 | * @return SimpleHtmlDomInterface|null |
||
| 619 | */ |
||
| 620 | 1 | View Code Duplication | public function nextSibling() |
| 631 | |||
| 632 | /** |
||
| 633 | * Returns the next sibling of node. |
||
| 634 | * |
||
| 635 | * @return SimpleHtmlDomInterface|null |
||
| 636 | */ |
||
| 637 | 1 | View Code Duplication | public function nextNonWhitespaceSibling() |
| 653 | |||
| 654 | /** |
||
| 655 | * Returns the parent of node. |
||
| 656 | * |
||
| 657 | * @return SimpleHtmlDomInterface |
||
| 658 | */ |
||
| 659 | 2 | public function parentNode(): SimpleHtmlDomInterface |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Returns the previous sibling of node. |
||
| 666 | * |
||
| 667 | * @return SimpleHtmlDomInterface|null |
||
| 668 | */ |
||
| 669 | 1 | View Code Duplication | public function previousSibling() |
| 680 | |||
| 681 | /** |
||
| 682 | * @param string|string[]|null $value <p> |
||
| 683 | * null === get the current input value |
||
| 684 | * text === set a new input value |
||
| 685 | * </p> |
||
| 686 | * |
||
| 687 | * @return string|string[]|null |
||
| 688 | */ |
||
| 689 | 1 | View Code Duplication | public function val($value = null) |
| 767 | |||
| 768 | /** |
||
| 769 | * @param HtmlDomParser $newDocument |
||
| 770 | * @param bool $removeExtraHeadTag |
||
| 771 | * |
||
| 772 | * @return HtmlDomParser |
||
| 773 | */ |
||
| 774 | 13 | protected function cleanHtmlWrapper( |
|
| 851 | |||
| 852 | /** |
||
| 853 | * Retrieve an external iterator. |
||
| 854 | * |
||
| 855 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 856 | * |
||
| 857 | * @return SimpleHtmlDomNode |
||
| 858 | * <p> |
||
| 859 | * An instance of an object implementing <b>Iterator</b> or |
||
| 860 | * <b>Traversable</b> |
||
| 861 | * </p> |
||
| 862 | */ |
||
| 863 | 3 | View Code Duplication | public function getIterator(): SimpleHtmlDomNodeInterface |
| 874 | |||
| 875 | /** |
||
| 876 | * Get dom node's inner html. |
||
| 877 | * |
||
| 878 | * @param bool $multiDecodeNewHtmlEntity |
||
| 879 | * |
||
| 880 | * @return string |
||
| 881 | */ |
||
| 882 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Normalize the given input for comparision. |
||
| 889 | * |
||
| 890 | * @param HtmlDomParser|string $input |
||
| 891 | * |
||
| 892 | * @return string |
||
| 893 | */ |
||
| 894 | 13 | private function normalizeStringForComparision($input): string |
|
| 930 | } |
||
| 931 |
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.