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 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private static $booleanAttributes = array( |
||
| 38 | 'allowfullscreen', |
||
| 39 | 'async', |
||
| 40 | 'autofocus', |
||
| 41 | 'autoplay', |
||
| 42 | 'checked', |
||
| 43 | 'compact', |
||
| 44 | 'controls', |
||
| 45 | 'declare', |
||
| 46 | 'default', |
||
| 47 | 'defaultchecked', |
||
| 48 | 'defaultmuted', |
||
| 49 | 'defaultselected', |
||
| 50 | 'defer', |
||
| 51 | 'disabled', |
||
| 52 | 'enabled', |
||
| 53 | 'formnovalidate', |
||
| 54 | 'hidden', |
||
| 55 | 'indeterminate', |
||
| 56 | 'inert', |
||
| 57 | 'ismap', |
||
| 58 | 'itemscope', |
||
| 59 | 'loop', |
||
| 60 | 'multiple', |
||
| 61 | 'muted', |
||
| 62 | 'nohref', |
||
| 63 | 'noresize', |
||
| 64 | 'noshade', |
||
| 65 | 'novalidate', |
||
| 66 | 'nowrap', |
||
| 67 | 'open', |
||
| 68 | 'pauseonexit', |
||
| 69 | 'readonly', |
||
| 70 | 'required', |
||
| 71 | 'reversed', |
||
| 72 | 'scoped', |
||
| 73 | 'seamless', |
||
| 74 | 'selected', |
||
| 75 | 'sortable', |
||
| 76 | 'truespeed', |
||
| 77 | 'typemustmatch', |
||
| 78 | 'visible', |
||
| 79 | ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * An random md5-hash, generated via "random_bytes()". |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $randomHash; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array |
||
| 90 | 21 | */ |
|
| 91 | private $protectedChildNodes; |
||
| 92 | 21 | ||
| 93 | 21 | /** |
|
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private static $skipTagsForRemoveWhitespace = array('style', 'pre', 'code', 'script', 'textarea'); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | 21 | */ |
|
| 101 | private $protectedChildNodesHelper; |
||
| 102 | 21 | ||
| 103 | 21 | /** |
|
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | private $booleanAttributesHelper; |
||
| 107 | 21 | ||
| 108 | 21 | /** |
|
| 109 | 2 | * HtmlMin constructor. |
|
| 110 | */ |
||
| 111 | public function __construct() |
||
| 119 | 19 | ||
| 120 | 19 | /** |
|
| 121 | * @param string $html |
||
| 122 | 19 | * |
|
| 123 | 2 | * @return string |
|
| 124 | 2 | */ |
|
| 125 | 2 | public function minify($html) |
|
| 222 | 19 | ||
| 223 | /** |
||
| 224 | * Prevent changes of inline "styles" and "scripts". |
||
| 225 | * |
||
| 226 | * @param HtmlDomParser $dom |
||
| 227 | * |
||
| 228 | * @return HtmlDomParser |
||
| 229 | */ |
||
| 230 | private function protectTagsInDom(HtmlDomParser $dom) |
||
| 261 | 8 | ||
| 262 | /** |
||
| 263 | 8 | * Optimize HTML-tag attributes in the dom. |
|
| 264 | * |
||
| 265 | 8 | * @param HtmlDomParser $dom |
|
| 266 | * |
||
| 267 | 3 | * @return HtmlDomParser |
|
| 268 | */ |
||
| 269 | private function optimizeAttributesInDom(HtmlDomParser $dom) |
||
| 279 | |||
| 280 | /** |
||
| 281 | 8 | * Remove comments in the dom. |
|
| 282 | 8 | * |
|
| 283 | 8 | * @param HtmlDomParser $dom |
|
| 284 | * |
||
| 285 | * @return HtmlDomParser |
||
| 286 | 8 | */ |
|
| 287 | private function removeCommentsInDom(HtmlDomParser $dom) |
||
| 301 | |||
| 302 | 8 | /** |
|
| 303 | * Trim tags in the dom. |
||
| 304 | * |
||
| 305 | * @param HtmlDomParser $dom |
||
| 306 | * |
||
| 307 | 8 | * @return HtmlDomParser |
|
| 308 | 1 | */ |
|
| 309 | private function trimTagsInDom(HtmlDomParser $dom) { |
||
| 338 | 1 | ||
| 339 | /** |
||
| 340 | * Remove whitespace from dom-nodes. |
||
| 341 | * |
||
| 342 | 8 | * @param HtmlDomParser $dom |
|
| 343 | 1 | * |
|
| 344 | * @return HtmlDomParser |
||
| 345 | */ |
||
| 346 | private function removeWhitespaceInDom(HtmlDomParser $dom) |
||
| 372 | 4 | ||
| 373 | 4 | /** |
|
| 374 | 4 | * Callback function for preg_replace_callback use. |
|
| 375 | 1 | * |
|
| 376 | * @param array $matches PREG matches |
||
| 377 | 4 | * |
|
| 378 | * @return string |
||
| 379 | 4 | */ |
|
| 380 | private function restoreProtectedHtml($matches) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Sort HTML-Attributes, so that gzip can do better work |
||
| 396 | * and remove some default attributes. |
||
| 397 | * |
||
| 398 | * @param SimpleHtmlDom $element |
||
| 399 | * @param array $attributs |
||
| 400 | * |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | private function optimizeAttributes(SimpleHtmlDom $element, &$attributs) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Check if the attribute (key / value) is default and can be skipped. |
||
| 449 | * |
||
| 450 | * @param string $tag |
||
| 451 | * @param string $attrName |
||
| 452 | * @param string $attrValue |
||
| 453 | * @param string $allAttr |
||
| 454 | * |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | private function optimizeAttributesFilters($tag, $attrName, $attrValue, $allAttr) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param $attrName |
||
| 514 | * @param $attrValue |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | private function sortCssClasses($attrName, $attrValue) |
||
| 541 | } |
||
| 542 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.