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 | 92 | public function __construct(DOMNode $node) |
|
66 | |||
67 | /** |
||
68 | * @param $name |
||
69 | * @param $arguments |
||
70 | * |
||
71 | * @return null|string|SimpleHtmlDom |
||
72 | * |
||
73 | */ |
||
74 | 8 | public function __call($name, $arguments) |
|
82 | |||
83 | /** |
||
84 | * @param $name |
||
85 | * |
||
86 | * @return array|null|string |
||
87 | */ |
||
88 | 40 | public function __get($name) |
|
110 | |||
111 | /** |
||
112 | * @param string $selector |
||
113 | * @param int $idx |
||
114 | * |
||
115 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
116 | */ |
||
117 | 12 | public function __invoke($selector, $idx = null) |
|
121 | |||
122 | /** |
||
123 | * @param $name |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | 1 | public function __isset($name) |
|
142 | |||
143 | /** |
||
144 | * @param $name |
||
145 | * @param $value |
||
146 | * |
||
147 | * @return SimpleHtmlDom |
||
148 | */ |
||
149 | 11 | public function __set($name, $value) |
|
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | 2 | public function __toString() |
|
172 | |||
173 | /** |
||
174 | * @param $name |
||
175 | * |
||
176 | * @return SimpleHtmlDom |
||
177 | */ |
||
178 | 1 | public function __unset($name) |
|
182 | |||
183 | /** |
||
184 | * Returns children of node |
||
185 | * |
||
186 | * @param int $idx |
||
187 | * |
||
188 | * @return SimpleHtmlDomNode|SimpleHtmlDom|null |
||
189 | */ |
||
190 | 2 | public function childNodes($idx = -1) |
|
204 | |||
205 | /** |
||
206 | * Find list of nodes with a CSS selector |
||
207 | * |
||
208 | * @param string $selector |
||
209 | * @param int $idx |
||
210 | * |
||
211 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
212 | */ |
||
213 | 25 | public function find($selector, $idx = null) |
|
217 | |||
218 | /** |
||
219 | * Returns the first child of node |
||
220 | * |
||
221 | * @return SimpleHtmlDom|null |
||
222 | */ |
||
223 | 4 | public function firstChild() |
|
233 | |||
234 | /** |
||
235 | * Returns array of attributes |
||
236 | * |
||
237 | * @return array|null |
||
238 | */ |
||
239 | 1 | public function getAllAttributes() |
|
252 | |||
253 | /** |
||
254 | * Return attribute value |
||
255 | * |
||
256 | * @param string $name |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | 12 | public function getAttribute($name) |
|
266 | |||
267 | /** |
||
268 | * Return SimpleHtmlDom by id. |
||
269 | * |
||
270 | * @param string $id |
||
271 | * |
||
272 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
273 | */ |
||
274 | 1 | public function getElementById($id) |
|
278 | |||
279 | /** |
||
280 | * Return SimpleHtmlDom by tag name. |
||
281 | * |
||
282 | * @param string $name |
||
283 | * |
||
284 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
285 | */ |
||
286 | 1 | public function getElementByTagName($name) |
|
296 | |||
297 | /** |
||
298 | * Returns Elements by id |
||
299 | * |
||
300 | * @param string $id |
||
301 | * @param null|int $idx |
||
302 | * |
||
303 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
304 | */ |
||
305 | public function getElementsById($id, $idx = null) |
||
309 | |||
310 | /** |
||
311 | * Returns Elements by tag name |
||
312 | * |
||
313 | * @param string $name |
||
314 | * @param null|int $idx |
||
315 | * |
||
316 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
317 | */ |
||
318 | 1 | View Code Duplication | public function getElementsByTagName($name, $idx = null) |
342 | |||
343 | /** |
||
344 | * Create a new "HtmlDomParser"-object from the current context. |
||
345 | * |
||
346 | * @return HtmlDomParser |
||
347 | */ |
||
348 | 50 | public function getHtmlDomParser() |
|
352 | |||
353 | /** |
||
354 | * Retrieve an external iterator |
||
355 | * |
||
356 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
357 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
358 | * <b>Traversable</b> |
||
359 | */ |
||
360 | 2 | public function getIterator() |
|
371 | |||
372 | /** |
||
373 | * @return DOMNode |
||
374 | */ |
||
375 | 51 | public function getNode() |
|
379 | |||
380 | /** |
||
381 | * Determine if an attribute exists on the element. |
||
382 | * |
||
383 | * @param $name |
||
384 | * |
||
385 | * @return bool |
||
386 | */ |
||
387 | 1 | public function hasAttribute($name) |
|
391 | |||
392 | /** |
||
393 | * Get dom node's outer html |
||
394 | * |
||
395 | * @return string |
||
396 | */ |
||
397 | 18 | public function html() |
|
401 | |||
402 | /** |
||
403 | * Get dom node's inner html |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | 8 | public function innerHtml() |
|
411 | |||
412 | /** |
||
413 | * Returns the last child of node |
||
414 | * |
||
415 | * @return SimpleHtmlDom|null |
||
416 | */ |
||
417 | 4 | public function lastChild() |
|
427 | |||
428 | /** |
||
429 | * Returns the next sibling of node |
||
430 | * |
||
431 | * @return SimpleHtmlDom|null |
||
432 | */ |
||
433 | 1 | public function nextSibling() |
|
443 | |||
444 | /** |
||
445 | * Returns the parent of node |
||
446 | * |
||
447 | * @return SimpleHtmlDom |
||
448 | */ |
||
449 | 1 | public function parentNode() |
|
453 | |||
454 | /** |
||
455 | * Returns the previous sibling of node |
||
456 | * |
||
457 | * @return SimpleHtmlDom|null |
||
458 | */ |
||
459 | 1 | public function previousSibling() |
|
469 | |||
470 | /** |
||
471 | * Replace child node |
||
472 | * |
||
473 | * @param $string |
||
474 | * |
||
475 | * @return $this |
||
476 | */ |
||
477 | 5 | protected function replaceChild($string) |
|
502 | |||
503 | /** |
||
504 | * Replace this node |
||
505 | * |
||
506 | * @param $string |
||
507 | * |
||
508 | * @return $this |
||
509 | */ |
||
510 | 3 | protected function replaceNode($string) |
|
537 | |||
538 | /** |
||
539 | * Normalize the given string for comparision. |
||
540 | * |
||
541 | * @param $string |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | 6 | private function normalizeStringForComparision($string) |
|
549 | |||
550 | /** |
||
551 | * @param HtmlDomParser $newDocument |
||
552 | * |
||
553 | * @return HtmlDomParser |
||
554 | */ |
||
555 | 6 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
|
585 | |||
586 | /** |
||
587 | * change the name of a tag in a "DOMNode" |
||
588 | * |
||
589 | * @param DOMNode $node |
||
590 | * @param string $name |
||
591 | * |
||
592 | * @return DOMElement |
||
593 | */ |
||
594 | 4 | protected function changeElementName(\DOMNode $node, $name) |
|
608 | |||
609 | /** |
||
610 | * Set attribute value |
||
611 | * |
||
612 | * @param $name |
||
613 | * @param $value |
||
614 | * |
||
615 | * @return $this |
||
616 | */ |
||
617 | 7 | public function setAttribute($name, $value) |
|
627 | |||
628 | /** |
||
629 | * Get dom node's plain text |
||
630 | * |
||
631 | * @return string |
||
632 | */ |
||
633 | 15 | public function text() |
|
637 | } |
||
638 |
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.