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() |
|
| 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 | 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 | public function hasAttribute(string $name): bool |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get dom node's inner html. |
||
| 119 | * |
||
| 120 | * @param bool $multiDecodeNewHtmlEntity |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Remove attribute. |
||
| 131 | * |
||
| 132 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 133 | * |
||
| 134 | * @return SimpleXmlDomInterface |
||
| 135 | */ |
||
| 136 | public function removeAttribute(string $name): SimpleXmlDomInterface |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Replace child node. |
||
| 147 | * |
||
| 148 | * @param string $string |
||
| 149 | * |
||
| 150 | * @return SimpleXmlDomInterface |
||
| 151 | */ |
||
| 152 | View Code Duplication | protected function replaceChildWithString(string $string): SimpleXmlDomInterface |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Replace this node. |
||
| 199 | * |
||
| 200 | * @param string $string |
||
| 201 | * |
||
| 202 | * @return SimpleXmlDomInterface |
||
| 203 | */ |
||
| 204 | protected function replaceNodeWithString(string $string): SimpleXmlDomInterface |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Replace this node with text |
||
| 243 | * |
||
| 244 | * @param string $string |
||
| 245 | * |
||
| 246 | * @return SimpleXmlDomInterface |
||
| 247 | */ |
||
| 248 | View Code Duplication | protected function replaceTextWithString($string): SimpleXmlDomInterface |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Set attribute value. |
||
| 269 | * |
||
| 270 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 271 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
| 272 | * @param bool $strict </p> |
||
| 273 | * $value must be NULL, to remove the attribute, |
||
| 274 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
| 275 | * </p> |
||
| 276 | * |
||
| 277 | * @return SimpleXmlDomInterface |
||
| 278 | */ |
||
| 279 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleXmlDomInterface |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get dom node's plain text. |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | public function text(): string |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get dom node's outer html. |
||
| 308 | * |
||
| 309 | * @param bool $multiDecodeNewHtmlEntity |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | public function xml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Change the name of a tag in a "DOMNode". |
||
| 320 | * |
||
| 321 | * @param \DOMNode $node |
||
| 322 | * @param string $name |
||
| 323 | * |
||
| 324 | * @return \DOMElement|false |
||
| 325 | * <p>DOMElement a new instance of class DOMElement or false |
||
| 326 | * if an error occured.</p> |
||
| 327 | */ |
||
| 328 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Returns children of node. |
||
| 356 | * |
||
| 357 | * @param int $idx |
||
| 358 | * |
||
| 359 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|null |
||
| 360 | */ |
||
| 361 | View Code Duplication | public function childNodes(int $idx = -1) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Find nodes with a CSS selector. |
||
| 374 | * |
||
| 375 | * @param string $selector |
||
| 376 | * |
||
| 377 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 378 | */ |
||
| 379 | public function findMulti(string $selector): SimpleXmlDomNodeInterface |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Find nodes with a CSS selector. |
||
| 386 | * |
||
| 387 | * @param string $selector |
||
| 388 | * |
||
| 389 | * @return false|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 390 | */ |
||
| 391 | public function findMultiOrFalse(string $selector) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Find one node with a CSS selector. |
||
| 398 | * |
||
| 399 | * @param string $selector |
||
| 400 | * |
||
| 401 | * @return SimpleXmlDomInterface |
||
| 402 | */ |
||
| 403 | public function findOne(string $selector): SimpleXmlDomInterface |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Find one node with a CSS selector or false, if no element is found. |
||
| 410 | * |
||
| 411 | * @param string $selector |
||
| 412 | * |
||
| 413 | * @return false|SimpleXmlDomInterface |
||
| 414 | */ |
||
| 415 | public function findOneOrFalse(string $selector) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Returns the first child of node. |
||
| 422 | * |
||
| 423 | * @return SimpleXmlDomInterface|null |
||
| 424 | */ |
||
| 425 | View Code Duplication | public function firstChild() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Return elements by ".class". |
||
| 439 | * |
||
| 440 | * @param string $class |
||
| 441 | * |
||
| 442 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 443 | */ |
||
| 444 | public function getElementByClass(string $class): SimpleXmlDomNodeInterface |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Return element by #id. |
||
| 451 | * |
||
| 452 | * @param string $id |
||
| 453 | * |
||
| 454 | * @return SimpleXmlDomInterface |
||
| 455 | */ |
||
| 456 | public function getElementById(string $id): SimpleXmlDomInterface |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Return element by tag name. |
||
| 463 | * |
||
| 464 | * @param string $name |
||
| 465 | * |
||
| 466 | * @return SimpleXmlDomInterface |
||
| 467 | */ |
||
| 468 | View Code Duplication | public function getElementByTagName(string $name): SimpleXmlDomInterface |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Returns elements by "#id". |
||
| 485 | * |
||
| 486 | * @param string $id |
||
| 487 | * @param int|null $idx |
||
| 488 | * |
||
| 489 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 490 | */ |
||
| 491 | public function getElementsById(string $id, $idx = null) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Returns elements by tag name. |
||
| 498 | * |
||
| 499 | * @param string $name |
||
| 500 | * @param int|null $idx |
||
| 501 | * |
||
| 502 | * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
| 503 | */ |
||
| 504 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * @return \DOMNode |
||
| 538 | */ |
||
| 539 | 1 | public function getNode(): \DOMNode |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Create a new "XmlDomParser"-object from the current context. |
||
| 546 | * |
||
| 547 | * @return XmlDomParser |
||
| 548 | */ |
||
| 549 | public function getXmlDomParser(): XmlDomParser |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Get dom node's inner html. |
||
| 556 | * |
||
| 557 | * @param bool $multiDecodeNewHtmlEntity |
||
| 558 | * |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Nodes can get partially destroyed in which they're still an |
||
| 568 | * actual DOM node (such as \DOMElement) but almost their entire |
||
| 569 | * body is gone, including the `nodeType` attribute. |
||
| 570 | * |
||
| 571 | * @return bool true if node has been destroyed |
||
| 572 | */ |
||
| 573 | public function isRemoved(): bool |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Returns the last child of node. |
||
| 580 | * |
||
| 581 | * @return SimpleXmlDomInterface|null |
||
| 582 | */ |
||
| 583 | View Code Duplication | public function lastChild() |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Returns the next sibling of node. |
||
| 597 | * |
||
| 598 | * @return SimpleXmlDomInterface|null |
||
| 599 | */ |
||
| 600 | View Code Duplication | public function nextSibling() |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Returns the next sibling of node. |
||
| 614 | * |
||
| 615 | * @return SimpleXmlDomInterface|null |
||
| 616 | */ |
||
| 617 | View Code Duplication | public function nextNonWhitespaceSibling() |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Returns the parent of node. |
||
| 636 | * |
||
| 637 | * @return SimpleXmlDomInterface |
||
| 638 | */ |
||
| 639 | public function parentNode(): SimpleXmlDomInterface |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Returns the previous sibling of node. |
||
| 646 | * |
||
| 647 | * @return SimpleXmlDomInterface|null |
||
| 648 | */ |
||
| 649 | View Code Duplication | public function previousSibling() |
|
| 660 | |||
| 661 | /** |
||
| 662 | * @param string|string[]|null $value <p> |
||
| 663 | * null === get the current input value |
||
| 664 | * text === set a new input value |
||
| 665 | * </p> |
||
| 666 | * |
||
| 667 | * @return string|string[]|null |
||
| 668 | */ |
||
| 669 | View Code Duplication | public function val($value = null) |
|
| 745 | |||
| 746 | /** |
||
| 747 | * Retrieve an external iterator. |
||
| 748 | * |
||
| 749 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 750 | * |
||
| 751 | * @return SimpleXmlDomNode |
||
| 752 | * <p> |
||
| 753 | * An instance of an object implementing <b>Iterator</b> or |
||
| 754 | * <b>Traversable</b> |
||
| 755 | * </p> |
||
| 756 | */ |
||
| 757 | View Code Duplication | public function getIterator(): SimpleXmlDomNodeInterface |
|
| 768 | |||
| 769 | /** |
||
| 770 | * Normalize the given input for comparision. |
||
| 771 | * |
||
| 772 | * @param string|XmlDomParser $input |
||
| 773 | * |
||
| 774 | * @return string |
||
| 775 | */ |
||
| 776 | private function normalizeStringForComparision($input): string |
||
| 807 | } |
||
| 808 |
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.