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 |
||
30 | class SimpleHtmlDom implements \IteratorAggregate |
||
31 | { |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $functionAliases = array( |
||
36 | 'children' => 'childNodes', |
||
37 | 'first_child' => 'firstChild', |
||
38 | 'last_child' => 'lastChild', |
||
39 | 'next_sibling' => 'nextSibling', |
||
40 | 'prev_sibling' => 'previousSibling', |
||
41 | 'parent' => 'parentNode', |
||
42 | 'outertext' => 'html', |
||
43 | 'innertext' => 'innerHtml', |
||
44 | ); |
||
45 | /** |
||
46 | * @var DOMElement |
||
47 | */ |
||
48 | protected $node; |
||
49 | |||
50 | /** |
||
51 | * SimpleHtmlDom constructor. |
||
52 | * |
||
53 | * @param DOMNode $node |
||
54 | */ |
||
55 | 69 | public function __construct(DOMNode $node) |
|
59 | |||
60 | /** |
||
61 | * @param $name |
||
62 | * @param $arguments |
||
63 | * |
||
64 | * @return null|string|SimpleHtmlDom |
||
65 | * |
||
66 | */ |
||
67 | 8 | View Code Duplication | public function __call($name, $arguments) |
74 | |||
75 | /** |
||
76 | * @param $name |
||
77 | * |
||
78 | * @return array|null|string |
||
79 | */ |
||
80 | 23 | public function __get($name) |
|
97 | |||
98 | /** |
||
99 | * @param string $selector |
||
100 | * @param int $idx |
||
101 | * |
||
102 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
103 | */ |
||
104 | 12 | public function __invoke($selector, $idx = null) |
|
108 | |||
109 | /** |
||
110 | * @param $name |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | 1 | View Code Duplication | public function __isset($name) |
115 | { |
||
116 | switch ($name) { |
||
117 | 1 | case 'outertext': |
|
118 | 1 | case 'innertext': |
|
119 | 1 | case 'plaintext': |
|
120 | 1 | case 'tag': |
|
121 | return true; |
||
122 | 1 | default: |
|
123 | 1 | return $this->hasAttribute($name); |
|
124 | 1 | } |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param $name |
||
129 | * @param $value |
||
130 | * |
||
131 | * @return SimpleHtmlDom |
||
132 | */ |
||
133 | 9 | public function __set($name, $value) |
|
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | 2 | public function __toString() |
|
152 | |||
153 | /** |
||
154 | * @param $name |
||
155 | * |
||
156 | * @return SimpleHtmlDom |
||
157 | */ |
||
158 | 1 | public function __unset($name) |
|
162 | |||
163 | /** |
||
164 | * Returns children of node |
||
165 | * |
||
166 | * @param int $idx |
||
167 | * |
||
168 | * @return SimpleHtmlDomNode|SimpleHtmlDom|null |
||
169 | */ |
||
170 | 2 | public function childNodes($idx = -1) |
|
184 | |||
185 | /** |
||
186 | * Find list of nodes with a CSS selector |
||
187 | * |
||
188 | * @param string $selector |
||
189 | * @param int $idx |
||
190 | * |
||
191 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
192 | */ |
||
193 | 24 | public function find($selector, $idx = null) |
|
197 | |||
198 | /** |
||
199 | * Returns the first child of node |
||
200 | * |
||
201 | * @return SimpleHtmlDom|null |
||
202 | */ |
||
203 | 4 | public function firstChild() |
|
213 | |||
214 | /** |
||
215 | * Returns array of attributes |
||
216 | * |
||
217 | * @return array|null |
||
218 | */ |
||
219 | 1 | public function getAllAttributes() |
|
232 | |||
233 | /** |
||
234 | * Return attribute value |
||
235 | * |
||
236 | * @param string $name |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 10 | public function getAttribute($name) |
|
244 | |||
245 | /** |
||
246 | * Return SimpleHtmlDom by id. |
||
247 | * |
||
248 | * @param string $id |
||
249 | * |
||
250 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
251 | */ |
||
252 | 1 | public function getElementById($id) |
|
256 | |||
257 | /** |
||
258 | * Return SimpleHtmlDom by tag name. |
||
259 | * |
||
260 | * @param string $name |
||
261 | * |
||
262 | * @return SimpleHtmlDomNode|SimpleHtmlDomNodeBlank |
||
263 | */ |
||
264 | 1 | public function getElementByTagName($name) |
|
265 | { |
||
266 | 1 | $node = $this->node->getElementsByTagName($name)->item(0); |
|
267 | |||
268 | 1 | if ($node !== null) { |
|
269 | 1 | return new self($node); |
|
270 | } else { |
||
271 | return new SimpleHtmlDomNodeBlank(); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Returns Elements by id |
||
277 | * |
||
278 | * @param string $id |
||
279 | * @param null|int $idx |
||
280 | * |
||
281 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
282 | */ |
||
283 | public function getElementsById($id, $idx = null) |
||
284 | { |
||
285 | return $this->find("#$id", $idx); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Returns Elements by tag name |
||
290 | * |
||
291 | * @param string $name |
||
292 | * @param null|int $idx |
||
293 | * |
||
294 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
295 | */ |
||
296 | 1 | View Code Duplication | public function getElementsByTagName($name, $idx = null) |
297 | { |
||
298 | 1 | $nodesList = $this->node->getElementsByTagName($name); |
|
299 | |||
300 | 1 | $elements = new SimpleHtmlDomNode(); |
|
301 | |||
302 | 1 | foreach ($nodesList as $node) { |
|
303 | 1 | $elements[] = new self($node); |
|
304 | 1 | } |
|
305 | |||
306 | 1 | if (null === $idx) { |
|
307 | 1 | return $elements; |
|
308 | } else { |
||
309 | if ($idx < 0) { |
||
310 | $idx = count($elements) + $idx; |
||
311 | } |
||
312 | } |
||
313 | |||
314 | if (isset($elements[$idx])) { |
||
315 | return $elements[$idx]; |
||
316 | } else { |
||
317 | return new SimpleHtmlDomNodeBlank(); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Create a new "HtmlDomParser"-object from the current context. |
||
323 | * |
||
324 | * @return HtmlDomParser |
||
325 | */ |
||
326 | 40 | public function getHtmlDomParser() |
|
330 | |||
331 | /** |
||
332 | * Retrieve an external iterator |
||
333 | * |
||
334 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
335 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
336 | * <b>Traversable</b> |
||
337 | */ |
||
338 | 2 | public function getIterator() |
|
339 | { |
||
340 | 2 | $elements = new SimpleHtmlDomNode(); |
|
341 | 2 | if ($this->node->hasChildNodes()) { |
|
342 | 2 | foreach ($this->node->childNodes as $node) { |
|
343 | 2 | $elements[] = new self($node); |
|
344 | 2 | } |
|
345 | 2 | } |
|
346 | |||
347 | 2 | return $elements; |
|
348 | } |
||
349 | |||
350 | /** |
||
351 | * @return DOMNode |
||
352 | */ |
||
353 | 41 | public function getNode() |
|
357 | |||
358 | /** |
||
359 | * Determine if an attribute exists on the element. |
||
360 | * |
||
361 | * @param $name |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | 1 | public function hasAttribute($name) |
|
369 | |||
370 | /** |
||
371 | * Get dom node's outer html |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | 13 | public function html() |
|
379 | |||
380 | /** |
||
381 | * Get dom node's inner html |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | 3 | public function innerHtml() |
|
389 | |||
390 | /** |
||
391 | * Returns the last child of node |
||
392 | * |
||
393 | * @return SimpleHtmlDom|null |
||
394 | */ |
||
395 | 4 | public function lastChild() |
|
405 | |||
406 | /** |
||
407 | * Returns the next sibling of node |
||
408 | * |
||
409 | * @return SimpleHtmlDom|null |
||
410 | */ |
||
411 | 1 | public function nextSibling() |
|
421 | |||
422 | /** |
||
423 | * Returns the parent of node |
||
424 | * |
||
425 | * @return SimpleHtmlDom |
||
426 | */ |
||
427 | 1 | public function parentNode() |
|
431 | |||
432 | /** |
||
433 | * Returns the previous sibling of node |
||
434 | * |
||
435 | * @return SimpleHtmlDom|null |
||
436 | */ |
||
437 | 1 | public function previousSibling() |
|
447 | |||
448 | /** |
||
449 | * Replace child node |
||
450 | * |
||
451 | * @param $string |
||
452 | * |
||
453 | * @return $this |
||
454 | */ |
||
455 | 3 | protected function replaceChild($string) |
|
480 | |||
481 | /** |
||
482 | * Replace this node |
||
483 | * |
||
484 | * @param $string |
||
485 | * |
||
486 | * @return $this |
||
487 | */ |
||
488 | 3 | protected function replaceNode($string) |
|
511 | |||
512 | /** |
||
513 | * @param HtmlDomParser $newDocument |
||
514 | * |
||
515 | * @return HtmlDomParser |
||
516 | */ |
||
517 | 6 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
|
547 | |||
548 | /** |
||
549 | * change the name of a tag in a "DOMNode" |
||
550 | * |
||
551 | * @param DOMNode $node |
||
552 | * @param string $name |
||
553 | * |
||
554 | * @return DOMElement |
||
555 | */ |
||
556 | 3 | protected function changeElementName(\DOMNode $node, $name) |
|
570 | |||
571 | /** |
||
572 | * Set attribute value |
||
573 | * |
||
574 | * @param $name |
||
575 | * @param $value |
||
576 | * |
||
577 | * @return $this |
||
578 | */ |
||
579 | 5 | public function setAttribute($name, $value) |
|
589 | |||
590 | /** |
||
591 | * Get dom node's plain text |
||
592 | * |
||
593 | * @return string |
||
594 | */ |
||
595 | 9 | public function text() |
|
599 | } |
||
600 |
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.