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) |
|
66 | { |
||
67 | 97 | $this->node = $node; |
|
|
|||
68 | 97 | } |
|
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) |
79 | { |
||
80 | 9 | $name = \strtolower($name); |
|
81 | |||
82 | 9 | if (isset(self::$functionAliases[$name])) { |
|
83 | 9 | return \call_user_func_array(array($this, self::$functionAliases[$name]), $arguments); |
|
84 | } |
||
85 | |||
86 | throw new BadMethodCallException('Method does not exist'); |
||
87 | } |
||
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 | switch ($name) { |
||
99 | 42 | case 'outerhtml': |
|
100 | 38 | case 'outertext': |
|
101 | 17 | return $this->html(); |
|
102 | 33 | case 'innerhtml': |
|
103 | 27 | case 'innertext': |
|
104 | 11 | return $this->innerHtml(); |
|
105 | 24 | case 'text': |
|
106 | 19 | case 'plaintext': |
|
107 | 15 | return $this->text(); |
|
108 | 11 | case 'tag': |
|
109 | 4 | return $this->node->nodeName; |
|
110 | 10 | 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) |
|
124 | { |
||
125 | 12 | return $this->find($selector, $idx); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param $name |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | 1 | public function __isset($name) |
|
134 | { |
||
135 | 1 | $name = strtolower($name); |
|
136 | |||
137 | switch ($name) { |
||
138 | 1 | case 'outertext': |
|
139 | 1 | case 'outerhtml': |
|
140 | 1 | case 'innertext': |
|
141 | 1 | case 'innerhtml': |
|
142 | 1 | case 'plaintext': |
|
143 | 1 | case 'text': |
|
144 | 1 | 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 | switch ($name) { |
||
162 | 14 | case 'outerhtml': |
|
163 | 13 | case 'outertext': |
|
164 | 3 | return $this->replaceNode($value); |
|
165 | 11 | case 'innertext': |
|
166 | 9 | 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() |
|
177 | { |
||
178 | 2 | return $this->html(); |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param $name |
||
183 | * |
||
184 | * @return SimpleHtmlDom |
||
185 | */ |
||
186 | 1 | public function __unset($name) |
|
187 | { |
||
188 | 1 | return $this->removeAttribute($name); |
|
189 | } |
||
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) |
|
199 | { |
||
200 | 2 | $nodeList = $this->getIterator(); |
|
201 | |||
202 | 2 | if ($idx === -1) { |
|
203 | 2 | return $nodeList; |
|
204 | } |
||
205 | |||
206 | 2 | if (isset($nodeList[$idx])) { |
|
207 | 2 | return $nodeList[$idx]; |
|
208 | } |
||
209 | |||
210 | 1 | return null; |
|
211 | } |
||
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) |
|
222 | { |
||
223 | 26 | return $this->getHtmlDomParser()->find($selector, $idx); |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Returns the first child of node. |
||
228 | * |
||
229 | * @return SimpleHtmlDom|null |
||
230 | */ |
||
231 | 4 | public function firstChild() |
|
232 | { |
||
233 | 4 | $node = $this->node->firstChild; |
|
234 | |||
235 | 4 | if ($node === null) { |
|
236 | 1 | return null; |
|
237 | } |
||
238 | |||
239 | 4 | return new self($node); |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * Returns an array of attributes. |
||
244 | * |
||
245 | * @return array|null |
||
246 | */ |
||
247 | 2 | public function getAllAttributes() |
|
260 | |||
261 | /** |
||
262 | * Return attribute value. |
||
263 | * |
||
264 | * @param string $name |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | 13 | public function getAttribute(string $name): string |
|
269 | { |
||
270 | 13 | $html = $this->node->getAttribute($name); |
|
271 | |||
274 | |||
275 | /** |
||
276 | * Return element by #id. |
||
277 | * |
||
278 | * @param string $id |
||
279 | * |
||
280 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
281 | */ |
||
282 | 1 | public function getElementById(string $id) |
|
286 | |||
287 | /** |
||
288 | * Return element by tag name. |
||
289 | * |
||
290 | * @param string $name |
||
291 | * |
||
292 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
293 | */ |
||
294 | 1 | public function getElementByTagName(string $name) |
|
304 | |||
305 | /** |
||
306 | * Returns elements by #id. |
||
307 | * |
||
308 | * @param string $id |
||
309 | * @param null|int $idx |
||
310 | * |
||
311 | * @return SimpleHtmlDom[]|SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
312 | */ |
||
313 | public function getElementsById(string $id, $idx = null) |
||
317 | |||
318 | /** |
||
319 | * Returns elements by tag name. |
||
320 | * |
||
321 | * @param string $name |
||
322 | * @param null|int $idx |
||
323 | * |
||
324 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
325 | */ |
||
326 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
354 | |||
355 | /** |
||
356 | * Create a new "HtmlDomParser"-object from the current context. |
||
357 | * |
||
358 | * @return HtmlDomParser |
||
359 | */ |
||
360 | 52 | public function getHtmlDomParser(): HtmlDomParser |
|
364 | |||
365 | /** |
||
366 | * Retrieve an external iterator. |
||
367 | * |
||
368 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
369 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
370 | * <b>Traversable</b> |
||
371 | */ |
||
372 | 2 | public function getIterator(): SimpleHtmlDomNode |
|
383 | |||
384 | /** |
||
385 | * @return DOMNode |
||
386 | */ |
||
387 | 53 | public function getNode(): \DOMNode |
|
391 | |||
392 | /** |
||
393 | * Determine if an attribute exists on the element. |
||
394 | * |
||
395 | * @param string $name |
||
396 | * |
||
397 | * @return bool |
||
398 | */ |
||
399 | 1 | public function hasAttribute(string $name): bool |
|
403 | |||
404 | /** |
||
405 | * Get dom node's outer html. |
||
406 | * |
||
407 | * @param bool $multiDecodeNewHtmlEntity |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | 18 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
|
415 | |||
416 | /** |
||
417 | * Get dom node's inner html. |
||
418 | * |
||
419 | * @param bool $multiDecodeNewHtmlEntity |
||
420 | * |
||
421 | * @return string |
||
422 | */ |
||
423 | 11 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
|
427 | |||
428 | /** |
||
429 | * Returns the last child of node. |
||
430 | * |
||
431 | * @return SimpleHtmlDom|null |
||
432 | */ |
||
433 | 4 | public function lastChild() |
|
443 | |||
444 | /** |
||
445 | * Returns the next sibling of node. |
||
446 | * |
||
447 | * @return SimpleHtmlDom|null |
||
448 | */ |
||
449 | 1 | public function nextSibling() |
|
459 | |||
460 | /** |
||
461 | * Returns the parent of node. |
||
462 | * |
||
463 | * @return SimpleHtmlDom |
||
464 | */ |
||
465 | 1 | public function parentNode(): self |
|
469 | |||
470 | /** |
||
471 | * Returns the previous sibling of node. |
||
472 | * |
||
473 | * @return SimpleHtmlDom|null |
||
474 | */ |
||
475 | 1 | public function previousSibling() |
|
485 | |||
486 | /** |
||
487 | * Replace child node. |
||
488 | * |
||
489 | * @param string $string |
||
490 | * |
||
491 | * @return $this |
||
492 | * |
||
493 | * @throws \RuntimeException |
||
494 | */ |
||
495 | 7 | protected function replaceChild(string $string) |
|
520 | |||
521 | /** |
||
522 | * Replace this node. |
||
523 | * |
||
524 | * @param string $string |
||
525 | * |
||
526 | * @return $this|null |
||
527 | * |
||
528 | * @throws \RuntimeException |
||
529 | */ |
||
530 | 3 | protected function replaceNode(string $string) |
|
553 | |||
554 | /** |
||
555 | * Normalize the given input for comparision. |
||
556 | * |
||
557 | * @param HtmlDomParser|string $input |
||
558 | * |
||
559 | * @return string |
||
560 | */ |
||
561 | 8 | private function normalizeStringForComparision($input): string |
|
596 | |||
597 | /** |
||
598 | * @param HtmlDomParser $newDocument |
||
599 | * |
||
600 | * @return HtmlDomParser |
||
601 | */ |
||
602 | 8 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument): HtmlDomParser |
|
645 | |||
646 | /** |
||
647 | * Change the name of a tag in a "DOMNode". |
||
648 | * |
||
649 | * @param DOMNode $node |
||
650 | * @param string $name |
||
651 | * |
||
652 | * @return DOMElement |
||
653 | */ |
||
654 | 4 | protected function changeElementName(\DOMNode $node, string $name): \DOMElement |
|
671 | |||
672 | /** |
||
673 | * Set attribute value. |
||
674 | * |
||
675 | * @param string $name <p>The name of the html-attribute.</p> |
||
676 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
677 | * @param bool $strict </p> |
||
678 | * $value must be NULL, to remove the attribute, |
||
679 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
680 | * </p> |
||
681 | * |
||
682 | * @return $this |
||
683 | */ |
||
684 | 9 | public function setAttribute(string $name, $value = null, bool $strict = false) |
|
698 | |||
699 | /** |
||
700 | * Remove attribute. |
||
701 | * |
||
702 | * @param string $name <p>The name of the html-attribute.</p> |
||
703 | * |
||
704 | * @return mixed |
||
705 | */ |
||
706 | 1 | public function removeAttribute(string $name) |
|
712 | |||
713 | /** |
||
714 | * Get dom node's plain text. |
||
715 | * |
||
716 | * @return string |
||
717 | */ |
||
718 | 15 | public function text(): string |
|
722 | } |
||
723 |
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.