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 |
||
| 32 | class SimpleHtmlDom implements \IteratorAggregate |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected static $functionAliases = [ |
||
| 38 | 'children' => 'childNodes', |
||
| 39 | 'first_child' => 'firstChild', |
||
| 40 | 'last_child' => 'lastChild', |
||
| 41 | 'next_sibling' => 'nextSibling', |
||
| 42 | 'prev_sibling' => 'previousSibling', |
||
| 43 | 'parent' => 'parentNode', |
||
| 44 | 'outertext' => 'html', |
||
| 45 | 'outerhtml' => 'html', |
||
| 46 | 'innertext' => 'innerHtml', |
||
| 47 | 'innerhtml' => 'innerHtml', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var DOMElement |
||
| 52 | */ |
||
| 53 | protected $node; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * SimpleHtmlDom constructor. |
||
| 57 | * |
||
| 58 | * @param DOMNode $node |
||
| 59 | */ |
||
| 60 | 100 | public function __construct(DOMNode $node) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $name |
||
| 67 | * @param array $arguments |
||
| 68 | * |
||
| 69 | * @throws \BadMethodCallException |
||
| 70 | * |
||
| 71 | * @return SimpleHtmlDom|string|null |
||
| 72 | */ |
||
| 73 | 9 | View Code Duplication | public function __call($name, $arguments) |
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $name |
||
| 86 | * |
||
| 87 | * @return array|string|null |
||
| 88 | */ |
||
| 89 | 44 | public function __get($name) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $selector |
||
| 114 | * @param int $idx |
||
| 115 | * |
||
| 116 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
| 117 | */ |
||
| 118 | 12 | public function __invoke($selector, $idx = null) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @param $name |
||
| 125 | * |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | 1 | public function __isset($name) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param $name |
||
| 148 | * @param $value |
||
| 149 | * |
||
| 150 | * @return SimpleHtmlDom |
||
| 151 | */ |
||
| 152 | 15 | public function __set($name, $value) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 2 | public function __toString() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param $name |
||
| 178 | * |
||
| 179 | * @return SimpleHtmlDom |
||
| 180 | */ |
||
| 181 | public function __unset($name) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Returns children of node. |
||
| 188 | * |
||
| 189 | * @param int $idx |
||
| 190 | * |
||
| 191 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNode|null |
||
| 192 | */ |
||
| 193 | 2 | public function childNodes(int $idx = -1) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Find list of nodes with a CSS selector. |
||
| 206 | * |
||
| 207 | * @param string $selector |
||
| 208 | * @param int|null $idx |
||
| 209 | * |
||
| 210 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
| 211 | */ |
||
| 212 | 26 | public function find(string $selector, $idx = null) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Find one node with a CSS selector. |
||
| 219 | * |
||
| 220 | * @param string $selector |
||
| 221 | * |
||
| 222 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
| 223 | */ |
||
| 224 | public function findOne(string $selector) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the first child of node. |
||
| 231 | * |
||
| 232 | * @return SimpleHtmlDom|null |
||
| 233 | */ |
||
| 234 | 4 | public function firstChild() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Returns an array of attributes. |
||
| 247 | * |
||
| 248 | * @return array|null |
||
| 249 | */ |
||
| 250 | 2 | public function getAllAttributes() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Return attribute value. |
||
| 266 | * |
||
| 267 | * @param string $name |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 13 | public function getAttribute(string $name): string |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Return element by #id. |
||
| 280 | * |
||
| 281 | * @param string $id |
||
| 282 | * |
||
| 283 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
| 284 | */ |
||
| 285 | 1 | public function getElementById(string $id) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Return element by tag name. |
||
| 292 | * |
||
| 293 | * @param string $name |
||
| 294 | * |
||
| 295 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
| 296 | */ |
||
| 297 | 1 | public function getElementByTagName(string $name) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Returns elements by #id. |
||
| 310 | * |
||
| 311 | * @param string $id |
||
| 312 | * @param int|null $idx |
||
| 313 | * |
||
| 314 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
| 315 | */ |
||
| 316 | public function getElementsById(string $id, $idx = null) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns elements by tag name. |
||
| 323 | * |
||
| 324 | * @param string $name |
||
| 325 | * @param int|null $idx |
||
| 326 | * |
||
| 327 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNode|SimpleHtmlDomNodeBlank |
||
| 328 | */ |
||
| 329 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
| 352 | |||
| 353 | /** |
||
| 354 | * Create a new "HtmlDomParser"-object from the current context. |
||
| 355 | * |
||
| 356 | * @return HtmlDomParser |
||
| 357 | */ |
||
| 358 | 64 | public function getHtmlDomParser(): HtmlDomParser |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Retrieve an external iterator. |
||
| 365 | * |
||
| 366 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 367 | * |
||
| 368 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
| 369 | * <b>Traversable</b> |
||
| 370 | */ |
||
| 371 | 2 | public function getIterator(): SimpleHtmlDomNode |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @return DOMNode |
||
| 385 | */ |
||
| 386 | 65 | public function getNode(): \DOMNode |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Determine if an attribute exists on the element. |
||
| 393 | * |
||
| 394 | * @param string $name |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | 1 | public function hasAttribute(string $name): bool |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get dom node's outer html. |
||
| 405 | * |
||
| 406 | * @param bool $multiDecodeNewHtmlEntity |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | 20 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Get dom node's inner html. |
||
| 417 | * |
||
| 418 | * @param bool $multiDecodeNewHtmlEntity |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | 11 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Returns the last child of node. |
||
| 429 | * |
||
| 430 | * @return SimpleHtmlDom|null |
||
| 431 | */ |
||
| 432 | 4 | public function lastChild() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Returns the next sibling of node. |
||
| 445 | * |
||
| 446 | * @return SimpleHtmlDom|null |
||
| 447 | */ |
||
| 448 | 1 | public function nextSibling() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Returns the parent of node. |
||
| 461 | * |
||
| 462 | * @return SimpleHtmlDom |
||
| 463 | */ |
||
| 464 | 1 | public function parentNode(): self |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Returns the previous sibling of node. |
||
| 471 | * |
||
| 472 | * @return SimpleHtmlDom|null |
||
| 473 | */ |
||
| 474 | 1 | public function previousSibling() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Replace child node. |
||
| 487 | * |
||
| 488 | * @param string $string |
||
| 489 | * |
||
| 490 | * @throws \RuntimeException |
||
| 491 | * |
||
| 492 | * @return $this |
||
| 493 | */ |
||
| 494 | 7 | protected function replaceChild(string $string): self |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Replace this node. |
||
| 529 | * |
||
| 530 | * @param string $string |
||
| 531 | * |
||
| 532 | * @throws \RuntimeException |
||
| 533 | * |
||
| 534 | * @return $this|null |
||
| 535 | */ |
||
| 536 | 4 | protected function replaceNode(string $string) |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Normalize the given input for comparision. |
||
| 585 | * |
||
| 586 | * @param HtmlDomParser|string $input |
||
| 587 | * |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | 9 | private function normalizeStringForComparision($input): string |
|
| 626 | |||
| 627 | /** |
||
| 628 | * @param HtmlDomParser $newDocument |
||
| 629 | * @param bool $removeExtraHeadTag |
||
| 630 | * |
||
| 631 | * @return HtmlDomParser |
||
| 632 | */ |
||
| 633 | 9 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument, $removeExtraHeadTag = false): HtmlDomParser |
|
| 703 | |||
| 704 | /** |
||
| 705 | * Change the name of a tag in a "DOMNode". |
||
| 706 | * |
||
| 707 | * @param DOMNode $node |
||
| 708 | * @param string $name |
||
| 709 | * |
||
| 710 | * @return DOMElement |
||
| 711 | */ |
||
| 712 | 4 | protected function changeElementName(\DOMNode $node, string $name): \DOMElement |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Set attribute value. |
||
| 735 | * |
||
| 736 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 737 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
| 738 | * @param bool $strict </p> |
||
| 739 | * $value must be NULL, to remove the attribute, |
||
| 740 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
| 741 | * </p> |
||
| 742 | * |
||
| 743 | * @return $this |
||
| 744 | */ |
||
| 745 | 9 | public function setAttribute(string $name, $value = null, bool $strict = false): self |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Remove attribute. |
||
| 763 | * |
||
| 764 | * @param string $name <p>The name of the html-attribute.</p> |
||
| 765 | * |
||
| 766 | * @return mixed |
||
| 767 | */ |
||
| 768 | public function removeAttribute(string $name) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Get dom node's plain text. |
||
| 777 | * |
||
| 778 | * @return string |
||
| 779 | */ |
||
| 780 | 16 | public function text(): string |
|
| 784 | } |
||
| 785 |
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.