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 |
||
32 | class SimpleHtmlDom implements \IteratorAggregate |
||
33 | { |
||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected static $functionAliases = [ |
||
38 | 'children' => 'childNodes', |
||
39 | 'first_child' => 'firstChild', |
||
40 | 'last_child' => 'lastChild', |
||
41 | 'next_sibling' => 'nextSibling', |
||
42 | 'prev_sibling' => 'previousSibling', |
||
43 | 'parent' => 'parentNode', |
||
44 | 'outertext' => 'html', |
||
45 | 'outerhtml' => 'html', |
||
46 | 'innertext' => 'innerHtml', |
||
47 | 'innerhtml' => 'innerHtml', |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @var DOMElement |
||
52 | */ |
||
53 | protected $node; |
||
54 | |||
55 | /** |
||
56 | * SimpleHtmlDom constructor. |
||
57 | * |
||
58 | * @param DOMNode $node |
||
59 | */ |
||
60 | 100 | public function __construct(DOMNode $node) |
|
64 | |||
65 | /** |
||
66 | * @param string $name |
||
67 | * @param array $arguments |
||
68 | * |
||
69 | * @throws \BadMethodCallException |
||
70 | * |
||
71 | * @return SimpleHtmlDom|string|null |
||
72 | */ |
||
73 | 9 | View Code Duplication | public function __call($name, $arguments) |
83 | |||
84 | /** |
||
85 | * @param string $name |
||
86 | * |
||
87 | * @return array|string|null |
||
88 | */ |
||
89 | 44 | public function __get($name) |
|
90 | { |
||
91 | 44 | $name = \strtolower($name); |
|
92 | |||
93 | 44 | switch ($name) { |
|
94 | 44 | case 'outerhtml': |
|
95 | 40 | case 'outertext': |
|
96 | 18 | return $this->html(); |
|
97 | 34 | case 'innerhtml': |
|
98 | 28 | case 'innertext': |
|
99 | 11 | return $this->innerHtml(); |
|
100 | 25 | case 'text': |
|
101 | 20 | case 'plaintext': |
|
102 | 16 | return $this->text(); |
|
103 | 11 | case 'tag': |
|
104 | 4 | return $this->node->nodeName; |
|
105 | 10 | case 'attr': |
|
106 | return $this->getAllAttributes(); |
||
107 | default: |
||
108 | 10 | return $this->getAttribute($name); |
|
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param string $selector |
||
114 | * @param int $idx |
||
115 | * |
||
116 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
117 | */ |
||
118 | 12 | public function __invoke($selector, $idx = null) |
|
122 | |||
123 | /** |
||
124 | * @param $name |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | 1 | public function __isset($name) |
|
129 | { |
||
130 | 1 | $name = \strtolower($name); |
|
131 | |||
132 | 1 | switch ($name) { |
|
133 | 1 | case 'outertext': |
|
134 | 1 | case 'outerhtml': |
|
135 | 1 | case 'innertext': |
|
136 | 1 | case 'innerhtml': |
|
137 | 1 | case 'plaintext': |
|
138 | 1 | case 'text': |
|
139 | 1 | case 'tag': |
|
140 | return true; |
||
141 | default: |
||
142 | 1 | return $this->hasAttribute($name); |
|
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param $name |
||
148 | * @param $value |
||
149 | * |
||
150 | * @return SimpleHtmlDom |
||
151 | */ |
||
152 | 14 | public function __set($name, $value) |
|
153 | { |
||
154 | 14 | $name = \strtolower($name); |
|
155 | |||
156 | 14 | switch ($name) { |
|
157 | 14 | case 'outerhtml': |
|
158 | 13 | case 'outertext': |
|
159 | 3 | return $this->replaceNode($value); |
|
160 | 11 | case 'innertext': |
|
161 | 9 | case 'innerhtml': |
|
162 | 7 | return $this->replaceChild($value); |
|
163 | default: |
||
164 | 8 | return $this->setAttribute($name, $value); |
|
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @return string |
||
170 | */ |
||
171 | 2 | public function __toString() |
|
175 | |||
176 | /** |
||
177 | * @param $name |
||
178 | * |
||
179 | * @return SimpleHtmlDom |
||
180 | */ |
||
181 | public function __unset($name) |
||
185 | |||
186 | /** |
||
187 | * Returns children of node. |
||
188 | * |
||
189 | * @param int $idx |
||
190 | * |
||
191 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNode|null |
||
192 | */ |
||
193 | 2 | public function childNodes(int $idx = -1) |
|
207 | |||
208 | /** |
||
209 | * Find list of nodes with a CSS selector. |
||
210 | * |
||
211 | * @param string $selector |
||
212 | * @param int|null $idx |
||
213 | * |
||
214 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
215 | */ |
||
216 | 26 | public function find(string $selector, $idx = null) |
|
220 | |||
221 | /** |
||
222 | * Find one node with a CSS selector. |
||
223 | * |
||
224 | * @param string $selector |
||
225 | * |
||
226 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
227 | */ |
||
228 | public function findOne(string $selector) |
||
232 | |||
233 | /** |
||
234 | * Returns the first child of node. |
||
235 | * |
||
236 | * @return SimpleHtmlDom|null |
||
237 | */ |
||
238 | 4 | public function firstChild() |
|
248 | |||
249 | /** |
||
250 | * Returns an array of attributes. |
||
251 | * |
||
252 | * @return array|null |
||
253 | */ |
||
254 | 2 | public function getAllAttributes() |
|
267 | |||
268 | /** |
||
269 | * Return attribute value. |
||
270 | * |
||
271 | * @param string $name |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | 13 | public function getAttribute(string $name): string |
|
281 | |||
282 | /** |
||
283 | * Return element by #id. |
||
284 | * |
||
285 | * @param string $id |
||
286 | * |
||
287 | * @return SimpleHtmlDom|SimpleHtmlDomNodeInterface |
||
288 | */ |
||
289 | 1 | public function getElementById(string $id) |
|
293 | |||
294 | /** |
||
295 | * Return element by tag name. |
||
296 | * |
||
297 | * @param string $name |
||
298 | * |
||
299 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
300 | */ |
||
301 | 1 | public function getElementByTagName(string $name) |
|
311 | |||
312 | /** |
||
313 | * Returns elements by #id. |
||
314 | * |
||
315 | * @param string $id |
||
316 | * @param int|null $idx |
||
317 | * |
||
318 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeInterface |
||
319 | */ |
||
320 | public function getElementsById(string $id, $idx = null) |
||
324 | |||
325 | /** |
||
326 | * Returns elements by tag name. |
||
327 | * |
||
328 | * @param string $name |
||
329 | * @param int|null $idx |
||
330 | * |
||
331 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNode|SimpleHtmlDomNodeBlank |
||
332 | */ |
||
333 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
361 | |||
362 | /** |
||
363 | * Create a new "HtmlDomParser"-object from the current context. |
||
364 | * |
||
365 | * @return HtmlDomParser |
||
366 | */ |
||
367 | 64 | public function getHtmlDomParser(): HtmlDomParser |
|
371 | |||
372 | /** |
||
373 | * Retrieve an external iterator. |
||
374 | * |
||
375 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
376 | * |
||
377 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
378 | * <b>Traversable</b> |
||
379 | */ |
||
380 | 2 | public function getIterator(): SimpleHtmlDomNode |
|
391 | |||
392 | /** |
||
393 | * @return DOMNode |
||
394 | */ |
||
395 | 65 | public function getNode(): \DOMNode |
|
399 | |||
400 | /** |
||
401 | * Determine if an attribute exists on the element. |
||
402 | * |
||
403 | * @param string $name |
||
404 | * |
||
405 | * @return bool |
||
406 | */ |
||
407 | 1 | public function hasAttribute(string $name): bool |
|
411 | |||
412 | /** |
||
413 | * Get dom node's outer html. |
||
414 | * |
||
415 | * @param bool $multiDecodeNewHtmlEntity |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | 20 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
|
423 | |||
424 | /** |
||
425 | * Get dom node's inner html. |
||
426 | * |
||
427 | * @param bool $multiDecodeNewHtmlEntity |
||
428 | * |
||
429 | * @return string |
||
430 | */ |
||
431 | 11 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
|
435 | |||
436 | /** |
||
437 | * Returns the last child of node. |
||
438 | * |
||
439 | * @return SimpleHtmlDom|null |
||
440 | */ |
||
441 | 4 | public function lastChild() |
|
451 | |||
452 | /** |
||
453 | * Returns the next sibling of node. |
||
454 | * |
||
455 | * @return SimpleHtmlDom|null |
||
456 | */ |
||
457 | 1 | public function nextSibling() |
|
467 | |||
468 | /** |
||
469 | * Returns the parent of node. |
||
470 | * |
||
471 | * @return SimpleHtmlDom |
||
472 | */ |
||
473 | 1 | public function parentNode(): self |
|
477 | |||
478 | /** |
||
479 | * Returns the previous sibling of node. |
||
480 | * |
||
481 | * @return SimpleHtmlDom|null |
||
482 | */ |
||
483 | 1 | public function previousSibling() |
|
493 | |||
494 | /** |
||
495 | * Replace child node. |
||
496 | * |
||
497 | * @param string $string |
||
498 | * |
||
499 | * @throws \RuntimeException |
||
500 | * |
||
501 | * @return $this |
||
502 | */ |
||
503 | 7 | protected function replaceChild(string $string) |
|
528 | |||
529 | /** |
||
530 | * Replace this node. |
||
531 | * |
||
532 | * @param string $string |
||
533 | * |
||
534 | * @throws \RuntimeException |
||
535 | * |
||
536 | * @return $this|null |
||
537 | */ |
||
538 | 3 | protected function replaceNode(string $string) |
|
561 | |||
562 | /** |
||
563 | * Normalize the given input for comparision. |
||
564 | * |
||
565 | * @param HtmlDomParser|string $input |
||
566 | * |
||
567 | * @return string |
||
568 | */ |
||
569 | 8 | private function normalizeStringForComparision($input): string |
|
605 | |||
606 | /** |
||
607 | * @param HtmlDomParser $newDocument |
||
608 | * |
||
609 | * @return HtmlDomParser |
||
610 | */ |
||
611 | 8 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument): HtmlDomParser |
|
654 | |||
655 | /** |
||
656 | * Change the name of a tag in a "DOMNode". |
||
657 | * |
||
658 | * @param DOMNode $node |
||
659 | * @param string $name |
||
660 | * |
||
661 | * @return DOMElement |
||
662 | */ |
||
663 | 4 | protected function changeElementName(\DOMNode $node, string $name): \DOMElement |
|
680 | |||
681 | /** |
||
682 | * Set attribute value. |
||
683 | * |
||
684 | * @param string $name <p>The name of the html-attribute.</p> |
||
685 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
686 | * @param bool $strict </p> |
||
687 | * $value must be NULL, to remove the attribute, |
||
688 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
689 | * </p> |
||
690 | * |
||
691 | * @return $this |
||
692 | */ |
||
693 | 9 | public function setAttribute(string $name, $value = null, bool $strict = false) |
|
707 | |||
708 | /** |
||
709 | * Remove attribute. |
||
710 | * |
||
711 | * @param string $name <p>The name of the html-attribute.</p> |
||
712 | * |
||
713 | * @return mixed |
||
714 | */ |
||
715 | public function removeAttribute(string $name) |
||
721 | |||
722 | /** |
||
723 | * Get dom node's plain text. |
||
724 | * |
||
725 | * @return string |
||
726 | */ |
||
727 | 16 | public function text(): string |
|
731 | } |
||
732 |
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.