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) |
| 68 | { |
||
| 69 | 8 | if (isset(self::$functionAliases[$name])) { |
|
| 70 | 8 | return call_user_func_array(array($this, self::$functionAliases[$name]), $arguments); |
|
| 71 | } |
||
| 72 | throw new BadMethodCallException('Method does not exist'); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param $name |
||
| 77 | * |
||
| 78 | * @return array|null|string |
||
| 79 | */ |
||
| 80 | 23 | public function __get($name) |
|
| 81 | { |
||
| 82 | switch ($name) { |
||
| 83 | 23 | case 'outertext': |
|
| 84 | 12 | return $this->html(); |
|
| 85 | 18 | case 'innertext': |
|
| 86 | 3 | return $this->innerHtml(); |
|
| 87 | 17 | case 'plaintext': |
|
| 88 | 9 | return $this->text(); |
|
| 89 | 9 | case 'tag': |
|
| 90 | 4 | return $this->node->nodeName; |
|
| 91 | 8 | case 'attr': |
|
| 92 | return $this->getAllAttributes(); |
||
| 93 | default: |
||
| 94 | 8 | return $this->getAttribute($name); |
|
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $selector |
||
| 100 | * @param int $idx |
||
| 101 | * |
||
| 102 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
| 103 | */ |
||
| 104 | 12 | public function __invoke($selector, $idx = null) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param $name |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 1 | View Code Duplication | public function __isset($name) |
| 115 | { |
||
| 116 | switch ($name) { |
||
| 117 | 1 | case 'outertext': |
|
| 118 | 1 | case 'innertext': |
|
| 119 | 1 | case 'plaintext': |
|
| 120 | 1 | case 'tag': |
|
| 121 | return true; |
||
| 122 | default: |
||
| 123 | 1 | return $this->hasAttribute($name); |
|
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param $name |
||
| 129 | * @param $value |
||
| 130 | * |
||
| 131 | * @return SimpleHtmlDom |
||
| 132 | */ |
||
| 133 | 9 | public function __set($name, $value) |
|
| 134 | { |
||
| 135 | switch ($name) { |
||
| 136 | 9 | case 'outertext': |
|
| 137 | 3 | return $this->replaceNode($value); |
|
| 138 | 6 | case 'innertext': |
|
| 139 | 3 | return $this->replaceChild($value); |
|
| 140 | default: |
||
| 141 | 5 | return $this->setAttribute($name, $value); |
|
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | 2 | public function __toString() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param $name |
||
| 155 | * |
||
| 156 | * @return SimpleHtmlDom |
||
| 157 | */ |
||
| 158 | 1 | public function __unset($name) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Returns children of node |
||
| 165 | * |
||
| 166 | * @param int $idx |
||
| 167 | * |
||
| 168 | * @return SimpleHtmlDomNode|SimpleHtmlDom|null |
||
| 169 | */ |
||
| 170 | 2 | public function childNodes($idx = -1) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Find list of nodes with a CSS selector |
||
| 187 | * |
||
| 188 | * @param string $selector |
||
| 189 | * @param int $idx |
||
| 190 | * |
||
| 191 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 192 | */ |
||
| 193 | 24 | public function find($selector, $idx = null) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Returns the first child of node |
||
| 200 | * |
||
| 201 | * @return SimpleHtmlDom|null |
||
| 202 | */ |
||
| 203 | 4 | public function firstChild() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Returns array of attributes |
||
| 216 | * |
||
| 217 | * @return array|null |
||
| 218 | */ |
||
| 219 | 1 | public function getAllAttributes() |
|
| 220 | { |
||
| 221 | 1 | if ($this->node->hasAttributes()) { |
|
| 222 | 1 | $attributes = array(); |
|
| 223 | 1 | foreach ($this->node->attributes as $attr) { |
|
| 224 | 1 | $attributes[$attr->name] = $attr->value; |
|
| 225 | } |
||
| 226 | |||
| 227 | 1 | return $attributes; |
|
| 228 | } |
||
| 229 | |||
| 230 | 1 | return null; |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return attribute value |
||
| 235 | * |
||
| 236 | * @param string $name |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 10 | public function getAttribute($name) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Return SimpleHtmlDom by id. |
||
| 247 | * |
||
| 248 | * @param string $id |
||
| 249 | * |
||
| 250 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 251 | */ |
||
| 252 | 1 | public function getElementById($id) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Return SimpleHtmlDom by tag name. |
||
| 259 | * |
||
| 260 | * @param string $name |
||
| 261 | * |
||
| 262 | * @return SimpleHtmlDomNode|SimpleHtmlDomNodeBlank |
||
| 263 | */ |
||
| 264 | 1 | public function getElementByTagName($name) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Returns Elements by id |
||
| 277 | * |
||
| 278 | * @param string $id |
||
| 279 | * @param null|int $idx |
||
| 280 | * |
||
| 281 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 282 | */ |
||
| 283 | public function getElementsById($id, $idx = null) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Returns Elements by tag name |
||
| 290 | * |
||
| 291 | * @param string $name |
||
| 292 | * @param null|int $idx |
||
| 293 | * |
||
| 294 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 295 | */ |
||
| 296 | 1 | View Code Duplication | public function getElementsByTagName($name, $idx = null) |
| 297 | { |
||
| 298 | 1 | $nodesList = $this->node->getElementsByTagName($name); |
|
| 299 | |||
| 300 | 1 | $elements = new SimpleHtmlDomNode(); |
|
| 301 | |||
| 302 | 1 | foreach ($nodesList as $node) { |
|
| 303 | 1 | $elements[] = new self($node); |
|
| 304 | } |
||
| 305 | |||
| 306 | 1 | if (null === $idx) { |
|
| 307 | 1 | return $elements; |
|
| 308 | } else { |
||
| 309 | if ($idx < 0) { |
||
| 310 | $idx = count($elements) + $idx; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | if (isset($elements[$idx])) { |
||
| 315 | return $elements[$idx]; |
||
| 316 | } else { |
||
| 317 | return new SimpleHtmlDomNodeBlank(); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Create a new "HtmlDomParser"-object from the current context. |
||
| 323 | * |
||
| 324 | * @return HtmlDomParser |
||
| 325 | */ |
||
| 326 | 40 | public function getHtmlDomParser() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Retrieve an external iterator |
||
| 333 | * |
||
| 334 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 335 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
| 336 | * <b>Traversable</b> |
||
| 337 | */ |
||
| 338 | 2 | public function getIterator() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @return DOMNode |
||
| 352 | */ |
||
| 353 | 41 | public function getNode() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Determine if an attribute exists on the element. |
||
| 360 | * |
||
| 361 | * @param $name |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | 1 | public function hasAttribute($name) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Get dom node's outer html |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | 13 | public function html() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Get dom node's inner html |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | 3 | public function innerHtml() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Returns the last child of node |
||
| 392 | * |
||
| 393 | * @return SimpleHtmlDom|null |
||
| 394 | */ |
||
| 395 | 4 | public function lastChild() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Returns the next sibling of node |
||
| 408 | * |
||
| 409 | * @return SimpleHtmlDom|null |
||
| 410 | */ |
||
| 411 | 1 | public function nextSibling() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Returns the parent of node |
||
| 424 | * |
||
| 425 | * @return SimpleHtmlDom |
||
| 426 | */ |
||
| 427 | 1 | public function parentNode() |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Returns the previous sibling of node |
||
| 434 | * |
||
| 435 | * @return SimpleHtmlDom|null |
||
| 436 | */ |
||
| 437 | 1 | public function previousSibling() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Replace child node |
||
| 450 | * |
||
| 451 | * @param $string |
||
| 452 | * |
||
| 453 | * @return $this |
||
| 454 | */ |
||
| 455 | 3 | protected function replaceChild($string) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Replace this node |
||
| 483 | * |
||
| 484 | * @param $string |
||
| 485 | * |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | 3 | protected function replaceNode($string) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @param HtmlDomParser $newDocument |
||
| 514 | * |
||
| 515 | * @return HtmlDomParser |
||
| 516 | */ |
||
| 517 | 6 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * change the name of a tag in a "DOMNode" |
||
| 550 | * |
||
| 551 | * @param DOMNode $node |
||
| 552 | * @param string $name |
||
| 553 | * |
||
| 554 | * @return DOMElement |
||
| 555 | */ |
||
| 556 | 3 | protected function changeElementName(\DOMNode $node, $name) |
|
| 557 | { |
||
| 558 | 3 | $newnode = $node->ownerDocument->createElement($name); |
|
| 559 | 3 | foreach ($node->childNodes as $child) { |
|
| 560 | 3 | $child = $node->ownerDocument->importNode($child, true); |
|
| 561 | 3 | $newnode->appendChild($child); |
|
| 562 | } |
||
| 563 | 3 | foreach ($node->attributes as $attrName => $attrNode) { |
|
| 564 | $newnode->setAttribute($attrName, $attrNode); |
||
| 565 | } |
||
| 566 | 3 | $newnode->ownerDocument->replaceChild($newnode, $node); |
|
| 567 | |||
| 568 | 3 | return $newnode; |
|
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Set attribute value |
||
| 573 | * |
||
| 574 | * @param $name |
||
| 575 | * @param $value |
||
| 576 | * |
||
| 577 | * @return $this |
||
| 578 | */ |
||
| 579 | 5 | public function setAttribute($name, $value) |
|
| 580 | { |
||
| 581 | 5 | if (empty($value)) { |
|
| 582 | 1 | $this->node->removeAttribute($name); |
|
| 583 | } else { |
||
| 584 | 5 | $this->node->setAttribute($name, $value); |
|
| 585 | } |
||
| 586 | |||
| 587 | 5 | return $this; |
|
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Get dom node's plain text |
||
| 592 | * |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | 9 | public function text() |
|
| 599 | } |
||
| 600 |
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.