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 | 68 | 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) |
| 74 | |||
| 75 | /** |
||
| 76 | * @param $name |
||
| 77 | * |
||
| 78 | * @return array|null|string |
||
| 79 | */ |
||
| 80 | 23 | public function __get($name) |
|
| 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 | 1 | default: |
|
| 123 | 1 | return $this->hasAttribute($name); |
|
| 124 | 1 | } |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param $name |
||
| 129 | * @param $value |
||
| 130 | * |
||
| 131 | * @return SimpleHtmlDom |
||
| 132 | */ |
||
| 133 | 8 | public function __set($name, $value) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | 1 | 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() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Return attribute value |
||
| 235 | * |
||
| 236 | * @param string $name |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 9 | 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 | View Code Duplication | public function getElementByTagName($name) |
| 265 | { |
||
| 266 | 1 | $node = $this->node->getElementsByTagName($name)->item(0); |
|
| 267 | |||
| 268 | 1 | if ($node !== null) { |
|
| 269 | 1 | return new SimpleHtmlDom($node); |
|
| 270 | } else { |
||
| 271 | return new SimpleHtmlDomNodeBlank(); |
||
| 272 | } |
||
| 273 | } |
||
| 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) |
||
| 284 | { |
||
| 285 | return $this->find("#$id", $idx); |
||
| 286 | } |
||
| 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 SimpleHtmlDom($node); |
|
| 304 | 1 | } |
|
| 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 | 39 | 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() |
|
| 339 | { |
||
| 340 | 2 | $elements = new SimpleHtmlDomNode(); |
|
| 341 | 2 | if ($this->node->hasChildNodes()) { |
|
| 342 | 2 | foreach ($this->node->childNodes as $node) { |
|
| 343 | 2 | $elements[] = new self($node); |
|
| 344 | 2 | } |
|
| 345 | 2 | } |
|
| 346 | |||
| 347 | 2 | return $elements; |
|
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return DOMNode |
||
| 352 | */ |
||
| 353 | 40 | 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 | 12 | 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) |
|
| 456 | { |
||
| 457 | 3 | if (!empty($string)) { |
|
| 458 | 3 | $newDocument = new HtmlDomParser($string); |
|
| 459 | |||
| 460 | 3 | if ($newDocument->outertext != $string) { |
|
| 461 | throw new RuntimeException('Not valid HTML fragment'); |
||
| 462 | } |
||
| 463 | 3 | } |
|
| 464 | |||
| 465 | 3 | foreach ($this->node->childNodes as $node) { |
|
| 466 | 3 | $this->node->removeChild($node); |
|
| 467 | 3 | } |
|
| 468 | |||
| 469 | 3 | if (!empty($newDocument)) { |
|
| 470 | |||
| 471 | 3 | $newDocument = $this->cleanHtmlWrapper($newDocument); |
|
| 472 | |||
| 473 | 3 | $newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
|
| 474 | |||
| 475 | 3 | $this->node->appendChild($newNode); |
|
| 476 | 3 | } |
|
| 477 | |||
| 478 | 3 | return $this; |
|
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Replace this node |
||
| 483 | * |
||
| 484 | * @param $string |
||
| 485 | * |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | 2 | protected function replaceNode($string) |
|
| 489 | { |
||
| 490 | 2 | if (empty($string)) { |
|
| 491 | 1 | $this->node->parentNode->removeChild($this->node); |
|
| 492 | |||
| 493 | 1 | return null; |
|
| 494 | } |
||
| 495 | |||
| 496 | 2 | $newDocument = new HtmlDomParser($string); |
|
| 497 | |||
| 498 | 2 | if ($newDocument->outertext != $string) { |
|
| 499 | throw new RuntimeException('Not valid HTML fragment'); |
||
| 500 | } |
||
| 501 | |||
| 502 | 2 | $newDocument = $this->cleanHtmlWrapper($newDocument); |
|
| 503 | |||
| 504 | 2 | $newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
|
| 505 | |||
| 506 | 2 | $this->node->parentNode->replaceChild($newNode, $this->node); |
|
| 507 | 2 | $this->node = $newNode; |
|
| 508 | |||
| 509 | 2 | return $this; |
|
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param HtmlDomParser $newDocument |
||
| 514 | * |
||
| 515 | * @return HtmlDomParser |
||
| 516 | */ |
||
| 517 | 5 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
|
| 518 | { |
||
| 519 | 5 | if ($newDocument->getIsDOMDocumentCreatedWithoutHtml() === true) { |
|
| 520 | |||
| 521 | // Remove doc-type node. |
||
| 522 | 3 | $newDocument->getDocument()->doctype->parentNode->removeChild($newDocument->getDocument()->doctype); |
|
| 523 | |||
| 524 | // Remove html element, preserving child nodes. |
||
| 525 | 3 | $html = $newDocument->getDocument()->getElementsByTagName('html')->item(0); |
|
| 526 | 3 | $fragment = $newDocument->getDocument()->createDocumentFragment(); |
|
| 527 | 3 | while ($html->childNodes->length > 0) { |
|
| 528 | 3 | $fragment->appendChild($html->childNodes->item(0)); |
|
| 529 | 3 | } |
|
| 530 | 3 | $html->parentNode->replaceChild($fragment, $html); |
|
| 531 | |||
| 532 | // Remove body element, preserving child nodes. |
||
| 533 | 3 | $body = $newDocument->getDocument()->getElementsByTagName('body')->item(0); |
|
| 534 | 3 | $fragment = $newDocument->getDocument()->createDocumentFragment(); |
|
| 535 | 3 | while ($body->childNodes->length > 0) { |
|
| 536 | 3 | $fragment->appendChild($body->childNodes->item(0)); |
|
| 537 | 3 | } |
|
| 538 | 3 | $body->parentNode->replaceChild($fragment, $body); |
|
| 539 | |||
| 540 | // At this point DOMDocument still added a "<p>"-wrapper around our string, |
||
| 541 | // so we replace it with "<simpleHtmlDomP>" and delete this at the ending ... |
||
| 542 | 3 | $this->changeElementName($newDocument->getDocument()->getElementsByTagName('p')->item(0), 'simpleHtmlDomP'); |
|
| 543 | 3 | } |
|
| 544 | |||
| 545 | 5 | return $newDocument; |
|
| 546 | } |
||
| 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 | 3 | $newnode = $node->ownerDocument->createElement($name); |
|
| 558 | 3 | foreach ($node->childNodes as $child){ |
|
| 559 | 3 | $child = $node->ownerDocument->importNode($child, true); |
|
| 560 | 3 | $newnode->appendChild($child); |
|
| 561 | 3 | } |
|
| 562 | 3 | foreach ($node->attributes as $attrName => $attrNode) { |
|
| 563 | $newnode->setAttribute($attrName, $attrNode); |
||
| 564 | 3 | } |
|
| 565 | 3 | $newnode->ownerDocument->replaceChild($newnode, $node); |
|
| 566 | |||
| 567 | 3 | return $newnode; |
|
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Set attribute value |
||
| 572 | * |
||
| 573 | * @param $name |
||
| 574 | * @param $value |
||
| 575 | * |
||
| 576 | * @return $this |
||
| 577 | */ |
||
| 578 | 5 | public function setAttribute($name, $value) |
|
| 579 | { |
||
| 580 | 5 | if (empty($value)) { |
|
| 581 | 1 | $this->node->removeAttribute($name); |
|
| 582 | 1 | } else { |
|
| 583 | 5 | $this->node->setAttribute($name, $value); |
|
| 584 | } |
||
| 585 | |||
| 586 | 5 | return $this; |
|
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get dom node's plain text |
||
| 591 | * |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | 9 | public function text() |
|
| 598 | } |
||
| 599 |
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.