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 HtmlDomParser 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 HtmlDomParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class HtmlDomParser |
||
32 | { |
||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected static $functionAliases = array( |
||
37 | 'outertext' => 'html', |
||
38 | 'outerhtml' => 'html', |
||
39 | 'innertext' => 'innerHtml', |
||
40 | 'innerhtml' => 'innerHtml', |
||
41 | 'load' => 'loadHtml', |
||
42 | 'load_file' => 'loadHtmlFile', |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private static $domLinkReplaceHelper = array( |
||
49 | 'orig' => array('[', ']', '{', '}',), |
||
50 | 'tmp' => array( |
||
51 | '!!!!HTML_DOM__SQUARE_BRACKET_LEFT!!!!', |
||
52 | '!!!!HTML_DOM__SQUARE_BRACKET_RIGHT!!!!', |
||
53 | '!!!!HTML_DOM__BRACKET_LEFT!!!!', |
||
54 | '!!!!HTML_DOM__BRACKET_RIGHT!!!!', |
||
55 | ), |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | protected static $domReplaceHelper = array( |
||
62 | 'orig' => array('&', '|', '+', '%'), |
||
63 | 'tmp' => array( |
||
64 | '!!!!HTML_DOM__AMP!!!!', |
||
65 | '!!!!HTML_DOM__PIPE!!!!', |
||
66 | '!!!!HTML_DOM__PLUS!!!!', |
||
67 | '!!!!HTML_DOM__PERCENT!!!!', |
||
68 | ), |
||
69 | ); |
||
70 | |||
71 | /** |
||
72 | * @var Callable |
||
73 | */ |
||
74 | protected static $callback; |
||
75 | |||
76 | /** |
||
77 | * @var DOMDocument |
||
78 | */ |
||
79 | protected $document; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $encoding = 'UTF-8'; |
||
85 | |||
86 | /** |
||
87 | * @var bool |
||
88 | */ |
||
89 | protected $isDOMDocumentCreatedWithoutHtml = false; |
||
90 | |||
91 | /** |
||
92 | * @var bool |
||
93 | */ |
||
94 | protected $isDOMDocumentCreatedWithoutHtmlWrapper = false; |
||
95 | |||
96 | /** |
||
97 | * Constructor |
||
98 | * |
||
99 | * @param string|SimpleHtmlDom|\DOMNode $element HTML code or SimpleHtmlDom, \DOMNode |
||
100 | */ |
||
101 | 113 | public function __construct($element = null) |
|
102 | { |
||
103 | 113 | $this->document = new \DOMDocument('1.0', $this->getEncoding()); |
|
104 | |||
105 | // DOMDocument settings |
||
106 | 113 | $this->document->preserveWhiteSpace = false; |
|
107 | 113 | $this->document->formatOutput = true; |
|
108 | |||
109 | 113 | if ($element instanceof SimpleHtmlDom) { |
|
110 | $element = $element->getNode(); |
||
111 | } |
||
112 | |||
113 | 113 | if ($element instanceof \DOMNode) { |
|
114 | $domNode = $this->document->importNode($element, true); |
||
115 | |||
116 | if ($domNode instanceof \DOMNode) { |
||
117 | $this->document->appendChild($domNode); |
||
118 | } |
||
119 | |||
120 | return; |
||
121 | } |
||
122 | |||
123 | 113 | if ($element !== null) { |
|
124 | 65 | $this->loadHtml($element); |
|
125 | } |
||
126 | 48 | } |
|
127 | |||
128 | /** |
||
129 | * @param $name |
||
130 | * @param $arguments |
||
131 | * |
||
132 | * @return bool|mixed |
||
133 | */ |
||
134 | 29 | public function __call($name, $arguments) |
|
135 | { |
||
136 | 29 | $name = strtolower($name); |
|
137 | |||
138 | 29 | View Code Duplication | if (isset(self::$functionAliases[$name])) { |
|
|||
139 | 28 | return call_user_func_array(array($this, self::$functionAliases[$name]), $arguments); |
|
140 | } |
||
141 | |||
142 | 1 | throw new BadMethodCallException('Method does not exist: ' . $name); |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param $name |
||
147 | * @param $arguments |
||
148 | * |
||
149 | * @return HtmlDomParser |
||
150 | */ |
||
151 | 9 | public static function __callStatic($name, $arguments) |
|
167 | |||
168 | /** |
||
169 | * @param $name |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | 1 | public function __get($name) |
|
174 | { |
||
175 | 1 | $name = strtolower($name); |
|
176 | |||
177 | switch ($name) { |
||
178 | 1 | case 'outerhtml': |
|
179 | 1 | case 'outertext': |
|
180 | return $this->html(); |
||
181 | 1 | case 'innerhtml': |
|
182 | 1 | case 'innertext': |
|
183 | return $this->innerHtml(); |
||
184 | 1 | case 'text': |
|
185 | 1 | case 'plaintext': |
|
186 | return $this->text(); |
||
187 | } |
||
188 | |||
189 | 1 | return null; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param string $selector |
||
194 | * @param int $idx |
||
195 | * |
||
196 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
197 | */ |
||
198 | public function __invoke($selector, $idx = null) |
||
199 | { |
||
200 | return $this->find($selector, $idx); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return string |
||
205 | */ |
||
206 | public function __toString() |
||
207 | { |
||
208 | return $this->html(); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * does nothing (only for api-compatibility-reasons) |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | 1 | public function clear() |
|
220 | |||
221 | /** |
||
222 | * @param string $html |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | private function replaceToPreserveHtmlEntities($html) |
||
253 | |||
254 | /** |
||
255 | * @param string $html |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | public static function putReplacedBackToPreserveHtmlEntities($html) |
||
275 | |||
276 | /** |
||
277 | * create DOMDocument from HTML |
||
278 | * |
||
279 | * @param string $html |
||
280 | * |
||
281 | * @return \DOMDocument |
||
282 | */ |
||
283 | 101 | private function createDOMDocument($html) |
|
362 | |||
363 | /** |
||
364 | * Return SimpleHtmlDom by id. |
||
365 | * |
||
366 | * @param string $id |
||
367 | * |
||
368 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
369 | */ |
||
370 | public function getElementById($id) |
||
374 | |||
375 | /** |
||
376 | * Return SimpleHtmlDom by tag name. |
||
377 | * |
||
378 | * @param string $name |
||
379 | * |
||
380 | * @return SimpleHtmlDom|SimpleHtmlDomNodeBlank |
||
381 | */ |
||
382 | public function getElementByTagName($name) |
||
392 | |||
393 | /** |
||
394 | * Returns Elements by id |
||
395 | * |
||
396 | * @param string $id |
||
397 | * @param null|int $idx |
||
398 | * |
||
399 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
400 | */ |
||
401 | public function getElementsById($id, $idx = null) |
||
405 | |||
406 | /** |
||
407 | * Returns Elements by tag name |
||
408 | * |
||
409 | * @param string $name |
||
410 | * @param null|int $idx |
||
411 | * |
||
412 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
413 | */ |
||
414 | View Code Duplication | public function getElementsByTagName($name, $idx = null) |
|
438 | |||
439 | /** |
||
440 | * Find list of nodes with a CSS selector. |
||
441 | * |
||
442 | * @param string $selector |
||
443 | * @param int $idx |
||
444 | * |
||
445 | * @return SimpleHtmlDom|SimpleHtmlDom[]|SimpleHtmlDomNodeBlank |
||
446 | */ |
||
447 | 1 | public function find($selector, $idx = null) |
|
473 | |||
474 | /** |
||
475 | * @param string $content |
||
476 | * |
||
477 | * @return string |
||
478 | */ |
||
479 | protected function fixHtmlOutput($content) |
||
521 | |||
522 | /** |
||
523 | * @return DOMDocument |
||
524 | */ |
||
525 | 1 | public function getDocument() |
|
529 | |||
530 | /** |
||
531 | * Get the encoding to use |
||
532 | * |
||
533 | * @return string |
||
534 | */ |
||
535 | 113 | private function getEncoding() |
|
539 | |||
540 | /** |
||
541 | * @return bool |
||
542 | */ |
||
543 | public function getIsDOMDocumentCreatedWithoutHtml() |
||
547 | |||
548 | /** |
||
549 | * @return bool |
||
550 | */ |
||
551 | public function getIsDOMDocumentCreatedWithoutHtmlWrapper() |
||
555 | |||
556 | /** |
||
557 | * Get dom node's outer html |
||
558 | * |
||
559 | * @return string |
||
560 | */ |
||
561 | public function html() |
||
575 | |||
576 | /** |
||
577 | * Get the HTML as XML. |
||
578 | * |
||
579 | * @return string |
||
580 | */ |
||
581 | public function xml() |
||
590 | |||
591 | /** |
||
592 | * Get dom node's inner html |
||
593 | * |
||
594 | * @return string |
||
595 | */ |
||
596 | public function innerHtml() |
||
606 | |||
607 | /** |
||
608 | * Load HTML from string |
||
609 | * |
||
610 | * @param string $html |
||
611 | * |
||
612 | * @return HtmlDomParser |
||
613 | * |
||
614 | * @throws InvalidArgumentException if argument is not string |
||
615 | */ |
||
616 | 104 | public function loadHtml($html) |
|
626 | |||
627 | /** |
||
628 | * Load HTML from file |
||
629 | * |
||
630 | * @param string $filePath |
||
631 | * |
||
632 | * @return HtmlDomParser |
||
633 | */ |
||
634 | 11 | public function loadHtmlFile($filePath) |
|
659 | |||
660 | /** |
||
661 | * Save dom as string |
||
662 | * |
||
663 | * @param string $filepath |
||
664 | * |
||
665 | * @return string |
||
666 | */ |
||
667 | public function save($filepath = '') |
||
676 | |||
677 | /** |
||
678 | * @param $functionName |
||
679 | */ |
||
680 | public function set_callback($functionName) |
||
684 | |||
685 | /** |
||
686 | * Get dom node's plain text |
||
687 | * |
||
688 | * @return string |
||
689 | */ |
||
690 | public function text() |
||
694 | } |
||
695 |
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.