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 | * // https://mathiasbynens.be/demo/javascript-mime-type |
||
| 21 | * // https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private static $executableScriptsMimeTypes = array( |
||
| 26 | 'text/javascript' => '', |
||
| 27 | 'text/ecmascript' => '', |
||
| 28 | 'text/jscript' => '', |
||
| 29 | 'application/javascript' => '', |
||
| 30 | 'application/x-javascript' => '', |
||
| 31 | 'application/ecmascript' => '', |
||
| 32 | ); |
||
| 33 | |||
| 34 | private static $selfClosingTags = array( |
||
| 35 | 'area', |
||
| 36 | 'base', |
||
| 37 | 'basefont', |
||
| 38 | 'br', |
||
| 39 | 'col', |
||
| 40 | 'command', |
||
| 41 | 'embed', |
||
| 42 | 'frame', |
||
| 43 | 'hr', |
||
| 44 | 'img', |
||
| 45 | 'input', |
||
| 46 | 'isindex', |
||
| 47 | 'keygen', |
||
| 48 | 'link', |
||
| 49 | 'meta', |
||
| 50 | 'param', |
||
| 51 | 'source', |
||
| 52 | 'track', |
||
| 53 | 'wbr', |
||
| 54 | ); |
||
| 55 | |||
| 56 | private static $trimWhitespaceFromTags = array( |
||
| 57 | 'article' => '', |
||
| 58 | 'br' => '', |
||
| 59 | 'div' => '', |
||
| 60 | 'footer' => '', |
||
| 61 | 'hr' => '', |
||
| 62 | 'nav' => '', |
||
| 63 | 'p' => '', |
||
| 64 | 'script' => '', |
||
| 65 | ); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private static $booleanAttributes = array( |
||
| 71 | 'allowfullscreen' => '', |
||
| 72 | 'async' => '', |
||
| 73 | 'autofocus' => '', |
||
| 74 | 'autoplay' => '', |
||
| 75 | 'checked' => '', |
||
| 76 | 'compact' => '', |
||
| 77 | 'controls' => '', |
||
| 78 | 'declare' => '', |
||
| 79 | 'default' => '', |
||
| 80 | 'defaultchecked' => '', |
||
| 81 | 'defaultmuted' => '', |
||
| 82 | 'defaultselected' => '', |
||
| 83 | 'defer' => '', |
||
| 84 | 'disabled' => '', |
||
| 85 | 'enabled' => '', |
||
| 86 | 'formnovalidate' => '', |
||
| 87 | 'hidden' => '', |
||
| 88 | 'indeterminate' => '', |
||
| 89 | 'inert' => '', |
||
| 90 | 'ismap' => '', |
||
| 91 | 'itemscope' => '', |
||
| 92 | 'loop' => '', |
||
| 93 | 'multiple' => '', |
||
| 94 | 'muted' => '', |
||
| 95 | 'nohref' => '', |
||
| 96 | 'noresize' => '', |
||
| 97 | 'noshade' => '', |
||
| 98 | 'novalidate' => '', |
||
| 99 | 'nowrap' => '', |
||
| 100 | 'open' => '', |
||
| 101 | 'pauseonexit' => '', |
||
| 102 | 'readonly' => '', |
||
| 103 | 'required' => '', |
||
| 104 | 'reversed' => '', |
||
| 105 | 'scoped' => '', |
||
| 106 | 'seamless' => '', |
||
| 107 | 'selected' => '', |
||
| 108 | 'sortable' => '', |
||
| 109 | 'truespeed' => '', |
||
| 110 | 'typemustmatch' => '', |
||
| 111 | 'visible' => '', |
||
| 112 | ); |
||
| 113 | /** |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | private static $skipTagsForRemoveWhitespace = array( |
||
| 117 | 'code', |
||
| 118 | 'pre', |
||
| 119 | 'script', |
||
| 120 | 'style', |
||
| 121 | 'textarea', |
||
| 122 | ); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | private $protectedChildNodes = array(); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | private $protectedChildNodesHelper = 'html-min--voku--saved-content'; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var string |
||
| 136 | 23 | */ |
|
| 137 | private $booleanAttributesHelper = 'html-min--voku--delete-this'; |
||
| 138 | 23 | ||
| 139 | 23 | /** |
|
| 140 | * @var bool |
||
| 141 | 23 | */ |
|
| 142 | 23 | private $doOptimizeViaHtmlDomParser = true; |
|
| 143 | 23 | ||
| 144 | /** |
||
| 145 | * @var bool |
||
| 146 | */ |
||
| 147 | private $doOptimizeAttributes = true; |
||
| 148 | |||
| 149 | /** |
||
| 150 | 23 | * @var bool |
|
| 151 | */ |
||
| 152 | 23 | private $doRemoveComments = true; |
|
| 153 | 23 | ||
| 154 | 1 | /** |
|
| 155 | * @var bool |
||
| 156 | */ |
||
| 157 | 23 | private $doRemoveWhitespaceAroundTags = true; |
|
| 158 | 23 | ||
| 159 | 3 | /** |
|
| 160 | * @var bool |
||
| 161 | */ |
||
| 162 | private $doRemoveHttpPrefixFromAttributes = true; |
||
| 163 | 20 | ||
| 164 | 20 | /** |
|
| 165 | 20 | * @var bool |
|
| 166 | */ |
||
| 167 | 20 | private $doSortCssClassNames = true; |
|
| 168 | 20 | ||
| 169 | 20 | /** |
|
| 170 | * @var bool |
||
| 171 | 20 | */ |
|
| 172 | private $doSortHtmlAttributes = true; |
||
| 173 | 20 | ||
| 174 | 20 | /** |
|
| 175 | 11 | * @var bool |
|
| 176 | 11 | */ |
|
| 177 | 11 | private $doRemoveDeprecatedScriptCharsetAttribute = true; |
|
| 178 | |||
| 179 | 11 | /** |
|
| 180 | * @var bool |
||
| 181 | */ |
||
| 182 | private $doRemoveDefaultAttributes = true; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var bool |
||
| 186 | 11 | */ |
|
| 187 | private $doRemoveDeprecatedAnchorName = true; |
||
| 188 | 11 | ||
| 189 | /** |
||
| 190 | 11 | * @var bool |
|
| 191 | */ |
||
| 192 | private $doRemoveDeprecatedTypeFromStylesheetLink = true; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @var bool |
||
| 196 | 11 | */ |
|
| 197 | 11 | private $doRemoveDeprecatedTypeFromScriptTag = true; |
|
| 198 | 11 | ||
| 199 | /** |
||
| 200 | 11 | * @var bool |
|
| 201 | 11 | */ |
|
| 202 | private $doRemoveValueFromEmptyInput = true; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var bool |
||
| 206 | */ |
||
| 207 | 11 | private $doRemoveEmptyAttributes = true; |
|
| 208 | |||
| 209 | 11 | /** |
|
| 210 | * @var bool |
||
| 211 | 11 | */ |
|
| 212 | 11 | private $doSumUpWhitespace = true; |
|
| 213 | 11 | ||
| 214 | 11 | /** |
|
| 215 | 11 | * HtmlMin constructor. |
|
| 216 | 11 | */ |
|
| 217 | public function __construct() |
||
| 220 | 11 | ||
| 221 | 11 | /** |
|
| 222 | 11 | * @param boolean $doOptimizeAttributes |
|
| 223 | 11 | */ |
|
| 224 | public function doOptimizeAttributes($doOptimizeAttributes = true) |
||
| 228 | |||
| 229 | 11 | /** |
|
| 230 | 11 | * @param boolean $doOptimizeViaHtmlDomParser |
|
| 231 | */ |
||
| 232 | 11 | public function doOptimizeViaHtmlDomParser($doOptimizeViaHtmlDomParser = true) |
|
| 236 | |||
| 237 | 11 | /** |
|
| 238 | 11 | * @param boolean $doRemoveComments |
|
| 239 | 1 | */ |
|
| 240 | 1 | public function doRemoveComments($doRemoveComments = true) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param boolean $doRemoveDefaultAttributes |
||
| 247 | 11 | */ |
|
| 248 | 3 | public function doRemoveDefaultAttributes($doRemoveDefaultAttributes = true) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @param boolean $doRemoveDeprecatedAnchorName |
||
| 255 | */ |
||
| 256 | public function doRemoveDeprecatedAnchorName($doRemoveDeprecatedAnchorName = true) |
||
| 260 | |||
| 261 | /** |
||
| 262 | 20 | * @param boolean $doRemoveDeprecatedScriptCharsetAttribute |
|
| 263 | */ |
||
| 264 | 20 | public function doRemoveDeprecatedScriptCharsetAttribute($doRemoveDeprecatedScriptCharsetAttribute = true) |
|
| 268 | |||
| 269 | 9 | /** |
|
| 270 | 9 | * @param boolean $doRemoveDeprecatedTypeFromScriptTag |
|
| 271 | */ |
||
| 272 | 9 | public function doRemoveDeprecatedTypeFromScriptTag($doRemoveDeprecatedTypeFromScriptTag = true) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param boolean $doRemoveDeprecatedTypeFromStylesheetLink |
||
| 279 | 9 | */ |
|
| 280 | 9 | public function doRemoveDeprecatedTypeFromStylesheetLink($doRemoveDeprecatedTypeFromStylesheetLink = true) |
|
| 284 | 9 | ||
| 285 | /** |
||
| 286 | * @param boolean $doRemoveEmptyAttributes |
||
| 287 | */ |
||
| 288 | 9 | public function doRemoveEmptyAttributes($doRemoveEmptyAttributes = true) |
|
| 292 | |||
| 293 | 9 | /** |
|
| 294 | * @param boolean $doRemoveHttpPrefixFromAttributes |
||
| 295 | 9 | */ |
|
| 296 | 9 | public function doRemoveHttpPrefixFromAttributes($doRemoveHttpPrefixFromAttributes = true) |
|
| 300 | 9 | ||
| 301 | 9 | /** |
|
| 302 | * @param boolean $doRemoveValueFromEmptyInput |
||
| 303 | */ |
||
| 304 | public function doRemoveValueFromEmptyInput($doRemoveValueFromEmptyInput = true) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param boolean $doRemoveWhitespaceAroundTags |
||
| 311 | */ |
||
| 312 | public function doRemoveWhitespaceAroundTags($doRemoveWhitespaceAroundTags = true) |
||
| 316 | |||
| 317 | /** |
||
| 318 | 9 | * @param boolean $doSortCssClassNames |
|
| 319 | */ |
||
| 320 | public function doSortCssClassNames($doSortCssClassNames = true) |
||
| 324 | |||
| 325 | /** |
||
| 326 | 9 | * @param boolean $doSortHtmlAttributes |
|
| 327 | */ |
||
| 328 | public function doSortHtmlAttributes($doSortHtmlAttributes = true) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param boolean $doSumUpWhitespace |
||
| 335 | */ |
||
| 336 | 9 | public function doSumUpWhitespace($doSumUpWhitespace = true) |
|
| 340 | |||
| 341 | 9 | /** |
|
| 342 | * Check if the current string is an conditional comment. |
||
| 343 | * |
||
| 344 | * INFO: since IE >= 10 conditional comment are not working anymore |
||
| 345 | * |
||
| 346 | 9 | * <!--[if expression]> HTML <![endif]--> |
|
| 347 | * <![if expression]> HTML <![endif]> |
||
| 348 | * |
||
| 349 | * @param string $comment |
||
| 350 | * |
||
| 351 | 9 | * @return bool |
|
| 352 | */ |
||
| 353 | private function isConditionalComment($comment) |
||
| 365 | |||
| 366 | 9 | /** |
|
| 367 | * @param string $html |
||
| 368 | * @param bool $decodeUtf8Specials <p>Use this only in special cases, e.g. for PHP 5.3</p> |
||
| 369 | * |
||
| 370 | 9 | * @return string |
|
| 371 | */ |
||
| 372 | public function minify($html, $decodeUtf8Specials = false) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param $html |
||
| 486 | * @param $decodeUtf8Specials |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | public function minifyHtmlDom($html, $decodeUtf8Specials) |
||
| 549 | |||
| 550 | 3 | /** |
|
| 551 | 3 | * Sort HTML-Attributes, so that gzip can do better work and remove some default attributes... |
|
| 552 | 3 | * |
|
| 553 | 3 | * @param SimpleHtmlDom $element |
|
| 554 | 3 | * |
|
| 555 | 3 | * @return bool |
|
| 556 | */ |
||
| 557 | 3 | private function optimizeAttributes(SimpleHtmlDom $element) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Prevent changes of inline "styles" and "scripts". |
||
| 629 | * |
||
| 630 | * @param HtmlDomParser $dom |
||
| 631 | * |
||
| 632 | * @return HtmlDomParser |
||
| 633 | */ |
||
| 634 | private function protectTags(HtmlDomParser $dom) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Check if the attribute can be removed. |
||
| 682 | * |
||
| 683 | * @param string $tag |
||
| 684 | * @param string $attrName |
||
| 685 | * @param string $attrValue |
||
| 686 | * @param array $allAttr |
||
| 687 | * |
||
| 688 | * @return bool |
||
| 689 | */ |
||
| 690 | private function removeAttributeHelper($tag, $attrName, $attrValue, $allAttr) |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Remove comments in the dom. |
||
| 759 | * |
||
| 760 | * @param HtmlDomParser $dom |
||
| 761 | * |
||
| 762 | * @return HtmlDomParser |
||
| 763 | */ |
||
| 764 | private function removeComments(HtmlDomParser $dom) |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Trim tags in the dom. |
||
| 781 | * |
||
| 782 | * @param SimpleHtmlDom $element |
||
| 783 | * |
||
| 784 | * @return void |
||
| 785 | */ |
||
| 786 | private function removeWhitespaceAroundTags(SimpleHtmlDom $element) |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Callback function for preg_replace_callback use. |
||
| 813 | * |
||
| 814 | * @param array $matches PREG matches |
||
| 815 | * |
||
| 816 | * @return string |
||
| 817 | */ |
||
| 818 | private function restoreProtectedHtml($matches) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @param $attrName |
||
| 832 | * @param $attrValue |
||
| 833 | * |
||
| 834 | * @return string |
||
| 835 | */ |
||
| 836 | private function sortCssClassNames($attrName, $attrValue) |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Sum-up extra whitespace from dom-nodes. |
||
| 863 | * |
||
| 864 | * @param HtmlDomParser $dom |
||
| 865 | * |
||
| 866 | * @return HtmlDomParser |
||
| 867 | */ |
||
| 868 | private function sumUpWhitespace(HtmlDomParser $dom) |
||
| 894 | } |
||
| 895 |
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.