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 |
||
12 | class SimpleHtmlDom extends AbstractSimpleHtmlDom implements \IteratorAggregate, SimpleHtmlDomInterface |
||
13 | { |
||
14 | /** |
||
15 | * @param \DOMElement|\DOMNode $node |
||
16 | */ |
||
17 | 137 | public function __construct(\DOMNode $node) |
|
21 | |||
22 | /** |
||
23 | * @param string $name |
||
24 | * @param array $arguments |
||
25 | * |
||
26 | * @throws \BadMethodCallException |
||
27 | * |
||
28 | * @return SimpleHtmlDomInterface|string|null |
||
29 | */ |
||
30 | 10 | public function __call($name, $arguments) |
|
40 | |||
41 | /** |
||
42 | * Find list of nodes with a CSS selector. |
||
43 | * |
||
44 | * @param string $selector |
||
45 | * @param int|null $idx |
||
46 | * |
||
47 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
48 | */ |
||
49 | 26 | public function find(string $selector, $idx = null) |
|
53 | |||
54 | /** |
||
55 | * Returns an array of attributes. |
||
56 | * |
||
57 | * @return array|null |
||
58 | */ |
||
59 | 3 | View Code Duplication | public function getAllAttributes() |
72 | |||
73 | /** |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function hasAttributes(): bool |
||
80 | |||
81 | /** |
||
82 | * Return attribute value. |
||
83 | * |
||
84 | * @param string $name |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 24 | public function getAttribute(string $name): string |
|
98 | |||
99 | /** |
||
100 | * Determine if an attribute exists on the element. |
||
101 | * |
||
102 | * @param string $name |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | 2 | public function hasAttribute(string $name): bool |
|
114 | |||
115 | /** |
||
116 | * Get dom node's outer html. |
||
117 | * |
||
118 | * @param bool $multiDecodeNewHtmlEntity |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | 31 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
|
126 | |||
127 | /** |
||
128 | * Get dom node's inner html. |
||
129 | * |
||
130 | * @param bool $multiDecodeNewHtmlEntity |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 20 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
|
138 | |||
139 | /** |
||
140 | * Remove attribute. |
||
141 | * |
||
142 | * @param string $name <p>The name of the html-attribute.</p> |
||
143 | * |
||
144 | * @return SimpleHtmlDomInterface |
||
145 | */ |
||
146 | 2 | public function removeAttribute(string $name): SimpleHtmlDomInterface |
|
154 | |||
155 | /** |
||
156 | * Replace child node. |
||
157 | * |
||
158 | * @param string $string |
||
159 | * |
||
160 | * @return SimpleHtmlDomInterface |
||
161 | */ |
||
162 | 8 | View Code Duplication | protected function replaceChildWithString(string $string): SimpleHtmlDomInterface |
163 | { |
||
164 | 8 | if (!empty($string)) { |
|
165 | 7 | $newDocument = new HtmlDomParser($string); |
|
166 | |||
167 | 7 | $tmpDomString = $this->normalizeStringForComparision($newDocument); |
|
168 | 7 | $tmpStr = $this->normalizeStringForComparision($string); |
|
169 | 7 | if ($tmpDomString !== $tmpStr) { |
|
170 | throw new \RuntimeException( |
||
171 | 'Not valid HTML fragment!' . "\n" . |
||
172 | $tmpDomString . "\n" . |
||
173 | $tmpStr |
||
174 | ); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** @var \DOMNode[] $remove_nodes */ |
||
179 | 8 | $remove_nodes = []; |
|
180 | 8 | if ($this->node->childNodes->length > 0) { |
|
181 | // INFO: We need to fetch the nodes first, before we can delete them, because of missing references in the dom, |
||
182 | // if we delete the elements on the fly. |
||
183 | 8 | foreach ($this->node->childNodes as $node) { |
|
184 | 8 | $remove_nodes[] = $node; |
|
185 | } |
||
186 | } |
||
187 | 8 | foreach ($remove_nodes as $remove_node) { |
|
188 | 8 | $this->node->removeChild($remove_node); |
|
189 | } |
||
190 | |||
191 | 8 | if (!empty($newDocument)) { |
|
192 | 7 | $newDocument = $this->cleanHtmlWrapper($newDocument); |
|
193 | 7 | $ownerDocument = $this->node->ownerDocument; |
|
194 | if ( |
||
195 | 7 | $ownerDocument !== null |
|
196 | && |
||
197 | 7 | $newDocument->getDocument()->documentElement !== null |
|
198 | ) { |
||
199 | 7 | $newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
|
200 | /** @noinspection UnusedFunctionResultInspection */ |
||
201 | 7 | $this->node->appendChild($newNode); |
|
202 | } |
||
203 | } |
||
204 | |||
205 | 8 | return $this; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Replace this node. |
||
210 | * |
||
211 | * @param string $string |
||
212 | * |
||
213 | * @return SimpleHtmlDomInterface |
||
214 | */ |
||
215 | 5 | protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface |
|
276 | |||
277 | /** |
||
278 | * Replace this node with text |
||
279 | * |
||
280 | * @param string $string |
||
281 | * |
||
282 | * @return SimpleHtmlDomInterface |
||
283 | */ |
||
284 | 1 | View Code Duplication | protected function replaceTextWithString($string): SimpleHtmlDomInterface |
302 | |||
303 | /** |
||
304 | * Set attribute value. |
||
305 | * |
||
306 | * @param string $name <p>The name of the html-attribute.</p> |
||
307 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
308 | * @param bool $strict </p> |
||
309 | * $value must be NULL, to remove the attribute, |
||
310 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
311 | * </p> |
||
312 | * |
||
313 | * @return SimpleHtmlDomInterface |
||
314 | */ |
||
315 | 14 | View Code Duplication | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface |
331 | |||
332 | /** |
||
333 | * Get dom node's plain text. |
||
334 | * |
||
335 | * @return string |
||
336 | */ |
||
337 | 18 | public function text(): string |
|
341 | |||
342 | /** |
||
343 | * Change the name of a tag in a "DOMNode". |
||
344 | * |
||
345 | * @param \DOMNode $node |
||
346 | * @param string $name |
||
347 | * |
||
348 | * @return \DOMElement|false |
||
349 | * <p>DOMElement a new instance of class DOMElement or false |
||
350 | * if an error occured.</p> |
||
351 | */ |
||
352 | 6 | View Code Duplication | protected function changeElementName(\DOMNode $node, string $name) |
377 | |||
378 | /** |
||
379 | * Returns children of node. |
||
380 | * |
||
381 | * @param int $idx |
||
382 | * |
||
383 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface|null |
||
384 | */ |
||
385 | 2 | View Code Duplication | public function childNodes(int $idx = -1) |
395 | |||
396 | /** |
||
397 | * Find nodes with a CSS selector. |
||
398 | * |
||
399 | * @param string $selector |
||
400 | * |
||
401 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
402 | */ |
||
403 | 1 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
|
407 | |||
408 | /** |
||
409 | * Find nodes with a CSS selector or false, if no element is found. |
||
410 | * |
||
411 | * @param string $selector |
||
412 | * |
||
413 | * @return false|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
414 | */ |
||
415 | 1 | public function findMultiOrFalse(string $selector) |
|
419 | |||
420 | /** |
||
421 | * Find one node with a CSS selector. |
||
422 | * |
||
423 | * @param string $selector |
||
424 | * |
||
425 | * @return SimpleHtmlDomInterface |
||
426 | */ |
||
427 | 2 | public function findOne(string $selector): SimpleHtmlDomInterface |
|
431 | |||
432 | /** |
||
433 | * Find one node with a CSS selector or false, if no element is found. |
||
434 | * |
||
435 | * @param string $selector |
||
436 | * |
||
437 | * @return false|SimpleHtmlDomInterface |
||
438 | */ |
||
439 | 1 | public function findOneOrFalse(string $selector) |
|
443 | |||
444 | /** |
||
445 | * Returns the first child of node. |
||
446 | * |
||
447 | * @return SimpleHtmlDomInterface|null |
||
448 | */ |
||
449 | 4 | View Code Duplication | public function firstChild() |
460 | |||
461 | /** |
||
462 | * Return elements by ".class". |
||
463 | * |
||
464 | * @param string $class |
||
465 | * |
||
466 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
467 | */ |
||
468 | public function getElementByClass(string $class): SimpleHtmlDomNodeInterface |
||
472 | |||
473 | /** |
||
474 | * Return element by #id. |
||
475 | * |
||
476 | * @param string $id |
||
477 | * |
||
478 | * @return SimpleHtmlDomInterface |
||
479 | */ |
||
480 | 1 | public function getElementById(string $id): SimpleHtmlDomInterface |
|
484 | |||
485 | /** |
||
486 | * Return element by tag name. |
||
487 | * |
||
488 | * @param string $name |
||
489 | * |
||
490 | * @return SimpleHtmlDomInterface |
||
491 | */ |
||
492 | 1 | View Code Duplication | public function getElementByTagName(string $name): SimpleHtmlDomInterface |
506 | |||
507 | /** |
||
508 | * Returns elements by "#id". |
||
509 | * |
||
510 | * @param string $id |
||
511 | * @param int|null $idx |
||
512 | * |
||
513 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
514 | */ |
||
515 | public function getElementsById(string $id, $idx = null) |
||
519 | |||
520 | /** |
||
521 | * Returns elements by tag name. |
||
522 | * |
||
523 | * @param string $name |
||
524 | * @param int|null $idx |
||
525 | * |
||
526 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
527 | */ |
||
528 | 1 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
559 | |||
560 | /** |
||
561 | * Create a new "HtmlDomParser"-object from the current context. |
||
562 | * |
||
563 | * @return HtmlDomParser |
||
564 | */ |
||
565 | 84 | public function getHtmlDomParser(): HtmlDomParser |
|
569 | |||
570 | /** |
||
571 | * @return \DOMNode |
||
572 | */ |
||
573 | 85 | public function getNode(): \DOMNode |
|
577 | |||
578 | /** |
||
579 | * Nodes can get partially destroyed in which they're still an |
||
580 | * actual DOM node (such as \DOMElement) but almost their entire |
||
581 | * body is gone, including the `nodeType` attribute. |
||
582 | * |
||
583 | * @return bool true if node has been destroyed |
||
584 | */ |
||
585 | public function isRemoved(): bool |
||
589 | |||
590 | /** |
||
591 | * Returns the last child of node. |
||
592 | * |
||
593 | * @return SimpleHtmlDomInterface|null |
||
594 | */ |
||
595 | 4 | View Code Duplication | public function lastChild() |
606 | |||
607 | /** |
||
608 | * Returns the next sibling of node. |
||
609 | * |
||
610 | * @return SimpleHtmlDomInterface|null |
||
611 | */ |
||
612 | 1 | View Code Duplication | public function nextSibling() |
623 | |||
624 | /** |
||
625 | * Returns the next sibling of node. |
||
626 | * |
||
627 | * @return SimpleHtmlDomInterface|null |
||
628 | */ |
||
629 | 1 | View Code Duplication | public function nextNonWhitespaceSibling() |
645 | |||
646 | /** |
||
647 | * Returns the parent of node. |
||
648 | * |
||
649 | * @return SimpleHtmlDomInterface |
||
650 | */ |
||
651 | 2 | public function parentNode(): SimpleHtmlDomInterface |
|
655 | |||
656 | /** |
||
657 | * Returns the previous sibling of node. |
||
658 | * |
||
659 | * @return SimpleHtmlDomInterface|null |
||
660 | */ |
||
661 | 1 | View Code Duplication | public function previousSibling() |
672 | |||
673 | /** |
||
674 | * @param string|string[]|null $value <p> |
||
675 | * null === get the current input value |
||
676 | * text === set a new input value |
||
677 | * </p> |
||
678 | * |
||
679 | * @return string|string[]|null |
||
680 | */ |
||
681 | 1 | View Code Duplication | public function val($value = null) |
757 | |||
758 | /** |
||
759 | * @param HtmlDomParser $newDocument |
||
760 | * @param bool $removeExtraHeadTag |
||
761 | * |
||
762 | * @return HtmlDomParser |
||
763 | */ |
||
764 | 11 | protected function cleanHtmlWrapper( |
|
847 | |||
848 | /** |
||
849 | * Retrieve an external iterator. |
||
850 | * |
||
851 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
852 | * |
||
853 | * @return SimpleHtmlDomNode |
||
854 | * <p> |
||
855 | * An instance of an object implementing <b>Iterator</b> or |
||
856 | * <b>Traversable</b> |
||
857 | * </p> |
||
858 | */ |
||
859 | 3 | View Code Duplication | public function getIterator(): SimpleHtmlDomNodeInterface |
870 | |||
871 | /** |
||
872 | * Get dom node's inner html. |
||
873 | * |
||
874 | * @param bool $multiDecodeNewHtmlEntity |
||
875 | * |
||
876 | * @return string |
||
877 | */ |
||
878 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
882 | |||
883 | /** |
||
884 | * Normalize the given input for comparision. |
||
885 | * |
||
886 | * @param HtmlDomParser|string $input |
||
887 | * |
||
888 | * @return string |
||
889 | */ |
||
890 | 11 | private function normalizeStringForComparision($input): string |
|
926 | } |
||
927 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.