| Total Complexity | 48 |
| Total Lines | 221 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like ElementConstructorTrait 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.
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 ElementConstructorTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | trait ElementConstructorTrait |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * |
||
| 15 | */ |
||
| 16 | protected static function parseAttributes(array $attributes): string |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * |
||
| 23 | */ |
||
| 24 | private static function parseAttributesReal(array $attrArr, string &$attrStr = ''): string |
||
| 25 | { |
||
| 26 | foreach ($attrArr as $attr => $val) { |
||
| 27 | |||
| 28 | // don't add empty strings or null values |
||
| 29 | if ('' === $val && 'value' !== $attr || null === $val) { |
||
|
|
|||
| 30 | continue; |
||
| 31 | } |
||
| 32 | |||
| 33 | // simple attribute |
||
| 34 | if (is_string($val) || is_numeric($val)) { |
||
| 35 | $attrStr .= static::renderAttribute($attr, $val); |
||
| 36 | continue; |
||
| 37 | } |
||
| 38 | |||
| 39 | // support interface for defining custom parsing schemes |
||
| 40 | if ($val instanceof HtmlAttributeInterface) { |
||
| 41 | $attrStr .= static::renderAttribute($attr, $val->parse()); |
||
| 42 | continue; |
||
| 43 | } |
||
| 44 | |||
| 45 | // boolean attribute |
||
| 46 | if ($val === true) { |
||
| 47 | $attrStr .= static::renderAttribute($attr, $attr); |
||
| 48 | continue; |
||
| 49 | } |
||
| 50 | |||
| 51 | // treat numerical keys as boolean values |
||
| 52 | if (is_int($attr)) { |
||
| 53 | $attrStr .= static::renderAttribute($val, $val); |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | |||
| 57 | // support for passing an array of boolean values |
||
| 58 | if ('@boolean' === $attr) { |
||
| 59 | foreach ((array) $val as $bool) { |
||
| 60 | $attrStr .= static::renderAttribute($bool, $bool); |
||
| 61 | } |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | |||
| 65 | // support for converting indexed array to DOMTokenList |
||
| 66 | if (is_array($val) && isset($val[0])) { |
||
| 67 | $val = implode(' ', array_filter($val)); |
||
| 68 | $attrStr .= static::renderAttribute($attr, $val); |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | // support for converting associative array to DOMStringMap |
||
| 73 | if (is_array($val)) { |
||
| 74 | foreach ($val as $set => $setval) { |
||
| 75 | static::parseAttributesReal(["{$attr}-{$set}" => $setval], $attrStr); |
||
| 76 | } |
||
| 77 | continue; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | return $attrStr; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | */ |
||
| 87 | private static function parseAttributesRealSwitch(array $attrArr, string &$attrStr = ''): string |
||
| 88 | { |
||
| 89 | foreach ($attrArr as $attr => $val) { |
||
| 90 | |||
| 91 | switch (true) { |
||
| 92 | // don't add empty strings or null values |
||
| 93 | case ('' === $val && 'value' !== $attr || null === $val): |
||
| 94 | break; |
||
| 95 | |||
| 96 | // simple attribute |
||
| 97 | case (is_string($val) || is_numeric($val)): |
||
| 98 | $attrStr .= static::renderAttribute($attr, $val); |
||
| 99 | break; |
||
| 100 | |||
| 101 | // support interface for defining custom parsing schemes |
||
| 102 | case ($val instanceof HtmlAttributeInterface): |
||
| 103 | $attrStr .= static::renderAttribute($attr, $val->parse()); |
||
| 104 | break; |
||
| 105 | |||
| 106 | // boolean attribute |
||
| 107 | case ($val === true): |
||
| 108 | $attrStr .= static::renderAttribute($attr, $attr); |
||
| 109 | break; |
||
| 110 | |||
| 111 | // treat numerical keys as boolean values |
||
| 112 | case (is_int($attr)): |
||
| 113 | $attrStr .= static::renderAttribute($val, $val); |
||
| 114 | break; |
||
| 115 | |||
| 116 | // support for passing an array of boolean values |
||
| 117 | case ('@bool' === $attr): |
||
| 118 | foreach ((array) $val as $bool) { |
||
| 119 | $attrStr .= static::renderAttribute($bool, $bool); |
||
| 120 | } |
||
| 121 | break; |
||
| 122 | |||
| 123 | // support for converting indexed array to DOMTokenList |
||
| 124 | case (is_array($val) && isset($val[0])): |
||
| 125 | $val = implode(' ', array_filter($val)); |
||
| 126 | $attrStr .= static::renderAttribute($attr, $val); |
||
| 127 | break; |
||
| 128 | |||
| 129 | // support for converting associative array to DOMStringMap |
||
| 130 | case (is_array($val)): |
||
| 131 | foreach ($val as $set => $setval) { |
||
| 132 | static::parseAttributesRealSwitch(["{$attr}-{$set}" => $setval], $attrStr); |
||
| 133 | } |
||
| 134 | break; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | return $attrStr; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * |
||
| 143 | */ |
||
| 144 | protected static function renderAttribute($attribute, $value): string |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * |
||
| 153 | */ |
||
| 154 | protected static function escapeAttribute($attribute): string |
||
| 155 | { |
||
| 156 | return htmlspecialchars($attribute); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * |
||
| 161 | */ |
||
| 162 | protected static function tag(string $tag, array $attributes = [], string $inner = ''): string |
||
| 163 | { |
||
| 164 | return static::open($tag, $attributes) . static::maybeClose($tag, $inner); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * |
||
| 169 | */ |
||
| 170 | protected static function open(string $tag, array $attributes = []): string |
||
| 171 | { |
||
| 172 | $attributes = static::maybeParseAttributes($attributes); |
||
| 173 | $slash = static::maybeAddSlash($tag); |
||
| 174 | |||
| 175 | return "<{$tag}{$attributes}{$slash}>"; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * |
||
| 180 | */ |
||
| 181 | protected static function close(string $tag): string |
||
| 182 | { |
||
| 183 | return "</{$tag}>"; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * |
||
| 188 | */ |
||
| 189 | protected static function maybeClose(string $tag, string $inner = ''): string |
||
| 190 | { |
||
| 191 | return static::tagIsVoid($tag) ? '' : $inner . static::close($tag); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * |
||
| 196 | */ |
||
| 197 | protected static function maybeParseAttributes(array $attributes): string |
||
| 198 | { |
||
| 199 | return empty($attributes) ? '' : static::parseAttributes($attributes); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * |
||
| 204 | */ |
||
| 205 | protected static function maybeAddSlash(string $tag): string |
||
| 206 | { |
||
| 207 | return static::tagIsVoid($tag) ? ' /' : ''; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * |
||
| 212 | */ |
||
| 213 | protected static function tagIsVoid(string $tag): bool |
||
| 214 | { |
||
| 215 | return TagSage::isIt('self_closing', $tag); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * |
||
| 220 | */ |
||
| 221 | protected static function indent(int $levels = 0): string |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * |
||
| 228 | */ |
||
| 229 | protected static function newLine(bool $newLine = false): string |
||
| 232 | } |
||
| 233 | } |
||
| 234 |