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 | 69 | ||
57 | /** |
||
58 | 69 | * SimpleHtmlDom constructor. |
|
59 | 69 | * |
|
60 | * @param DOMNode $node |
||
61 | */ |
||
62 | public function __construct(DOMNode $node) |
||
66 | |||
67 | /** |
||
68 | 8 | * @param $name |
|
69 | * @param $arguments |
||
70 | 8 | * |
|
71 | 8 | * @return null|string|SimpleHtmlDom |
|
72 | * |
||
73 | */ |
||
74 | public function __call($name, $arguments) |
||
82 | 23 | ||
83 | /** |
||
84 | * @param $name |
||
85 | 23 | * |
|
86 | 12 | * @return array|null|string |
|
87 | 18 | */ |
|
88 | 3 | public function __get($name) |
|
110 | |||
111 | /** |
||
112 | * @param string $selector |
||
113 | * @param int $idx |
||
114 | * |
||
115 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
116 | 1 | */ |
|
117 | public function __invoke($selector, $idx = null) |
||
121 | 1 | ||
122 | 1 | /** |
|
123 | * @param $name |
||
124 | * |
||
125 | 1 | * @return bool |
|
126 | */ |
||
127 | public function __isset($name) |
||
140 | 6 | ||
141 | 3 | /** |
|
142 | * @param $name |
||
143 | 5 | * @param $value |
|
144 | * |
||
145 | * @return SimpleHtmlDom |
||
146 | */ |
||
147 | public function __set($name, $value) |
||
158 | |||
159 | /** |
||
160 | 1 | * @return string |
|
161 | */ |
||
162 | 1 | public function __toString() |
|
166 | |||
167 | /** |
||
168 | * @param $name |
||
169 | * |
||
170 | * @return SimpleHtmlDom |
||
171 | */ |
||
172 | 2 | public function __unset($name) |
|
176 | 2 | ||
177 | 2 | /** |
|
178 | * Returns children of node |
||
179 | * |
||
180 | 2 | * @param int $idx |
|
181 | 2 | * |
|
182 | * @return SimpleHtmlDomNode|SimpleHtmlDom|null |
||
183 | */ |
||
184 | 1 | public function childNodes($idx = -1) |
|
198 | |||
199 | /** |
||
200 | * Find list of nodes with a CSS selector |
||
201 | * |
||
202 | * @param string $selector |
||
203 | * @param int $idx |
||
204 | * |
||
205 | 4 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
|
206 | */ |
||
207 | 4 | public function find($selector, $idx = null) |
|
211 | |||
212 | /** |
||
213 | 4 | * Returns the first child of node |
|
214 | * |
||
215 | * @return SimpleHtmlDom|null |
||
216 | */ |
||
217 | public function firstChild() |
||
227 | |||
228 | /** |
||
229 | 1 | * Returns array of attributes |
|
230 | * |
||
231 | * @return array|null |
||
232 | 1 | */ |
|
233 | public function getAllAttributes() |
||
246 | 10 | ||
247 | /** |
||
248 | * Return attribute value |
||
249 | * |
||
250 | * @param string $name |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getAttribute($name) |
||
260 | |||
261 | /** |
||
262 | * Return SimpleHtmlDom by id. |
||
263 | * |
||
264 | * @param string $id |
||
265 | * |
||
266 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
267 | */ |
||
268 | 1 | public function getElementById($id) |
|
272 | 1 | ||
273 | 1 | /** |
|
274 | * Return SimpleHtmlDom by tag name. |
||
275 | * |
||
276 | * @param string $name |
||
277 | * |
||
278 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
279 | */ |
||
280 | public function getElementByTagName($name) |
||
290 | |||
291 | /** |
||
292 | * Returns Elements by id |
||
293 | * |
||
294 | * @param string $id |
||
295 | * @param null|int $idx |
||
296 | * |
||
297 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
298 | */ |
||
299 | public function getElementsById($id, $idx = null) |
||
303 | |||
304 | 1 | /** |
|
305 | * Returns Elements by tag name |
||
306 | 1 | * |
|
307 | 1 | * @param string $name |
|
308 | * @param null|int $idx |
||
309 | * |
||
310 | 1 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
|
311 | 1 | */ |
|
312 | View Code Duplication | public function getElementsByTagName($name, $idx = null) |
|
336 | |||
337 | /** |
||
338 | * Create a new "HtmlDomParser"-object from the current context. |
||
339 | * |
||
340 | * @return HtmlDomParser |
||
341 | */ |
||
342 | 2 | public function getHtmlDomParser() |
|
346 | 2 | ||
347 | 2 | /** |
|
348 | * Retrieve an external iterator |
||
349 | * |
||
350 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
351 | 2 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
|
352 | * <b>Traversable</b> |
||
353 | */ |
||
354 | public function getIterator() |
||
365 | |||
366 | /** |
||
367 | * @return DOMNode |
||
368 | */ |
||
369 | 1 | public function getNode() |
|
373 | |||
374 | /** |
||
375 | * Determine if an attribute exists on the element. |
||
376 | * |
||
377 | * @param $name |
||
378 | * |
||
379 | 13 | * @return bool |
|
380 | */ |
||
381 | 13 | public function hasAttribute($name) |
|
385 | |||
386 | /** |
||
387 | * Get dom node's outer html |
||
388 | * |
||
389 | 3 | * @return string |
|
390 | */ |
||
391 | 3 | public function html() |
|
395 | |||
396 | /** |
||
397 | * Get dom node's inner html |
||
398 | * |
||
399 | 4 | * @return string |
|
400 | */ |
||
401 | 4 | public function innerHtml() |
|
405 | |||
406 | /** |
||
407 | 4 | * Returns the last child of node |
|
408 | * |
||
409 | * @return SimpleHtmlDom|null |
||
410 | */ |
||
411 | public function lastChild() |
||
421 | |||
422 | /** |
||
423 | 1 | * Returns the next sibling of node |
|
424 | * |
||
425 | * @return SimpleHtmlDom|null |
||
426 | */ |
||
427 | public function nextSibling() |
||
437 | |||
438 | /** |
||
439 | * Returns the parent of node |
||
440 | * |
||
441 | 1 | * @return SimpleHtmlDom |
|
442 | */ |
||
443 | 1 | public function parentNode() |
|
447 | |||
448 | /** |
||
449 | 1 | * Returns the previous sibling of node |
|
450 | * |
||
451 | * @return SimpleHtmlDom|null |
||
452 | */ |
||
453 | public function previousSibling() |
||
463 | |||
464 | 3 | /** |
|
465 | * Replace child node |
||
466 | * |
||
467 | * @param $string |
||
468 | * |
||
469 | 3 | * @return $this |
|
470 | 3 | */ |
|
471 | protected function replaceChild($string) |
||
496 | |||
497 | 1 | /** |
|
498 | * Replace this node |
||
499 | * |
||
500 | 3 | * @param $string |
|
501 | * |
||
502 | * @return $this |
||
503 | */ |
||
504 | protected function replaceNode($string) |
||
531 | |||
532 | /** |
||
533 | * Normalize the given string for comparision. |
||
534 | * |
||
535 | * @param $string |
||
536 | * |
||
537 | 6 | * @return string |
|
538 | */ |
||
539 | 6 | private function normalizeStringForComparision($string) |
|
543 | |||
544 | /** |
||
545 | 3 | * @param HtmlDomParser $newDocument |
|
546 | 3 | * |
|
547 | 3 | * @return HtmlDomParser |
|
548 | 3 | */ |
|
549 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
||
579 | 3 | ||
580 | 3 | /** |
|
581 | 3 | * change the name of a tag in a "DOMNode" |
|
582 | * |
||
583 | 3 | * @param DOMNode $node |
|
584 | * @param string $name |
||
585 | * |
||
586 | 3 | * @return DOMElement |
|
587 | */ |
||
588 | 3 | protected function changeElementName(\DOMNode $node, $name) |
|
602 | 1 | ||
603 | /** |
||
604 | 5 | * Set attribute value |
|
605 | * |
||
606 | * @param $name |
||
607 | 5 | * @param $value |
|
608 | * |
||
609 | * @return $this |
||
610 | */ |
||
611 | public function setAttribute($name, $value) |
||
621 | |||
622 | /** |
||
623 | * Get dom node's plain text |
||
624 | * |
||
625 | * @return string |
||
626 | */ |
||
627 | public function text() |
||
631 | } |
||
632 |
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.