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 |
||
| 16 | class HtmlMin |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * // https://mathiasbynens.be/demo/javascript-mime-type |
||
| 20 | * // https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private static $executableScriptsMimeTypes = array( |
||
| 25 | 'text/javascript', |
||
| 26 | 'text/ecmascript', |
||
| 27 | 'text/jscript', |
||
| 28 | 'application/javascript', |
||
| 29 | 'application/x-javascript', |
||
| 30 | 'application/ecmascript', |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private static $booleanAttributes = array( |
||
| 37 | 'allowfullscreen', |
||
| 38 | 'async', |
||
| 39 | 'autofocus', |
||
| 40 | 'autoplay', |
||
| 41 | 'checked', |
||
| 42 | 'compact', |
||
| 43 | 'controls', |
||
| 44 | 'declare', |
||
| 45 | 'default', |
||
| 46 | 'defaultchecked', |
||
| 47 | 'defaultmuted', |
||
| 48 | 'defaultselected', |
||
| 49 | 'defer', |
||
| 50 | 'disabled', |
||
| 51 | 'enabled', |
||
| 52 | 'formnovalidate', |
||
| 53 | 'hidden', |
||
| 54 | 'indeterminate', |
||
| 55 | 'inert', |
||
| 56 | 'ismap', |
||
| 57 | 'itemscope', |
||
| 58 | 'loop', |
||
| 59 | 'multiple', |
||
| 60 | 'muted', |
||
| 61 | 'nohref', |
||
| 62 | 'noresize', |
||
| 63 | 'noshade', |
||
| 64 | 'novalidate', |
||
| 65 | 'nowrap', |
||
| 66 | 'open', |
||
| 67 | 'pauseonexit', |
||
| 68 | 'readonly', |
||
| 69 | 'required', |
||
| 70 | 'reversed', |
||
| 71 | 'scoped', |
||
| 72 | 'seamless', |
||
| 73 | 'selected', |
||
| 74 | 'sortable', |
||
| 75 | 'truespeed', |
||
| 76 | 'typemustmatch', |
||
| 77 | 'visible', |
||
| 78 | ); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * An random md5-hash, generated via "random_bytes()". |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $randomHash; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * HtmlMin constructor. |
||
| 89 | */ |
||
| 90 | 21 | public function __construct() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $html |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | 21 | public function minify($html) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Sort HTML-Attributes, so that gzip can do better work |
||
| 227 | * and remove some default attributes. |
||
| 228 | * |
||
| 229 | * @param SimpleHtmlDom $element |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | 19 | private function optimizeAttributes(SimpleHtmlDom $element) |
|
| 234 | { |
||
| 235 | 19 | $attributs = $element->getAllAttributes(); |
|
| 236 | |||
| 237 | 19 | if (!$attributs) { |
|
| 238 | 19 | return false; |
|
| 239 | } |
||
| 240 | |||
| 241 | /* |
||
|
|
|||
| 242 | if ( |
||
| 243 | ($element->tag === 'script' || $element->tag === 'style') |
||
| 244 | && |
||
| 245 | !isset($attributs['src']) |
||
| 246 | ) { |
||
| 247 | // TODO: protect inline css / js |
||
| 248 | } |
||
| 249 | */ |
||
| 250 | |||
| 251 | 8 | $attrs = array(); |
|
| 252 | 8 | foreach ((array)$attributs as $attrName => $attrValue) { |
|
| 253 | |||
| 254 | 8 | if (in_array($attrName, self::$booleanAttributes, true)) { |
|
| 255 | 6 | $attrs[$attrName] = 'delete-this-' . $this->randomHash; |
|
| 256 | 6 | $element->{$attrName} = null; |
|
| 257 | 6 | continue; |
|
| 258 | } |
||
| 259 | |||
| 260 | if ( |
||
| 261 | 8 | ($attrName === 'href' || $attrName === 'src' || $attrName === 'action') |
|
| 262 | && |
||
| 263 | 8 | !(isset($attributs['rel']) && $attributs['rel'] === 'external') |
|
| 264 | && |
||
| 265 | 8 | !(isset($attributs['target']) && $attributs['target'] === '_blank') |
|
| 266 | ) { |
||
| 267 | 3 | $attrValue = str_replace('http://', '//', $attrValue); |
|
| 268 | } |
||
| 269 | |||
| 270 | 8 | if ($this->optimizeAttributesFilters($element->tag, $attrName, $attrValue, $attributs)) { |
|
| 271 | 2 | $element->{$attrName} = null; |
|
| 272 | 2 | continue; |
|
| 273 | } |
||
| 274 | |||
| 275 | 8 | $attrValue = $this->sortCssClasses($attrName, $attrValue); |
|
| 276 | |||
| 277 | 8 | $attrs[$attrName] = $attrValue; |
|
| 278 | 8 | $element->{$attrName} = null; |
|
| 279 | } |
||
| 280 | |||
| 281 | 8 | ksort($attrs); |
|
| 282 | 8 | foreach ($attrs as $attrName => $attrValue) { |
|
| 283 | 8 | $element->setAttribute($attrName, $attrValue, true); |
|
| 284 | } |
||
| 285 | |||
| 286 | 8 | return true; |
|
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Check if the attribute (key / value) is default and can be skipped. |
||
| 291 | * |
||
| 292 | * @param string $tag |
||
| 293 | * @param string $attrName |
||
| 294 | * @param string $attrValue |
||
| 295 | * @param string $allAttr |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 8 | private function optimizeAttributesFilters($tag, $attrName, $attrValue, $allAttr) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param $attrName |
||
| 356 | * @param $attrValue |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | 8 | private function sortCssClasses($attrName, $attrValue) |
|
| 383 | } |
||
| 384 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.