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 |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Replace this node with text |
||
| 247 | * |
||
| 248 | * @param string $string |
||
| 249 | * |
||
| 250 | * @return SimpleXmlDomInterface |
||
| 251 | */ |
||
| 252 | View Code Duplication | protected function replaceTextWithString($string): SimpleXmlDomInterface |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Set attribute value. |
||
| 273 | * |
||
| 274 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 275 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
| 276 | * @param bool $strict </p> |
||
| 277 | * $value must be NULL, to remove the attribute, |
||
| 278 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
| 279 | * </p> |
||
| 280 | * |
||
| 281 | * @return SimpleXmlDomInterface |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleXmlDomInterface |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get dom node's plain text. |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function text(): string |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get dom node's outer html. |
||
| 312 | * |
||
| 313 | * @param bool $multiDecodeNewHtmlEntity |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public function xml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Change the name of a tag in a "DOMNode". |
||
| 324 | * |
||
| 325 | * @param \DOMNode $node |
||
| 326 | * @param string $name |
||
| 327 | * |
||
| 328 | * @return \DOMElement|false |
||
| 329 | * <p>DOMElement a new instance of class DOMElement or false |
||
| 330 | * if an error occured.</p> |
||
| 331 | */ |
||
| 332 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Returns children of node. |
||
| 362 | * |
||
| 363 | * @param int $idx |
||
| 364 | * |
||
| 365 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|null |
||
| 366 | */ |
||
| 367 | View Code Duplication | public function childNodes(int $idx = -1) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Find nodes with a CSS selector. |
||
| 380 | * |
||
| 381 | * @param string $selector |
||
| 382 | * |
||
| 383 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 384 | */ |
||
| 385 | public function findMulti(string $selector): SimpleXmlDomNodeInterface |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Find nodes with a CSS selector. |
||
| 392 | * |
||
| 393 | * @param string $selector |
||
| 394 | * |
||
| 395 | * @return false|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 396 | */ |
||
| 397 | public function findMultiOrFalse(string $selector) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Find one node with a CSS selector. |
||
| 404 | * |
||
| 405 | * @param string $selector |
||
| 406 | * |
||
| 407 | * @return SimpleXmlDomInterface |
||
| 408 | */ |
||
| 409 | public function findOne(string $selector): SimpleXmlDomInterface |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Find one node with a CSS selector or false, if no element is found. |
||
| 416 | * |
||
| 417 | * @param string $selector |
||
| 418 | * |
||
| 419 | * @return false|SimpleXmlDomInterface |
||
| 420 | */ |
||
| 421 | public function findOneOrFalse(string $selector) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Returns the first child of node. |
||
| 428 | * |
||
| 429 | * @return SimpleXmlDomInterface|null |
||
| 430 | */ |
||
| 431 | View Code Duplication | public function firstChild() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Return elements by ".class". |
||
| 445 | * |
||
| 446 | * @param string $class |
||
| 447 | * |
||
| 448 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 449 | */ |
||
| 450 | public function getElementByClass(string $class): SimpleXmlDomNodeInterface |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Return element by #id. |
||
| 457 | * |
||
| 458 | * @param string $id |
||
| 459 | * |
||
| 460 | * @return SimpleXmlDomInterface |
||
| 461 | */ |
||
| 462 | public function getElementById(string $id): SimpleXmlDomInterface |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Return element by tag name. |
||
| 469 | * |
||
| 470 | * @param string $name |
||
| 471 | * |
||
| 472 | * @return SimpleXmlDomInterface |
||
| 473 | */ |
||
| 474 | View Code Duplication | public function getElementByTagName(string $name): SimpleXmlDomInterface |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Returns elements by "#id". |
||
| 491 | * |
||
| 492 | * @param string $id |
||
| 493 | * @param int|null $idx |
||
| 494 | * |
||
| 495 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 496 | */ |
||
| 497 | public function getElementsById(string $id, $idx = null) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Returns elements by tag name. |
||
| 504 | * |
||
| 505 | * @param string $name |
||
| 506 | * @param int|null $idx |
||
| 507 | * |
||
| 508 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 509 | */ |
||
| 510 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * @return \DOMNode |
||
| 544 | */ |
||
| 545 | 1 | public function getNode(): \DOMNode |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Create a new "XmlDomParser"-object from the current context. |
||
| 552 | * |
||
| 553 | * @return XmlDomParser |
||
| 554 | */ |
||
| 555 | public function getXmlDomParser(): XmlDomParser |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Get dom node's inner html. |
||
| 562 | * |
||
| 563 | * @param bool $multiDecodeNewHtmlEntity |
||
| 564 | * |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Nodes can get partially destroyed in which they're still an |
||
| 574 | * actual DOM node (such as \DOMElement) but almost their entire |
||
| 575 | * body is gone, including the `nodeType` attribute. |
||
| 576 | * |
||
| 577 | * @return bool true if node has been destroyed |
||
| 578 | */ |
||
| 579 | public function isRemoved(): bool |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Returns the last child of node. |
||
| 586 | * |
||
| 587 | * @return SimpleXmlDomInterface|null |
||
| 588 | */ |
||
| 589 | View Code Duplication | public function lastChild() |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Returns the next sibling of node. |
||
| 603 | * |
||
| 604 | * @return SimpleXmlDomInterface|null |
||
| 605 | */ |
||
| 606 | View Code Duplication | public function nextSibling() |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Returns the next sibling of node. |
||
| 620 | * |
||
| 621 | * @return SimpleXmlDomInterface|null |
||
| 622 | */ |
||
| 623 | View Code Duplication | public function nextNonWhitespaceSibling() |
|
| 639 | |||
| 640 | /** |
||
| 641 | * Returns the parent of node. |
||
| 642 | * |
||
| 643 | * @return SimpleXmlDomInterface |
||
| 644 | */ |
||
| 645 | public function parentNode(): SimpleXmlDomInterface |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Returns the previous sibling of node. |
||
| 652 | * |
||
| 653 | * @return SimpleXmlDomInterface|null |
||
| 654 | */ |
||
| 655 | View Code Duplication | public function previousSibling() |
|
| 666 | |||
| 667 | /** |
||
| 668 | * @param string|string[]|null $value <p> |
||
| 669 | * null === get the current input value |
||
| 670 | * text === set a new input value |
||
| 671 | * </p> |
||
| 672 | * |
||
| 673 | * @return string|string[]|null |
||
| 674 | */ |
||
| 675 | View Code Duplication | public function val($value = null) |
|
| 753 | |||
| 754 | /** |
||
| 755 | * Retrieve an external iterator. |
||
| 756 | * |
||
| 757 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 758 | * |
||
| 759 | * @return SimpleXmlDomNode |
||
| 760 | * <p> |
||
| 761 | * An instance of an object implementing <b>Iterator</b> or |
||
| 762 | * <b>Traversable</b> |
||
| 763 | * </p> |
||
| 764 | */ |
||
| 765 | View Code Duplication | public function getIterator(): SimpleXmlDomNodeInterface |
|
| 776 | |||
| 777 | /** |
||
| 778 | * Normalize the given input for comparision. |
||
| 779 | * |
||
| 780 | * @param string|XmlDomParser $input |
||
| 781 | * |
||
| 782 | * @return string |
||
| 783 | */ |
||
| 784 | private function normalizeStringForComparision($input): string |
||
| 815 | } |
||
| 816 |
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.