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 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 |
||
| 19 | class HtmlMin |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private static $regExSpace = "/[[:space:]]{2,}|[\r\n]+/u"; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $optional_end_tags = [ |
||
| 30 | 'html', |
||
| 31 | 'head', |
||
| 32 | 'body', |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * // https://mathiasbynens.be/demo/javascript-mime-type |
||
| 37 | * // https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private static $executableScriptsMimeTypes = [ |
||
| 42 | 'text/javascript' => '', |
||
| 43 | 'text/ecmascript' => '', |
||
| 44 | 'text/jscript' => '', |
||
| 45 | 'application/javascript' => '', |
||
| 46 | 'application/x-javascript' => '', |
||
| 47 | 'application/ecmascript' => '', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | private static $selfClosingTags = [ |
||
| 51 | 'area', |
||
| 52 | 'base', |
||
| 53 | 'basefont', |
||
| 54 | 'br', |
||
| 55 | 'col', |
||
| 56 | 'command', |
||
| 57 | 'embed', |
||
| 58 | 'frame', |
||
| 59 | 'hr', |
||
| 60 | 'img', |
||
| 61 | 'input', |
||
| 62 | 'isindex', |
||
| 63 | 'keygen', |
||
| 64 | 'link', |
||
| 65 | 'meta', |
||
| 66 | 'param', |
||
| 67 | 'source', |
||
| 68 | 'track', |
||
| 69 | 'wbr', |
||
| 70 | ]; |
||
| 71 | |||
| 72 | private static $trimWhitespaceFromTags = [ |
||
| 73 | 'article' => '', |
||
| 74 | 'br' => '', |
||
| 75 | 'div' => '', |
||
| 76 | 'footer' => '', |
||
| 77 | 'hr' => '', |
||
| 78 | 'nav' => '', |
||
| 79 | 'p' => '', |
||
| 80 | 'script' => '', |
||
| 81 | ]; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private static $booleanAttributes = [ |
||
| 87 | 'allowfullscreen' => '', |
||
| 88 | 'async' => '', |
||
| 89 | 'autofocus' => '', |
||
| 90 | 'autoplay' => '', |
||
| 91 | 'checked' => '', |
||
| 92 | 'compact' => '', |
||
| 93 | 'controls' => '', |
||
| 94 | 'declare' => '', |
||
| 95 | 'default' => '', |
||
| 96 | 'defaultchecked' => '', |
||
| 97 | 'defaultmuted' => '', |
||
| 98 | 'defaultselected' => '', |
||
| 99 | 'defer' => '', |
||
| 100 | 'disabled' => '', |
||
| 101 | 'enabled' => '', |
||
| 102 | 'formnovalidate' => '', |
||
| 103 | 'hidden' => '', |
||
| 104 | 'indeterminate' => '', |
||
| 105 | 'inert' => '', |
||
| 106 | 'ismap' => '', |
||
| 107 | 'itemscope' => '', |
||
| 108 | 'loop' => '', |
||
| 109 | 'multiple' => '', |
||
| 110 | 'muted' => '', |
||
| 111 | 'nohref' => '', |
||
| 112 | 'noresize' => '', |
||
| 113 | 'noshade' => '', |
||
| 114 | 'novalidate' => '', |
||
| 115 | 'nowrap' => '', |
||
| 116 | 'open' => '', |
||
| 117 | 'pauseonexit' => '', |
||
| 118 | 'readonly' => '', |
||
| 119 | 'required' => '', |
||
| 120 | 'reversed' => '', |
||
| 121 | 'scoped' => '', |
||
| 122 | 'seamless' => '', |
||
| 123 | 'selected' => '', |
||
| 124 | 'sortable' => '', |
||
| 125 | 'truespeed' => '', |
||
| 126 | 'typemustmatch' => '', |
||
| 127 | 'visible' => '', |
||
| 128 | ]; |
||
| 129 | /** |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private static $skipTagsForRemoveWhitespace = [ |
||
| 133 | 'code', |
||
| 134 | 'pre', |
||
| 135 | 'script', |
||
| 136 | 'style', |
||
| 137 | 'textarea', |
||
| 138 | ]; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var array |
||
| 142 | */ |
||
| 143 | private $protectedChildNodes = []; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var string |
||
| 147 | */ |
||
| 148 | private $protectedChildNodesHelper = 'html-min--voku--saved-content'; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var bool |
||
| 152 | */ |
||
| 153 | private $doOptimizeViaHtmlDomParser = true; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var bool |
||
| 157 | */ |
||
| 158 | private $doOptimizeAttributes = true; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var bool |
||
| 162 | */ |
||
| 163 | private $doRemoveComments = true; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var bool |
||
| 167 | */ |
||
| 168 | private $doRemoveWhitespaceAroundTags = true; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var bool |
||
| 172 | */ |
||
| 173 | private $doRemoveHttpPrefixFromAttributes = false; |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * @var array |
||
| 178 | */ |
||
| 179 | private $domainsToRemoveHttpPrefixFromAttributes = [ |
||
| 180 | 'google.com', |
||
| 181 | 'google.de', |
||
| 182 | ]; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var bool |
||
| 186 | */ |
||
| 187 | private $doSortCssClassNames = true; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var bool |
||
| 191 | */ |
||
| 192 | private $doSortHtmlAttributes = true; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @var bool |
||
| 196 | */ |
||
| 197 | private $doRemoveDeprecatedScriptCharsetAttribute = true; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var bool |
||
| 201 | */ |
||
| 202 | private $doRemoveDefaultAttributes = false; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var bool |
||
| 206 | */ |
||
| 207 | private $doRemoveDeprecatedAnchorName = true; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @var bool |
||
| 211 | */ |
||
| 212 | private $doRemoveDeprecatedTypeFromStylesheetLink = true; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @var bool |
||
| 216 | */ |
||
| 217 | private $doRemoveDeprecatedTypeFromScriptTag = true; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @var bool |
||
| 221 | */ |
||
| 222 | private $doRemoveValueFromEmptyInput = true; |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @var bool |
||
| 226 | */ |
||
| 227 | private $doRemoveEmptyAttributes = true; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @var bool |
||
| 231 | */ |
||
| 232 | private $doSumUpWhitespace = true; |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @var bool |
||
| 236 | */ |
||
| 237 | private $doRemoveSpacesBetweenTags = false; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * HtmlMin constructor. |
||
| 241 | */ |
||
| 242 | 25 | public function __construct() |
|
| 243 | { |
||
| 244 | 25 | } |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param boolean $doOptimizeAttributes |
||
| 248 | * |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | 2 | public function doOptimizeAttributes(bool $doOptimizeAttributes = true) |
|
| 252 | { |
||
| 253 | 2 | $this->doOptimizeAttributes = $doOptimizeAttributes; |
|
| 254 | |||
| 255 | 2 | return $this; |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param boolean $doOptimizeViaHtmlDomParser |
||
| 260 | * |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | 1 | public function doOptimizeViaHtmlDomParser(bool $doOptimizeViaHtmlDomParser = true) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param boolean $doRemoveComments |
||
| 272 | * |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | 2 | public function doRemoveComments(bool $doRemoveComments = true) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @param boolean $doRemoveDefaultAttributes |
||
| 284 | * |
||
| 285 | * @return $this |
||
| 286 | */ |
||
| 287 | 2 | public function doRemoveDefaultAttributes(bool $doRemoveDefaultAttributes = true) |
|
| 288 | { |
||
| 289 | 2 | $this->doRemoveDefaultAttributes = $doRemoveDefaultAttributes; |
|
| 290 | |||
| 291 | 2 | return $this; |
|
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param boolean $doRemoveDeprecatedAnchorName |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | 2 | public function doRemoveDeprecatedAnchorName(bool $doRemoveDeprecatedAnchorName = true) |
|
| 300 | { |
||
| 301 | 2 | $this->doRemoveDeprecatedAnchorName = $doRemoveDeprecatedAnchorName; |
|
| 302 | |||
| 303 | 2 | return $this; |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param boolean $doRemoveDeprecatedScriptCharsetAttribute |
||
| 308 | * |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | 2 | public function doRemoveDeprecatedScriptCharsetAttribute(bool $doRemoveDeprecatedScriptCharsetAttribute = true) |
|
| 312 | { |
||
| 313 | 2 | $this->doRemoveDeprecatedScriptCharsetAttribute = $doRemoveDeprecatedScriptCharsetAttribute; |
|
| 314 | |||
| 315 | 2 | return $this; |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param boolean $doRemoveDeprecatedTypeFromScriptTag |
||
| 320 | * |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | 2 | public function doRemoveDeprecatedTypeFromScriptTag(bool $doRemoveDeprecatedTypeFromScriptTag = true) |
|
| 324 | { |
||
| 325 | 2 | $this->doRemoveDeprecatedTypeFromScriptTag = $doRemoveDeprecatedTypeFromScriptTag; |
|
| 326 | |||
| 327 | 2 | return $this; |
|
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param boolean $doRemoveDeprecatedTypeFromStylesheetLink |
||
| 332 | * |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | 2 | public function doRemoveDeprecatedTypeFromStylesheetLink(bool $doRemoveDeprecatedTypeFromStylesheetLink = true) |
|
| 336 | { |
||
| 337 | 2 | $this->doRemoveDeprecatedTypeFromStylesheetLink = $doRemoveDeprecatedTypeFromStylesheetLink; |
|
| 338 | |||
| 339 | 2 | return $this; |
|
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param boolean $doRemoveEmptyAttributes |
||
| 344 | * |
||
| 345 | * @return $this |
||
| 346 | */ |
||
| 347 | 2 | public function doRemoveEmptyAttributes(bool $doRemoveEmptyAttributes = true) |
|
| 348 | { |
||
| 349 | 2 | $this->doRemoveEmptyAttributes = $doRemoveEmptyAttributes; |
|
| 350 | |||
| 351 | 2 | return $this; |
|
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param boolean $doRemoveHttpPrefixFromAttributes |
||
| 356 | * |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | 4 | public function doRemoveHttpPrefixFromAttributes(bool $doRemoveHttpPrefixFromAttributes = true) |
|
| 360 | { |
||
| 361 | 4 | $this->doRemoveHttpPrefixFromAttributes = $doRemoveHttpPrefixFromAttributes; |
|
| 362 | |||
| 363 | 4 | return $this; |
|
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param boolean $doRemoveSpacesBetweenTags |
||
| 368 | * |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function doRemoveSpacesBetweenTags(bool $doRemoveSpacesBetweenTags = true) |
||
| 372 | { |
||
| 373 | $this->doRemoveSpacesBetweenTags = $doRemoveSpacesBetweenTags; |
||
| 374 | |||
| 375 | return $this; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param boolean $doRemoveValueFromEmptyInput |
||
| 380 | * |
||
| 381 | * @return $this |
||
| 382 | */ |
||
| 383 | 2 | public function doRemoveValueFromEmptyInput(bool $doRemoveValueFromEmptyInput = true) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * @param boolean $doRemoveWhitespaceAroundTags |
||
| 392 | * |
||
| 393 | * @return $this |
||
| 394 | */ |
||
| 395 | 2 | public function doRemoveWhitespaceAroundTags(bool $doRemoveWhitespaceAroundTags = true) |
|
| 396 | { |
||
| 397 | 2 | $this->doRemoveWhitespaceAroundTags = $doRemoveWhitespaceAroundTags; |
|
| 398 | |||
| 399 | 2 | return $this; |
|
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param boolean $doSortCssClassNames |
||
| 404 | * |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | 2 | public function doSortCssClassNames(bool $doSortCssClassNames = true) |
|
| 408 | { |
||
| 409 | 2 | $this->doSortCssClassNames = $doSortCssClassNames; |
|
| 410 | |||
| 411 | 2 | return $this; |
|
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param boolean $doSortHtmlAttributes |
||
| 416 | * |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | 2 | public function doSortHtmlAttributes(bool $doSortHtmlAttributes = true) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @param boolean $doSumUpWhitespace |
||
| 428 | * |
||
| 429 | * @return $this |
||
| 430 | */ |
||
| 431 | 2 | public function doSumUpWhitespace(bool $doSumUpWhitespace = true) |
|
| 432 | { |
||
| 433 | 2 | $this->doSumUpWhitespace = $doSumUpWhitespace; |
|
| 434 | |||
| 435 | 2 | return $this; |
|
| 436 | } |
||
| 437 | |||
| 438 | 21 | private function domNodeAttributesToString(\DOMNode $node): string |
|
| 462 | |||
| 463 | 21 | private function domNodeClosingTagOptional(\DOMNode $node): bool |
|
| 632 | |||
| 633 | 21 | protected function domNodeToString(\DOMNode $node): string |
|
| 680 | |||
| 681 | /** |
||
| 682 | * @param \DOMNode $node |
||
| 683 | * |
||
| 684 | * @return \DOMNode|null |
||
| 685 | */ |
||
| 686 | 21 | protected function getNextSiblingOfTypeDOMElement(\DOMNode $node) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Check if the current string is an conditional comment. |
||
| 697 | * |
||
| 698 | * INFO: since IE >= 10 conditional comment are not working anymore |
||
| 699 | * |
||
| 700 | * <!--[if expression]> HTML <![endif]--> |
||
| 701 | * <![if expression]> HTML <![endif]> |
||
| 702 | * |
||
| 703 | * @param string $comment |
||
| 704 | * |
||
| 705 | * @return bool |
||
| 706 | */ |
||
| 707 | 2 | private function isConditionalComment($comment): bool |
|
| 719 | |||
| 720 | /** |
||
| 721 | * @param string $html |
||
| 722 | * @param bool $decodeUtf8Specials <p>Use this only in special cases, e.g. for PHP 5.3</p> |
||
| 723 | * |
||
| 724 | * @return string |
||
| 725 | */ |
||
| 726 | 25 | public function minify($html, $decodeUtf8Specials = false): string |
|
| 851 | |||
| 852 | /** |
||
| 853 | * @param $html |
||
| 854 | * @param $decodeUtf8Specials |
||
| 855 | * |
||
| 856 | * @return string |
||
| 857 | */ |
||
| 858 | 21 | private function minifyHtmlDom($html, $decodeUtf8Specials): string |
|
| 920 | |||
| 921 | /** |
||
| 922 | * Sort HTML-Attributes, so that gzip can do better work and remove some default attributes... |
||
| 923 | * |
||
| 924 | * @param SimpleHtmlDom $element |
||
| 925 | * |
||
| 926 | * @return bool |
||
| 927 | */ |
||
| 928 | 20 | private function optimizeAttributes(SimpleHtmlDom $element): bool |
|
| 991 | |||
| 992 | /** |
||
| 993 | * Prevent changes of inline "styles" and "scripts". |
||
| 994 | * |
||
| 995 | * @param HtmlDomParser $dom |
||
| 996 | * |
||
| 997 | * @return HtmlDomParser |
||
| 998 | */ |
||
| 999 | 21 | private function protectTags(HtmlDomParser $dom): HtmlDomParser |
|
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Check if the attribute can be removed. |
||
| 1047 | * |
||
| 1048 | * @param string $tag |
||
| 1049 | * @param string $attrName |
||
| 1050 | * @param string $attrValue |
||
| 1051 | * @param array $allAttr |
||
| 1052 | * |
||
| 1053 | * @return bool |
||
| 1054 | */ |
||
| 1055 | 9 | private function removeAttributeHelper($tag, $attrName, $attrValue, $allAttr): bool |
|
| 1121 | |||
| 1122 | /** |
||
| 1123 | * Remove comments in the dom. |
||
| 1124 | * |
||
| 1125 | * @param HtmlDomParser $dom |
||
| 1126 | * |
||
| 1127 | * @return HtmlDomParser |
||
| 1128 | */ |
||
| 1129 | 20 | private function removeComments(HtmlDomParser $dom): HtmlDomParser |
|
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Trim tags in the dom. |
||
| 1146 | * |
||
| 1147 | * @param SimpleHtmlDom $element |
||
| 1148 | * |
||
| 1149 | * @return void |
||
| 1150 | */ |
||
| 1151 | 20 | private function removeWhitespaceAroundTags(SimpleHtmlDom $element) |
|
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Callback function for preg_replace_callback use. |
||
| 1178 | * |
||
| 1179 | * @param array $matches PREG matches |
||
| 1180 | * |
||
| 1181 | * @return string |
||
| 1182 | */ |
||
| 1183 | 2 | private function restoreProtectedHtml($matches): string |
|
| 1194 | |||
| 1195 | /** |
||
| 1196 | * @param array $domainsToRemoveHttpPrefixFromAttributes |
||
| 1197 | * |
||
| 1198 | * @return $this |
||
| 1199 | */ |
||
| 1200 | 2 | public function setDomainsToRemoveHttpPrefixFromAttributes($domainsToRemoveHttpPrefixFromAttributes) |
|
| 1206 | |||
| 1207 | /** |
||
| 1208 | * @param $attrName |
||
| 1209 | * @param $attrValue |
||
| 1210 | * |
||
| 1211 | * @return string |
||
| 1212 | */ |
||
| 1213 | 9 | private function sortCssClassNames($attrName, $attrValue): string |
|
| 1237 | |||
| 1238 | /** |
||
| 1239 | * Sum-up extra whitespace from dom-nodes. |
||
| 1240 | * |
||
| 1241 | * @param HtmlDomParser $dom |
||
| 1242 | * |
||
| 1243 | * @return HtmlDomParser |
||
| 1244 | */ |
||
| 1245 | 20 | private function sumUpWhitespace(HtmlDomParser $dom): HtmlDomParser |
|
| 1271 | } |
||
| 1272 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.