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 BaseElement 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 BaseElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class BaseElement implements Htmlable, HtmlElement |
||
| 13 | { |
||
| 14 | /** @var string */ |
||
| 15 | protected $tag; |
||
| 16 | |||
| 17 | /** @var \Spatie\Html\Attributes */ |
||
| 18 | protected $attributes; |
||
| 19 | |||
| 20 | /** @var \Illuminate\Support\Collection */ |
||
| 21 | protected $children; |
||
| 22 | |||
| 23 | public function __construct() |
||
| 32 | |||
| 33 | public static function create() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param string $attribute |
||
| 40 | * @param string|null $value |
||
| 41 | * |
||
| 42 | * @return static |
||
| 43 | */ |
||
| 44 | public function attribute($attribute, $value = null) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param bool $condition |
||
| 55 | * @param string $attribute |
||
| 56 | * @param string|null $value |
||
| 57 | * |
||
| 58 | * @return static |
||
| 59 | */ |
||
| 60 | public function attributeIf($condition, $attribute, $value = null) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param iterable $attributes |
||
| 69 | * |
||
| 70 | * @return static |
||
| 71 | */ |
||
| 72 | public function attributes($attributes) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $attribute |
||
| 83 | * |
||
| 84 | * @return static |
||
| 85 | */ |
||
| 86 | public function forgetAttribute($attribute) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $attribute |
||
| 97 | * @param mixed $fallback |
||
| 98 | * |
||
| 99 | * @return mixed |
||
| 100 | */ |
||
| 101 | public function getAttribute($attribute, $fallback = null) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $attribute |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function hasAttribute($attribute) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param iterable|string $class |
||
| 118 | * |
||
| 119 | * @return static |
||
| 120 | */ |
||
| 121 | public function class($class) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Alias for `class`. |
||
| 128 | * |
||
| 129 | * @param iterable|string $class |
||
| 130 | * |
||
| 131 | * @return static |
||
| 132 | */ |
||
| 133 | public function addClass($class) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $id |
||
| 144 | * |
||
| 145 | * @return static |
||
| 146 | */ |
||
| 147 | public function id($id) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param array|string|null $style |
||
| 154 | * |
||
| 155 | * @return static |
||
| 156 | */ |
||
| 157 | public function style($style) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $name |
||
| 170 | * @param string $value |
||
| 171 | * |
||
| 172 | * @return static |
||
| 173 | */ |
||
| 174 | public function data($name, $value) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 181 | * @param callable|null $mapper |
||
| 182 | * |
||
| 183 | * @return static |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function addChildren($children, $mapper = null) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Alias for `addChildren`. |
||
| 202 | * |
||
| 203 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 204 | * @param callable|null $mapper |
||
| 205 | * |
||
| 206 | * @return static |
||
| 207 | */ |
||
| 208 | public function addChild($child, $mapper = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Alias for `addChildren`. |
||
| 215 | * |
||
| 216 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 217 | * @param callable|null $mapper |
||
| 218 | * |
||
| 219 | * @return static |
||
| 220 | */ |
||
| 221 | public function child($child, $mapper = null) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Alias for `addChildren`. |
||
| 228 | * |
||
| 229 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 230 | * @param callable|null $mapper |
||
| 231 | * |
||
| 232 | * @return static |
||
| 233 | */ |
||
| 234 | public function children($children, $mapper = null) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Replace all children with an array of elements. |
||
| 241 | * |
||
| 242 | * @param \Spatie\Html\HtmlElement[] $children |
||
| 243 | * @param callable|null $mapper |
||
| 244 | * |
||
| 245 | * @return static |
||
| 246 | */ |
||
| 247 | public function setChildren($children, $mapper = null) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 258 | * @param callable|null $mapper |
||
| 259 | * |
||
| 260 | * @return static |
||
| 261 | */ |
||
| 262 | View Code Duplication | public function prependChildren($children, $mapper = null) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Alias for `prependChildren`. |
||
| 275 | * |
||
| 276 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
| 277 | * @param callable|null $mapper |
||
| 278 | * |
||
| 279 | * @return static |
||
| 280 | */ |
||
| 281 | public function prependChild($children, $mapper = null) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string|null $text |
||
| 288 | * |
||
| 289 | * @return static |
||
| 290 | */ |
||
| 291 | public function text($text) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string|null $html |
||
| 298 | * |
||
| 299 | * @return static |
||
| 300 | */ |
||
| 301 | public function html($html) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Condintionally transform the element. Note that since elements are |
||
| 316 | * immutable, you'll need to return a new instance from the callback. |
||
| 317 | * |
||
| 318 | * @param bool $condition |
||
| 319 | * @param callable $callback |
||
| 320 | */ |
||
| 321 | public function if(bool $condition, callable $callback) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @return \Illuminate\Contracts\Support\Htmlable |
||
| 332 | */ |
||
| 333 | public function open() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return \Illuminate\Contracts\Support\Htmlable |
||
| 360 | */ |
||
| 361 | public function close() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return \Illuminate\Contracts\Support\Htmlable |
||
| 372 | */ |
||
| 373 | public function render() |
||
| 379 | |||
| 380 | public function isVoidElement(): bool |
||
| 388 | |||
| 389 | public function __clone() |
||
| 394 | |||
| 395 | public function __toString(): string |
||
| 399 | |||
| 400 | public function toHtml(): string |
||
| 404 | |||
| 405 | protected function parseChildren($children, $mapper = null): Collection |
||
| 423 | |||
| 424 | protected function guardAgainstInvalidChildren(Collection $children) |
||
| 432 | } |
||
| 433 |