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 | 94 | 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) |
84 | |||
85 | /** |
||
86 | * @param $name |
||
87 | * |
||
88 | * @return array|null|string |
||
89 | */ |
||
90 | 41 | public function __get($name) |
|
112 | |||
113 | /** |
||
114 | * @param string $selector |
||
115 | * @param int $idx |
||
116 | * |
||
117 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
118 | */ |
||
119 | 12 | public function __invoke($selector, $idx = null) |
|
123 | |||
124 | /** |
||
125 | * @param $name |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | 1 | public function __isset($name) |
|
144 | |||
145 | /** |
||
146 | * @param $name |
||
147 | * @param $value |
||
148 | * |
||
149 | * @return SimpleHtmlDom |
||
150 | */ |
||
151 | 12 | public function __set($name, $value) |
|
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() |
|
254 | |||
255 | /** |
||
256 | * Return attribute value |
||
257 | * |
||
258 | * @param string $name |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | 12 | 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) |
|
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) |
||
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) |
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() |
|
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 | { |
||
481 | 5 | if (!empty($string)) { |
|
482 | 4 | $newDocument = new HtmlDomParser($string); |
|
483 | |||
484 | 4 | if ($this->normalizeStringForComparision($newDocument->outertext) != $this->normalizeStringForComparision($string)) { |
|
485 | throw new RuntimeException('Not valid HTML fragment'); |
||
486 | } |
||
487 | 4 | } |
|
488 | |||
489 | /** @noinspection PhpParamsInspection */ |
||
490 | 5 | if (count($this->node->childNodes) > 0) { |
|
491 | 5 | foreach ($this->node->childNodes as $node) { |
|
492 | 5 | $this->node->removeChild($node); |
|
493 | 5 | } |
|
494 | 5 | } |
|
495 | |||
496 | 5 | if (!empty($newDocument)) { |
|
497 | 4 | $newDocument = $this->cleanHtmlWrapper($newDocument); |
|
498 | 4 | $newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
|
499 | 4 | $this->node->appendChild($newNode); |
|
500 | 4 | } |
|
501 | |||
502 | 5 | return $this; |
|
503 | } |
||
504 | |||
505 | /** |
||
506 | * Replace this node |
||
507 | * |
||
508 | * @param $string |
||
509 | * |
||
510 | * @return $this |
||
511 | */ |
||
512 | 3 | protected function replaceNode($string) |
|
513 | { |
||
514 | 3 | if (empty($string)) { |
|
515 | 2 | $this->node->parentNode->removeChild($this->node); |
|
516 | |||
517 | 2 | return null; |
|
518 | } |
||
519 | |||
520 | 2 | $newDocument = new HtmlDomParser($string); |
|
521 | |||
522 | 2 | if ($this->normalizeStringForComparision($newDocument->outertext) != $this->normalizeStringForComparision($string)) { |
|
523 | throw new RuntimeException('Not valid HTML fragment'); |
||
524 | } |
||
525 | |||
526 | 2 | $newDocument = $this->cleanHtmlWrapper($newDocument); |
|
527 | |||
528 | 2 | $newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
|
529 | |||
530 | 2 | $this->node->parentNode->replaceChild($newNode, $this->node); |
|
531 | 2 | $this->node = $newNode; |
|
532 | |||
533 | 2 | return $this; |
|
534 | } |
||
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) |
|
554 | { |
||
555 | 6 | if ($newDocument->getIsDOMDocumentCreatedWithoutHtml() === true) { |
|
556 | |||
557 | // Remove doc-type node. |
||
558 | 4 | $newDocument->getDocument()->doctype->parentNode->removeChild($newDocument->getDocument()->doctype); |
|
559 | |||
560 | // Remove html element, preserving child nodes. |
||
561 | 4 | $html = $newDocument->getDocument()->getElementsByTagName('html')->item(0); |
|
562 | 4 | $fragment = $newDocument->getDocument()->createDocumentFragment(); |
|
563 | 4 | while ($html->childNodes->length > 0) { |
|
564 | 4 | $fragment->appendChild($html->childNodes->item(0)); |
|
565 | 4 | } |
|
566 | 4 | $html->parentNode->replaceChild($fragment, $html); |
|
567 | |||
568 | // Remove body element, preserving child nodes. |
||
569 | 4 | $body = $newDocument->getDocument()->getElementsByTagName('body')->item(0); |
|
570 | 4 | $fragment = $newDocument->getDocument()->createDocumentFragment(); |
|
571 | 4 | while ($body->childNodes->length > 0) { |
|
572 | 4 | $fragment->appendChild($body->childNodes->item(0)); |
|
573 | 4 | } |
|
574 | 4 | $body->parentNode->replaceChild($fragment, $body); |
|
575 | |||
576 | // At this point DOMDocument still added a "<p>"-wrapper around our string, |
||
577 | // so we replace it with "<simpleHtmlDomP>" and delete this at the ending ... |
||
578 | 4 | $this->changeElementName($newDocument->getDocument()->getElementsByTagName('p')->item(0), 'simpleHtmlDomP'); |
|
579 | 4 | } |
|
580 | |||
581 | 6 | return $newDocument; |
|
582 | } |
||
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) |
|
593 | { |
||
594 | 4 | $newnode = $node->ownerDocument->createElement($name); |
|
595 | 4 | foreach ($node->childNodes as $child) { |
|
596 | 4 | $child = $node->ownerDocument->importNode($child, true); |
|
597 | 4 | $newnode->appendChild($child); |
|
598 | 4 | } |
|
599 | 4 | foreach ($node->attributes as $attrName => $attrNode) { |
|
600 | $newnode->setAttribute($attrName, $attrNode); |
||
601 | 4 | } |
|
602 | 4 | $newnode->ownerDocument->replaceChild($newnode, $node); |
|
603 | |||
604 | 4 | return $newnode; |
|
605 | } |
||
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) |
|
618 | { |
||
619 | if ( |
||
620 | 8 | ($strict === true && null === $value) |
|
621 | || |
||
622 | 8 | ($strict === false && empty($value)) |
|
623 | 8 | ) { |
|
624 | 1 | $this->node->removeAttribute($name); |
|
625 | 1 | } else { |
|
626 | 8 | $this->node->setAttribute($name, $value); |
|
627 | } |
||
628 | |||
629 | 8 | return $this; |
|
630 | } |
||
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.