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 |
||
| 23 | class HtmlMin |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private static $regExSpace = "/[[:space:]]{2,}|[\r\n]+/u"; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private static $optional_end_tags = [ |
||
| 34 | 'html', |
||
| 35 | 'head', |
||
| 36 | 'body', |
||
| 37 | ]; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * // https://mathiasbynens.be/demo/javascript-mime-type |
||
| 41 | * // https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private static $executableScriptsMimeTypes = [ |
||
| 46 | 'text/javascript' => '', |
||
| 47 | 'text/ecmascript' => '', |
||
| 48 | 'text/jscript' => '', |
||
| 49 | 'application/javascript' => '', |
||
| 50 | 'application/x-javascript' => '', |
||
| 51 | 'application/ecmascript' => '', |
||
| 52 | ]; |
||
| 53 | |||
| 54 | private static $selfClosingTags = [ |
||
| 55 | 'area', |
||
| 56 | 'base', |
||
| 57 | 'basefont', |
||
| 58 | 'br', |
||
| 59 | 'col', |
||
| 60 | 'command', |
||
| 61 | 'embed', |
||
| 62 | 'frame', |
||
| 63 | 'hr', |
||
| 64 | 'img', |
||
| 65 | 'input', |
||
| 66 | 'isindex', |
||
| 67 | 'keygen', |
||
| 68 | 'link', |
||
| 69 | 'meta', |
||
| 70 | 'param', |
||
| 71 | 'source', |
||
| 72 | 'track', |
||
| 73 | 'wbr', |
||
| 74 | ]; |
||
| 75 | |||
| 76 | private static $trimWhitespaceFromTags = [ |
||
| 77 | 'article' => '', |
||
| 78 | 'br' => '', |
||
| 79 | 'div' => '', |
||
| 80 | 'footer' => '', |
||
| 81 | 'hr' => '', |
||
| 82 | 'nav' => '', |
||
| 83 | 'p' => '', |
||
| 84 | 'script' => '', |
||
| 85 | ]; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private static $booleanAttributes = [ |
||
| 91 | 'allowfullscreen' => '', |
||
| 92 | 'async' => '', |
||
| 93 | 'autofocus' => '', |
||
| 94 | 'autoplay' => '', |
||
| 95 | 'checked' => '', |
||
| 96 | 'compact' => '', |
||
| 97 | 'controls' => '', |
||
| 98 | 'declare' => '', |
||
| 99 | 'default' => '', |
||
| 100 | 'defaultchecked' => '', |
||
| 101 | 'defaultmuted' => '', |
||
| 102 | 'defaultselected' => '', |
||
| 103 | 'defer' => '', |
||
| 104 | 'disabled' => '', |
||
| 105 | 'enabled' => '', |
||
| 106 | 'formnovalidate' => '', |
||
| 107 | 'hidden' => '', |
||
| 108 | 'indeterminate' => '', |
||
| 109 | 'inert' => '', |
||
| 110 | 'ismap' => '', |
||
| 111 | 'itemscope' => '', |
||
| 112 | 'loop' => '', |
||
| 113 | 'multiple' => '', |
||
| 114 | 'muted' => '', |
||
| 115 | 'nohref' => '', |
||
| 116 | 'noresize' => '', |
||
| 117 | 'noshade' => '', |
||
| 118 | 'novalidate' => '', |
||
| 119 | 'nowrap' => '', |
||
| 120 | 'open' => '', |
||
| 121 | 'pauseonexit' => '', |
||
| 122 | 'readonly' => '', |
||
| 123 | 'required' => '', |
||
| 124 | 'reversed' => '', |
||
| 125 | 'scoped' => '', |
||
| 126 | 'seamless' => '', |
||
| 127 | 'selected' => '', |
||
| 128 | 'sortable' => '', |
||
| 129 | 'truespeed' => '', |
||
| 130 | 'typemustmatch' => '', |
||
| 131 | 'visible' => '', |
||
| 132 | ]; |
||
| 133 | /** |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | private static $skipTagsForRemoveWhitespace = [ |
||
| 137 | 'code', |
||
| 138 | 'pre', |
||
| 139 | 'script', |
||
| 140 | 'style', |
||
| 141 | 'textarea', |
||
| 142 | ]; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | private $protectedChildNodes = []; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var string |
||
| 151 | */ |
||
| 152 | private $protectedChildNodesHelper = 'html-min--voku--saved-content'; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var bool |
||
| 156 | */ |
||
| 157 | private $doOptimizeViaHtmlDomParser = true; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var bool |
||
| 161 | */ |
||
| 162 | private $doOptimizeAttributes = true; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var bool |
||
| 166 | */ |
||
| 167 | private $doRemoveComments = true; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var bool |
||
| 171 | */ |
||
| 172 | private $doRemoveWhitespaceAroundTags = false; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var bool |
||
| 176 | */ |
||
| 177 | private $doRemoveOmittedQuotes = true; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var bool |
||
| 181 | */ |
||
| 182 | private $doRemoveOmittedHtmlTags = true; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var bool |
||
| 186 | */ |
||
| 187 | private $doRemoveHttpPrefixFromAttributes = false; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var array |
||
| 191 | */ |
||
| 192 | private $domainsToRemoveHttpPrefixFromAttributes = [ |
||
| 193 | 'google.com', |
||
| 194 | 'google.de', |
||
| 195 | ]; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @var bool |
||
| 199 | */ |
||
| 200 | private $doSortCssClassNames = true; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @var bool |
||
| 204 | */ |
||
| 205 | private $doSortHtmlAttributes = true; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @var bool |
||
| 209 | */ |
||
| 210 | private $doRemoveDeprecatedScriptCharsetAttribute = true; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @var bool |
||
| 214 | */ |
||
| 215 | private $doRemoveDefaultAttributes = false; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @var bool |
||
| 219 | */ |
||
| 220 | private $doRemoveDeprecatedAnchorName = true; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @var bool |
||
| 224 | */ |
||
| 225 | private $doRemoveDeprecatedTypeFromStylesheetLink = true; |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @var bool |
||
| 229 | */ |
||
| 230 | private $doRemoveDeprecatedTypeFromScriptTag = true; |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @var bool |
||
| 234 | */ |
||
| 235 | private $doRemoveValueFromEmptyInput = true; |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @var bool |
||
| 239 | */ |
||
| 240 | private $doRemoveEmptyAttributes = true; |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @var bool |
||
| 244 | */ |
||
| 245 | private $doSumUpWhitespace = true; |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @var bool |
||
| 249 | */ |
||
| 250 | private $doRemoveSpacesBetweenTags = false; |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @var bool |
||
| 254 | */ |
||
| 255 | private $keepBrokenHtml = false; |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @var |
||
| 259 | */ |
||
| 260 | private $withDocType; |
||
| 261 | |||
| 262 | /** |
||
| 263 | * HtmlMin constructor. |
||
| 264 | */ |
||
| 265 | 35 | public function __construct() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @param boolean $doOptimizeAttributes |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | 2 | public function doOptimizeAttributes(bool $doOptimizeAttributes = true): self |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @param boolean $doOptimizeViaHtmlDomParser |
||
| 283 | * |
||
| 284 | * @return $this |
||
| 285 | */ |
||
| 286 | 1 | public function doOptimizeViaHtmlDomParser(bool $doOptimizeViaHtmlDomParser = true): self |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @param boolean $doRemoveComments |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | 2 | public function doRemoveComments(bool $doRemoveComments = true): self |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param boolean $doRemoveDefaultAttributes |
||
| 307 | * |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | 2 | public function doRemoveDefaultAttributes(bool $doRemoveDefaultAttributes = true): self |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param boolean $doRemoveDeprecatedAnchorName |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | 2 | public function doRemoveDeprecatedAnchorName(bool $doRemoveDeprecatedAnchorName = true): self |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @param boolean $doRemoveDeprecatedScriptCharsetAttribute |
||
| 331 | * |
||
| 332 | * @return $this |
||
| 333 | */ |
||
| 334 | 2 | public function doRemoveDeprecatedScriptCharsetAttribute(bool $doRemoveDeprecatedScriptCharsetAttribute = true): self |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param boolean $doRemoveDeprecatedTypeFromScriptTag |
||
| 343 | * |
||
| 344 | * @return $this |
||
| 345 | */ |
||
| 346 | 2 | public function doRemoveDeprecatedTypeFromScriptTag(bool $doRemoveDeprecatedTypeFromScriptTag = true): self |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @param boolean $doRemoveDeprecatedTypeFromStylesheetLink |
||
| 355 | * |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | 2 | public function doRemoveDeprecatedTypeFromStylesheetLink(bool $doRemoveDeprecatedTypeFromStylesheetLink = true): self |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param boolean $doRemoveEmptyAttributes |
||
| 367 | * |
||
| 368 | * @return $this |
||
| 369 | */ |
||
| 370 | 2 | public function doRemoveEmptyAttributes(bool $doRemoveEmptyAttributes = true): self |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param boolean $doRemoveHttpPrefixFromAttributes |
||
| 379 | * |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | 4 | public function doRemoveHttpPrefixFromAttributes(bool $doRemoveHttpPrefixFromAttributes = true): self |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @param boolean $doRemoveSpacesBetweenTags |
||
| 391 | * |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | public function doRemoveSpacesBetweenTags(bool $doRemoveSpacesBetweenTags = true): self |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param boolean $doRemoveValueFromEmptyInput |
||
| 403 | * |
||
| 404 | * @return $this |
||
| 405 | */ |
||
| 406 | 2 | public function doRemoveValueFromEmptyInput(bool $doRemoveValueFromEmptyInput = true): self |
|
| 412 | |||
| 413 | /** |
||
| 414 | * @param boolean $doRemoveWhitespaceAroundTags |
||
| 415 | * |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | 4 | public function doRemoveWhitespaceAroundTags(bool $doRemoveWhitespaceAroundTags = true): self |
|
| 424 | |||
| 425 | /** |
||
| 426 | * @param bool $doRemoveOmittedQuotes |
||
| 427 | * |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | 1 | public function doRemoveOmittedQuotes(bool $doRemoveOmittedQuotes = true): self |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @param bool $doRemoveOmittedHtmlTags |
||
| 439 | * |
||
| 440 | * @return $this |
||
| 441 | */ |
||
| 442 | 1 | public function doRemoveOmittedHtmlTags(bool $doRemoveOmittedHtmlTags = true): self |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @param boolean $doSortCssClassNames |
||
| 451 | * |
||
| 452 | * @return $this |
||
| 453 | */ |
||
| 454 | 2 | public function doSortCssClassNames(bool $doSortCssClassNames = true): self |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @param boolean $doSortHtmlAttributes |
||
| 463 | * |
||
| 464 | * @return $this |
||
| 465 | */ |
||
| 466 | 2 | public function doSortHtmlAttributes(bool $doSortHtmlAttributes = true): self |
|
| 472 | |||
| 473 | /** |
||
| 474 | * @param boolean $doSumUpWhitespace |
||
| 475 | * |
||
| 476 | * @return $this |
||
| 477 | */ |
||
| 478 | 2 | public function doSumUpWhitespace(bool $doSumUpWhitespace = true): self |
|
| 484 | |||
| 485 | 31 | private function domNodeAttributesToString(\DOMNode $node): string |
|
| 519 | |||
| 520 | /** |
||
| 521 | * @param \DOMNode $node |
||
| 522 | * |
||
| 523 | * @return bool |
||
| 524 | */ |
||
| 525 | 30 | private function domNodeClosingTagOptional(\DOMNode $node): bool |
|
| 526 | { |
||
| 527 | 30 | $tag_name = $node->nodeName; |
|
| 528 | 30 | $nextSibling = $this->getNextSiblingOfTypeDOMElement($node); |
|
| 529 | |||
| 530 | // https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission |
||
| 531 | |||
| 532 | // Implemented: |
||
| 533 | // |
||
| 534 | // A <p> element's end tag may be omitted if the p element is immediately followed by an address, article, aside, blockquote, details, div, dl, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, main, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is an HTML element that is not an a, audio, del, ins, map, noscript, or video element, or an autonomous custom element. |
||
| 535 | // An <li> element's end tag may be omitted if the li element is immediately followed by another li element or if there is no more content in the parent element. |
||
| 536 | // A <td> element's end tag may be omitted if the td element is immediately followed by a td or th element, or if there is no more content in the parent element. |
||
| 537 | // An <option> element's end tag may be omitted if the option element is immediately followed by another option element, or if it is immediately followed by an optgroup element, or if there is no more content in the parent element. |
||
| 538 | // A <tr> element's end tag may be omitted if the tr element is immediately followed by another tr element, or if there is no more content in the parent element. |
||
| 539 | // A <th> element's end tag may be omitted if the th element is immediately followed by a td or th element, or if there is no more content in the parent element. |
||
| 540 | // A <dt> element's end tag may be omitted if the dt element is immediately followed by another dt element or a dd element. |
||
| 541 | // A <dd> element's end tag may be omitted if the dd element is immediately followed by another dd element or a dt element, or if there is no more content in the parent element. |
||
| 542 | // An <rp> element's end tag may be omitted if the rp element is immediately followed by an rt or rp element, or if there is no more content in the parent element. |
||
| 543 | |||
| 544 | // TODO: |
||
| 545 | // |
||
| 546 | // <html> may be omitted if first thing inside is not comment |
||
| 547 | // <head> may be omitted if first thing inside is an element |
||
| 548 | // <body> may be omitted if first thing inside is not space, comment, <meta>, <link>, <script>, <style> or <template> |
||
| 549 | // <colgroup> may be omitted if first thing inside is <col> |
||
| 550 | // <tbody> may be omitted if first thing inside is <tr> |
||
| 551 | // An <optgroup> element's end tag may be omitted if the optgroup element is immediately followed by another optgroup element, or if there is no more content in the parent element. |
||
| 552 | // A <colgroup> element's start tag may be omitted if the first thing inside the colgroup element is a col element, and if the element is not immediately preceded by another colgroup element whose end tag has been omitted. (It can't be omitted if the element is empty.) |
||
| 553 | // A <colgroup> element's end tag may be omitted if the colgroup element is not immediately followed by ASCII whitespace or a comment. |
||
| 554 | // A <caption> element's end tag may be omitted if the caption element is not immediately followed by ASCII whitespace or a comment. |
||
| 555 | // A <thead> element's end tag may be omitted if the thead element is immediately followed by a tbody or tfoot element. |
||
| 556 | // A <tbody> element's start tag may be omitted if the first thing inside the tbody element is a tr element, and if the element is not immediately preceded by a tbody, thead, or tfoot element whose end tag has been omitted. (It can't be omitted if the element is empty.) |
||
| 557 | // A <tbody> element's end tag may be omitted if the tbody element is immediately followed by a tbody or tfoot element, or if there is no more content in the parent element. |
||
| 558 | // A <tfoot> element's end tag may be omitted if there is no more content in the parent element. |
||
| 559 | // |
||
| 560 | // <-- However, a start tag must never be omitted if it has any attributes. |
||
| 561 | |||
| 562 | 30 | return \in_array($tag_name, self::$optional_end_tags, true) |
|
| 563 | || |
||
| 564 | ( |
||
| 565 | 28 | $tag_name == 'li' |
|
| 566 | && |
||
| 567 | ( |
||
| 568 | 5 | $nextSibling === null |
|
| 569 | || |
||
| 570 | ( |
||
| 571 | 3 | $nextSibling instanceof \DOMElement |
|
| 572 | && |
||
| 573 | 28 | $nextSibling->tagName == 'li' |
|
| 574 | ) |
||
| 575 | ) |
||
| 576 | ) |
||
| 577 | || |
||
| 578 | ( |
||
| 579 | ( |
||
| 580 | 28 | $tag_name == 'rp' |
|
| 581 | ) |
||
| 582 | && |
||
| 583 | ( |
||
| 584 | $nextSibling === null |
||
| 585 | || |
||
| 586 | ( |
||
| 587 | $nextSibling instanceof \DOMElement |
||
| 588 | && |
||
| 589 | ( |
||
| 590 | $nextSibling->tagName == 'rp' |
||
| 591 | || |
||
| 592 | 28 | $nextSibling->tagName == 'rt' |
|
| 593 | ) |
||
| 594 | ) |
||
| 595 | ) |
||
| 596 | ) |
||
| 597 | || |
||
| 598 | ( |
||
| 599 | 28 | $tag_name == 'tr' |
|
| 600 | && |
||
| 601 | ( |
||
| 602 | 1 | $nextSibling === null |
|
| 603 | || |
||
| 604 | ( |
||
| 605 | 1 | $nextSibling instanceof \DOMElement |
|
| 606 | && |
||
| 607 | 28 | $nextSibling->tagName == 'tr' |
|
| 608 | ) |
||
| 609 | ) |
||
| 610 | ) |
||
| 611 | || |
||
| 612 | ( |
||
| 613 | ( |
||
| 614 | 28 | $tag_name == 'td' |
|
| 615 | || |
||
| 616 | 28 | $tag_name == 'th' |
|
| 617 | ) |
||
| 618 | && |
||
| 619 | ( |
||
| 620 | 1 | $nextSibling === null |
|
| 621 | || |
||
| 622 | ( |
||
| 623 | 1 | $nextSibling instanceof \DOMElement |
|
| 624 | && |
||
| 625 | ( |
||
| 626 | 1 | $nextSibling->tagName == 'td' |
|
| 627 | || |
||
| 628 | 28 | $nextSibling->tagName == 'th' |
|
| 629 | ) |
||
| 630 | ) |
||
| 631 | ) |
||
| 632 | ) |
||
| 633 | || |
||
| 634 | ( |
||
| 635 | ( |
||
| 636 | 28 | $tag_name == 'dd' |
|
| 637 | || |
||
| 638 | 28 | $tag_name == 'dt' |
|
| 639 | ) |
||
| 640 | && |
||
| 641 | ( |
||
| 642 | ( |
||
| 643 | 3 | $nextSibling === null |
|
| 644 | && |
||
| 645 | 3 | $tag_name == 'dd' |
|
| 646 | ) |
||
| 647 | || |
||
| 648 | ( |
||
| 649 | 3 | $nextSibling instanceof \DOMElement |
|
| 650 | && |
||
| 651 | ( |
||
| 652 | 3 | $nextSibling->tagName == 'dd' |
|
| 653 | || |
||
| 654 | 28 | $nextSibling->tagName == 'dt' |
|
| 655 | ) |
||
| 656 | ) |
||
| 657 | ) |
||
| 658 | ) |
||
| 659 | || |
||
| 660 | ( |
||
| 661 | 28 | $tag_name == 'option' |
|
| 662 | && |
||
| 663 | ( |
||
| 664 | $nextSibling === null |
||
| 665 | || |
||
| 666 | ( |
||
| 667 | $nextSibling instanceof \DOMElement |
||
| 668 | && |
||
| 669 | ( |
||
| 670 | $nextSibling->tagName == 'option' |
||
| 671 | || |
||
| 672 | 28 | $nextSibling->tagName == 'optgroup' |
|
| 673 | ) |
||
| 674 | ) |
||
| 675 | ) |
||
| 676 | ) |
||
| 677 | || |
||
| 678 | ( |
||
| 679 | 28 | $tag_name == 'p' |
|
| 680 | && |
||
| 681 | ( |
||
| 682 | ( |
||
| 683 | 9 | $nextSibling === null |
|
| 684 | && |
||
| 685 | ( |
||
| 686 | 8 | $node->parentNode !== null |
|
| 687 | && |
||
| 688 | !\in_array( |
||
| 689 | 8 | $node->parentNode->nodeName, |
|
| 690 | [ |
||
| 691 | 'a', |
||
| 692 | 'audio', |
||
| 693 | 'del', |
||
| 694 | 'ins', |
||
| 695 | 'map', |
||
| 696 | 'noscript', |
||
| 697 | 'video', |
||
| 698 | ], |
||
| 699 | true |
||
| 700 | ) |
||
| 701 | ) |
||
| 702 | ) |
||
| 703 | || |
||
| 704 | ( |
||
| 705 | 6 | $nextSibling instanceof \DOMElement |
|
| 706 | && |
||
| 707 | \in_array( |
||
| 708 | 30 | $nextSibling->tagName, |
|
| 709 | [ |
||
| 710 | 'address', |
||
| 711 | 'article', |
||
| 712 | 'aside', |
||
| 713 | 'blockquote', |
||
| 714 | 'dir', |
||
| 715 | 'div', |
||
| 716 | 'dl', |
||
| 717 | 'fieldset', |
||
| 718 | 'footer', |
||
| 719 | 'form', |
||
| 720 | 'h1', |
||
| 721 | 'h2', |
||
| 722 | 'h3', |
||
| 723 | 'h4', |
||
| 724 | 'h5', |
||
| 725 | 'h6', |
||
| 726 | 'header', |
||
| 727 | 'hgroup', |
||
| 728 | 'hr', |
||
| 729 | 'menu', |
||
| 730 | 'nav', |
||
| 731 | 'ol', |
||
| 732 | 'p', |
||
| 733 | 'pre', |
||
| 734 | 'section', |
||
| 735 | 'table', |
||
| 736 | 'ul', |
||
| 737 | ], |
||
| 738 | true |
||
| 739 | ) |
||
| 740 | ) |
||
| 741 | ) |
||
| 742 | ); |
||
| 743 | } |
||
| 744 | |||
| 745 | 31 | protected function domNodeToString(\DOMNode $node): string |
|
| 845 | |||
| 846 | /** |
||
| 847 | * @param \DOMNode $node |
||
| 848 | * |
||
| 849 | * @return \DOMNode|null |
||
| 850 | */ |
||
| 851 | 30 | protected function getNextSiblingOfTypeDOMElement(\DOMNode $node) |
|
| 859 | |||
| 860 | /** |
||
| 861 | * Check if the current string is an conditional comment. |
||
| 862 | * |
||
| 863 | * INFO: since IE >= 10 conditional comment are not working anymore |
||
| 864 | * |
||
| 865 | * <!--[if expression]> HTML <![endif]--> |
||
| 866 | * <![if expression]> HTML <![endif]> |
||
| 867 | * |
||
| 868 | * @param string $comment |
||
| 869 | * |
||
| 870 | * @return bool |
||
| 871 | */ |
||
| 872 | 3 | private function isConditionalComment($comment): bool |
|
| 884 | |||
| 885 | /** |
||
| 886 | * @param string $html |
||
| 887 | * @param bool $decodeUtf8Specials <p>Use this only in special cases, e.g. for PHP 5.3</p> |
||
| 888 | * |
||
| 889 | * @return string |
||
| 890 | */ |
||
| 891 | 35 | public function minify($html, $decodeUtf8Specials = false): string |
|
| 1015 | |||
| 1016 | /** |
||
| 1017 | * @param $html |
||
| 1018 | * @param $decodeUtf8Specials |
||
| 1019 | * |
||
| 1020 | * @return string |
||
| 1021 | */ |
||
| 1022 | 31 | private function minifyHtmlDom($html, $decodeUtf8Specials): string |
|
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Sort HTML-Attributes, so that gzip can do better work and remove some default attributes... |
||
| 1091 | * |
||
| 1092 | * @param SimpleHtmlDom $element |
||
| 1093 | * |
||
| 1094 | * @return bool |
||
| 1095 | */ |
||
| 1096 | 30 | private function optimizeAttributes(SimpleHtmlDom $element): bool |
|
| 1155 | |||
| 1156 | /** |
||
| 1157 | * Prevent changes of inline "styles" and "scripts". |
||
| 1158 | * |
||
| 1159 | * @param HtmlDomParser $dom |
||
| 1160 | * |
||
| 1161 | * @return HtmlDomParser |
||
| 1162 | */ |
||
| 1163 | 31 | private function protectTags(HtmlDomParser $dom): HtmlDomParser |
|
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Check if the attribute can be removed. |
||
| 1211 | * |
||
| 1212 | * @param string $tag |
||
| 1213 | * @param string $attrName |
||
| 1214 | * @param string $attrValue |
||
| 1215 | * @param array $allAttr |
||
| 1216 | * |
||
| 1217 | * @return bool |
||
| 1218 | */ |
||
| 1219 | 19 | private function removeAttributeHelper($tag, $attrName, $attrValue, $allAttr): bool |
|
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Remove comments in the dom. |
||
| 1288 | * |
||
| 1289 | * @param HtmlDomParser $dom |
||
| 1290 | * |
||
| 1291 | * @return HtmlDomParser |
||
| 1292 | */ |
||
| 1293 | 30 | private function removeComments(HtmlDomParser $dom): HtmlDomParser |
|
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Trim tags in the dom. |
||
| 1310 | * |
||
| 1311 | * @param SimpleHtmlDom $element |
||
| 1312 | * |
||
| 1313 | * @return void |
||
| 1314 | */ |
||
| 1315 | 3 | private function removeWhitespaceAroundTags(SimpleHtmlDom $element) |
|
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Callback function for preg_replace_callback use. |
||
| 1342 | * |
||
| 1343 | * @param array $matches PREG matches |
||
| 1344 | * |
||
| 1345 | * @return string |
||
| 1346 | */ |
||
| 1347 | 2 | private function restoreProtectedHtml($matches): string |
|
| 1358 | |||
| 1359 | /** |
||
| 1360 | * WARNING: maybe bad for performance ... |
||
| 1361 | * |
||
| 1362 | * @param bool $keepBrokenHtml |
||
| 1363 | * |
||
| 1364 | * @return HtmlMin |
||
| 1365 | */ |
||
| 1366 | 1 | public function useKeepBrokenHtml(bool $keepBrokenHtml): self |
|
| 1372 | |||
| 1373 | /** |
||
| 1374 | * @param array $domainsToRemoveHttpPrefixFromAttributes |
||
| 1375 | * |
||
| 1376 | * @return $this |
||
| 1377 | */ |
||
| 1378 | 2 | public function setDomainsToRemoveHttpPrefixFromAttributes($domainsToRemoveHttpPrefixFromAttributes): self |
|
| 1384 | |||
| 1385 | /** |
||
| 1386 | * @param $attrName |
||
| 1387 | * @param $attrValue |
||
| 1388 | * |
||
| 1389 | * @return string |
||
| 1390 | */ |
||
| 1391 | 19 | private function sortCssClassNames($attrName, $attrValue): string |
|
| 1415 | |||
| 1416 | /** |
||
| 1417 | * Sum-up extra whitespace from dom-nodes. |
||
| 1418 | * |
||
| 1419 | * @param HtmlDomParser $dom |
||
| 1420 | * |
||
| 1421 | * @return HtmlDomParser |
||
| 1422 | */ |
||
| 1423 | 30 | private function sumUpWhitespace(HtmlDomParser $dom): HtmlDomParser |
|
| 1449 | } |
||
| 1450 |
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.