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 Html 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 Html, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Html |
||
28 | { |
||
29 | use Macroable; |
||
30 | |||
31 | /** @var \Illuminate\Http\Request */ |
||
32 | protected $request; |
||
33 | |||
34 | /** @var \ArrayAccess|array */ |
||
35 | protected $model; |
||
36 | |||
37 | public function __construct(Request $request) |
||
41 | |||
42 | /** |
||
43 | * @param string|null $href |
||
44 | * @param string|null $text |
||
45 | * |
||
46 | * @return \Spatie\Html\Elements\A |
||
47 | */ |
||
48 | public function a($href = null, $contents = null) |
||
54 | |||
55 | /** |
||
56 | * @param string|null $href |
||
57 | * @param string|null $text |
||
58 | * |
||
59 | * @return \Spatie\Html\Elements\I |
||
60 | */ |
||
61 | public function i($contents = null) |
||
66 | |||
67 | /** |
||
68 | * @param string|null $type |
||
69 | * @param string|null $text |
||
70 | * |
||
71 | * @return \Spatie\Html\Elements\Button |
||
72 | */ |
||
73 | public function button($contents = null, $type = null) |
||
79 | |||
80 | /** |
||
81 | * @param \Illuminate\Support\Collection|iterable|string $classes |
||
82 | * |
||
83 | * @return \Illuminate\Contracts\Support\Htmlable |
||
84 | */ |
||
85 | public function class($classes): Htmlable |
||
98 | |||
99 | /** |
||
100 | * @param string|null $name |
||
101 | * @param bool $checked |
||
102 | * @param string|null $value |
||
103 | * |
||
104 | * @return \Spatie\Html\Elements\Input |
||
105 | */ |
||
106 | public function checkbox($name = null, $checked = false, $value = '1') |
||
115 | |||
116 | /** |
||
117 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
118 | * |
||
119 | * @return \Spatie\Html\Elements\Div |
||
120 | */ |
||
121 | public function div($contents = null) |
||
125 | |||
126 | /** |
||
127 | * @param string|null $name |
||
128 | * @param string|null $value |
||
129 | * |
||
130 | * @return \Spatie\Html\Elements\Input |
||
131 | */ |
||
132 | public function email($name = '', $value = '') |
||
136 | |||
137 | /** |
||
138 | * @param string|null $name |
||
139 | * @param string|null $value |
||
140 | * |
||
141 | * @return \Spatie\Html\Elements\Input |
||
142 | */ |
||
143 | public function date($name = '', $value = '') |
||
147 | |||
148 | /** |
||
149 | * @param string|null $name |
||
150 | * @param string|null $value |
||
151 | * |
||
152 | * @return \Spatie\Html\Elements\Input |
||
153 | */ |
||
154 | public function time($name = '', $value = '') |
||
158 | |||
159 | /** |
||
160 | * @param string $tag |
||
161 | * |
||
162 | * @return \Spatie\Html\Elements\Element |
||
163 | */ |
||
164 | public function element($tag) |
||
168 | |||
169 | /** |
||
170 | * @param string|null $type |
||
171 | * @param string|null $name |
||
172 | * @param string|null $value |
||
173 | * |
||
174 | * @return \Spatie\Html\Elements\Input |
||
175 | */ |
||
176 | public function input($type = null, $name = null, $value = null) |
||
186 | |||
187 | /** |
||
188 | * @param \Spatie\Html\HtmlElement|string|null $legend |
||
189 | * |
||
190 | * @return \Spatie\Html\Elements\Fieldset |
||
191 | */ |
||
192 | public function fieldset($legend = null) |
||
198 | |||
199 | /** |
||
200 | * @param string $method |
||
201 | * @param string|null $action |
||
202 | * |
||
203 | * @return \Spatie\Html\Elements\Form |
||
204 | */ |
||
205 | public function form($method = 'POST', $action = null) |
||
225 | |||
226 | /** |
||
227 | * @param string|null $name |
||
228 | * @param string|null $value |
||
229 | * |
||
230 | * @return \Spatie\Html\Elements\Input |
||
231 | */ |
||
232 | public function hidden($name = null, $value = null) |
||
236 | |||
237 | /** |
||
238 | * @param string|null $src |
||
239 | * @param string|null $alt |
||
240 | * |
||
241 | * @return \Spatie\Html\Elements\Img |
||
242 | */ |
||
243 | public function img($src = null, $alt = null) |
||
249 | |||
250 | /** |
||
251 | * @param \Spatie\Html\HtmlElement|iterable|string|null $contents |
||
252 | * @param string|null $for |
||
253 | * |
||
254 | * @return \Spatie\Html\Elements\Label |
||
255 | */ |
||
256 | public function label($contents = null, $for = null) |
||
262 | |||
263 | /** |
||
264 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
265 | * |
||
266 | * @return \Spatie\Html\Elements\Legend |
||
267 | */ |
||
268 | public function legend($contents = null) |
||
272 | |||
273 | /** |
||
274 | * @param string $email |
||
275 | * @param string|null $text |
||
276 | * |
||
277 | * @return \Spatie\Html\Elements\A |
||
278 | */ |
||
279 | public function mailto($email, $text = null) |
||
283 | |||
284 | /** |
||
285 | * @param string|null $name |
||
286 | * @param iterable $options |
||
287 | * @param string|iterable|null $value |
||
288 | * |
||
289 | * @return \Spatie\Html\Elements\Select |
||
290 | */ |
||
291 | View Code Duplication | public function multiselect($name = null, $options = [], $value = null) |
|
300 | |||
301 | /** |
||
302 | * @param string|null $text |
||
303 | * @param string|null $value |
||
304 | * @param bool $selected |
||
305 | * |
||
306 | * @return \Spatie\Html\Elements\Option |
||
307 | */ |
||
308 | public function option($text = null, $value = null, $selected = false) |
||
315 | |||
316 | /** |
||
317 | * @param string|null $value |
||
318 | * |
||
319 | * @return \Spatie\Html\Elements\Input |
||
320 | */ |
||
321 | public function password($name = null) |
||
325 | |||
326 | /** |
||
327 | * @param string|null $name |
||
328 | * @param bool $checked |
||
329 | * @param string|null $value |
||
330 | * |
||
331 | * @return \Spatie\Html\Elements\Input |
||
332 | */ |
||
333 | public function radio($name = null, $checked = false, $value = null) |
||
338 | |||
339 | /** |
||
340 | * @param string|null $name |
||
341 | * @param iterable $options |
||
342 | * @param string|iterable|null $value |
||
343 | * |
||
344 | * @return \Spatie\Html\Elements\Select |
||
345 | */ |
||
346 | View Code Duplication | public function select($name = null, $options = [], $value = null) |
|
354 | |||
355 | /** |
||
356 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
357 | * |
||
358 | * @return \Spatie\Html\Elements\Span |
||
359 | */ |
||
360 | public function span($contents = null) |
||
364 | |||
365 | /** |
||
366 | * @param string|null $text |
||
367 | * |
||
368 | * @return \Spatie\Html\Elements\Button |
||
369 | */ |
||
370 | public function submit($text = null) |
||
374 | |||
375 | /** |
||
376 | * @param string|null $text |
||
377 | * |
||
378 | * @return \Spatie\Html\Elements\Button |
||
379 | */ |
||
380 | public function reset($text = null) |
||
384 | |||
385 | /** |
||
386 | * @param string $number |
||
387 | * @param string|null $text |
||
388 | * |
||
389 | * @return \Spatie\Html\Elements\A |
||
390 | */ |
||
391 | public function tel($number, $text = null) |
||
395 | |||
396 | /** |
||
397 | * @param string|null $name |
||
398 | * @param string|null $value |
||
399 | * |
||
400 | * @return \Spatie\Html\Elements\Input |
||
401 | */ |
||
402 | public function text($name = null, $value = null) |
||
406 | |||
407 | /** |
||
408 | * @param string|null $name |
||
409 | * |
||
410 | * @return \Spatie\Html\Elements\File |
||
411 | */ |
||
412 | public function file($name = null) |
||
418 | |||
419 | /** |
||
420 | * @param string|null $name |
||
421 | * @param string|null $value |
||
422 | * |
||
423 | * @return \Spatie\Html\Elements\Textarea |
||
424 | */ |
||
425 | public function textarea($name = null, $value = null) |
||
432 | |||
433 | /** |
||
434 | * @return \Spatie\Html\Elements\Input |
||
435 | */ |
||
436 | public function token() |
||
443 | |||
444 | /** |
||
445 | * @param \ArrayAccess|array $model |
||
446 | * |
||
447 | * @return $this |
||
448 | */ |
||
449 | public function model($model) |
||
455 | |||
456 | /** |
||
457 | * @param \ArrayAccess|array $model |
||
458 | * @param string|null $method |
||
459 | * @param string|null $action |
||
460 | * |
||
461 | * @return \Spatie\Html\Elements\Form |
||
462 | */ |
||
463 | public function modelForm($model, $method = 'POST', $action = null): Form |
||
469 | |||
470 | /** |
||
471 | * @return $this |
||
472 | */ |
||
473 | public function endModel() |
||
479 | |||
480 | /** |
||
481 | * @return \Illuminate\Contracts\Support\Htmlable |
||
482 | */ |
||
483 | public function closeModelForm(): Htmlable |
||
489 | |||
490 | /** |
||
491 | * @param string $name |
||
492 | * @param mixed $value |
||
493 | * |
||
494 | * @return mixed |
||
495 | */ |
||
496 | protected function old($name, $value = null) |
||
513 | |||
514 | /** |
||
515 | * Retrieve the value from the current session or assigned model. This is |
||
516 | * a public alias for `old`. |
||
517 | * |
||
518 | * @param string $name |
||
519 | * @param mixed $value |
||
520 | * |
||
521 | * @return mixed |
||
522 | */ |
||
523 | public function value($name, $default = null) |
||
527 | |||
528 | /** |
||
529 | * @param string $name |
||
530 | * |
||
531 | * @return string |
||
532 | */ |
||
533 | protected function fieldName($name) |
||
537 | |||
538 | protected function ensureModelIsAvailable() |
||
544 | } |
||
545 |