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 HtmlMinDomObserverOptimizeAttributes 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 HtmlMinDomObserverOptimizeAttributes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | final class HtmlMinDomObserverOptimizeAttributes implements HtmlMinDomObserverInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * // https://mathiasbynens.be/demo/javascript-mime-type |
||
| 16 | * // https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type |
||
| 17 | * |
||
| 18 | * @var string[] |
||
| 19 | * |
||
| 20 | * @psalm-var array<string, string> |
||
| 21 | */ |
||
| 22 | private static $executableScriptsMimeTypes = [ |
||
| 23 | 'text/javascript' => '', |
||
| 24 | 'text/ecmascript' => '', |
||
| 25 | 'text/jscript' => '', |
||
| 26 | 'application/javascript' => '', |
||
| 27 | 'application/x-javascript' => '', |
||
| 28 | 'application/ecmascript' => '', |
||
| 29 | ]; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Receive dom elements before the minification. |
||
| 33 | * |
||
| 34 | * @param SimpleHtmlDomInterface $element |
||
| 35 | * @param HtmlMinInterface $htmlMin |
||
| 36 | * |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | 50 | public function domElementBeforeMinification(SimpleHtmlDomInterface $element, HtmlMinInterface $htmlMin) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Receive dom elements after the minification. |
||
| 45 | * |
||
| 46 | * @param SimpleHtmlDomInterface $element |
||
| 47 | * @param HtmlMinInterface $htmlMin |
||
| 48 | * |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | 50 | public function domElementAfterMinification(SimpleHtmlDomInterface $element, HtmlMinInterface $htmlMin) |
|
| 52 | { |
||
| 53 | 50 | $tag_name = $element->getNode()->nodeName; |
|
| 54 | 50 | $attributes = $element->getAllAttributes(); |
|
| 55 | 50 | if ($attributes === null) { |
|
| 56 | 49 | return; |
|
| 57 | } |
||
| 58 | |||
| 59 | 33 | $attrs = []; |
|
| 60 | 33 | foreach ((array) $attributes as $attrName => $attrValue) { |
|
| 61 | |||
| 62 | // ------------------------------------------------------------------------- |
||
| 63 | // Remove optional "http:"-prefix from attributes. |
||
| 64 | // ------------------------------------------------------------------------- |
||
| 65 | |||
| 66 | 33 | if ($htmlMin->isDoRemoveHttpPrefixFromAttributes()) { |
|
| 67 | 5 | $attrValue = $this->removeHttpPrefixHelper( |
|
| 68 | 5 | $attrValue, |
|
| 69 | 5 | $attrName, |
|
| 70 | 5 | 'http', |
|
| 71 | 5 | $attributes, |
|
| 72 | 5 | $tag_name, |
|
| 73 | 5 | $htmlMin |
|
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | 33 | if ($htmlMin->isDoRemoveHttpsPrefixFromAttributes()) { |
|
| 78 | 1 | $attrValue = $this->removeHttpPrefixHelper( |
|
| 79 | 1 | $attrValue, |
|
| 80 | 1 | $attrName, |
|
| 81 | 1 | 'https', |
|
| 82 | 1 | $attributes, |
|
| 83 | 1 | $tag_name, |
|
| 84 | 1 | $htmlMin |
|
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | 33 | if ($htmlMin->isDoMakeSameDomainLinksRelative()) { |
|
| 89 | |||
| 90 | 1 | $localDomain = $htmlMin->getLocalDomain(); |
|
| 91 | /** @noinspection InArrayCanBeUsedInspection */ |
||
| 92 | if ( |
||
| 93 | ( |
||
| 94 | 1 | $attrName === 'href' |
|
| 95 | || |
||
| 96 | 1 | $attrName === 'src' |
|
| 97 | || |
||
| 98 | 1 | $attrName === 'srcset' |
|
| 99 | || |
||
| 100 | 1 | $attrName === 'action' |
|
| 101 | ) |
||
| 102 | && |
||
| 103 | 1 | !(isset($attributes['rel']) && $attributes['rel'] === 'external') |
|
| 104 | && |
||
| 105 | 1 | !(isset($attributes['target']) && $attributes['target'] === '_blank') |
|
| 106 | && |
||
| 107 | 1 | \stripos($attrValue, $localDomain) !== false |
|
| 108 | ) { |
||
| 109 | 1 | $localDomainEscaped = \preg_quote($localDomain, '/'); |
|
| 110 | |||
| 111 | 1 | $attrValue = (string) \preg_replace("/^(?:(?:https?:)?\/\/)?{$localDomainEscaped}(?!\w)(?:\/?)/i", '/', $attrValue); |
|
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | 33 | if ($this->removeAttributeHelper( |
|
| 116 | 33 | $element->tag, |
|
|
|
|||
| 117 | 33 | $attrName, |
|
| 118 | 33 | $attrValue, |
|
| 119 | 33 | $attributes, |
|
| 120 | 33 | $htmlMin |
|
| 121 | )) { |
||
| 122 | 6 | $element->{$attrName} = null; |
|
| 123 | |||
| 124 | 6 | continue; |
|
| 125 | } |
||
| 126 | |||
| 127 | // ------------------------------------------------------------------------- |
||
| 128 | // Sort css-class-names, for better gzip results. |
||
| 129 | // ------------------------------------------------------------------------- |
||
| 130 | |||
| 131 | 33 | if ($htmlMin->isDoSortCssClassNames()) { |
|
| 132 | 32 | $attrValue = $this->sortCssClassNames($attrName, $attrValue); |
|
| 133 | } |
||
| 134 | |||
| 135 | 33 | if ($htmlMin->isDoSortHtmlAttributes()) { |
|
| 136 | 32 | $attrs[$attrName] = $attrValue; |
|
| 137 | 32 | $element->{$attrName} = null; |
|
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | // ------------------------------------------------------------------------- |
||
| 142 | // Sort html-attributes, for better gzip results. |
||
| 143 | // ------------------------------------------------------------------------- |
||
| 144 | |||
| 145 | 33 | if ($htmlMin->isDoSortHtmlAttributes()) { |
|
| 146 | 32 | \ksort($attrs); |
|
| 147 | 32 | foreach ($attrs as $attrName => $attrValue) { |
|
| 148 | 32 | $attrValue = HtmlDomParser::replaceToPreserveHtmlEntities($attrValue); |
|
| 149 | 32 | $element->setAttribute((string) $attrName, $attrValue, true); |
|
| 150 | } |
||
| 151 | } |
||
| 152 | 33 | } |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Check if the attribute can be removed. |
||
| 156 | * |
||
| 157 | * @param string $tag |
||
| 158 | * @param string $attrName |
||
| 159 | * @param string $attrValue |
||
| 160 | * @param array $allAttr |
||
| 161 | * @param HtmlMinInterface $htmlMin |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 33 | private function removeAttributeHelper($tag, $attrName, $attrValue, $allAttr, HtmlMinInterface $htmlMin): bool |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $attrValue |
||
| 239 | * @param string $attrName |
||
| 240 | * @param string $scheme |
||
| 241 | * @param array $attributes |
||
| 242 | * @param HtmlMinInterface $htmlMin |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | 5 | private function removeHttpPrefixHelper( |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $attrName |
||
| 286 | * @param string $attrValue |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | 32 | private function sortCssClassNames($attrName, $attrValue): string |
|
| 312 | } |
||
| 313 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: