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 |
||
| 30 | class SimpleHtmlDom implements \IteratorAggregate |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected static $functionAliases = array( |
||
| 36 | 'children' => 'childNodes', |
||
| 37 | 'first_child' => 'firstChild', |
||
| 38 | 'last_child' => 'lastChild', |
||
| 39 | 'next_sibling' => 'nextSibling', |
||
| 40 | 'prev_sibling' => 'previousSibling', |
||
| 41 | 'parent' => 'parentNode', |
||
| 42 | 'outertext' => 'html', |
||
| 43 | 'innertext' => 'innerHtml', |
||
| 44 | ); |
||
| 45 | /** |
||
| 46 | * @var DOMElement |
||
| 47 | */ |
||
| 48 | protected $node; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * SimpleHtmlDom constructor. |
||
| 52 | * |
||
| 53 | * @param DOMNode $node |
||
| 54 | */ |
||
| 55 | 69 | public function __construct(DOMNode $node) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @param $name |
||
| 62 | * @param $arguments |
||
| 63 | * |
||
| 64 | * @return null|string|SimpleHtmlDom |
||
| 65 | * |
||
| 66 | */ |
||
| 67 | 8 | View Code Duplication | public function __call($name, $arguments) |
| 75 | |||
| 76 | /** |
||
| 77 | * @param $name |
||
| 78 | * |
||
| 79 | * @return array|null|string |
||
| 80 | */ |
||
| 81 | 23 | public function __get($name) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $selector |
||
| 101 | * @param int $idx |
||
| 102 | * |
||
| 103 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
| 104 | */ |
||
| 105 | 12 | public function __invoke($selector, $idx = null) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @param $name |
||
| 112 | * |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | 1 | View Code Duplication | public function __isset($name) |
| 127 | |||
| 128 | /** |
||
| 129 | * @param $name |
||
| 130 | * @param $value |
||
| 131 | * |
||
| 132 | * @return SimpleHtmlDom |
||
| 133 | */ |
||
| 134 | 9 | public function __set($name, $value) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 2 | public function __toString() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param $name |
||
| 156 | * |
||
| 157 | * @return SimpleHtmlDom |
||
| 158 | */ |
||
| 159 | 1 | public function __unset($name) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Returns children of node |
||
| 166 | * |
||
| 167 | * @param int $idx |
||
| 168 | * |
||
| 169 | * @return SimpleHtmlDomNode|SimpleHtmlDom|null |
||
| 170 | */ |
||
| 171 | 2 | public function childNodes($idx = -1) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Find list of nodes with a CSS selector |
||
| 188 | * |
||
| 189 | * @param string $selector |
||
| 190 | * @param int $idx |
||
| 191 | * |
||
| 192 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 193 | */ |
||
| 194 | 24 | public function find($selector, $idx = null) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the first child of node |
||
| 201 | * |
||
| 202 | * @return SimpleHtmlDom|null |
||
| 203 | */ |
||
| 204 | 4 | public function firstChild() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Returns array of attributes |
||
| 217 | * |
||
| 218 | * @return array|null |
||
| 219 | */ |
||
| 220 | 1 | public function getAllAttributes() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Return attribute value |
||
| 236 | * |
||
| 237 | * @param string $name |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | 10 | public function getAttribute($name) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Return SimpleHtmlDom by id. |
||
| 250 | * |
||
| 251 | * @param string $id |
||
| 252 | * |
||
| 253 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 254 | */ |
||
| 255 | 1 | public function getElementById($id) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Return SimpleHtmlDom by tag name. |
||
| 262 | * |
||
| 263 | * @param string $name |
||
| 264 | * |
||
| 265 | * @return SimpleHtmlDomNode|SimpleHtmlDomNodeBlank |
||
| 266 | */ |
||
| 267 | 1 | public function getElementByTagName($name) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Returns Elements by id |
||
| 280 | * |
||
| 281 | * @param string $id |
||
| 282 | * @param null|int $idx |
||
| 283 | * |
||
| 284 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 285 | */ |
||
| 286 | public function getElementsById($id, $idx = null) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns Elements by tag name |
||
| 293 | * |
||
| 294 | * @param string $name |
||
| 295 | * @param null|int $idx |
||
| 296 | * |
||
| 297 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 298 | */ |
||
| 299 | 1 | View Code Duplication | public function getElementsByTagName($name, $idx = null) |
| 323 | |||
| 324 | /** |
||
| 325 | * Create a new "HtmlDomParser"-object from the current context. |
||
| 326 | * |
||
| 327 | * @return HtmlDomParser |
||
| 328 | */ |
||
| 329 | 40 | public function getHtmlDomParser() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Retrieve an external iterator |
||
| 336 | * |
||
| 337 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 338 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
| 339 | * <b>Traversable</b> |
||
| 340 | */ |
||
| 341 | 2 | public function getIterator() |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @return DOMNode |
||
| 355 | */ |
||
| 356 | 41 | public function getNode() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Determine if an attribute exists on the element. |
||
| 363 | * |
||
| 364 | * @param $name |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | 1 | public function hasAttribute($name) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Get dom node's outer html |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | 13 | public function html() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Get dom node's inner html |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | 3 | public function innerHtml() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Returns the last child of node |
||
| 395 | * |
||
| 396 | * @return SimpleHtmlDom|null |
||
| 397 | */ |
||
| 398 | 4 | public function lastChild() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Returns the next sibling of node |
||
| 411 | * |
||
| 412 | * @return SimpleHtmlDom|null |
||
| 413 | */ |
||
| 414 | 1 | public function nextSibling() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Returns the parent of node |
||
| 427 | * |
||
| 428 | * @return SimpleHtmlDom |
||
| 429 | */ |
||
| 430 | 1 | public function parentNode() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Returns the previous sibling of node |
||
| 437 | * |
||
| 438 | * @return SimpleHtmlDom|null |
||
| 439 | */ |
||
| 440 | 1 | public function previousSibling() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Replace child node |
||
| 453 | * |
||
| 454 | * @param $string |
||
| 455 | * |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | 3 | protected function replaceChild($string) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Replace this node |
||
| 486 | * |
||
| 487 | * @param $string |
||
| 488 | * |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | 3 | protected function replaceNode($string) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Normalize the given string for comparision. |
||
| 521 | * |
||
| 522 | * @param $string |
||
| 523 | * |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | 6 | private function normalizeStringForComparision($string) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @param HtmlDomParser $newDocument |
||
| 533 | * |
||
| 534 | * @return HtmlDomParser |
||
| 535 | */ |
||
| 536 | 6 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * change the name of a tag in a "DOMNode" |
||
| 569 | * |
||
| 570 | * @param DOMNode $node |
||
| 571 | * @param string $name |
||
| 572 | * |
||
| 573 | * @return DOMElement |
||
| 574 | */ |
||
| 575 | 3 | protected function changeElementName(\DOMNode $node, $name) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Set attribute value |
||
| 592 | * |
||
| 593 | * @param $name |
||
| 594 | * @param $value |
||
| 595 | * |
||
| 596 | * @return $this |
||
| 597 | */ |
||
| 598 | 5 | public function setAttribute($name, $value) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Get dom node's plain text |
||
| 611 | * |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | 9 | public function text() |
|
| 618 | } |
||
| 619 |
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.