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 | 48 | * @param HtmlMinInterface $htmlMin |
|
36 | * |
||
37 | 48 | * @return void |
|
38 | */ |
||
39 | public function domElementBeforeMinification(SimpleHtmlDomInterface $element, HtmlMinInterface $htmlMin) |
||
42 | |||
43 | /** |
||
44 | * Receive dom elements after the minification. |
||
45 | 48 | * |
|
46 | * @param SimpleHtmlDomInterface $element |
||
47 | 48 | * @param HtmlMinInterface $htmlMin |
|
48 | 48 | * |
|
49 | 47 | * @return void |
|
50 | */ |
||
51 | public function domElementAfterMinification(SimpleHtmlDomInterface $element, HtmlMinInterface $htmlMin) |
||
115 | |||
116 | /** |
||
117 | 31 | * Check if the attribute can be removed. |
|
118 | * |
||
119 | * @param string $tag |
||
120 | 31 | * @param string $attrName |
|
121 | 1 | * @param string $attrValue |
|
122 | * @param array $allAttr |
||
123 | * @param HtmlMinInterface $htmlMin |
||
124 | * |
||
125 | 1 | * @return bool |
|
126 | */ |
||
127 | private function removeAttributeHelper($tag, $attrName, $attrValue, $allAttr, HtmlMinInterface $htmlMin): bool |
||
198 | 25 | ||
199 | /** |
||
200 | * @param string $attrValue |
||
201 | 18 | * @param string $attrName |
|
202 | 18 | * @param string $scheme |
|
203 | * @param array $attributes |
||
204 | 18 | * |
|
205 | * @return string |
||
206 | 18 | */ |
|
207 | 18 | private function removeHttpPrefixHelper( |
|
226 | |||
227 | /** |
||
228 | * @param string $attrName |
||
229 | * @param string $attrValue |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | private function sortCssClassNames($attrName, $attrValue): string |
||
255 | } |
||
256 |
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: