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 |
||
| 17 | class HtmlMin |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private static $regExSpace = "/[[:space:]]{2,}|[\r\n]+/u"; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * // https://mathiasbynens.be/demo/javascript-mime-type |
||
| 26 | * // https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $executableScriptsMimeTypes = array( |
||
| 31 | 'text/javascript' => '', |
||
| 32 | 'text/ecmascript' => '', |
||
| 33 | 'text/jscript' => '', |
||
| 34 | 'application/javascript' => '', |
||
| 35 | 'application/x-javascript' => '', |
||
| 36 | 'application/ecmascript' => '', |
||
| 37 | ); |
||
| 38 | |||
| 39 | private static $selfClosingTags = array( |
||
| 40 | 'area', |
||
| 41 | 'base', |
||
| 42 | 'basefont', |
||
| 43 | 'br', |
||
| 44 | 'col', |
||
| 45 | 'command', |
||
| 46 | 'embed', |
||
| 47 | 'frame', |
||
| 48 | 'hr', |
||
| 49 | 'img', |
||
| 50 | 'input', |
||
| 51 | 'isindex', |
||
| 52 | 'keygen', |
||
| 53 | 'link', |
||
| 54 | 'meta', |
||
| 55 | 'param', |
||
| 56 | 'source', |
||
| 57 | 'track', |
||
| 58 | 'wbr', |
||
| 59 | ); |
||
| 60 | |||
| 61 | private static $trimWhitespaceFromTags = array( |
||
| 62 | 'article' => '', |
||
| 63 | 'br' => '', |
||
| 64 | 'div' => '', |
||
| 65 | 'footer' => '', |
||
| 66 | 'hr' => '', |
||
| 67 | 'nav' => '', |
||
| 68 | 'p' => '', |
||
| 69 | 'script' => '', |
||
| 70 | ); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private static $booleanAttributes = array( |
||
| 76 | 'allowfullscreen' => '', |
||
| 77 | 'async' => '', |
||
| 78 | 'autofocus' => '', |
||
| 79 | 'autoplay' => '', |
||
| 80 | 'checked' => '', |
||
| 81 | 'compact' => '', |
||
| 82 | 'controls' => '', |
||
| 83 | 'declare' => '', |
||
| 84 | 'default' => '', |
||
| 85 | 'defaultchecked' => '', |
||
| 86 | 'defaultmuted' => '', |
||
| 87 | 'defaultselected' => '', |
||
| 88 | 'defer' => '', |
||
| 89 | 'disabled' => '', |
||
| 90 | 'enabled' => '', |
||
| 91 | 'formnovalidate' => '', |
||
| 92 | 'hidden' => '', |
||
| 93 | 'indeterminate' => '', |
||
| 94 | 'inert' => '', |
||
| 95 | 'ismap' => '', |
||
| 96 | 'itemscope' => '', |
||
| 97 | 'loop' => '', |
||
| 98 | 'multiple' => '', |
||
| 99 | 'muted' => '', |
||
| 100 | 'nohref' => '', |
||
| 101 | 'noresize' => '', |
||
| 102 | 'noshade' => '', |
||
| 103 | 'novalidate' => '', |
||
| 104 | 'nowrap' => '', |
||
| 105 | 'open' => '', |
||
| 106 | 'pauseonexit' => '', |
||
| 107 | 'readonly' => '', |
||
| 108 | 'required' => '', |
||
| 109 | 'reversed' => '', |
||
| 110 | 'scoped' => '', |
||
| 111 | 'seamless' => '', |
||
| 112 | 'selected' => '', |
||
| 113 | 'sortable' => '', |
||
| 114 | 'truespeed' => '', |
||
| 115 | 'typemustmatch' => '', |
||
| 116 | 'visible' => '', |
||
| 117 | ); |
||
| 118 | /** |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private static $skipTagsForRemoveWhitespace = array( |
||
| 122 | 'code', |
||
| 123 | 'pre', |
||
| 124 | 'script', |
||
| 125 | 'style', |
||
| 126 | 'textarea', |
||
| 127 | ); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private $protectedChildNodes = array(); |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var string |
||
| 136 | 23 | */ |
|
| 137 | private $protectedChildNodesHelper = 'html-min--voku--saved-content'; |
||
| 138 | 23 | ||
| 139 | 23 | /** |
|
| 140 | * @var string |
||
| 141 | 23 | */ |
|
| 142 | 23 | private $booleanAttributesHelper = 'html-min--voku--delete-this'; |
|
| 143 | 23 | ||
| 144 | /** |
||
| 145 | * @var bool |
||
| 146 | */ |
||
| 147 | private $doOptimizeViaHtmlDomParser = true; |
||
| 148 | |||
| 149 | /** |
||
| 150 | 23 | * @var bool |
|
| 151 | */ |
||
| 152 | 23 | private $doOptimizeAttributes = true; |
|
| 153 | 23 | ||
| 154 | 1 | /** |
|
| 155 | * @var bool |
||
| 156 | */ |
||
| 157 | 23 | private $doRemoveComments = true; |
|
| 158 | 23 | ||
| 159 | 3 | /** |
|
| 160 | * @var bool |
||
| 161 | */ |
||
| 162 | private $doRemoveWhitespaceAroundTags = true; |
||
| 163 | 20 | ||
| 164 | 20 | /** |
|
| 165 | 20 | * @var bool |
|
| 166 | */ |
||
| 167 | 20 | private $doRemoveHttpPrefixFromAttributes = false; |
|
| 168 | 20 | ||
| 169 | 20 | ||
| 170 | /** |
||
| 171 | 20 | * @var array |
|
| 172 | */ |
||
| 173 | 20 | private $domainsToRemoveHttpPrefixFromAttributes = array( |
|
| 174 | 20 | 'google.com', |
|
| 175 | 11 | 'google.de', |
|
| 176 | 11 | ); |
|
| 177 | 11 | ||
| 178 | /** |
||
| 179 | 11 | * @var bool |
|
| 180 | */ |
||
| 181 | private $doSortCssClassNames = true; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var bool |
||
| 185 | */ |
||
| 186 | 11 | private $doSortHtmlAttributes = true; |
|
| 187 | |||
| 188 | 11 | /** |
|
| 189 | * @var bool |
||
| 190 | 11 | */ |
|
| 191 | private $doRemoveDeprecatedScriptCharsetAttribute = true; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var bool |
||
| 195 | */ |
||
| 196 | 11 | private $doRemoveDefaultAttributes = false; |
|
| 197 | 11 | ||
| 198 | 11 | /** |
|
| 199 | * @var bool |
||
| 200 | 11 | */ |
|
| 201 | 11 | private $doRemoveDeprecatedAnchorName = true; |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @var bool |
||
| 205 | */ |
||
| 206 | private $doRemoveDeprecatedTypeFromStylesheetLink = true; |
||
| 207 | 11 | ||
| 208 | /** |
||
| 209 | 11 | * @var bool |
|
| 210 | */ |
||
| 211 | 11 | private $doRemoveDeprecatedTypeFromScriptTag = true; |
|
| 212 | 11 | ||
| 213 | 11 | /** |
|
| 214 | 11 | * @var bool |
|
| 215 | 11 | */ |
|
| 216 | 11 | private $doRemoveValueFromEmptyInput = true; |
|
| 217 | |||
| 218 | 11 | /** |
|
| 219 | 11 | * @var bool |
|
| 220 | 11 | */ |
|
| 221 | 11 | private $doRemoveEmptyAttributes = true; |
|
| 222 | 11 | ||
| 223 | 11 | /** |
|
| 224 | * @var bool |
||
| 225 | 11 | */ |
|
| 226 | private $doSumUpWhitespace = true; |
||
| 227 | 11 | ||
| 228 | /** |
||
| 229 | 11 | * @var bool |
|
| 230 | 11 | */ |
|
| 231 | private $doRemoveSpacesBetweenTags = false; |
||
| 232 | 11 | ||
| 233 | 11 | /** |
|
| 234 | * HtmlMin constructor. |
||
| 235 | 11 | */ |
|
| 236 | public function __construct() |
||
| 239 | 1 | ||
| 240 | 1 | /** |
|
| 241 | 11 | * @param boolean $doOptimizeAttributes |
|
| 242 | * |
||
| 243 | * @return $this |
||
| 244 | */ |
||
| 245 | public function doOptimizeAttributes($doOptimizeAttributes = true) |
||
| 251 | 11 | ||
| 252 | /** |
||
| 253 | * @param boolean $doOptimizeViaHtmlDomParser |
||
| 254 | * |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | public function doOptimizeViaHtmlDomParser($doOptimizeViaHtmlDomParser = true) |
||
| 263 | |||
| 264 | 20 | /** |
|
| 265 | 20 | * @param boolean $doRemoveComments |
|
| 266 | 17 | * |
|
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | 9 | public function doRemoveComments($doRemoveComments = true) |
|
| 275 | 5 | ||
| 276 | /** |
||
| 277 | * @param boolean $doRemoveDefaultAttributes |
||
| 278 | * |
||
| 279 | 9 | * @return $this |
|
| 280 | 9 | */ |
|
| 281 | public function doRemoveDefaultAttributes($doRemoveDefaultAttributes = true) |
||
| 287 | |||
| 288 | 9 | /** |
|
| 289 | * @param boolean $doRemoveDeprecatedAnchorName |
||
| 290 | * |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | 9 | public function doRemoveDeprecatedAnchorName($doRemoveDeprecatedAnchorName = true) |
|
| 299 | 9 | ||
| 300 | 9 | /** |
|
| 301 | 9 | * @param boolean $doRemoveDeprecatedScriptCharsetAttribute |
|
| 302 | * |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | public function doRemoveDeprecatedScriptCharsetAttribute($doRemoveDeprecatedScriptCharsetAttribute = true) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param boolean $doRemoveDeprecatedTypeFromScriptTag |
||
| 314 | * |
||
| 315 | * @return $this |
||
| 316 | */ |
||
| 317 | public function doRemoveDeprecatedTypeFromScriptTag($doRemoveDeprecatedTypeFromScriptTag = true) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param boolean $doRemoveDeprecatedTypeFromStylesheetLink |
||
| 326 | 9 | * |
|
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | public function doRemoveDeprecatedTypeFromStylesheetLink($doRemoveDeprecatedTypeFromStylesheetLink = true) |
||
| 335 | |||
| 336 | 9 | /** |
|
| 337 | * @param boolean $doRemoveEmptyAttributes |
||
| 338 | * |
||
| 339 | * @return $this |
||
| 340 | */ |
||
| 341 | 9 | public function doRemoveEmptyAttributes($doRemoveEmptyAttributes = true) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param boolean $doRemoveHttpPrefixFromAttributes |
||
| 350 | * |
||
| 351 | 9 | * @return $this |
|
| 352 | */ |
||
| 353 | public function doRemoveHttpPrefixFromAttributes($doRemoveHttpPrefixFromAttributes = true) |
||
| 359 | |||
| 360 | /** |
||
| 361 | 9 | * @param boolean $doRemoveValueFromEmptyInput |
|
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function doRemoveValueFromEmptyInput($doRemoveValueFromEmptyInput = true) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param boolean $doRemoveWhitespaceAroundTags |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function doRemoveWhitespaceAroundTags($doRemoveWhitespaceAroundTags = true) |
||
| 383 | 20 | ||
| 384 | 17 | /** |
|
| 385 | * @param boolean $doSortCssClassNames |
||
| 386 | 11 | * |
|
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | public function doSortCssClassNames($doSortCssClassNames = true) |
||
| 395 | |||
| 396 | 20 | /** |
|
| 397 | * @param boolean $doSortHtmlAttributes |
||
| 398 | * |
||
| 399 | 20 | * @return $this |
|
| 400 | */ |
||
| 401 | 20 | public function doSortHtmlAttributes($doSortHtmlAttributes = true) |
|
| 407 | 2 | ||
| 408 | /** |
||
| 409 | 3 | * @param boolean $doSumUpWhitespace |
|
| 410 | * |
||
| 411 | 3 | * @return $this |
|
| 412 | 3 | */ |
|
| 413 | 3 | public function doSumUpWhitespace($doSumUpWhitespace = true) |
|
| 419 | |||
| 420 | 3 | /** |
|
| 421 | * @param boolean $doRemoveSpacesBetweenTags |
||
| 422 | 3 | * |
|
| 423 | 20 | * @return $this |
|
| 424 | */ |
||
| 425 | 20 | public function doRemoveSpacesBetweenTags($doRemoveSpacesBetweenTags = true) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Check if the current string is an conditional comment. |
||
| 434 | * |
||
| 435 | 11 | * INFO: since IE >= 10 conditional comment are not working anymore |
|
| 436 | * |
||
| 437 | 11 | * <!--[if expression]> HTML <![endif]--> |
|
| 438 | * <![if expression]> HTML <![endif]> |
||
| 439 | * |
||
| 440 | * @param string $comment |
||
| 441 | * |
||
| 442 | * @return bool |
||
| 443 | 11 | */ |
|
| 444 | private function isConditionalComment($comment) |
||
| 456 | |||
| 457 | 12 | /** |
|
| 458 | * @param string $html |
||
| 459 | 11 | * @param bool $decodeUtf8Specials <p>Use this only in special cases, e.g. for PHP 5.3</p> |
|
| 460 | 11 | * |
|
| 461 | 11 | * @return string |
|
| 462 | 11 | */ |
|
| 463 | public function minify($html, $decodeUtf8Specials = false) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param $html |
||
| 579 | * @param $decodeUtf8Specials |
||
| 580 | * |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | private function minifyHtmlDom($html, $decodeUtf8Specials) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Sort HTML-Attributes, so that gzip can do better work and remove some default attributes... |
||
| 645 | * |
||
| 646 | * @param SimpleHtmlDom $element |
||
| 647 | * |
||
| 648 | * @return bool |
||
| 649 | */ |
||
| 650 | private function optimizeAttributes(SimpleHtmlDom $element) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Prevent changes of inline "styles" and "scripts". |
||
| 722 | * |
||
| 723 | * @param HtmlDomParser $dom |
||
| 724 | * |
||
| 725 | * @return HtmlDomParser |
||
| 726 | */ |
||
| 727 | private function protectTags(HtmlDomParser $dom) |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Check if the attribute can be removed. |
||
| 775 | * |
||
| 776 | * @param string $tag |
||
| 777 | * @param string $attrName |
||
| 778 | * @param string $attrValue |
||
| 779 | * @param array $allAttr |
||
| 780 | * |
||
| 781 | * @return bool |
||
| 782 | */ |
||
| 783 | private function removeAttributeHelper($tag, $attrName, $attrValue, $allAttr) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Remove comments in the dom. |
||
| 852 | * |
||
| 853 | * @param HtmlDomParser $dom |
||
| 854 | * |
||
| 855 | * @return HtmlDomParser |
||
| 856 | */ |
||
| 857 | private function removeComments(HtmlDomParser $dom) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Trim tags in the dom. |
||
| 874 | * |
||
| 875 | * @param SimpleHtmlDom $element |
||
| 876 | * |
||
| 877 | * @return void |
||
| 878 | */ |
||
| 879 | private function removeWhitespaceAroundTags(SimpleHtmlDom $element) |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Callback function for preg_replace_callback use. |
||
| 906 | * |
||
| 907 | * @param array $matches PREG matches |
||
| 908 | * |
||
| 909 | * @return string |
||
| 910 | */ |
||
| 911 | private function restoreProtectedHtml($matches) |
||
| 922 | |||
| 923 | /** |
||
| 924 | * @param array $domainsToRemoveHttpPrefixFromAttributes |
||
| 925 | * |
||
| 926 | * @return $this |
||
| 927 | */ |
||
| 928 | public function setDomainsToRemoveHttpPrefixFromAttributes($domainsToRemoveHttpPrefixFromAttributes) |
||
| 934 | |||
| 935 | /** |
||
| 936 | * @param $attrName |
||
| 937 | * @param $attrValue |
||
| 938 | * |
||
| 939 | * @return string |
||
| 940 | */ |
||
| 941 | private function sortCssClassNames($attrName, $attrValue) |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Sum-up extra whitespace from dom-nodes. |
||
| 968 | * |
||
| 969 | * @param HtmlDomParser $dom |
||
| 970 | * |
||
| 971 | * @return HtmlDomParser |
||
| 972 | */ |
||
| 973 | private function sumUpWhitespace(HtmlDomParser $dom) |
||
| 999 | } |
||
| 1000 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.