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 |
||
| 33 | class SimpleHtmlDom implements \IteratorAggregate |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected static $functionAliases = [ |
||
| 39 | 'children' => 'childNodes', |
||
| 40 | 'first_child' => 'firstChild', |
||
| 41 | 'last_child' => 'lastChild', |
||
| 42 | 'next_sibling' => 'nextSibling', |
||
| 43 | 'prev_sibling' => 'previousSibling', |
||
| 44 | 'parent' => 'parentNode', |
||
| 45 | 'outertext' => 'html', |
||
| 46 | 'outerhtml' => 'html', |
||
| 47 | 'innertext' => 'innerHtml', |
||
| 48 | 'innerhtml' => 'innerHtml', |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var DOMElement |
||
| 53 | */ |
||
| 54 | protected $node; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * SimpleHtmlDom constructor. |
||
| 58 | * |
||
| 59 | * @param DOMNode $node |
||
| 60 | */ |
||
| 61 | 102 | public function __construct(DOMNode $node) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $name |
||
| 68 | * @param array $arguments |
||
| 69 | * |
||
| 70 | * @throws \BadMethodCallException |
||
| 71 | * |
||
| 72 | * @return SimpleHtmlDom|string|null |
||
| 73 | */ |
||
| 74 | 9 | View Code Duplication | public function __call($name, $arguments) |
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $name |
||
| 87 | * |
||
| 88 | * @return array|string|null |
||
| 89 | */ |
||
| 90 | 46 | public function __get($name) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $selector |
||
| 121 | * @param int $idx |
||
| 122 | * |
||
| 123 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
| 124 | */ |
||
| 125 | 12 | public function __invoke($selector, $idx = null) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param $name |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | 1 | public function __isset($name) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param $name |
||
| 160 | * @param $value |
||
| 161 | * |
||
| 162 | * @return SimpleHtmlDom |
||
| 163 | */ |
||
| 164 | 16 | public function __set($name, $value) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | 2 | public function __toString() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param $name |
||
| 197 | * |
||
| 198 | * @return SimpleHtmlDom |
||
| 199 | */ |
||
| 200 | public function __unset($name) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns children of node. |
||
| 207 | * |
||
| 208 | * @param int $idx |
||
| 209 | * |
||
| 210 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNode|null |
||
| 211 | */ |
||
| 212 | 2 | public function childNodes(int $idx = -1) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Find list of nodes with a CSS selector. |
||
| 225 | * |
||
| 226 | * @param string $selector |
||
| 227 | * @param int|null $idx |
||
| 228 | * |
||
| 229 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
| 230 | */ |
||
| 231 | 26 | public function find(string $selector, $idx = null) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Find one node with a CSS selector. |
||
| 238 | * |
||
| 239 | * @param string $selector |
||
| 240 | * |
||
| 241 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
| 242 | */ |
||
| 243 | public function findOne(string $selector) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns the first child of node. |
||
| 250 | * |
||
| 251 | * @return SimpleHtmlDom|null |
||
| 252 | */ |
||
| 253 | 4 | public function firstChild() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Returns an array of attributes. |
||
| 266 | * |
||
| 267 | * @return array|null |
||
| 268 | */ |
||
| 269 | 2 | public function getAllAttributes() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Return attribute value. |
||
| 285 | * |
||
| 286 | * @param string $name |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | 14 | public function getAttribute(string $name): string |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Return element by #id. |
||
| 299 | * |
||
| 300 | * @param string $id |
||
| 301 | * |
||
| 302 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
| 303 | */ |
||
| 304 | 1 | public function getElementById(string $id) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Return element by tag name. |
||
| 311 | * |
||
| 312 | * @param string $name |
||
| 313 | * |
||
| 314 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
| 315 | */ |
||
| 316 | public function getElementByTagName(string $name) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns elements by #id. |
||
| 329 | * |
||
| 330 | * @param string $id |
||
| 331 | * @param int|null $idx |
||
| 332 | * |
||
| 333 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
| 334 | */ |
||
| 335 | public function getElementsById(string $id, $idx = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns elements by tag name. |
||
| 342 | * |
||
| 343 | * @param string $name |
||
| 344 | * @param int|null $idx |
||
| 345 | * |
||
| 346 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNode|SimpleHtmlDomNodeBlank |
||
| 347 | */ |
||
| 348 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Create a new "HtmlDomParser"-object from the current context. |
||
| 374 | * |
||
| 375 | * @return HtmlDomParser |
||
| 376 | */ |
||
| 377 | public function getHtmlDomParser(): HtmlDomParser |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Retrieve an external iterator. |
||
| 384 | * |
||
| 385 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 386 | * |
||
| 387 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
| 388 | * <b>Traversable</b> |
||
| 389 | */ |
||
| 390 | public function getIterator(): SimpleHtmlDomNode |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return DOMNode |
||
| 404 | */ |
||
| 405 | public function getNode(): \DOMNode |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Determine if an attribute exists on the element. |
||
| 412 | * |
||
| 413 | * @param string $name |
||
| 414 | * |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | public function hasAttribute(string $name): bool |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Get dom node's outer html. |
||
| 424 | * |
||
| 425 | * @param bool $multiDecodeNewHtmlEntity |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get dom node's inner html. |
||
| 436 | * |
||
| 437 | * @param bool $multiDecodeNewHtmlEntity |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Returns the last child of node. |
||
| 448 | * |
||
| 449 | * @return SimpleHtmlDom|null |
||
| 450 | */ |
||
| 451 | public function lastChild() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Returns the next sibling of node. |
||
| 464 | * |
||
| 465 | * @return SimpleHtmlDom|null |
||
| 466 | */ |
||
| 467 | public function nextSibling() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Returns the parent of node. |
||
| 480 | * |
||
| 481 | * @return SimpleHtmlDom |
||
| 482 | */ |
||
| 483 | public function parentNode(): self |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Returns the previous sibling of node. |
||
| 490 | * |
||
| 491 | * @return SimpleHtmlDom|null |
||
| 492 | */ |
||
| 493 | public function previousSibling() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Replace child node. |
||
| 506 | * |
||
| 507 | * @param string $string |
||
| 508 | * |
||
| 509 | * @throws \RuntimeException |
||
| 510 | * |
||
| 511 | * @return $this |
||
| 512 | */ |
||
| 513 | protected function replaceChildWithString(string $string): self |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Replace this node with text |
||
| 548 | * |
||
| 549 | * @param string $string |
||
| 550 | * |
||
| 551 | * @return $this|null |
||
| 552 | */ |
||
| 553 | protected function replaceTextWithString($string) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Replace this node. |
||
| 571 | * |
||
| 572 | * @param string $string |
||
| 573 | * |
||
| 574 | * @throws \RuntimeException |
||
| 575 | * |
||
| 576 | * @return $this|null |
||
| 577 | */ |
||
| 578 | protected function replaceNodeWithString(string $string) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Normalize the given input for comparision. |
||
| 627 | * |
||
| 628 | * @param HtmlDomParser|string $input |
||
| 629 | * |
||
| 630 | * @return string |
||
| 631 | */ |
||
| 632 | private function normalizeStringForComparision($input): string |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @param HtmlDomParser $newDocument |
||
| 671 | * @param bool $removeExtraHeadTag |
||
| 672 | * |
||
| 673 | * @return HtmlDomParser |
||
| 674 | */ |
||
| 675 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument, $removeExtraHeadTag = false): HtmlDomParser |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Change the name of a tag in a "DOMNode". |
||
| 748 | * |
||
| 749 | * @param DOMNode $node |
||
| 750 | * @param string $name |
||
| 751 | * |
||
| 752 | * @return DOMElement |
||
| 753 | */ |
||
| 754 | protected function changeElementName(\DOMNode $node, string $name): \DOMElement |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Set attribute value. |
||
| 777 | * |
||
| 778 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 779 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
| 780 | * @param bool $strict </p> |
||
| 781 | * $value must be NULL, to remove the attribute, |
||
| 782 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
| 783 | * </p> |
||
| 784 | * |
||
| 785 | * @return $this |
||
| 786 | */ |
||
| 787 | public function setAttribute(string $name, $value = null, bool $strict = false): self |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @param string|string[]|null $value <p> |
||
| 805 | * null === get the current input value |
||
| 806 | * text === set a new input value |
||
| 807 | * </p> |
||
| 808 | * |
||
| 809 | * @return string|string[]|null |
||
| 810 | */ |
||
| 811 | public function val($value = null) |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Remove attribute. |
||
| 884 | * |
||
| 885 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 886 | * |
||
| 887 | * @return mixed |
||
| 888 | */ |
||
| 889 | public function removeAttribute(string $name) |
||
| 895 | |||
| 896 | /** |
||
| 897 | * Get dom node's plain text. |
||
| 898 | * |
||
| 899 | * @return string |
||
| 900 | */ |
||
| 901 | public function text(): string |
||
| 905 | } |
||
| 906 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.