Complex classes like HtmlMin 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 HtmlMin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class HtmlMin implements HtmlMinInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private static $regExSpace = "/[[:space:]]{2,}|[\r\n]+/u"; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $optional_end_tags = [ |
||
32 | 'html', |
||
33 | 'head', |
||
34 | 'body', |
||
35 | ]; |
||
36 | |||
37 | private static $selfClosingTags = [ |
||
38 | 'area', |
||
39 | 'base', |
||
40 | 'basefont', |
||
41 | 'br', |
||
42 | 'col', |
||
43 | 'command', |
||
44 | 'embed', |
||
45 | 'frame', |
||
46 | 'hr', |
||
47 | 'img', |
||
48 | 'input', |
||
49 | 'isindex', |
||
50 | 'keygen', |
||
51 | 'link', |
||
52 | 'meta', |
||
53 | 'param', |
||
54 | 'source', |
||
55 | 'track', |
||
56 | 'wbr', |
||
57 | ]; |
||
58 | |||
59 | private static $trimWhitespaceFromTags = [ |
||
60 | 'article' => '', |
||
61 | 'br' => '', |
||
62 | 'div' => '', |
||
63 | 'footer' => '', |
||
64 | 'hr' => '', |
||
65 | 'nav' => '', |
||
66 | 'p' => '', |
||
67 | 'script' => '', |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | private static $booleanAttributes = [ |
||
74 | 'allowfullscreen' => '', |
||
75 | 'async' => '', |
||
76 | 'autofocus' => '', |
||
77 | 'autoplay' => '', |
||
78 | 'checked' => '', |
||
79 | 'compact' => '', |
||
80 | 'controls' => '', |
||
81 | 'declare' => '', |
||
82 | 'default' => '', |
||
83 | 'defaultchecked' => '', |
||
84 | 'defaultmuted' => '', |
||
85 | 'defaultselected' => '', |
||
86 | 'defer' => '', |
||
87 | 'disabled' => '', |
||
88 | 'enabled' => '', |
||
89 | 'formnovalidate' => '', |
||
90 | 'hidden' => '', |
||
91 | 'indeterminate' => '', |
||
92 | 'inert' => '', |
||
93 | 'ismap' => '', |
||
94 | 'itemscope' => '', |
||
95 | 'loop' => '', |
||
96 | 'multiple' => '', |
||
97 | 'muted' => '', |
||
98 | 'nohref' => '', |
||
99 | 'noresize' => '', |
||
100 | 'noshade' => '', |
||
101 | 'novalidate' => '', |
||
102 | 'nowrap' => '', |
||
103 | 'open' => '', |
||
104 | 'pauseonexit' => '', |
||
105 | 'readonly' => '', |
||
106 | 'required' => '', |
||
107 | 'reversed' => '', |
||
108 | 'scoped' => '', |
||
109 | 'seamless' => '', |
||
110 | 'selected' => '', |
||
111 | 'sortable' => '', |
||
112 | 'truespeed' => '', |
||
113 | 'typemustmatch' => '', |
||
114 | 'visible' => '', |
||
115 | ]; |
||
116 | |||
117 | /** |
||
118 | * @var array |
||
119 | */ |
||
120 | private static $skipTagsForRemoveWhitespace = [ |
||
121 | 'code', |
||
122 | 'pre', |
||
123 | 'script', |
||
124 | 'style', |
||
125 | 'textarea', |
||
126 | ]; |
||
127 | |||
128 | /** |
||
129 | * @var array |
||
130 | */ |
||
131 | private $protectedChildNodes = []; |
||
132 | |||
133 | /** |
||
134 | * @var string |
||
135 | */ |
||
136 | private $protectedChildNodesHelper = 'html-min--voku--saved-content'; |
||
137 | |||
138 | /** |
||
139 | * @var bool |
||
140 | */ |
||
141 | private $doOptimizeViaHtmlDomParser = true; |
||
142 | |||
143 | /** |
||
144 | * @var bool |
||
145 | */ |
||
146 | private $doOptimizeAttributes = true; |
||
147 | |||
148 | /** |
||
149 | * @var bool |
||
150 | */ |
||
151 | private $doRemoveComments = true; |
||
152 | |||
153 | /** |
||
154 | * @var bool |
||
155 | */ |
||
156 | private $doRemoveWhitespaceAroundTags = false; |
||
157 | |||
158 | /** |
||
159 | * @var bool |
||
160 | */ |
||
161 | private $doRemoveOmittedQuotes = true; |
||
162 | |||
163 | /** |
||
164 | * @var bool |
||
165 | */ |
||
166 | private $doRemoveOmittedHtmlTags = true; |
||
167 | |||
168 | /** |
||
169 | * @var bool |
||
170 | */ |
||
171 | private $doRemoveHttpPrefixFromAttributes = false; |
||
172 | |||
173 | /** |
||
174 | * @var array |
||
175 | */ |
||
176 | private $domainsToRemoveHttpPrefixFromAttributes = [ |
||
177 | 'google.com', |
||
178 | 'google.de', |
||
179 | ]; |
||
180 | |||
181 | /** |
||
182 | * @var bool |
||
183 | */ |
||
184 | private $doSortCssClassNames = true; |
||
185 | |||
186 | /** |
||
187 | * @var bool |
||
188 | */ |
||
189 | private $doSortHtmlAttributes = true; |
||
190 | |||
191 | /** |
||
192 | * @var bool |
||
193 | */ |
||
194 | private $doRemoveDeprecatedScriptCharsetAttribute = true; |
||
195 | |||
196 | /** |
||
197 | * @var bool |
||
198 | */ |
||
199 | private $doRemoveDefaultAttributes = false; |
||
200 | |||
201 | /** |
||
202 | * @var bool |
||
203 | */ |
||
204 | private $doRemoveDeprecatedAnchorName = true; |
||
205 | |||
206 | /** |
||
207 | * @var bool |
||
208 | */ |
||
209 | private $doRemoveDeprecatedTypeFromStylesheetLink = true; |
||
210 | |||
211 | /** |
||
212 | * @var bool |
||
213 | */ |
||
214 | private $doRemoveDeprecatedTypeFromScriptTag = true; |
||
215 | |||
216 | /** |
||
217 | * @var bool |
||
218 | */ |
||
219 | private $doRemoveValueFromEmptyInput = true; |
||
220 | |||
221 | /** |
||
222 | * @var bool |
||
223 | */ |
||
224 | private $doRemoveEmptyAttributes = true; |
||
225 | |||
226 | /** |
||
227 | * @var bool |
||
228 | */ |
||
229 | private $doSumUpWhitespace = true; |
||
230 | |||
231 | /** |
||
232 | * @var bool |
||
233 | */ |
||
234 | private $doRemoveSpacesBetweenTags = false; |
||
235 | |||
236 | /** |
||
237 | * @var bool |
||
238 | */ |
||
239 | private $keepBrokenHtml = false; |
||
240 | |||
241 | /** |
||
242 | * @var bool |
||
243 | */ |
||
244 | private $withDocType = false; |
||
245 | |||
246 | /** |
||
247 | * @var HtmlMinDomObserverInterface[]|\SplObjectStorage |
||
248 | */ |
||
249 | private $domLoopObservers; |
||
250 | |||
251 | /** |
||
252 | * @var int |
||
253 | */ |
||
254 | private $protected_tags_counter = 0; |
||
255 | |||
256 | /** |
||
257 | * HtmlMin constructor. |
||
258 | */ |
||
259 | 49 | public function __construct() |
|
265 | |||
266 | /** |
||
267 | * @param HtmlMinDomObserverInterface $observer |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | 49 | public function attachObserverToTheDomLoop(HtmlMinDomObserverInterface $observer) |
|
275 | |||
276 | /** |
||
277 | * @param bool $doOptimizeAttributes |
||
278 | * |
||
279 | * @return $this |
||
280 | */ |
||
281 | 2 | public function doOptimizeAttributes(bool $doOptimizeAttributes = true): self |
|
287 | |||
288 | /** |
||
289 | * @param bool $doOptimizeViaHtmlDomParser |
||
290 | * |
||
291 | * @return $this |
||
292 | */ |
||
293 | 1 | public function doOptimizeViaHtmlDomParser(bool $doOptimizeViaHtmlDomParser = true): self |
|
299 | |||
300 | /** |
||
301 | * @param bool $doRemoveComments |
||
302 | * |
||
303 | * @return $this |
||
304 | */ |
||
305 | 3 | public function doRemoveComments(bool $doRemoveComments = true): self |
|
311 | |||
312 | /** |
||
313 | * @param bool $doRemoveDefaultAttributes |
||
314 | * |
||
315 | * @return $this |
||
316 | */ |
||
317 | 2 | public function doRemoveDefaultAttributes(bool $doRemoveDefaultAttributes = true): self |
|
323 | |||
324 | /** |
||
325 | * @param bool $doRemoveDeprecatedAnchorName |
||
326 | * |
||
327 | * @return $this |
||
328 | */ |
||
329 | 2 | public function doRemoveDeprecatedAnchorName(bool $doRemoveDeprecatedAnchorName = true): self |
|
335 | |||
336 | /** |
||
337 | * @param bool $doRemoveDeprecatedScriptCharsetAttribute |
||
338 | * |
||
339 | * @return $this |
||
340 | */ |
||
341 | 2 | public function doRemoveDeprecatedScriptCharsetAttribute(bool $doRemoveDeprecatedScriptCharsetAttribute = true): self |
|
347 | |||
348 | /** |
||
349 | * @param bool $doRemoveDeprecatedTypeFromScriptTag |
||
350 | * |
||
351 | * @return $this |
||
352 | */ |
||
353 | 2 | public function doRemoveDeprecatedTypeFromScriptTag(bool $doRemoveDeprecatedTypeFromScriptTag = true): self |
|
359 | |||
360 | /** |
||
361 | * @param bool $doRemoveDeprecatedTypeFromStylesheetLink |
||
362 | * |
||
363 | * @return $this |
||
364 | */ |
||
365 | 2 | public function doRemoveDeprecatedTypeFromStylesheetLink(bool $doRemoveDeprecatedTypeFromStylesheetLink = true): self |
|
371 | |||
372 | /** |
||
373 | * @param bool $doRemoveEmptyAttributes |
||
374 | * |
||
375 | * @return $this |
||
376 | */ |
||
377 | 2 | public function doRemoveEmptyAttributes(bool $doRemoveEmptyAttributes = true): self |
|
383 | |||
384 | /** |
||
385 | * @param bool $doRemoveHttpPrefixFromAttributes |
||
386 | * |
||
387 | * @return $this |
||
388 | */ |
||
389 | 4 | public function doRemoveHttpPrefixFromAttributes(bool $doRemoveHttpPrefixFromAttributes = true): self |
|
395 | |||
396 | /** |
||
397 | * @param bool $doRemoveOmittedHtmlTags |
||
398 | * |
||
399 | * @return $this |
||
400 | */ |
||
401 | 1 | public function doRemoveOmittedHtmlTags(bool $doRemoveOmittedHtmlTags = true): self |
|
407 | |||
408 | /** |
||
409 | * @param bool $doRemoveOmittedQuotes |
||
410 | * |
||
411 | * @return $this |
||
412 | */ |
||
413 | 1 | public function doRemoveOmittedQuotes(bool $doRemoveOmittedQuotes = true): self |
|
419 | |||
420 | /** |
||
421 | * @param bool $doRemoveSpacesBetweenTags |
||
422 | * |
||
423 | * @return $this |
||
424 | */ |
||
425 | 1 | public function doRemoveSpacesBetweenTags(bool $doRemoveSpacesBetweenTags = true): self |
|
431 | |||
432 | /** |
||
433 | * @param bool $doRemoveValueFromEmptyInput |
||
434 | * |
||
435 | * @return $this |
||
436 | */ |
||
437 | 2 | public function doRemoveValueFromEmptyInput(bool $doRemoveValueFromEmptyInput = true): self |
|
443 | |||
444 | /** |
||
445 | * @param bool $doRemoveWhitespaceAroundTags |
||
446 | * |
||
447 | * @return $this |
||
448 | */ |
||
449 | 4 | public function doRemoveWhitespaceAroundTags(bool $doRemoveWhitespaceAroundTags = true): self |
|
455 | |||
456 | /** |
||
457 | * @param bool $doSortCssClassNames |
||
458 | * |
||
459 | * @return $this |
||
460 | */ |
||
461 | 2 | public function doSortCssClassNames(bool $doSortCssClassNames = true): self |
|
467 | |||
468 | /** |
||
469 | * @param bool $doSortHtmlAttributes |
||
470 | * |
||
471 | * @return $this |
||
472 | */ |
||
473 | 2 | public function doSortHtmlAttributes(bool $doSortHtmlAttributes = true): self |
|
479 | |||
480 | /** |
||
481 | * @param bool $doSumUpWhitespace |
||
482 | * |
||
483 | * @return $this |
||
484 | */ |
||
485 | 2 | public function doSumUpWhitespace(bool $doSumUpWhitespace = true): self |
|
491 | |||
492 | 45 | private function domNodeAttributesToString(\DOMNode $node): string |
|
553 | |||
554 | /** |
||
555 | * @param \DOMNode $node |
||
556 | * |
||
557 | * @return bool |
||
558 | */ |
||
559 | 44 | private function domNodeClosingTagOptional(\DOMNode $node): bool |
|
814 | |||
815 | 45 | protected function domNodeToString(\DOMNode $node): string |
|
903 | |||
904 | /** |
||
905 | * @return array |
||
906 | */ |
||
907 | public function getDomainsToRemoveHttpPrefixFromAttributes(): array |
||
911 | |||
912 | /** |
||
913 | * @return bool |
||
914 | */ |
||
915 | public function isDoOptimizeAttributes(): bool |
||
919 | |||
920 | /** |
||
921 | * @return bool |
||
922 | */ |
||
923 | public function isDoOptimizeViaHtmlDomParser(): bool |
||
927 | |||
928 | /** |
||
929 | * @return bool |
||
930 | */ |
||
931 | public function isDoRemoveComments(): bool |
||
935 | |||
936 | /** |
||
937 | * @return bool |
||
938 | */ |
||
939 | 28 | public function isDoRemoveDefaultAttributes(): bool |
|
943 | |||
944 | /** |
||
945 | * @return bool |
||
946 | */ |
||
947 | 28 | public function isDoRemoveDeprecatedAnchorName(): bool |
|
951 | |||
952 | /** |
||
953 | * @return bool |
||
954 | */ |
||
955 | 28 | public function isDoRemoveDeprecatedScriptCharsetAttribute(): bool |
|
959 | |||
960 | /** |
||
961 | * @return bool |
||
962 | */ |
||
963 | 28 | public function isDoRemoveDeprecatedTypeFromScriptTag(): bool |
|
967 | |||
968 | /** |
||
969 | * @return bool |
||
970 | */ |
||
971 | 28 | public function isDoRemoveDeprecatedTypeFromStylesheetLink(): bool |
|
975 | |||
976 | /** |
||
977 | * @return bool |
||
978 | */ |
||
979 | 28 | public function isDoRemoveEmptyAttributes(): bool |
|
983 | |||
984 | /** |
||
985 | * @return bool |
||
986 | */ |
||
987 | 28 | public function isDoRemoveHttpPrefixFromAttributes(): bool |
|
991 | |||
992 | /** |
||
993 | * @return bool |
||
994 | */ |
||
995 | public function isDoRemoveOmittedHtmlTags(): bool |
||
999 | |||
1000 | /** |
||
1001 | * @return bool |
||
1002 | */ |
||
1003 | public function isDoRemoveOmittedQuotes(): bool |
||
1007 | |||
1008 | /** |
||
1009 | * @return bool |
||
1010 | */ |
||
1011 | public function isDoRemoveSpacesBetweenTags(): bool |
||
1015 | |||
1016 | /** |
||
1017 | * @return bool |
||
1018 | */ |
||
1019 | 28 | public function isDoRemoveValueFromEmptyInput(): bool |
|
1023 | |||
1024 | /** |
||
1025 | * @return bool |
||
1026 | */ |
||
1027 | public function isDoRemoveWhitespaceAroundTags(): bool |
||
1031 | |||
1032 | /** |
||
1033 | * @return bool |
||
1034 | */ |
||
1035 | 28 | public function isDoSortCssClassNames(): bool |
|
1039 | |||
1040 | /** |
||
1041 | * @return bool |
||
1042 | */ |
||
1043 | 28 | public function isDoSortHtmlAttributes(): bool |
|
1047 | |||
1048 | /** |
||
1049 | * @return bool |
||
1050 | */ |
||
1051 | public function isDoSumUpWhitespace(): bool |
||
1055 | |||
1056 | /** |
||
1057 | * @param string $html |
||
1058 | * @param bool $decodeUtf8Specials <p>Use this only in special cases, e.g. for PHP 5.3</p> |
||
1059 | * |
||
1060 | * @return string |
||
1061 | */ |
||
1062 | 49 | public function minify($html, $decodeUtf8Specials = false): string |
|
1186 | |||
1187 | /** |
||
1188 | * @param \DOMNode $node |
||
1189 | * |
||
1190 | * @return \DOMNode|null |
||
1191 | */ |
||
1192 | 44 | protected function getNextSiblingOfTypeDOMElement(\DOMNode $node) |
|
1201 | |||
1202 | /** |
||
1203 | * Check if the current string is an conditional comment. |
||
1204 | * |
||
1205 | * INFO: since IE >= 10 conditional comment are not working anymore |
||
1206 | * |
||
1207 | * <!--[if expression]> HTML <![endif]--> |
||
1208 | * <![if expression]> HTML <![endif]> |
||
1209 | * |
||
1210 | * @param string $comment |
||
1211 | * |
||
1212 | * @return bool |
||
1213 | */ |
||
1214 | 4 | private function isConditionalComment($comment): bool |
|
1226 | |||
1227 | /** |
||
1228 | * @param string $html |
||
1229 | * @param bool $decodeUtf8Specials |
||
1230 | * |
||
1231 | * @return string |
||
1232 | */ |
||
1233 | 45 | private function minifyHtmlDom($html, $decodeUtf8Specials): string |
|
1311 | |||
1312 | /** |
||
1313 | * @param SimpleHtmlDomInterface $domElement |
||
1314 | * |
||
1315 | * @return void |
||
1316 | */ |
||
1317 | 45 | private function notifyObserversAboutDomElementAfterMinification(SimpleHtmlDomInterface $domElement) |
|
1323 | |||
1324 | /** |
||
1325 | * @param SimpleHtmlDomInterface $domElement |
||
1326 | * |
||
1327 | * @return void |
||
1328 | */ |
||
1329 | 45 | private function notifyObserversAboutDomElementBeforeMinification(SimpleHtmlDomInterface $domElement) |
|
1335 | |||
1336 | /** |
||
1337 | * @param HtmlDomParser $dom |
||
1338 | * @param string $selector |
||
1339 | * |
||
1340 | * @return HtmlDomParser |
||
1341 | */ |
||
1342 | 45 | private function protectTagHelper(HtmlDomParser $dom, string $selector): HtmlDomParser |
|
1357 | |||
1358 | /** |
||
1359 | * Prevent changes of inline "styles" and "scripts". |
||
1360 | * |
||
1361 | * @param HtmlDomParser $dom |
||
1362 | * |
||
1363 | * @return HtmlDomParser |
||
1364 | */ |
||
1365 | 45 | private function protectTags(HtmlDomParser $dom): HtmlDomParser |
|
1413 | |||
1414 | /** |
||
1415 | * Remove comments in the dom. |
||
1416 | * |
||
1417 | * @param HtmlDomParser $dom |
||
1418 | * |
||
1419 | * @return HtmlDomParser |
||
1420 | */ |
||
1421 | 43 | private function removeComments(HtmlDomParser $dom): HtmlDomParser |
|
1436 | |||
1437 | /** |
||
1438 | * Trim tags in the dom. |
||
1439 | * |
||
1440 | * @param SimpleHtmlDomInterface $element |
||
1441 | * |
||
1442 | * @return void |
||
1443 | */ |
||
1444 | 3 | private function removeWhitespaceAroundTags(SimpleHtmlDomInterface $element) |
|
1473 | |||
1474 | /** |
||
1475 | * Callback function for preg_replace_callback use. |
||
1476 | * |
||
1477 | * @param array $matches PREG matches |
||
1478 | * |
||
1479 | * @return string |
||
1480 | */ |
||
1481 | 8 | private function restoreProtectedHtml($matches): string |
|
1492 | |||
1493 | /** |
||
1494 | * @param array $domainsToRemoveHttpPrefixFromAttributes |
||
1495 | * |
||
1496 | * @return $this |
||
1497 | */ |
||
1498 | 2 | public function setDomainsToRemoveHttpPrefixFromAttributes($domainsToRemoveHttpPrefixFromAttributes): self |
|
1504 | |||
1505 | /** |
||
1506 | * Sum-up extra whitespace from dom-nodes. |
||
1507 | * |
||
1508 | * @param HtmlDomParser $dom |
||
1509 | * |
||
1510 | * @return HtmlDomParser |
||
1511 | */ |
||
1512 | 44 | private function sumUpWhitespace(HtmlDomParser $dom): HtmlDomParser |
|
1545 | |||
1546 | /** |
||
1547 | * WARNING: maybe bad for performance ... |
||
1548 | * |
||
1549 | * @param bool $keepBrokenHtml |
||
1550 | * |
||
1551 | * @return HtmlMin |
||
1552 | */ |
||
1553 | public function useKeepBrokenHtml(bool $keepBrokenHtml): self |
||
1559 | } |
||
1560 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: