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 |
||
35 | class SimpleHtmlDom implements \IteratorAggregate |
||
36 | { |
||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected static $functionAliases = array( |
||
41 | 'children' => 'childNodes', |
||
42 | 'first_child' => 'firstChild', |
||
43 | 'last_child' => 'lastChild', |
||
44 | 'next_sibling' => 'nextSibling', |
||
45 | 'prev_sibling' => 'previousSibling', |
||
46 | 'parent' => 'parentNode', |
||
47 | 'outertext' => 'html', |
||
48 | 'outerhtml' => 'html', |
||
49 | 'innertext' => 'innerHtml', |
||
50 | 'innerhtml' => 'innerHtml', |
||
51 | ); |
||
52 | |||
53 | /** |
||
54 | * @var DOMElement |
||
55 | */ |
||
56 | protected $node; |
||
57 | |||
58 | /** |
||
59 | * SimpleHtmlDom constructor. |
||
60 | * |
||
61 | * @param DOMNode $node |
||
62 | */ |
||
63 | 97 | public function __construct(DOMNode $node) |
|
67 | |||
68 | /** |
||
69 | * @param $name |
||
70 | * @param $arguments |
||
71 | * |
||
72 | * @return null|string|SimpleHtmlDom |
||
73 | * |
||
74 | * @throws \BadMethodCallException |
||
75 | */ |
||
76 | 9 | View Code Duplication | public function __call($name, $arguments) |
86 | |||
87 | /** |
||
88 | * @param string $name |
||
89 | * |
||
90 | * @return array|null|string |
||
91 | */ |
||
92 | 43 | public function __get($name) |
|
114 | |||
115 | /** |
||
116 | * @param string $selector |
||
117 | * @param int $idx |
||
118 | * |
||
119 | * @return SimpleHtmlDom[]|SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
120 | */ |
||
121 | 12 | public function __invoke($selector, $idx = null) |
|
125 | |||
126 | /** |
||
127 | * @param $name |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | 1 | public function __isset($name) |
|
148 | |||
149 | /** |
||
150 | * @param $name |
||
151 | * @param $value |
||
152 | * |
||
153 | * @return SimpleHtmlDom |
||
154 | */ |
||
155 | 14 | public function __set($name, $value) |
|
170 | |||
171 | /** |
||
172 | * @return string |
||
173 | */ |
||
174 | 2 | public function __toString() |
|
178 | |||
179 | /** |
||
180 | * @param $name |
||
181 | * |
||
182 | * @return SimpleHtmlDom |
||
183 | */ |
||
184 | 1 | public function __unset($name) |
|
188 | |||
189 | /** |
||
190 | * Returns children of node. |
||
191 | * |
||
192 | * @param int $idx |
||
193 | * |
||
194 | * @return SimpleHtmlDomNode|SimpleHtmlDom|null |
||
195 | */ |
||
196 | 2 | public function childNodes($idx = -1) |
|
210 | |||
211 | /** |
||
212 | * Find list of nodes with a CSS selector. |
||
213 | * |
||
214 | * @param string $selector |
||
215 | * @param int|null $idx |
||
216 | * |
||
217 | * @return SimpleHtmlDom[]|SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
218 | */ |
||
219 | 26 | public function find($selector, $idx = null) |
|
223 | |||
224 | /** |
||
225 | * Returns the first child of node. |
||
226 | * |
||
227 | * @return SimpleHtmlDom|null |
||
228 | */ |
||
229 | 4 | public function firstChild() |
|
239 | |||
240 | /** |
||
241 | * Returns an array of attributes. |
||
242 | * |
||
243 | * @return array|null |
||
244 | */ |
||
245 | 2 | public function getAllAttributes() |
|
258 | |||
259 | /** |
||
260 | * Return attribute value. |
||
261 | * |
||
262 | * @param string $name |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | 13 | public function getAttribute($name) |
|
272 | |||
273 | /** |
||
274 | * Return element by #id. |
||
275 | * |
||
276 | * @param string $id |
||
277 | * |
||
278 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
279 | */ |
||
280 | 1 | public function getElementById($id) |
|
284 | |||
285 | /** |
||
286 | * Return element by tag name. |
||
287 | * |
||
288 | * @param string $name |
||
289 | * |
||
290 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
291 | */ |
||
292 | 1 | public function getElementByTagName($name) |
|
302 | |||
303 | /** |
||
304 | * Returns elements by #id. |
||
305 | * |
||
306 | * @param string $id |
||
307 | * @param null|int $idx |
||
308 | * |
||
309 | * @return SimpleHtmlDom[]|SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
310 | */ |
||
311 | public function getElementsById($id, $idx = null) |
||
315 | |||
316 | /** |
||
317 | * Returns elements by tag name. |
||
318 | * |
||
319 | * @param string $name |
||
320 | * @param null|int $idx |
||
321 | * |
||
322 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
323 | */ |
||
324 | 1 | View Code Duplication | public function getElementsByTagName($name, $idx = null) |
352 | |||
353 | /** |
||
354 | * Create a new "HtmlDomParser"-object from the current context. |
||
355 | * |
||
356 | * @return HtmlDomParser |
||
357 | */ |
||
358 | 52 | public function getHtmlDomParser() |
|
362 | |||
363 | /** |
||
364 | * Retrieve an external iterator. |
||
365 | * |
||
366 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
367 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
368 | * <b>Traversable</b> |
||
369 | */ |
||
370 | 2 | public function getIterator() |
|
381 | |||
382 | /** |
||
383 | * @return DOMNode |
||
384 | */ |
||
385 | 53 | public function getNode() |
|
389 | |||
390 | /** |
||
391 | * Determine if an attribute exists on the element. |
||
392 | * |
||
393 | * @param $name |
||
394 | * |
||
395 | * @return bool |
||
396 | */ |
||
397 | 1 | public function hasAttribute($name) |
|
401 | |||
402 | /** |
||
403 | * Get dom node's outer html. |
||
404 | * |
||
405 | * @param bool $multiDecodeNewHtmlEntity |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | 18 | public function html($multiDecodeNewHtmlEntity = false) |
|
413 | |||
414 | /** |
||
415 | * Get dom node's inner html. |
||
416 | * |
||
417 | * @param bool $multiDecodeNewHtmlEntity |
||
418 | * |
||
419 | * @return string |
||
420 | */ |
||
421 | 11 | public function innerHtml($multiDecodeNewHtmlEntity = false) |
|
425 | |||
426 | /** |
||
427 | * Returns the last child of node. |
||
428 | * |
||
429 | * @return SimpleHtmlDom|null |
||
430 | */ |
||
431 | 4 | public function lastChild() |
|
441 | |||
442 | /** |
||
443 | * Returns the next sibling of node. |
||
444 | * |
||
445 | * @return SimpleHtmlDom|null |
||
446 | */ |
||
447 | 1 | public function nextSibling() |
|
457 | |||
458 | /** |
||
459 | * Returns the parent of node. |
||
460 | * |
||
461 | * @return SimpleHtmlDom |
||
462 | */ |
||
463 | 1 | public function parentNode() |
|
467 | |||
468 | /** |
||
469 | * Returns the previous sibling of node. |
||
470 | * |
||
471 | * @return SimpleHtmlDom|null |
||
472 | */ |
||
473 | 1 | public function previousSibling() |
|
483 | |||
484 | /** |
||
485 | * Replace child node. |
||
486 | * |
||
487 | * @param $string |
||
488 | * |
||
489 | * @return $this |
||
490 | * |
||
491 | * @throws \RuntimeException |
||
492 | */ |
||
493 | 7 | protected function replaceChild($string) |
|
518 | |||
519 | /** |
||
520 | * Replace this node. |
||
521 | * |
||
522 | * @param $string |
||
523 | * |
||
524 | * @return $this |
||
525 | * |
||
526 | * @throws \RuntimeException |
||
527 | */ |
||
528 | 3 | protected function replaceNode($string) |
|
551 | |||
552 | /** |
||
553 | * Normalize the given input for comparision. |
||
554 | * |
||
555 | * @param HtmlDomParser|string $input |
||
556 | * |
||
557 | * @return string |
||
558 | */ |
||
559 | 8 | private function normalizeStringForComparision($input) |
|
594 | |||
595 | /** |
||
596 | * @param HtmlDomParser $newDocument |
||
597 | * |
||
598 | * @return HtmlDomParser |
||
599 | */ |
||
600 | 8 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
|
640 | |||
641 | /** |
||
642 | * Change the name of a tag in a "DOMNode". |
||
643 | * |
||
644 | * @param DOMNode $node |
||
645 | * @param string $name |
||
646 | * |
||
647 | * @return DOMElement |
||
648 | */ |
||
649 | 4 | protected function changeElementName(\DOMNode $node, $name) |
|
666 | |||
667 | /** |
||
668 | * Set attribute value. |
||
669 | * |
||
670 | * @param string $name <p>The name of the html-attribute.</p> |
||
671 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
672 | * @param bool $strict </p> |
||
673 | * $value must be NULL, to remove the attribute, |
||
674 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
675 | * </p> |
||
676 | * |
||
677 | * @return $this |
||
678 | */ |
||
679 | 9 | public function setAttribute($name, $value = null, $strict = false) |
|
693 | |||
694 | /** |
||
695 | * Remove attribute. |
||
696 | * |
||
697 | * @param $name <p>The name of the html-attribute.</p> |
||
698 | * |
||
699 | * @return mixed |
||
700 | */ |
||
701 | 1 | public function removeAttribute($name) |
|
707 | |||
708 | /** |
||
709 | * Get dom node's plain text. |
||
710 | * |
||
711 | * @return string |
||
712 | */ |
||
713 | 16 | public function text() |
|
717 | } |
||
718 |
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.