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 |
||
| 38 | class SimpleHtmlDom implements \IteratorAggregate |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected static $functionAliases = [ |
||
| 44 | 'children' => 'childNodes', |
||
| 45 | 'first_child' => 'firstChild', |
||
| 46 | 'last_child' => 'lastChild', |
||
| 47 | 'next_sibling' => 'nextSibling', |
||
| 48 | 'prev_sibling' => 'previousSibling', |
||
| 49 | 'parent' => 'parentNode', |
||
| 50 | 'outertext' => 'html', |
||
| 51 | 'outerhtml' => 'html', |
||
| 52 | 'innertext' => 'innerHtml', |
||
| 53 | 'innerhtml' => 'innerHtml', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var DOMElement |
||
| 58 | */ |
||
| 59 | protected $node; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * SimpleHtmlDom constructor. |
||
| 63 | * |
||
| 64 | * @param DOMNode $node |
||
| 65 | */ |
||
| 66 | 99 | public function __construct(DOMNode $node) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $name |
||
| 73 | * @param array $arguments |
||
| 74 | * |
||
| 75 | * @return null|string|SimpleHtmlDom |
||
| 76 | * |
||
| 77 | * @throws \BadMethodCallException |
||
| 78 | */ |
||
| 79 | 9 | View Code Duplication | public function __call($name, $arguments) |
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $name |
||
| 92 | * |
||
| 93 | * @return array|null|string |
||
| 94 | */ |
||
| 95 | 44 | public function __get($name) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $selector |
||
| 120 | * @param int $idx |
||
| 121 | * |
||
| 122 | * @return SimpleHtmlDom[]|SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
| 123 | */ |
||
| 124 | 12 | public function __invoke($selector, $idx = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param $name |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | 1 | public function __isset($name) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param $name |
||
| 154 | * @param $value |
||
| 155 | * |
||
| 156 | * @return SimpleHtmlDom |
||
| 157 | */ |
||
| 158 | 14 | public function __set($name, $value) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 2 | public function __toString() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param $name |
||
| 184 | * |
||
| 185 | * @return SimpleHtmlDom |
||
| 186 | */ |
||
| 187 | 1 | public function __unset($name) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Returns children of node. |
||
| 194 | * |
||
| 195 | * @param int $idx |
||
| 196 | * |
||
| 197 | * @return SimpleHtmlDomNode|SimpleHtmlDom[]|SimpleHtmlDom|null |
||
| 198 | */ |
||
| 199 | 2 | public function childNodes(int $idx = -1) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Find list of nodes with a CSS selector. |
||
| 216 | * |
||
| 217 | * @param string $selector |
||
| 218 | * @param int|null $idx |
||
| 219 | * |
||
| 220 | * @return SimpleHtmlDom[]|SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
| 221 | */ |
||
| 222 | 26 | public function find(string $selector, $idx = null) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Find one node with a CSS selector. |
||
| 229 | * |
||
| 230 | * @param string $selector |
||
| 231 | * |
||
| 232 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
| 233 | */ |
||
| 234 | public function findOne(string $selector) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns the first child of node. |
||
| 241 | * |
||
| 242 | * @return SimpleHtmlDom|null |
||
| 243 | */ |
||
| 244 | 4 | public function firstChild() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Returns an array of attributes. |
||
| 257 | * |
||
| 258 | * @return array|null |
||
| 259 | */ |
||
| 260 | 2 | public function getAllAttributes() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Return attribute value. |
||
| 276 | * |
||
| 277 | * @param string $name |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | 13 | public function getAttribute(string $name): string |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Return element by #id. |
||
| 290 | * |
||
| 291 | * @param string $id |
||
| 292 | * |
||
| 293 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
| 294 | */ |
||
| 295 | 1 | public function getElementById(string $id) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Return element by tag name. |
||
| 302 | * |
||
| 303 | * @param string $name |
||
| 304 | * |
||
| 305 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
| 306 | */ |
||
| 307 | 1 | public function getElementByTagName(string $name) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Returns elements by #id. |
||
| 320 | * |
||
| 321 | * @param string $id |
||
| 322 | * @param null|int $idx |
||
| 323 | * |
||
| 324 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
| 325 | */ |
||
| 326 | public function getElementsById(string $id, $idx = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Returns elements by tag name. |
||
| 333 | * |
||
| 334 | * @param string $name |
||
| 335 | * @param null|int $idx |
||
| 336 | * |
||
| 337 | * @return SimpleHtmlDomNode|SimpleHtmlDom[]|SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
| 338 | */ |
||
| 339 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
| 367 | |||
| 368 | /** |
||
| 369 | * Create a new "HtmlDomParser"-object from the current context. |
||
| 370 | * |
||
| 371 | * @return HtmlDomParser |
||
| 372 | */ |
||
| 373 | 63 | public function getHtmlDomParser(): HtmlDomParser |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Retrieve an external iterator. |
||
| 380 | * |
||
| 381 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 382 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
| 383 | * <b>Traversable</b> |
||
| 384 | */ |
||
| 385 | 2 | public function getIterator(): SimpleHtmlDomNode |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @return DOMNode |
||
| 399 | */ |
||
| 400 | 64 | public function getNode(): \DOMNode |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Determine if an attribute exists on the element. |
||
| 407 | * |
||
| 408 | * @param string $name |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | 1 | public function hasAttribute(string $name): bool |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Get dom node's outer html. |
||
| 419 | * |
||
| 420 | * @param bool $multiDecodeNewHtmlEntity |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | 19 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Get dom node's inner html. |
||
| 431 | * |
||
| 432 | * @param bool $multiDecodeNewHtmlEntity |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | 11 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Returns the last child of node. |
||
| 443 | * |
||
| 444 | * @return SimpleHtmlDom|null |
||
| 445 | */ |
||
| 446 | 4 | public function lastChild() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Returns the next sibling of node. |
||
| 459 | * |
||
| 460 | * @return SimpleHtmlDom|null |
||
| 461 | */ |
||
| 462 | 1 | public function nextSibling() |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Returns the parent of node. |
||
| 475 | * |
||
| 476 | * @return SimpleHtmlDom |
||
| 477 | */ |
||
| 478 | 1 | public function parentNode(): self |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Returns the previous sibling of node. |
||
| 485 | * |
||
| 486 | * @return SimpleHtmlDom|null |
||
| 487 | */ |
||
| 488 | 1 | public function previousSibling() |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Replace child node. |
||
| 501 | * |
||
| 502 | * @param string $string |
||
| 503 | * |
||
| 504 | * @return $this |
||
| 505 | * |
||
| 506 | * @throws \RuntimeException |
||
| 507 | */ |
||
| 508 | 7 | protected function replaceChild(string $string) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Replace this node. |
||
| 536 | * |
||
| 537 | * @param string $string |
||
| 538 | * |
||
| 539 | * @return $this|null |
||
| 540 | * |
||
| 541 | * @throws \RuntimeException |
||
| 542 | */ |
||
| 543 | 3 | protected function replaceNode(string $string) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Normalize the given input for comparision. |
||
| 569 | * |
||
| 570 | * @param HtmlDomParser|string $input |
||
| 571 | * |
||
| 572 | * @return string |
||
| 573 | */ |
||
| 574 | 8 | private function normalizeStringForComparision($input): string |
|
| 609 | |||
| 610 | /** |
||
| 611 | * @param HtmlDomParser $newDocument |
||
| 612 | * |
||
| 613 | * @return HtmlDomParser |
||
| 614 | */ |
||
| 615 | 8 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument): HtmlDomParser |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Change the name of a tag in a "DOMNode". |
||
| 661 | * |
||
| 662 | * @param DOMNode $node |
||
| 663 | * @param string $name |
||
| 664 | * |
||
| 665 | * @return DOMElement |
||
| 666 | */ |
||
| 667 | 4 | protected function changeElementName(\DOMNode $node, string $name): \DOMElement |
|
| 684 | |||
| 685 | /** |
||
| 686 | * Set attribute value. |
||
| 687 | * |
||
| 688 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 689 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
| 690 | * @param bool $strict </p> |
||
| 691 | * $value must be NULL, to remove the attribute, |
||
| 692 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
| 693 | * </p> |
||
| 694 | * |
||
| 695 | * @return $this |
||
| 696 | */ |
||
| 697 | 9 | public function setAttribute(string $name, $value = null, bool $strict = false) |
|
| 711 | |||
| 712 | /** |
||
| 713 | * Remove attribute. |
||
| 714 | * |
||
| 715 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 716 | * |
||
| 717 | * @return mixed |
||
| 718 | */ |
||
| 719 | 1 | public function removeAttribute(string $name) |
|
| 725 | |||
| 726 | /** |
||
| 727 | * Get dom node's plain text. |
||
| 728 | * |
||
| 729 | * @return string |
||
| 730 | */ |
||
| 731 | 16 | public function text(): string |
|
| 735 | } |
||
| 736 |
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.