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 |
||
| 34 | class SimpleHtmlDom implements \IteratorAggregate |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected static $functionAliases = array( |
||
| 40 | 'children' => 'childNodes', |
||
| 41 | 'first_child' => 'firstChild', |
||
| 42 | 'last_child' => 'lastChild', |
||
| 43 | 'next_sibling' => 'nextSibling', |
||
| 44 | 'prev_sibling' => 'previousSibling', |
||
| 45 | 'parent' => 'parentNode', |
||
| 46 | 'outertext' => 'html', |
||
| 47 | 'outerhtml' => 'html', |
||
| 48 | 'innertext' => 'innerHtml', |
||
| 49 | 'innerhtml' => 'innerHtml', |
||
| 50 | ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var DOMElement |
||
| 54 | */ |
||
| 55 | protected $node; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * SimpleHtmlDom constructor. |
||
| 59 | * |
||
| 60 | * @param DOMNode $node |
||
| 61 | */ |
||
| 62 | 93 | public function __construct(DOMNode $node) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param $name |
||
| 69 | * @param $arguments |
||
| 70 | * |
||
| 71 | * @return null|string|SimpleHtmlDom |
||
| 72 | * |
||
| 73 | */ |
||
| 74 | 8 | View Code Duplication | public function __call($name, $arguments) |
| 75 | { |
||
| 76 | 8 | $name = strtolower($name); |
|
| 77 | |||
| 78 | 8 | if (isset(self::$functionAliases[$name])) { |
|
| 79 | 8 | return call_user_func_array(array($this, self::$functionAliases[$name]), $arguments); |
|
| 80 | } |
||
| 81 | |||
| 82 | throw new BadMethodCallException('Method does not exist'); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $name |
||
| 87 | * |
||
| 88 | * @return array|null|string |
||
| 89 | */ |
||
| 90 | 40 | public function __get($name) |
|
| 91 | { |
||
| 92 | 40 | $name = strtolower($name); |
|
| 93 | |||
| 94 | switch ($name) { |
||
| 95 | 40 | case 'outerhtml': |
|
| 96 | 36 | case 'outertext': |
|
| 97 | 17 | return $this->html(); |
|
| 98 | 30 | case 'innerhtml': |
|
| 99 | 24 | case 'innertext': |
|
| 100 | 9 | return $this->innerHtml(); |
|
| 101 | 23 | case 'text': |
|
| 102 | 18 | case 'plaintext': |
|
| 103 | 15 | return $this->text(); |
|
| 104 | 9 | case 'tag': |
|
| 105 | 4 | return $this->node->nodeName; |
|
| 106 | 8 | case 'attr': |
|
| 107 | return $this->getAllAttributes(); |
||
| 108 | default: |
||
| 109 | 8 | return $this->getAttribute($name); |
|
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $selector |
||
| 115 | * @param int $idx |
||
| 116 | * |
||
| 117 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
| 118 | */ |
||
| 119 | 11 | public function __invoke($selector, $idx = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @param $name |
||
| 126 | * |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | 1 | public function __isset($name) |
|
| 130 | { |
||
| 131 | switch ($name) { |
||
| 132 | 1 | case 'outertext': |
|
| 133 | 1 | case 'outerhtml': |
|
| 134 | 1 | case 'innertext': |
|
| 135 | 1 | case 'innerhtml': |
|
| 136 | 1 | case 'plaintext': |
|
| 137 | 1 | case 'text': |
|
| 138 | 1 | case 'tag': |
|
| 139 | return true; |
||
| 140 | default: |
||
| 141 | 1 | return $this->hasAttribute($name); |
|
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param $name |
||
| 147 | * @param $value |
||
| 148 | * |
||
| 149 | * @return SimpleHtmlDom |
||
| 150 | */ |
||
| 151 | 12 | public function __set($name, $value) |
|
| 152 | { |
||
| 153 | 12 | $name = strtolower($name); |
|
| 154 | |||
| 155 | switch ($name) { |
||
| 156 | 12 | case 'outerhtml': |
|
| 157 | 11 | case 'outertext': |
|
| 158 | 3 | return $this->replaceNode($value); |
|
| 159 | 9 | case 'innertext': |
|
| 160 | 8 | case 'innerhtml': |
|
| 161 | 5 | return $this->replaceChild($value); |
|
| 162 | default: |
||
| 163 | 7 | return $this->setAttribute($name, $value); |
|
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | 2 | public function __toString() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param $name |
||
| 177 | * |
||
| 178 | * @return SimpleHtmlDom |
||
| 179 | */ |
||
| 180 | 1 | public function __unset($name) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Returns children of node |
||
| 187 | * |
||
| 188 | * @param int $idx |
||
| 189 | * |
||
| 190 | * @return SimpleHtmlDomNode|SimpleHtmlDom|null |
||
| 191 | */ |
||
| 192 | 2 | public function childNodes($idx = -1) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Find list of nodes with a CSS selector |
||
| 209 | * |
||
| 210 | * @param string $selector |
||
| 211 | * @param int $idx |
||
| 212 | * |
||
| 213 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 214 | */ |
||
| 215 | 25 | public function find($selector, $idx = null) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Returns the first child of node |
||
| 222 | * |
||
| 223 | * @return SimpleHtmlDom|null |
||
| 224 | */ |
||
| 225 | 4 | public function firstChild() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Returns array of attributes |
||
| 238 | * |
||
| 239 | * @return array|null |
||
| 240 | */ |
||
| 241 | 2 | public function getAllAttributes() |
|
| 242 | { |
||
| 243 | 2 | if ($this->node->hasAttributes()) { |
|
| 244 | 2 | $attributes = array(); |
|
| 245 | 2 | foreach ($this->node->attributes as $attr) { |
|
| 246 | 2 | $attributes[$attr->name] = HtmlDomParser::putReplacedBackToPreserveHtmlEntities($attr->value); |
|
| 247 | } |
||
| 248 | |||
| 249 | 2 | return $attributes; |
|
| 250 | } |
||
| 251 | |||
| 252 | 1 | return null; |
|
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Return attribute value |
||
| 257 | * |
||
| 258 | * @param string $name |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | 11 | public function getAttribute($name) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Return SimpleHtmlDom by id. |
||
| 271 | * |
||
| 272 | * @param string $id |
||
| 273 | * |
||
| 274 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 275 | */ |
||
| 276 | 1 | public function getElementById($id) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Return SimpleHtmlDom by tag name. |
||
| 283 | * |
||
| 284 | * @param string $name |
||
| 285 | * |
||
| 286 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
| 287 | */ |
||
| 288 | 1 | public function getElementByTagName($name) |
|
| 289 | { |
||
| 290 | 1 | $node = $this->node->getElementsByTagName($name)->item(0); |
|
| 291 | |||
| 292 | 1 | if ($node !== null) { |
|
| 293 | 1 | return new self($node); |
|
| 294 | } else { |
||
| 295 | return new SimpleHtmlDomNodeBlank(); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns Elements by id |
||
| 301 | * |
||
| 302 | * @param string $id |
||
| 303 | * @param null|int $idx |
||
| 304 | * |
||
| 305 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 306 | */ |
||
| 307 | public function getElementsById($id, $idx = null) |
||
| 308 | { |
||
| 309 | return $this->find("#$id", $idx); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Returns Elements by tag name |
||
| 314 | * |
||
| 315 | * @param string $name |
||
| 316 | * @param null|int $idx |
||
| 317 | * |
||
| 318 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 319 | */ |
||
| 320 | 1 | View Code Duplication | public function getElementsByTagName($name, $idx = null) |
| 321 | { |
||
| 322 | 1 | $nodesList = $this->node->getElementsByTagName($name); |
|
| 323 | |||
| 324 | 1 | $elements = new SimpleHtmlDomNode(); |
|
| 325 | |||
| 326 | 1 | foreach ($nodesList as $node) { |
|
| 327 | 1 | $elements[] = new self($node); |
|
| 328 | } |
||
| 329 | |||
| 330 | 1 | if (null === $idx) { |
|
| 331 | 1 | return $elements; |
|
| 332 | } else { |
||
| 333 | if ($idx < 0) { |
||
| 334 | $idx = count($elements) + $idx; |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | if (isset($elements[$idx])) { |
||
| 339 | return $elements[$idx]; |
||
| 340 | } else { |
||
| 341 | return new SimpleHtmlDomNodeBlank(); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Create a new "HtmlDomParser"-object from the current context. |
||
| 347 | * |
||
| 348 | * @return HtmlDomParser |
||
| 349 | */ |
||
| 350 | 51 | public function getHtmlDomParser() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Retrieve an external iterator |
||
| 357 | * |
||
| 358 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 359 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
| 360 | * <b>Traversable</b> |
||
| 361 | */ |
||
| 362 | 2 | public function getIterator() |
|
| 363 | { |
||
| 364 | 2 | $elements = new SimpleHtmlDomNode(); |
|
| 365 | 2 | if ($this->node->hasChildNodes()) { |
|
| 366 | 2 | foreach ($this->node->childNodes as $node) { |
|
| 367 | 2 | $elements[] = new self($node); |
|
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | 2 | return $elements; |
|
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @return DOMNode |
||
| 376 | */ |
||
| 377 | 52 | public function getNode() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Determine if an attribute exists on the element. |
||
| 384 | * |
||
| 385 | * @param $name |
||
| 386 | * |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | 1 | public function hasAttribute($name) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Get dom node's outer html |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 18 | public function html() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Get dom node's inner html |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | 9 | public function innerHtml() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Returns the last child of node |
||
| 416 | * |
||
| 417 | * @return SimpleHtmlDom|null |
||
| 418 | */ |
||
| 419 | 4 | public function lastChild() |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Returns the next sibling of node |
||
| 432 | * |
||
| 433 | * @return SimpleHtmlDom|null |
||
| 434 | */ |
||
| 435 | 1 | public function nextSibling() |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Returns the parent of node |
||
| 448 | * |
||
| 449 | * @return SimpleHtmlDom |
||
| 450 | */ |
||
| 451 | 1 | public function parentNode() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Returns the previous sibling of node |
||
| 458 | * |
||
| 459 | * @return SimpleHtmlDom|null |
||
| 460 | */ |
||
| 461 | 1 | public function previousSibling() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Replace child node |
||
| 474 | * |
||
| 475 | * @param $string |
||
| 476 | * |
||
| 477 | * @return $this |
||
| 478 | */ |
||
| 479 | 5 | protected function replaceChild($string) |
|
| 480 | { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Replace this node |
||
| 507 | * |
||
| 508 | * @param $string |
||
| 509 | * |
||
| 510 | * @return $this |
||
| 511 | */ |
||
| 512 | 3 | protected function replaceNode($string) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Normalize the given string for comparision. |
||
| 538 | * |
||
| 539 | * @param $string |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | 6 | private function normalizeStringForComparision($string) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * @param HtmlDomParser $newDocument |
||
| 550 | * |
||
| 551 | * @return HtmlDomParser |
||
| 552 | */ |
||
| 553 | 6 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * change the name of a tag in a "DOMNode" |
||
| 586 | * |
||
| 587 | * @param DOMNode $node |
||
| 588 | * @param string $name |
||
| 589 | * |
||
| 590 | * @return DOMElement |
||
| 591 | */ |
||
| 592 | 4 | protected function changeElementName(\DOMNode $node, $name) |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Set attribute value |
||
| 609 | * |
||
| 610 | * @param string $name |
||
| 611 | * @param string|null $value Set to NULL or empty string, to remove the attribute. |
||
| 612 | * @param bool $strict $value must be NULL, to remove the attribute, |
||
| 613 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
| 614 | * |
||
| 615 | * @return $this |
||
| 616 | */ |
||
| 617 | 8 | public function setAttribute($name, $value = null, $strict = false) |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Remove attribute |
||
| 634 | * |
||
| 635 | * @param $name |
||
| 636 | * |
||
| 637 | * @return mixed |
||
| 638 | */ |
||
| 639 | 1 | public function removeAttribute($name) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Get dom node's plain text |
||
| 648 | * |
||
| 649 | * @return string |
||
| 650 | */ |
||
| 651 | 15 | public function text() |
|
| 655 | } |
||
| 656 |
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.