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 SimpleXmlDom 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 SimpleXmlDom, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class SimpleXmlDom extends AbstractSimpleXmlDom implements \IteratorAggregate, SimpleXmlDomInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @param \DOMElement|\DOMNode $node |
||
| 18 | */ |
||
| 19 | 1 | public function __construct(\DOMNode $node) |
|
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $name |
||
| 26 | * @param array $arguments |
||
| 27 | * |
||
| 28 | * @throws \BadMethodCallException |
||
| 29 | * |
||
| 30 | * @return SimpleXmlDomInterface|string|null |
||
| 31 | */ |
||
| 32 | 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 SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
|
|
|||
| 50 | */ |
||
| 51 | public function find(string $selector, $idx = null) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns an array of attributes. |
||
| 58 | * |
||
| 59 | * @return string[]|null |
||
| 60 | */ |
||
| 61 | 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 | 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 | public function hasAttribute(string $name): bool |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get dom node's inner html. |
||
| 123 | * |
||
| 124 | * @param bool $multiDecodeNewHtmlEntity |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Remove attribute. |
||
| 135 | * |
||
| 136 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 137 | * |
||
| 138 | * @return SimpleXmlDomInterface |
||
| 139 | */ |
||
| 140 | public function removeAttribute(string $name): SimpleXmlDomInterface |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Replace child node. |
||
| 151 | * |
||
| 152 | * @param string $string |
||
| 153 | * |
||
| 154 | * @return SimpleXmlDomInterface |
||
| 155 | */ |
||
| 156 | View Code Duplication | protected function replaceChildWithString(string $string): SimpleXmlDomInterface |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Replace this node. |
||
| 203 | * |
||
| 204 | * @param string $string |
||
| 205 | * |
||
| 206 | * @return SimpleXmlDomInterface |
||
| 207 | */ |
||
| 208 | protected function replaceNodeWithString(string $string): SimpleXmlDomInterface |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Replace this node with text |
||
| 249 | * |
||
| 250 | * @param string $string |
||
| 251 | * |
||
| 252 | * @return SimpleXmlDomInterface |
||
| 253 | */ |
||
| 254 | View Code Duplication | protected function replaceTextWithString($string): SimpleXmlDomInterface |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Set attribute value. |
||
| 277 | * |
||
| 278 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 279 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
| 280 | * @param bool $strict </p> |
||
| 281 | * $value must be NULL, to remove the attribute, |
||
| 282 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
| 283 | * </p> |
||
| 284 | * |
||
| 285 | * @return SimpleXmlDomInterface |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleXmlDomInterface |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Get dom node's plain text. |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function text(): string |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get dom node's outer html. |
||
| 316 | * |
||
| 317 | * @param bool $multiDecodeNewHtmlEntity |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function xml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Change the name of a tag in a "DOMNode". |
||
| 328 | * |
||
| 329 | * @param \DOMNode $node |
||
| 330 | * @param string $name |
||
| 331 | * |
||
| 332 | * @return \DOMElement|false |
||
| 333 | * <p>DOMElement a new instance of class DOMElement or false |
||
| 334 | * if an error occured.</p> |
||
| 335 | */ |
||
| 336 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Returns children of node. |
||
| 365 | * |
||
| 366 | * @param int $idx |
||
| 367 | * |
||
| 368 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|null |
||
| 369 | */ |
||
| 370 | View Code Duplication | public function childNodes(int $idx = -1) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Find nodes with a CSS selector. |
||
| 383 | * |
||
| 384 | * @param string $selector |
||
| 385 | * |
||
| 386 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 387 | */ |
||
| 388 | public function findMulti(string $selector): SimpleXmlDomNodeInterface |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Find nodes with a CSS selector. |
||
| 395 | * |
||
| 396 | * @param string $selector |
||
| 397 | * |
||
| 398 | * @return false|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 399 | */ |
||
| 400 | public function findMultiOrFalse(string $selector) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Find one node with a CSS selector. |
||
| 407 | * |
||
| 408 | * @param string $selector |
||
| 409 | * |
||
| 410 | * @return SimpleXmlDomInterface |
||
| 411 | */ |
||
| 412 | public function findOne(string $selector): SimpleXmlDomInterface |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Find one node with a CSS selector or false, if no element is found. |
||
| 419 | * |
||
| 420 | * @param string $selector |
||
| 421 | * |
||
| 422 | * @return false|SimpleXmlDomInterface |
||
| 423 | */ |
||
| 424 | public function findOneOrFalse(string $selector) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Returns the first child of node. |
||
| 431 | * |
||
| 432 | * @return SimpleXmlDomInterface|null |
||
| 433 | */ |
||
| 434 | View Code Duplication | public function firstChild() |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Return elements by ".class". |
||
| 448 | * |
||
| 449 | * @param string $class |
||
| 450 | * |
||
| 451 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 452 | */ |
||
| 453 | public function getElementByClass(string $class): SimpleXmlDomNodeInterface |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Return element by #id. |
||
| 460 | * |
||
| 461 | * @param string $id |
||
| 462 | * |
||
| 463 | * @return SimpleXmlDomInterface |
||
| 464 | */ |
||
| 465 | public function getElementById(string $id): SimpleXmlDomInterface |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Return element by tag name. |
||
| 472 | * |
||
| 473 | * @param string $name |
||
| 474 | * |
||
| 475 | * @return SimpleXmlDomInterface |
||
| 476 | */ |
||
| 477 | View Code Duplication | public function getElementByTagName(string $name): SimpleXmlDomInterface |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Returns elements by "#id". |
||
| 494 | * |
||
| 495 | * @param string $id |
||
| 496 | * @param int|null $idx |
||
| 497 | * |
||
| 498 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 499 | */ |
||
| 500 | public function getElementsById(string $id, $idx = null) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Returns elements by tag name. |
||
| 507 | * |
||
| 508 | * @param string $name |
||
| 509 | * @param int|null $idx |
||
| 510 | * |
||
| 511 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 512 | */ |
||
| 513 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * @return \DOMNode |
||
| 547 | */ |
||
| 548 | 1 | public function getNode(): \DOMNode |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Create a new "XmlDomParser"-object from the current context. |
||
| 555 | * |
||
| 556 | * @return XmlDomParser |
||
| 557 | */ |
||
| 558 | public function getXmlDomParser(): XmlDomParser |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Get dom node's inner html. |
||
| 565 | * |
||
| 566 | * @param bool $multiDecodeNewHtmlEntity |
||
| 567 | * |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Nodes can get partially destroyed in which they're still an |
||
| 577 | * actual DOM node (such as \DOMElement) but almost their entire |
||
| 578 | * body is gone, including the `nodeType` attribute. |
||
| 579 | * |
||
| 580 | * @return bool true if node has been destroyed |
||
| 581 | */ |
||
| 582 | public function isRemoved(): bool |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Returns the last child of node. |
||
| 589 | * |
||
| 590 | * @return SimpleXmlDomInterface|null |
||
| 591 | */ |
||
| 592 | View Code Duplication | public function lastChild() |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Returns the next sibling of node. |
||
| 606 | * |
||
| 607 | * @return SimpleXmlDomInterface|null |
||
| 608 | */ |
||
| 609 | View Code Duplication | public function nextSibling() |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Returns the next sibling of node. |
||
| 623 | * |
||
| 624 | * @return SimpleXmlDomInterface|null |
||
| 625 | */ |
||
| 626 | View Code Duplication | public function nextNonWhitespaceSibling() |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Returns the parent of node. |
||
| 645 | * |
||
| 646 | * @return SimpleXmlDomInterface |
||
| 647 | */ |
||
| 648 | public function parentNode(): SimpleXmlDomInterface |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Returns the previous sibling of node. |
||
| 655 | * |
||
| 656 | * @return SimpleXmlDomInterface|null |
||
| 657 | */ |
||
| 658 | View Code Duplication | public function previousSibling() |
|
| 669 | |||
| 670 | /** |
||
| 671 | * @param string|string[]|null $value <p> |
||
| 672 | * null === get the current input value |
||
| 673 | * text === set a new input value |
||
| 674 | * </p> |
||
| 675 | * |
||
| 676 | * @return string|string[]|null |
||
| 677 | */ |
||
| 678 | View Code Duplication | public function val($value = null) |
|
| 756 | |||
| 757 | /** |
||
| 758 | * Retrieve an external iterator. |
||
| 759 | * |
||
| 760 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 761 | * |
||
| 762 | * @return SimpleXmlDomNode |
||
| 763 | * <p> |
||
| 764 | * An instance of an object implementing <b>Iterator</b> or |
||
| 765 | * <b>Traversable</b> |
||
| 766 | * </p> |
||
| 767 | */ |
||
| 768 | View Code Duplication | public function getIterator(): SimpleXmlDomNodeInterface |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Normalize the given input for comparision. |
||
| 782 | * |
||
| 783 | * @param string|XmlDomParser $input |
||
| 784 | * |
||
| 785 | * @return string |
||
| 786 | */ |
||
| 787 | private function normalizeStringForComparision($input): string |
||
| 818 | } |
||
| 819 |
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.