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 |
||
34 | class HtmlDomParser |
||
35 | { |
||
36 | /** |
||
37 | * @var string[] |
||
38 | */ |
||
39 | protected static $functionAliases = [ |
||
40 | 'outertext' => 'html', |
||
41 | 'outerhtml' => 'html', |
||
42 | 'innertext' => 'innerHtml', |
||
43 | 'innerhtml' => 'innerHtml', |
||
44 | 'load' => 'loadHtml', |
||
45 | 'load_file' => 'loadHtmlFile', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @var string[][] |
||
50 | */ |
||
51 | protected static $domLinkReplaceHelper = [ |
||
52 | 'orig' => ['[', ']', '{', '}'], |
||
53 | 'tmp' => [ |
||
54 | '____SIMPLE_HTML_DOM__VOKU__SQUARE_BRACKET_LEFT____', |
||
55 | '____SIMPLE_HTML_DOM__VOKU__SQUARE_BRACKET_RIGHT____', |
||
56 | '____SIMPLE_HTML_DOM__VOKU__BRACKET_LEFT____', |
||
57 | '____SIMPLE_HTML_DOM__VOKU__BRACKET_RIGHT____', |
||
58 | ], |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * @var string[][] |
||
63 | */ |
||
64 | protected static $domReplaceHelper = [ |
||
65 | 'orig' => ['&', '|', '+', '%', '@', '<html ⚡'], |
||
66 | 'tmp' => [ |
||
67 | '____SIMPLE_HTML_DOM__VOKU__AMP____', |
||
68 | '____SIMPLE_HTML_DOM__VOKU__PIPE____', |
||
69 | '____SIMPLE_HTML_DOM__VOKU__PLUS____', |
||
70 | '____SIMPLE_HTML_DOM__VOKU__PERCENT____', |
||
71 | '____SIMPLE_HTML_DOM__VOKU__AT____', |
||
72 | '<html ____SIMPLE_HTML_DOM__VOKU__GOOGLE_AMP____="true"', |
||
73 | ], |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected static $domHtmlWrapperHelper = '____simple_html_dom__voku__html_wrapper____'; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | protected static $domHtmlSpecialScriptHelper = '____simple_html_dom__voku__html_special_sctipt____'; |
||
85 | |||
86 | /** |
||
87 | * @var array |
||
88 | */ |
||
89 | protected static $domBrokenReplaceHelper = []; |
||
90 | |||
91 | /** |
||
92 | * @var callable |
||
93 | */ |
||
94 | protected static $callback; |
||
95 | |||
96 | /** |
||
97 | * @var string[] |
||
98 | */ |
||
99 | protected $namespaces = []; |
||
100 | |||
101 | /** |
||
102 | * @var \DOMDocument |
||
103 | */ |
||
104 | protected $document; |
||
105 | |||
106 | /** |
||
107 | * @var string |
||
108 | */ |
||
109 | protected $encoding = 'UTF-8'; |
||
110 | |||
111 | /** |
||
112 | * @var bool |
||
113 | */ |
||
114 | protected $isDOMDocumentCreatedWithoutHtml = false; |
||
115 | |||
116 | /** |
||
117 | * @var bool |
||
118 | */ |
||
119 | protected $isDOMDocumentCreatedWithoutWrapper = false; |
||
120 | |||
121 | /** |
||
122 | * @var bool |
||
123 | */ |
||
124 | protected $isDOMDocumentCreatedWithoutHeadWrapper = false; |
||
125 | |||
126 | /** |
||
127 | * @var bool |
||
128 | */ |
||
129 | protected $isDOMDocumentCreatedWithoutHtmlWrapper = false; |
||
130 | |||
131 | /** |
||
132 | * @var bool |
||
133 | */ |
||
134 | protected $isDOMDocumentCreatedWithFakeEndScript = false; |
||
135 | |||
136 | /** |
||
137 | * @var bool |
||
138 | */ |
||
139 | protected $keepBrokenHtml; |
||
140 | |||
141 | /** |
||
142 | * Constructor |
||
143 | * |
||
144 | * @param \DOMNode|SimpleHtmlDomInterface|string $element HTML code or SimpleHtmlDomInterface, \DOMNode |
||
145 | */ |
||
146 | 146 | public function __construct($element = null) |
|
177 | |||
178 | /** |
||
179 | * @param string $name |
||
180 | * @param array $arguments |
||
181 | * |
||
182 | * @return bool|mixed |
||
183 | */ |
||
184 | 53 | View Code Duplication | public function __call($name, $arguments) |
194 | |||
195 | /** |
||
196 | * @param string $name |
||
197 | * @param array $arguments |
||
198 | * |
||
199 | * @throws \RuntimeException |
||
200 | * @throws \BadMethodCallException |
||
201 | * |
||
202 | * @return HtmlDomParser |
||
203 | */ |
||
204 | 21 | View Code Duplication | public static function __callStatic($name, $arguments) |
224 | |||
225 | public function __clone() |
||
229 | |||
230 | /** @noinspection MagicMethodsValidityInspection */ |
||
231 | |||
232 | /** |
||
233 | * @param string $name |
||
234 | * |
||
235 | * @return string|null |
||
236 | */ |
||
237 | 14 | public function __get($name) |
|
255 | |||
256 | /** |
||
257 | * @param string $selector |
||
258 | * @param int $idx |
||
259 | * |
||
260 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
261 | */ |
||
262 | 3 | public function __invoke($selector, $idx = null) |
|
266 | |||
267 | /** |
||
268 | * @return string |
||
269 | */ |
||
270 | 17 | public function __toString() |
|
274 | |||
275 | /** |
||
276 | * does nothing (only for api-compatibility-reasons) |
||
277 | * |
||
278 | * @return bool |
||
279 | * |
||
280 | * @deprecated |
||
281 | */ |
||
282 | 1 | public function clear(): bool |
|
286 | |||
287 | /** |
||
288 | * Create DOMDocument from HTML. |
||
289 | * |
||
290 | * @param string $html |
||
291 | * @param int|null $libXMLExtraOptions |
||
292 | * |
||
293 | * @return \DOMDocument |
||
294 | */ |
||
295 | 131 | protected function createDOMDocument(string $html, $libXMLExtraOptions = null): \DOMDocument |
|
415 | |||
416 | /** |
||
417 | * @param string $content |
||
418 | * @param bool $multiDecodeNewHtmlEntity |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | 78 | protected function decodeHtmlEntity(string $content, bool $multiDecodeNewHtmlEntity): string |
|
457 | |||
458 | /** |
||
459 | * Find list of nodes with a CSS selector. |
||
460 | * |
||
461 | * @param string $selector |
||
462 | * @param int|null $idx |
||
463 | * |
||
464 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
465 | */ |
||
466 | 95 | public function find(string $selector, $idx = null) |
|
500 | |||
501 | /** |
||
502 | * Find nodes with a CSS selector. |
||
503 | * |
||
504 | * @param string $selector |
||
505 | * |
||
506 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
507 | */ |
||
508 | 4 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
|
512 | |||
513 | /** |
||
514 | * Find one node with a CSS selector. |
||
515 | * |
||
516 | * @param string $selector |
||
517 | * |
||
518 | * @return SimpleHtmlDomInterface |
||
519 | */ |
||
520 | 6 | public function findOne(string $selector): SimpleHtmlDomInterface |
|
524 | |||
525 | /** |
||
526 | * @param string $content |
||
527 | * @param bool $multiDecodeNewHtmlEntity |
||
528 | * |
||
529 | * @return string |
||
530 | */ |
||
531 | 76 | public function fixHtmlOutput(string $content, bool $multiDecodeNewHtmlEntity = false): string |
|
614 | |||
615 | /** |
||
616 | * @return \DOMDocument |
||
617 | */ |
||
618 | 39 | public function getDocument(): \DOMDocument |
|
622 | |||
623 | /** |
||
624 | * Return elements by .class. |
||
625 | * |
||
626 | * @param string $class |
||
627 | * |
||
628 | * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
629 | */ |
||
630 | public function getElementByClass(string $class): SimpleHtmlDomNodeInterface |
||
634 | |||
635 | /** |
||
636 | * Return element by #id. |
||
637 | * |
||
638 | * @param string $id |
||
639 | * |
||
640 | * @return SimpleHtmlDomInterface |
||
641 | */ |
||
642 | public function getElementById(string $id): SimpleHtmlDomInterface |
||
646 | |||
647 | /** |
||
648 | * Return element by tag name. |
||
649 | * |
||
650 | * @param string $name |
||
651 | * |
||
652 | * @return SimpleHtmlDomInterface |
||
653 | */ |
||
654 | public function getElementByTagName(string $name): SimpleHtmlDomInterface |
||
664 | |||
665 | /** |
||
666 | * Returns elements by #id. |
||
667 | * |
||
668 | * @param string $id |
||
669 | * @param int|null $idx |
||
670 | * |
||
671 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
672 | */ |
||
673 | public function getElementsById(string $id, $idx = null) |
||
677 | |||
678 | /** |
||
679 | * Returns elements by tag name. |
||
680 | * |
||
681 | * @param string $name |
||
682 | * @param int|null $idx |
||
683 | * |
||
684 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
685 | */ |
||
686 | View Code Duplication | public function getElementsByTagName(string $name, $idx = null) |
|
713 | |||
714 | /** |
||
715 | * Get the encoding to use. |
||
716 | * |
||
717 | * @return string |
||
718 | */ |
||
719 | protected function getEncoding(): string |
||
723 | |||
724 | /** |
||
725 | * @return bool |
||
726 | */ |
||
727 | public function getIsDOMDocumentCreatedWithoutHeadWrapper(): bool |
||
731 | |||
732 | /** |
||
733 | * @return bool |
||
734 | */ |
||
735 | public function getIsDOMDocumentCreatedWithoutHtml(): bool |
||
739 | |||
740 | /** |
||
741 | * @return bool |
||
742 | */ |
||
743 | public function getIsDOMDocumentCreatedWithoutHtmlWrapper(): bool |
||
747 | |||
748 | /** |
||
749 | * @return bool |
||
750 | */ |
||
751 | public function getIsDOMDocumentCreatedWithoutWrapper(): bool |
||
755 | |||
756 | /** |
||
757 | * Get the list of registered namespaces as an array. |
||
758 | * |
||
759 | * @return array |
||
760 | * An array in form ['prefix' => 'namespace-uri'] |
||
761 | */ |
||
762 | public function getNamespaces(): array |
||
766 | |||
767 | /** |
||
768 | * Get dom node's outer html. |
||
769 | * |
||
770 | * @param bool $multiDecodeNewHtmlEntity |
||
771 | * |
||
772 | * @return string |
||
773 | */ |
||
774 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
||
792 | |||
793 | /** |
||
794 | * workaround for bug: https://bugs.php.net/bug.php?id=74628 |
||
795 | * |
||
796 | * @param string $html |
||
797 | */ |
||
798 | protected function html5FallbackForScriptTags(string &$html) |
||
811 | |||
812 | /** |
||
813 | * Get dom node's inner html. |
||
814 | * |
||
815 | * @param bool $multiDecodeNewHtmlEntity |
||
816 | * |
||
817 | * @return string |
||
818 | */ |
||
819 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
||
832 | |||
833 | /** |
||
834 | * @param string $html |
||
835 | * |
||
836 | * @return string |
||
837 | */ |
||
838 | protected function keepBrokenHtml(string $html): string |
||
883 | |||
884 | /** |
||
885 | * @param string $html |
||
886 | */ |
||
887 | protected function keepSpecialScriptTags(string &$html) |
||
905 | |||
906 | /** |
||
907 | * Load HTML from string. |
||
908 | * |
||
909 | * @param string $html |
||
910 | * @param int|null $libXMLExtraOptions |
||
911 | * |
||
912 | * @return HtmlDomParser |
||
913 | */ |
||
914 | public function loadHtml(string $html, $libXMLExtraOptions = null): self |
||
920 | |||
921 | /** |
||
922 | * Load HTML from file. |
||
923 | * |
||
924 | * @param string $filePath |
||
925 | * @param int|null $libXMLExtraOptions |
||
926 | * |
||
927 | * @throws \RuntimeException |
||
928 | * |
||
929 | * @return HtmlDomParser |
||
930 | */ |
||
931 | View Code Duplication | public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): self |
|
958 | |||
959 | /** |
||
960 | * @param string $html |
||
961 | * |
||
962 | * @return string |
||
963 | */ |
||
964 | public static function putReplacedBackToPreserveHtmlEntities(string $html): string |
||
1001 | |||
1002 | /** |
||
1003 | * Register a namespace to be used in xpath queries. |
||
1004 | * |
||
1005 | * @param string $prefix |
||
1006 | * Namespace prefix to register |
||
1007 | * @param string $url |
||
1008 | * Connonical URL for this namespace prefix |
||
1009 | */ |
||
1010 | protected function registerNamespace($prefix, $url) |
||
1014 | |||
1015 | /** |
||
1016 | * @param string $html |
||
1017 | * |
||
1018 | * @return string |
||
1019 | */ |
||
1020 | public static function replaceToPreserveHtmlEntities(string $html): string |
||
1055 | |||
1056 | /** |
||
1057 | * Save the html-dom as string. |
||
1058 | * |
||
1059 | * @param string $filepath |
||
1060 | * |
||
1061 | * @return string |
||
1062 | */ |
||
1063 | public function save(string $filepath = ''): string |
||
1072 | |||
1073 | /** |
||
1074 | * @param callable $functionName |
||
1075 | */ |
||
1076 | public function set_callback($functionName) |
||
1080 | |||
1081 | /** |
||
1082 | * Get dom node's plain text. |
||
1083 | * |
||
1084 | * @param bool $multiDecodeNewHtmlEntity |
||
1085 | * |
||
1086 | * @return string |
||
1087 | */ |
||
1088 | public function text(bool $multiDecodeNewHtmlEntity = false): string |
||
1092 | |||
1093 | /** |
||
1094 | * @param bool $keepBrokenHtml |
||
1095 | * |
||
1096 | * @return HtmlDomParser |
||
1097 | */ |
||
1098 | public function useKeepBrokenHtml(bool $keepBrokenHtml): self |
||
1104 | |||
1105 | /** |
||
1106 | * Get the HTML as XML or plain XML if needed. |
||
1107 | * |
||
1108 | * @param bool $multiDecodeNewHtmlEntity |
||
1109 | * @param bool $htmlToXml |
||
1110 | * @param bool $removeXmlHeader |
||
1111 | * @param int $options |
||
1112 | * |
||
1113 | * @return string |
||
1114 | */ |
||
1115 | public function xml( |
||
1137 | } |
||
1138 |
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.