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 |
||
8 | class SimpleHtmlDom extends AbstractSimpleHtmlDom implements \IteratorAggregate, SimpleHtmlDomInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var \DOMElement|\DOMNode |
||
12 | */ |
||
13 | protected $node; |
||
14 | |||
15 | /** |
||
16 | * @param \DOMElement|\DOMNode $node |
||
17 | */ |
||
18 | 107 | public function __construct(\DOMNode $node) |
|
22 | |||
23 | /** |
||
24 | * @param string $name |
||
25 | * @param array $arguments |
||
26 | * |
||
27 | * @throws \BadMethodCallException |
||
28 | * |
||
29 | * @return SimpleHtmlDomInterface|string|null |
||
30 | */ |
||
31 | 9 | View Code Duplication | public function __call($name, $arguments) |
41 | |||
42 | /** |
||
43 | * Returns children of node. |
||
44 | * |
||
45 | * @param int $idx |
||
46 | * |
||
47 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface|null |
||
48 | */ |
||
49 | 2 | public function childNodes(int $idx = -1) |
|
59 | |||
60 | /** |
||
61 | * Find list of nodes with a CSS selector. |
||
62 | * |
||
63 | * @param string $selector |
||
64 | * @param int|null $idx |
||
65 | * |
||
66 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
67 | */ |
||
68 | 26 | public function find(string $selector, $idx = null) |
|
72 | |||
73 | /** |
||
74 | * Find one node with a CSS selector. |
||
75 | * |
||
76 | * @param string $selector |
||
77 | * |
||
78 | * @return SimpleHtmlDomInterface |
||
79 | */ |
||
80 | 1 | public function findOne(string $selector): SimpleHtmlDomInterface |
|
84 | |||
85 | /** |
||
86 | * Find nodes with a CSS selector. |
||
87 | * |
||
88 | * @param string $selector |
||
89 | * |
||
90 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
91 | */ |
||
92 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
||
96 | |||
97 | /** |
||
98 | * Returns the first child of node. |
||
99 | * |
||
100 | * @return SimpleHtmlDomInterface|null |
||
101 | */ |
||
102 | 4 | public function firstChild() |
|
113 | |||
114 | /** |
||
115 | * Returns an array of attributes. |
||
116 | * |
||
117 | * @return array|null |
||
118 | */ |
||
119 | 2 | public function getAllAttributes() |
|
132 | |||
133 | /** |
||
134 | * Return attribute value. |
||
135 | * |
||
136 | * @param string $name |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 14 | public function getAttribute(string $name): string |
|
150 | |||
151 | /** |
||
152 | * Return element by #id. |
||
153 | * |
||
154 | * @param string $id |
||
155 | * |
||
156 | * @return SimpleHtmlDomInterface |
||
157 | */ |
||
158 | 1 | public function getElementById(string $id): SimpleHtmlDomInterface |
|
162 | |||
163 | /** |
||
164 | * Returns elements by #id. |
||
165 | * |
||
166 | * @param string $id |
||
167 | * @param int|null $idx |
||
168 | * |
||
169 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
170 | */ |
||
171 | public function getElementsById(string $id, $idx = null) |
||
175 | |||
176 | /** |
||
177 | * Return elements by .class. |
||
178 | * |
||
179 | * @param string $class |
||
180 | * |
||
181 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
182 | */ |
||
183 | public function getElementByClass(string $class): SimpleHtmlDomNodeInterface |
||
187 | |||
188 | /** |
||
189 | * Return element by tag name. |
||
190 | * |
||
191 | * @param string $name |
||
192 | * |
||
193 | * @return SimpleHtmlDomInterface |
||
194 | */ |
||
195 | public function getElementByTagName(string $name): SimpleHtmlDomInterface |
||
209 | |||
210 | /** |
||
211 | * Returns elements by tag name. |
||
212 | * |
||
213 | * @param string $name |
||
214 | * @param int|null $idx |
||
215 | * |
||
216 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
217 | */ |
||
218 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
|
249 | |||
250 | /** |
||
251 | * Create a new "HtmlDomParser"-object from the current context. |
||
252 | * |
||
253 | * @return HtmlDomParser |
||
254 | */ |
||
255 | public function getHtmlDomParser(): HtmlDomParser |
||
259 | |||
260 | /** |
||
261 | * Retrieve an external iterator. |
||
262 | * |
||
263 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
264 | * |
||
265 | * @return SimpleHtmlDomNode |
||
266 | * <p> |
||
267 | * An instance of an object implementing <b>Iterator</b> or |
||
268 | * <b>Traversable</b> |
||
269 | * </p> |
||
270 | */ |
||
271 | public function getIterator(): SimpleHtmlDomNodeInterface |
||
282 | |||
283 | /** |
||
284 | * @return \DOMNode |
||
285 | */ |
||
286 | public function getNode(): \DOMNode |
||
290 | |||
291 | /** |
||
292 | * Determine if an attribute exists on the element. |
||
293 | * |
||
294 | * @param string $name |
||
295 | * |
||
296 | * @return bool |
||
297 | */ |
||
298 | public function hasAttribute(string $name): bool |
||
306 | |||
307 | /** |
||
308 | * Get dom node's outer html. |
||
309 | * |
||
310 | * @param bool $multiDecodeNewHtmlEntity |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
||
318 | |||
319 | /** |
||
320 | * Get dom node's inner html. |
||
321 | * |
||
322 | * @param bool $multiDecodeNewHtmlEntity |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
||
330 | |||
331 | /** |
||
332 | * Returns the last child of node. |
||
333 | * |
||
334 | * @return SimpleHtmlDomInterface|null |
||
335 | */ |
||
336 | public function lastChild() |
||
347 | |||
348 | /** |
||
349 | * Returns the next sibling of node. |
||
350 | * |
||
351 | * @return SimpleHtmlDomInterface|null |
||
352 | */ |
||
353 | public function nextSibling() |
||
364 | |||
365 | /** |
||
366 | * Returns the parent of node. |
||
367 | * |
||
368 | * @return SimpleHtmlDomInterface |
||
369 | */ |
||
370 | public function parentNode(): SimpleHtmlDomInterface |
||
374 | |||
375 | /** |
||
376 | * Nodes can get partially destroyed in which they're still an |
||
377 | * actual DOM node (such as \DOMElement) but almost their entire |
||
378 | * body is gone, including the `nodeType` attribute. |
||
379 | * |
||
380 | * @return bool true if node has been destroyed |
||
381 | */ |
||
382 | public function isRemoved(): bool |
||
386 | |||
387 | /** |
||
388 | * Returns the previous sibling of node. |
||
389 | * |
||
390 | * @return SimpleHtmlDomInterface|null |
||
391 | */ |
||
392 | public function previousSibling() |
||
403 | |||
404 | /** |
||
405 | * Replace child node. |
||
406 | * |
||
407 | * @param string $string |
||
408 | * |
||
409 | * @return SimpleHtmlDomInterface |
||
410 | */ |
||
411 | protected function replaceChildWithString(string $string): SimpleHtmlDomInterface |
||
449 | |||
450 | /** |
||
451 | * Replace this node with text |
||
452 | * |
||
453 | * @param string $string |
||
454 | * |
||
455 | * @return SimpleHtmlDomInterface |
||
456 | */ |
||
457 | protected function replaceTextWithString($string): SimpleHtmlDomInterface |
||
475 | |||
476 | /** |
||
477 | * Replace this node. |
||
478 | * |
||
479 | * @param string $string |
||
480 | * |
||
481 | * @return SimpleHtmlDomInterface |
||
482 | */ |
||
483 | protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface |
||
544 | |||
545 | /** |
||
546 | * Normalize the given input for comparision. |
||
547 | * |
||
548 | * @param HtmlDomParser|string $input |
||
549 | * |
||
550 | * @return string |
||
551 | */ |
||
552 | private function normalizeStringForComparision($input): string |
||
588 | |||
589 | /** |
||
590 | * @param HtmlDomParser $newDocument |
||
591 | * @param bool $removeExtraHeadTag |
||
592 | * |
||
593 | * @return HtmlDomParser |
||
594 | */ |
||
595 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument, $removeExtraHeadTag = false): HtmlDomParser |
||
676 | |||
677 | /** |
||
678 | * Change the name of a tag in a "DOMNode". |
||
679 | * |
||
680 | * @param \DOMNode $node |
||
681 | * @param string $name |
||
682 | * |
||
683 | * @return \DOMElement|false |
||
684 | * <p>DOMElement a new instance of class DOMElement or false |
||
685 | * if an error occured.</p> |
||
686 | */ |
||
687 | protected function changeElementName(\DOMNode $node, string $name) |
||
712 | |||
713 | /** |
||
714 | * Set attribute value. |
||
715 | * |
||
716 | * @param string $name <p>The name of the html-attribute.</p> |
||
717 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
718 | * @param bool $strict </p> |
||
719 | * $value must be NULL, to remove the attribute, |
||
720 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
721 | * </p> |
||
722 | * |
||
723 | * @return SimpleHtmlDomInterface |
||
724 | */ |
||
725 | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface |
||
741 | |||
742 | /** |
||
743 | * @param string|string[]|null $value <p> |
||
744 | * null === get the current input value |
||
745 | * text === set a new input value |
||
746 | * </p> |
||
747 | * |
||
748 | * @return string|string[]|null |
||
749 | */ |
||
750 | public function val($value = null) |
||
826 | |||
827 | /** |
||
828 | * Remove attribute. |
||
829 | * |
||
830 | * @param string $name <p>The name of the html-attribute.</p> |
||
831 | * |
||
832 | * @return SimpleHtmlDomInterface |
||
833 | */ |
||
834 | public function removeAttribute(string $name): SimpleHtmlDomInterface |
||
842 | |||
843 | /** |
||
844 | * Get dom node's plain text. |
||
845 | * |
||
846 | * @return string |
||
847 | */ |
||
848 | public function text(): string |
||
852 | } |
||
853 |
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.