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 |
||
14 | abstract class BaseElement implements Htmlable, HtmlElement |
||
15 | { |
||
16 | use Macroable { |
||
17 | __call as __macro_call; |
||
18 | } |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $tag; |
||
22 | |||
23 | /** @var \Spatie\Html\Attributes */ |
||
24 | protected $attributes; |
||
25 | |||
26 | /** @var \Illuminate\Support\Collection */ |
||
27 | protected $children; |
||
28 | |||
29 | public function __construct() |
||
38 | |||
39 | public static function create() |
||
43 | |||
44 | /** |
||
45 | * @param string $attribute |
||
46 | * @param string|null $value |
||
47 | * |
||
48 | * @return static |
||
49 | */ |
||
50 | public function attribute($attribute, $value = null) |
||
58 | |||
59 | /** |
||
60 | * @param iterable $attributes |
||
61 | * |
||
62 | * @return static |
||
63 | */ |
||
64 | public function attributes($attributes) |
||
72 | |||
73 | /** |
||
74 | * @param string $attribute |
||
75 | * |
||
76 | * @return static |
||
77 | */ |
||
78 | public function forgetAttribute($attribute) |
||
86 | |||
87 | /** |
||
88 | * @param string $attribute |
||
89 | * @param mixed $fallback |
||
90 | * |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public function getAttribute($attribute, $fallback = null) |
||
97 | |||
98 | /** |
||
99 | * @param string $attribute |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | public function hasAttribute($attribute) |
||
107 | |||
108 | /** |
||
109 | * @param iterable|string $class |
||
110 | * |
||
111 | * @return static |
||
112 | */ |
||
113 | public function class($class) |
||
117 | |||
118 | /** |
||
119 | * Alias for `class`. |
||
120 | * |
||
121 | * @param iterable|string $class |
||
122 | * |
||
123 | * @return static |
||
124 | */ |
||
125 | public function addClass($class) |
||
133 | |||
134 | /** |
||
135 | * @param string $id |
||
136 | * |
||
137 | * @return static |
||
138 | */ |
||
139 | public function id($id) |
||
143 | |||
144 | /** |
||
145 | * @param array|string|null $style |
||
146 | * |
||
147 | * @return static |
||
148 | */ |
||
149 | public function style($style) |
||
159 | |||
160 | /** |
||
161 | * @param string $name |
||
162 | * @param string $value |
||
163 | * |
||
164 | * @return static |
||
165 | */ |
||
166 | public function data($name, $value) |
||
170 | |||
171 | /** |
||
172 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
173 | * @param callable|null $mapper |
||
174 | * |
||
175 | * @return static |
||
176 | */ |
||
177 | View Code Duplication | public function addChildren($children, $mapper = null) |
|
191 | |||
192 | /** |
||
193 | * Alias for `addChildren`. |
||
194 | * |
||
195 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
196 | * @param callable|null $mapper |
||
197 | * |
||
198 | * @return static |
||
199 | */ |
||
200 | public function addChild($child, $mapper = null) |
||
204 | |||
205 | /** |
||
206 | * Alias for `addChildren`. |
||
207 | * |
||
208 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
209 | * @param callable|null $mapper |
||
210 | * |
||
211 | * @return static |
||
212 | */ |
||
213 | public function child($child, $mapper = null) |
||
217 | |||
218 | /** |
||
219 | * Alias for `addChildren`. |
||
220 | * |
||
221 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
222 | * @param callable|null $mapper |
||
223 | * |
||
224 | * @return static |
||
225 | */ |
||
226 | public function children($children, $mapper = null) |
||
230 | |||
231 | /** |
||
232 | * Replace all children with an array of elements. |
||
233 | * |
||
234 | * @param \Spatie\Html\HtmlElement[] $children |
||
235 | * @param callable|null $mapper |
||
236 | * |
||
237 | * @return static |
||
238 | */ |
||
239 | public function setChildren($children, $mapper = null) |
||
247 | |||
248 | /** |
||
249 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
250 | * @param callable|null $mapper |
||
251 | * |
||
252 | * @return static |
||
253 | */ |
||
254 | View Code Duplication | public function prependChildren($children, $mapper = null) |
|
264 | |||
265 | /** |
||
266 | * Alias for `prependChildren`. |
||
267 | * |
||
268 | * @param \Spatie\Html\HtmlElement|string|iterable|null $children |
||
269 | * @param callable|null $mapper |
||
270 | * |
||
271 | * @return static |
||
272 | */ |
||
273 | public function prependChild($children, $mapper = null) |
||
277 | |||
278 | /** |
||
279 | * @param string|null $text |
||
280 | * |
||
281 | * @return static |
||
282 | */ |
||
283 | public function text($text) |
||
287 | |||
288 | /** |
||
289 | * @param string|null $html |
||
290 | * |
||
291 | * @return static |
||
292 | */ |
||
293 | public function html($html) |
||
301 | |||
302 | /** |
||
303 | * Condintionally transform the element. Note that since elements are |
||
304 | * immutable, you'll need to return a new instance from the callback. |
||
305 | * |
||
306 | * @param bool $condition |
||
307 | * @param callable $callback |
||
308 | */ |
||
309 | public function if(bool $condition, callable $callback) |
||
317 | |||
318 | /** |
||
319 | * @return \Illuminate\Contracts\Support\Htmlable |
||
320 | */ |
||
321 | public function open() |
||
345 | |||
346 | /** |
||
347 | * @return \Illuminate\Contracts\Support\Htmlable |
||
348 | */ |
||
349 | public function close() |
||
357 | |||
358 | /** |
||
359 | * @return \Illuminate\Contracts\Support\Htmlable |
||
360 | */ |
||
361 | public function render() |
||
367 | |||
368 | public function isVoidElement(): bool |
||
376 | |||
377 | /** |
||
378 | * Dynamically handle calls to the class. |
||
379 | * Check for methods finishing by If or fallback to Macroable. |
||
380 | * |
||
381 | * @param string $method |
||
382 | * @param array $parameters |
||
383 | * @return mixed |
||
384 | * |
||
385 | * @throws BadMethodCallException |
||
386 | */ |
||
387 | public function __call($name, $arguments) |
||
404 | |||
405 | public function __clone() |
||
410 | |||
411 | public function __toString(): string |
||
415 | |||
416 | public function toHtml(): string |
||
420 | |||
421 | protected function parseChildren($children, $mapper = null): Collection |
||
437 | |||
438 | protected function guardAgainstInvalidChildren(Collection $children) |
||
446 | } |
||
447 |