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) |
|
504 | |||
505 | /** |
||
506 | * Replace this node |
||
507 | * |
||
508 | * @param $string |
||
509 | * |
||
510 | * @return $this |
||
511 | */ |
||
512 | 3 | protected function replaceNode($string) |
|
539 | |||
540 | /** |
||
541 | * Normalize the given string for comparision. |
||
542 | * |
||
543 | * @param $string |
||
544 | * |
||
545 | * @return string |
||
546 | */ |
||
547 | 6 | private function normalizeStringForComparision($string) |
|
551 | |||
552 | /** |
||
553 | * @param HtmlDomParser $newDocument |
||
554 | * |
||
555 | * @return HtmlDomParser |
||
556 | */ |
||
557 | 6 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
|
587 | |||
588 | /** |
||
589 | * change the name of a tag in a "DOMNode" |
||
590 | * |
||
591 | * @param DOMNode $node |
||
592 | * @param string $name |
||
593 | * |
||
594 | * @return DOMElement |
||
595 | */ |
||
596 | 4 | protected function changeElementName(\DOMNode $node, $name) |
|
610 | |||
611 | /** |
||
612 | * Set attribute value |
||
613 | * |
||
614 | * @param string $name |
||
615 | * @param string|null $value Set to NULL or empty string, to remove the attribute. |
||
616 | * @param bool $strict $value must be NULL, to remove the attribute, |
||
617 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
618 | * |
||
619 | * @return $this |
||
620 | */ |
||
621 | 8 | public function setAttribute($name, $value = null, $strict = false) |
|
635 | |||
636 | /** |
||
637 | * Remove attribute |
||
638 | * |
||
639 | * @param $name |
||
640 | * |
||
641 | * @return mixed |
||
642 | */ |
||
643 | 1 | public function removeAttribute($name) |
|
649 | |||
650 | /** |
||
651 | * Get dom node's plain text |
||
652 | * |
||
653 | * @return string |
||
654 | */ |
||
655 | 15 | public function text() |
|
659 | } |
||
660 |
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.