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 |
||
37 | class SimpleHtmlDom implements \IteratorAggregate |
||
38 | { |
||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected static $functionAliases = array( |
||
43 | 'children' => 'childNodes', |
||
44 | 'first_child' => 'firstChild', |
||
45 | 'last_child' => 'lastChild', |
||
46 | 'next_sibling' => 'nextSibling', |
||
47 | 'prev_sibling' => 'previousSibling', |
||
48 | 'parent' => 'parentNode', |
||
49 | 'outertext' => 'html', |
||
50 | 'outerhtml' => 'html', |
||
51 | 'innertext' => 'innerHtml', |
||
52 | 'innerhtml' => 'innerHtml', |
||
53 | ); |
||
54 | |||
55 | /** |
||
56 | * @var DOMElement |
||
57 | */ |
||
58 | protected $node; |
||
59 | |||
60 | /** |
||
61 | * SimpleHtmlDom constructor. |
||
62 | * |
||
63 | * @param DOMNode $node |
||
64 | */ |
||
65 | 97 | public function __construct(DOMNode $node) |
|
69 | |||
70 | /** |
||
71 | * @param string $name |
||
72 | * @param array $arguments |
||
73 | * |
||
74 | * @return null|string|SimpleHtmlDom |
||
75 | * |
||
76 | * @throws \BadMethodCallException |
||
77 | */ |
||
78 | 9 | View Code Duplication | public function __call($name, $arguments) |
88 | |||
89 | /** |
||
90 | * @param string $name |
||
91 | * |
||
92 | * @return array|null|string |
||
93 | */ |
||
94 | 42 | public function __get($name) |
|
95 | { |
||
96 | 42 | $name = \strtolower($name); |
|
97 | |||
98 | 42 | switch ($name) { |
|
99 | case 'outerhtml': |
||
100 | case 'outertext': |
||
101 | 17 | return $this->html(); |
|
102 | case 'innerhtml': |
||
103 | case 'innertext': |
||
104 | 11 | return $this->innerHtml(); |
|
105 | case 'text': |
||
106 | case 'plaintext': |
||
107 | 15 | return $this->text(); |
|
108 | case 'tag': |
||
109 | 4 | return $this->node->nodeName; |
|
110 | case 'attr': |
||
111 | return $this->getAllAttributes(); |
||
112 | default: |
||
113 | 10 | return $this->getAttribute($name); |
|
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $selector |
||
119 | * @param int $idx |
||
120 | * |
||
121 | * @return SimpleHtmlDom[]|SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
122 | */ |
||
123 | 12 | public function __invoke($selector, $idx = null) |
|
127 | |||
128 | /** |
||
129 | * @param $name |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | 1 | public function __isset($name) |
|
134 | { |
||
135 | 1 | $name = strtolower($name); |
|
136 | |||
137 | 1 | switch ($name) { |
|
138 | case 'outertext': |
||
139 | case 'outerhtml': |
||
140 | case 'innertext': |
||
141 | case 'innerhtml': |
||
142 | case 'plaintext': |
||
143 | case 'text': |
||
144 | case 'tag': |
||
145 | return true; |
||
146 | default: |
||
147 | 1 | return $this->hasAttribute($name); |
|
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param $name |
||
153 | * @param $value |
||
154 | * |
||
155 | * @return SimpleHtmlDom |
||
156 | */ |
||
157 | 14 | public function __set($name, $value) |
|
158 | { |
||
159 | 14 | $name = strtolower($name); |
|
160 | |||
161 | 14 | switch ($name) { |
|
162 | case 'outerhtml': |
||
163 | case 'outertext': |
||
164 | 3 | return $this->replaceNode($value); |
|
165 | case 'innertext': |
||
166 | case 'innerhtml': |
||
167 | 7 | return $this->replaceChild($value); |
|
168 | default: |
||
169 | 8 | return $this->setAttribute($name, $value); |
|
170 | } |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return string |
||
175 | */ |
||
176 | 2 | public function __toString() |
|
180 | |||
181 | /** |
||
182 | * @param $name |
||
183 | * |
||
184 | * @return SimpleHtmlDom |
||
185 | */ |
||
186 | 1 | public function __unset($name) |
|
190 | |||
191 | /** |
||
192 | * Returns children of node. |
||
193 | * |
||
194 | * @param int $idx |
||
195 | * |
||
196 | * @return SimpleHtmlDomNode|SimpleHtmlDom|null |
||
197 | */ |
||
198 | 2 | public function childNodes(int $idx = -1) |
|
212 | |||
213 | /** |
||
214 | * Find list of nodes with a CSS selector. |
||
215 | * |
||
216 | * @param string $selector |
||
217 | * @param int|null $idx |
||
218 | * |
||
219 | * @return SimpleHtmlDom[]|SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
220 | */ |
||
221 | 26 | public function find(string $selector, $idx = null) |
|
225 | |||
226 | /** |
||
227 | * Find one node with a CSS selector. |
||
228 | * |
||
229 | * @param string $selector |
||
230 | * |
||
231 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
232 | */ |
||
233 | public function findOne(string $selector) |
||
237 | |||
238 | /** |
||
239 | * Returns the first child of node. |
||
240 | * |
||
241 | * @return SimpleHtmlDom|null |
||
242 | */ |
||
243 | 4 | public function firstChild() |
|
253 | |||
254 | /** |
||
255 | * Returns an array of attributes. |
||
256 | * |
||
257 | * @return array|null |
||
258 | */ |
||
259 | 2 | public function getAllAttributes() |
|
272 | |||
273 | /** |
||
274 | * Return attribute value. |
||
275 | * |
||
276 | * @param string $name |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | 13 | public function getAttribute(string $name): string |
|
286 | |||
287 | /** |
||
288 | * Return element by #id. |
||
289 | * |
||
290 | * @param string $id |
||
291 | * |
||
292 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
293 | */ |
||
294 | 1 | public function getElementById(string $id) |
|
298 | |||
299 | /** |
||
300 | * Return element by tag name. |
||
301 | * |
||
302 | * @param string $name |
||
303 | * |
||
304 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
305 | */ |
||
306 | 1 | public function getElementByTagName(string $name) |
|
316 | |||
317 | /** |
||
318 | * Returns elements by #id. |
||
319 | * |
||
320 | * @param string $id |
||
321 | * @param null|int $idx |
||
322 | * |
||
323 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
324 | */ |
||
325 | public function getElementsById(string $id, $idx = null) |
||
329 | |||
330 | /** |
||
331 | * Returns elements by tag name. |
||
332 | * |
||
333 | * @param string $name |
||
334 | * @param null|int $idx |
||
335 | * |
||
336 | * @return SimpleHtmlDomNode|SimpleHtmlDom[]|SimpleHtmlDomNodeBlank |
||
337 | */ |
||
338 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
366 | |||
367 | /** |
||
368 | * Create a new "HtmlDomParser"-object from the current context. |
||
369 | * |
||
370 | * @return HtmlDomParser |
||
371 | */ |
||
372 | 52 | public function getHtmlDomParser(): HtmlDomParser |
|
376 | |||
377 | /** |
||
378 | * Retrieve an external iterator. |
||
379 | * |
||
380 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
381 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
382 | * <b>Traversable</b> |
||
383 | */ |
||
384 | 2 | public function getIterator(): SimpleHtmlDomNode |
|
395 | |||
396 | /** |
||
397 | * @return DOMNode |
||
398 | */ |
||
399 | 53 | public function getNode(): \DOMNode |
|
403 | |||
404 | /** |
||
405 | * Determine if an attribute exists on the element. |
||
406 | * |
||
407 | * @param string $name |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | 1 | public function hasAttribute(string $name): bool |
|
415 | |||
416 | /** |
||
417 | * Get dom node's outer html. |
||
418 | * |
||
419 | * @param bool $multiDecodeNewHtmlEntity |
||
420 | * |
||
421 | * @return string |
||
422 | */ |
||
423 | 18 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
|
427 | |||
428 | /** |
||
429 | * Get dom node's inner html. |
||
430 | * |
||
431 | * @param bool $multiDecodeNewHtmlEntity |
||
432 | * |
||
433 | * @return string |
||
434 | */ |
||
435 | 11 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
|
439 | |||
440 | /** |
||
441 | * Returns the last child of node. |
||
442 | * |
||
443 | * @return SimpleHtmlDom|null |
||
444 | */ |
||
445 | 4 | public function lastChild() |
|
455 | |||
456 | /** |
||
457 | * Returns the next sibling of node. |
||
458 | * |
||
459 | * @return SimpleHtmlDom|null |
||
460 | */ |
||
461 | 1 | public function nextSibling() |
|
471 | |||
472 | /** |
||
473 | * Returns the parent of node. |
||
474 | * |
||
475 | * @return SimpleHtmlDom |
||
476 | */ |
||
477 | 1 | public function parentNode(): self |
|
481 | |||
482 | /** |
||
483 | * Returns the previous sibling of node. |
||
484 | * |
||
485 | * @return SimpleHtmlDom|null |
||
486 | */ |
||
487 | 1 | public function previousSibling() |
|
497 | |||
498 | /** |
||
499 | * Replace child node. |
||
500 | * |
||
501 | * @param string $string |
||
502 | * |
||
503 | * @return $this |
||
504 | * |
||
505 | * @throws \RuntimeException |
||
506 | */ |
||
507 | 7 | protected function replaceChild(string $string) |
|
532 | |||
533 | /** |
||
534 | * Replace this node. |
||
535 | * |
||
536 | * @param string $string |
||
537 | * |
||
538 | * @return $this|null |
||
539 | * |
||
540 | * @throws \RuntimeException |
||
541 | */ |
||
542 | 3 | protected function replaceNode(string $string) |
|
565 | |||
566 | /** |
||
567 | * Normalize the given input for comparision. |
||
568 | * |
||
569 | * @param HtmlDomParser|string $input |
||
570 | * |
||
571 | * @return string |
||
572 | */ |
||
573 | 8 | private function normalizeStringForComparision($input): string |
|
608 | |||
609 | /** |
||
610 | * @param HtmlDomParser $newDocument |
||
611 | * |
||
612 | * @return HtmlDomParser |
||
613 | */ |
||
614 | 8 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument): HtmlDomParser |
|
657 | |||
658 | /** |
||
659 | * Change the name of a tag in a "DOMNode". |
||
660 | * |
||
661 | * @param DOMNode $node |
||
662 | * @param string $name |
||
663 | * |
||
664 | * @return DOMElement |
||
665 | */ |
||
666 | 4 | protected function changeElementName(\DOMNode $node, string $name): \DOMElement |
|
683 | |||
684 | /** |
||
685 | * Set attribute value. |
||
686 | * |
||
687 | * @param string $name <p>The name of the html-attribute.</p> |
||
688 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
689 | * @param bool $strict </p> |
||
690 | * $value must be NULL, to remove the attribute, |
||
691 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
692 | * </p> |
||
693 | * |
||
694 | * @return $this |
||
695 | */ |
||
696 | 9 | public function setAttribute(string $name, $value = null, bool $strict = false) |
|
710 | |||
711 | /** |
||
712 | * Remove attribute. |
||
713 | * |
||
714 | * @param string $name <p>The name of the html-attribute.</p> |
||
715 | * |
||
716 | * @return mixed |
||
717 | */ |
||
718 | 1 | public function removeAttribute(string $name) |
|
724 | |||
725 | /** |
||
726 | * Get dom node's plain text. |
||
727 | * |
||
728 | * @return string |
||
729 | */ |
||
730 | 15 | public function text(): string |
|
734 | } |
||
735 |
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.