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 | 154 | 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() |
| 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 | 25 | 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 | 33 | 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 | 22 | 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 |
|
| 218 | { |
||
| 219 | 6 | if (empty($string)) { |
|
| 220 | 2 | $this->node->parentNode->removeChild($this->node); |
|
| 221 | |||
| 222 | 2 | return $this; |
|
| 223 | } |
||
| 224 | |||
| 225 | 5 | $newDocument = new HtmlDomParser($string); |
|
| 226 | |||
| 227 | 5 | $tmpDomOuterTextString = $this->normalizeStringForComparision($newDocument); |
|
| 228 | 5 | $tmpStr = $this->normalizeStringForComparision($string); |
|
| 229 | 5 | View Code Duplication | if ($tmpDomOuterTextString !== $tmpStr) { |
| 230 | throw new \RuntimeException( |
||
| 231 | 'Not valid HTML fragment!' . "\n" |
||
| 232 | . $tmpDomOuterTextString . "\n" . |
||
| 233 | $tmpStr |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | |||
| 237 | 5 | $newDocument = $this->cleanHtmlWrapper($newDocument, true); |
|
| 238 | 5 | $ownerDocument = $this->node->ownerDocument; |
|
| 239 | if ( |
||
| 240 | 5 | $ownerDocument === null |
|
| 241 | || |
||
| 242 | 5 | $newDocument->getDocument()->documentElement === null |
|
| 243 | ) { |
||
| 244 | return $this; |
||
| 245 | } |
||
| 246 | |||
| 247 | 5 | $newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
|
| 248 | |||
| 249 | 5 | $this->node->parentNode->replaceChild($newNode, $this->node); |
|
| 250 | 5 | $this->node = $newNode; |
|
| 251 | |||
| 252 | // Remove head element, preserving child nodes. (again) |
||
| 253 | View Code Duplication | if ( |
|
| 254 | 5 | $this->node->parentNode instanceof \DOMElement |
|
| 255 | && |
||
| 256 | 5 | $newDocument->getIsDOMDocumentCreatedWithoutHeadWrapper() |
|
| 257 | ) { |
||
| 258 | 3 | $html = $this->node->parentNode->getElementsByTagName('head')[0]; |
|
| 259 | |||
| 260 | 3 | if ($this->node->parentNode->ownerDocument !== null) { |
|
| 261 | 3 | $fragment = $this->node->parentNode->ownerDocument->createDocumentFragment(); |
|
| 262 | 3 | if ($html !== null) { |
|
| 263 | /** @var \DOMNode $html */ |
||
| 264 | 1 | while ($html->childNodes->length > 0) { |
|
| 265 | 1 | $tmpNode = $html->childNodes->item(0); |
|
| 266 | 1 | if ($tmpNode !== null) { |
|
| 267 | /** @noinspection UnusedFunctionResultInspection */ |
||
| 268 | 1 | $fragment->appendChild($tmpNode); |
|
| 269 | } |
||
| 270 | } |
||
| 271 | /** @noinspection UnusedFunctionResultInspection */ |
||
| 272 | 1 | $html->parentNode->replaceChild($fragment, $html); |
|
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | 5 | return $this; |
|
| 278 | } |
||
| 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 | 15 | 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 | 28 | 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 | 3 | 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 | 1 | 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 | 1 | 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 | 1 | 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 | 97 | public function getHtmlDomParser(): HtmlDomParser |
|
| 572 | |||
| 573 | /** |
||
| 574 | * @return \DOMNode |
||
| 575 | */ |
||
| 576 | 98 | 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 | 4 | View Code Duplication | public function lastChild() |
| 609 | |||
| 610 | /** |
||
| 611 | * Returns the next sibling of node. |
||
| 612 | * |
||
| 613 | * @return SimpleHtmlDomInterface|null |
||
| 614 | */ |
||
| 615 | 1 | View Code Duplication | public function nextSibling() |
| 626 | |||
| 627 | /** |
||
| 628 | * Returns the next sibling of node. |
||
| 629 | * |
||
| 630 | * @return SimpleHtmlDomInterface|null |
||
| 631 | */ |
||
| 632 | 1 | View Code Duplication | public function nextNonWhitespaceSibling() |
| 648 | |||
| 649 | /** |
||
| 650 | * Returns the parent of node. |
||
| 651 | * |
||
| 652 | * @return SimpleHtmlDomInterface |
||
| 653 | */ |
||
| 654 | 2 | public function parentNode(): SimpleHtmlDomInterface |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Returns the previous sibling of node. |
||
| 661 | * |
||
| 662 | * @return SimpleHtmlDomInterface|null |
||
| 663 | */ |
||
| 664 | 1 | 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 | 1 | View Code Duplication | public function val($value = null) |
| 762 | |||
| 763 | /** |
||
| 764 | * @param HtmlDomParser $newDocument |
||
| 765 | * @param bool $removeExtraHeadTag |
||
| 766 | * |
||
| 767 | * @return HtmlDomParser |
||
| 768 | */ |
||
| 769 | 12 | protected function cleanHtmlWrapper( |
|
| 840 | |||
| 841 | /** |
||
| 842 | * Retrieve an external iterator. |
||
| 843 | * |
||
| 844 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 845 | * |
||
| 846 | * @return SimpleHtmlDomNode |
||
| 847 | * <p> |
||
| 848 | * An instance of an object implementing <b>Iterator</b> or |
||
| 849 | * <b>Traversable</b> |
||
| 850 | * </p> |
||
| 851 | */ |
||
| 852 | 3 | View Code Duplication | public function getIterator(): SimpleHtmlDomNodeInterface |
| 863 | |||
| 864 | /** |
||
| 865 | * Get dom node's inner html. |
||
| 866 | * |
||
| 867 | * @param bool $multiDecodeNewHtmlEntity |
||
| 868 | * |
||
| 869 | * @return string |
||
| 870 | */ |
||
| 871 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Normalize the given input for comparision. |
||
| 878 | * |
||
| 879 | * @param HtmlDomParser|string $input |
||
| 880 | * |
||
| 881 | * @return string |
||
| 882 | */ |
||
| 883 | 12 | private function normalizeStringForComparision($input): string |
|
| 919 | } |
||
| 920 |
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.