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 | 'br', |
||
| 38 | 'col', |
||
| 39 | 'command', |
||
| 40 | 'embed', |
||
| 41 | 'hr', |
||
| 42 | 'img', |
||
| 43 | 'input', |
||
| 44 | 'keygen', |
||
| 45 | 'link', |
||
| 46 | 'meta', |
||
| 47 | 'param', |
||
| 48 | 'source', |
||
| 49 | 'track', |
||
| 50 | 'wbr', |
||
| 51 | ); |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private static $booleanAttributes = array( |
||
| 58 | 'allowfullscreen' => '', |
||
| 59 | 'async' => '', |
||
| 60 | 'autofocus' => '', |
||
| 61 | 'autoplay' => '', |
||
| 62 | 'checked' => '', |
||
| 63 | 'compact' => '', |
||
| 64 | 'controls' => '', |
||
| 65 | 'declare' => '', |
||
| 66 | 'default' => '', |
||
| 67 | 'defaultchecked' => '', |
||
| 68 | 'defaultmuted' => '', |
||
| 69 | 'defaultselected' => '', |
||
| 70 | 'defer' => '', |
||
| 71 | 'disabled' => '', |
||
| 72 | 'enabled' => '', |
||
| 73 | 'formnovalidate' => '', |
||
| 74 | 'hidden' => '', |
||
| 75 | 'indeterminate' => '', |
||
| 76 | 'inert' => '', |
||
| 77 | 'ismap' => '', |
||
| 78 | 'itemscope' => '', |
||
| 79 | 'loop' => '', |
||
| 80 | 'multiple' => '', |
||
| 81 | 'muted' => '', |
||
| 82 | 'nohref' => '', |
||
| 83 | 'noresize' => '', |
||
| 84 | 'noshade' => '', |
||
| 85 | 'novalidate' => '', |
||
| 86 | 'nowrap' => '', |
||
| 87 | 'open' => '', |
||
| 88 | 'pauseonexit' => '', |
||
| 89 | 'readonly' => '', |
||
| 90 | 'required' => '', |
||
| 91 | 'reversed' => '', |
||
| 92 | 'scoped' => '', |
||
| 93 | 'seamless' => '', |
||
| 94 | 'selected' => '', |
||
| 95 | 'sortable' => '', |
||
| 96 | 'truespeed' => '', |
||
| 97 | 'typemustmatch' => '', |
||
| 98 | 'visible' => '', |
||
| 99 | ); |
||
| 100 | /** |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | private static $skipTagsForRemoveWhitespace = array( |
||
| 104 | 'style', |
||
| 105 | 'pre', |
||
| 106 | 'code', |
||
| 107 | 'script', |
||
| 108 | 'textarea', |
||
| 109 | ); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * An random md5-hash, generated via "random_bytes()". |
||
| 113 | * |
||
| 114 | * @var string |
||
| 115 | */ |
||
| 116 | private $randomHash; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private $protectedChildNodes; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | private $protectedChildNodesHelper; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | private $booleanAttributesHelper; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * HtmlMin constructor. |
||
| 135 | */ |
||
| 136 | 23 | public function __construct() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $html |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | 23 | public function minify($html) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Sort HTML-Attributes, so that gzip can do better work |
||
| 256 | * and remove some default attributes. |
||
| 257 | * |
||
| 258 | * @param SimpleHtmlDom $element |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | 20 | private function optimizeAttributes(SimpleHtmlDom $element) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Check if the attribute (key / value) is default and can be skipped. |
||
| 310 | * |
||
| 311 | * @param string $tag |
||
| 312 | * @param string $attrName |
||
| 313 | * @param string $attrValue |
||
| 314 | * @param string $allAttr |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | 9 | private function optimizeAttributesFilters($tag, $attrName, $attrValue, $allAttr) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Optimize HTML-tag attributes in the dom. |
||
| 375 | * |
||
| 376 | * @param HtmlDomParser $dom |
||
| 377 | * |
||
| 378 | * @return HtmlDomParser |
||
| 379 | */ |
||
| 380 | 20 | private function optimizeAttributesInDom(HtmlDomParser $dom) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Prevent changes of inline "styles" and "scripts". |
||
| 391 | * |
||
| 392 | * @param HtmlDomParser $dom |
||
| 393 | * |
||
| 394 | * @return HtmlDomParser |
||
| 395 | */ |
||
| 396 | 20 | private function protectTagsInDom(HtmlDomParser $dom) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Remove comments in the dom. |
||
| 430 | * |
||
| 431 | * @param HtmlDomParser $dom |
||
| 432 | * |
||
| 433 | * @return HtmlDomParser |
||
| 434 | */ |
||
| 435 | 11 | private function removeCommentsInDom(HtmlDomParser $dom) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Remove whitespace from dom-nodes. |
||
| 452 | * |
||
| 453 | * @param HtmlDomParser $dom |
||
| 454 | * |
||
| 455 | * @return HtmlDomParser |
||
| 456 | */ |
||
| 457 | 12 | private function removeWhitespaceInDom(HtmlDomParser $dom) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Callback function for preg_replace_callback use. |
||
| 486 | * |
||
| 487 | * @param array $matches PREG matches |
||
| 488 | * |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | private function restoreProtectedHtml($matches) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param $attrName |
||
| 507 | * @param $attrValue |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | 9 | private function sortCssClasses($attrName, $attrValue) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Trim tags in the dom. |
||
| 537 | * |
||
| 538 | * @param HtmlDomParser $dom |
||
| 539 | * |
||
| 540 | * @return HtmlDomParser |
||
| 541 | */ |
||
| 542 | 11 | private function trimTagsInDom(HtmlDomParser $dom) |
|
| 572 | } |
||
| 573 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: