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 |
||
29 | class Html |
||
30 | { |
||
31 | use Macroable; |
||
32 | |||
33 | const HTML_DATE_FORMAT = 'Y-m-d'; |
||
34 | const HTML_TIME_FORMAT = 'H:i:s'; |
||
35 | |||
36 | /** @var \Illuminate\Http\Request */ |
||
37 | protected $request; |
||
38 | |||
39 | /** @var \ArrayAccess|array */ |
||
40 | protected $model; |
||
41 | |||
42 | public function __construct(Request $request) |
||
|
|||
43 | { |
||
44 | $this->request = $request; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param string|null $href |
||
49 | * @param string|null $text |
||
50 | * |
||
51 | * @return \Spatie\Html\Elements\A |
||
52 | */ |
||
53 | public function a($href = null, $contents = null) |
||
54 | { |
||
55 | return A::create() |
||
56 | ->attributeIf($href, 'href', $href) |
||
57 | ->html($contents); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string|null $href |
||
62 | * @param string|null $text |
||
63 | * |
||
64 | * @return \Spatie\Html\Elements\I |
||
65 | */ |
||
66 | public function i($contents = null) |
||
67 | { |
||
68 | return I::create() |
||
69 | ->html($contents); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param string|null $type |
||
74 | * @param string|null $text |
||
75 | * |
||
76 | * @return \Spatie\Html\Elements\Button |
||
77 | */ |
||
78 | public function button($contents = null, $type = null, $name = null) |
||
79 | { |
||
80 | return Button::create() |
||
81 | ->attributeIf($type, 'type', $type) |
||
82 | ->attributeIf($name, 'name', $this->fieldName($name)) |
||
83 | ->html($contents); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param \Illuminate\Support\Collection|iterable|string $classes |
||
88 | * |
||
89 | * @return \Illuminate\Contracts\Support\Htmlable |
||
90 | */ |
||
91 | public function class($classes): Htmlable |
||
92 | { |
||
93 | if ($classes instanceof Collection) { |
||
94 | $classes = $classes->toArray(); |
||
95 | } |
||
96 | |||
97 | $attributes = new Attributes(); |
||
98 | $attributes->addClass($classes); |
||
99 | |||
100 | return new HtmlString( |
||
101 | $attributes->render() |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param string|null $name |
||
107 | * @param bool $checked |
||
108 | * @param string|null $value |
||
109 | * |
||
110 | * @return \Spatie\Html\Elements\Input |
||
111 | */ |
||
112 | public function checkbox($name = null, $checked = null, $value = '1') |
||
113 | { |
||
114 | return $this->input('checkbox', $name, $value) |
||
115 | ->attributeIf(!is_null($value), 'value', $value) |
||
116 | ->attributeIf((bool) $this->old($name, $checked), 'checked'); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
121 | * |
||
122 | * @return \Spatie\Html\Elements\Div |
||
123 | */ |
||
124 | public function div($contents = null) |
||
125 | { |
||
126 | return Div::create()->children($contents); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param string|null $name |
||
131 | * @param string|null $value |
||
132 | * |
||
133 | * @return \Spatie\Html\Elements\Input |
||
134 | */ |
||
135 | public function email($name = null, $value = null) |
||
136 | { |
||
137 | return $this->input('email', $name, $value); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string|null $name |
||
142 | * @param string|null $value |
||
143 | * @param bool $format |
||
144 | * |
||
145 | * @return \Spatie\Html\Elements\Input |
||
146 | */ |
||
147 | View Code Duplication | public function date($name = '', $value = null, $format = true) |
|
148 | { |
||
149 | $element = $this->input('date', $name, $value); |
||
150 | |||
151 | if (!$format || empty($element->getAttribute('value'))) { |
||
152 | return $element; |
||
153 | } |
||
154 | |||
155 | return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_DATE_FORMAT)); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param string|null $name |
||
160 | * @param string|null $value |
||
161 | * @param string|null $min |
||
162 | * @param string|null $max |
||
163 | * @param string|null $step |
||
164 | * |
||
165 | * @return \Spatie\Html\Elements\Input |
||
166 | */ |
||
167 | public function range($name = '', $value = '', $min = null, $max = null, $step = null) |
||
168 | { |
||
169 | return $this->input('range', $name, $value) |
||
170 | ->attributeIfNotNull($min, 'min', $min) |
||
171 | ->attributeIfNotNull($max, 'max', $max) |
||
172 | ->attributeIfNotNull($step, 'step', $step); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param string|null $name |
||
177 | * @param string|null $value |
||
178 | * @param bool $format |
||
179 | * |
||
180 | * @return \Spatie\Html\Elements\Input |
||
181 | */ |
||
182 | View Code Duplication | public function time($name = '', $value = null, $format = true) |
|
183 | { |
||
184 | $element = $this->input('time', $name, $value); |
||
185 | |||
186 | if (!$format || empty($element->getAttribute('value'))) { |
||
187 | return $element; |
||
188 | } |
||
189 | |||
190 | return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_TIME_FORMAT)); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param string $tag |
||
195 | * |
||
196 | * @return \Spatie\Html\Elements\Element |
||
197 | */ |
||
198 | public function element($tag) |
||
199 | { |
||
200 | return Element::withTag($tag); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param string|null $type |
||
205 | * @param string|null $name |
||
206 | * @param string|null $value |
||
207 | * |
||
208 | * @return \Spatie\Html\Elements\Input |
||
209 | */ |
||
210 | public function input($type = null, $name = null, $value = null) |
||
211 | { |
||
212 | $hasValue = $name && ($type !== 'password' && !is_null($this->old($name, $value)) || !is_null($value)); |
||
213 | |||
214 | return Input::create() |
||
215 | ->attributeIf($type, 'type', $type) |
||
216 | ->attributeIf($name, 'name', $this->fieldName($name)) |
||
217 | ->attributeIf($name, 'id', $this->fieldName($name)) |
||
218 | ->attributeIf($hasValue, 'value', $this->old($name, $value)); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param \Spatie\Html\HtmlElement|string|null $legend |
||
223 | * |
||
224 | * @return \Spatie\Html\Elements\Fieldset |
||
225 | */ |
||
226 | public function fieldset($legend = null) |
||
227 | { |
||
228 | return $legend ? |
||
229 | Fieldset::create()->legend($legend) : Fieldset::create(); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param string $method |
||
234 | * @param string|null $action |
||
235 | * |
||
236 | * @return \Spatie\Html\Elements\Form |
||
237 | */ |
||
238 | public function form($method = 'POST', $action = null) |
||
258 | |||
259 | /** |
||
260 | * @param string|null $name |
||
261 | * @param string|null $value |
||
262 | * |
||
263 | * @return \Spatie\Html\Elements\Input |
||
264 | */ |
||
265 | public function hidden($name = null, $value = null) |
||
269 | |||
270 | /** |
||
271 | * @param string|null $src |
||
272 | * @param string|null $alt |
||
273 | * |
||
274 | * @return \Spatie\Html\Elements\Img |
||
275 | */ |
||
276 | public function img($src = null, $alt = null) |
||
282 | |||
283 | /** |
||
284 | * @param \Spatie\Html\HtmlElement|iterable|string|null $contents |
||
285 | * @param string|null $for |
||
286 | * |
||
287 | * @return \Spatie\Html\Elements\Label |
||
288 | */ |
||
289 | public function label($contents = null, $for = null) |
||
295 | |||
296 | /** |
||
297 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
298 | * |
||
299 | * @return \Spatie\Html\Elements\Legend |
||
300 | */ |
||
301 | public function legend($contents = null) |
||
305 | |||
306 | /** |
||
307 | * @param string $email |
||
308 | * @param string|null $text |
||
309 | * |
||
310 | * @return \Spatie\Html\Elements\A |
||
311 | */ |
||
312 | public function mailto($email, $text = null) |
||
316 | |||
317 | /** |
||
318 | * @param string|null $name |
||
319 | * @param iterable $options |
||
320 | * @param string|iterable|null $value |
||
321 | * |
||
322 | * @return \Spatie\Html\Elements\Select |
||
323 | */ |
||
324 | View Code Duplication | public function multiselect($name = null, $options = [], $value = null) |
|
333 | |||
334 | /** |
||
335 | * @param string|null $name |
||
336 | * @param string|null $value |
||
337 | * @param string|null $min |
||
338 | * @param string|null $max |
||
339 | * @param string|null $step |
||
340 | * |
||
341 | * @return \Spatie\Html\Elements\Input |
||
342 | */ |
||
343 | public function number($name = null, $value = null, $min = null, $max = null, $step = null) |
||
344 | { |
||
345 | return $this->input('number', $name, $value) |
||
346 | ->attributeIfNotNull($min, 'min', $min) |
||
347 | ->attributeIfNotNull($max, 'max', $max) |
||
350 | |||
351 | /** |
||
352 | * @param string|null $text |
||
353 | * @param string|null $value |
||
354 | * @param bool $selected |
||
355 | * |
||
356 | * @return \Spatie\Html\Elements\Option |
||
357 | */ |
||
358 | public function option($text = null, $value = null, $selected = false) |
||
365 | |||
366 | /** |
||
367 | * @param string|null $value |
||
368 | * |
||
369 | * @return \Spatie\Html\Elements\Input |
||
370 | */ |
||
371 | public function password($name = null) |
||
375 | |||
376 | /** |
||
377 | * @param string|null $name |
||
378 | * @param bool $checked |
||
379 | * @param string|null $value |
||
380 | * |
||
381 | * @return \Spatie\Html\Elements\Input |
||
382 | */ |
||
383 | public function radio($name = null, $checked = null, $value = null) |
||
390 | |||
391 | /** |
||
392 | * @param string|null $name |
||
393 | * @param iterable $options |
||
394 | * @param string|iterable|null $value |
||
395 | * |
||
396 | * @return \Spatie\Html\Elements\Select |
||
397 | */ |
||
398 | View Code Duplication | public function select($name = null, $options = [], $value = null) |
|
406 | |||
407 | /** |
||
408 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
409 | * |
||
410 | * @return \Spatie\Html\Elements\Span |
||
411 | */ |
||
412 | public function span($contents = null) |
||
416 | |||
417 | /** |
||
418 | * @param string|null $text |
||
419 | * |
||
420 | * @return \Spatie\Html\Elements\Button |
||
421 | */ |
||
422 | public function submit($text = null) |
||
426 | |||
427 | /** |
||
428 | * @param string|null $text |
||
429 | * |
||
430 | * @return \Spatie\Html\Elements\Button |
||
431 | */ |
||
432 | public function reset($text = null) |
||
436 | |||
437 | /** |
||
438 | * @param string $number |
||
439 | * @param string|null $text |
||
440 | * |
||
441 | * @return \Spatie\Html\Elements\A |
||
442 | */ |
||
443 | public function tel($number, $text = null) |
||
447 | |||
448 | /** |
||
449 | * @param string|null $name |
||
450 | * @param string|null $value |
||
451 | * |
||
452 | * @return \Spatie\Html\Elements\Input |
||
453 | */ |
||
454 | public function text($name = null, $value = null) |
||
458 | |||
459 | /** |
||
460 | * @param string|null $name |
||
461 | * |
||
462 | * @return \Spatie\Html\Elements\File |
||
463 | */ |
||
464 | public function file($name = null) |
||
470 | |||
471 | /** |
||
472 | * @param string|null $name |
||
473 | * @param string|null $value |
||
474 | * |
||
475 | * @return \Spatie\Html\Elements\Textarea |
||
476 | */ |
||
477 | public function textarea($name = null, $value = null) |
||
484 | |||
485 | /** |
||
486 | * @return \Spatie\Html\Elements\Input |
||
487 | */ |
||
488 | public function token() |
||
495 | |||
496 | /** |
||
497 | * @param \ArrayAccess|array $model |
||
498 | * |
||
499 | * @return $this |
||
500 | */ |
||
501 | public function model($model) |
||
507 | |||
508 | /** |
||
509 | * @param \ArrayAccess|array $model |
||
510 | * @param string|null $method |
||
511 | * @param string|null $action |
||
512 | * |
||
513 | * @return \Spatie\Html\Elements\Form |
||
514 | */ |
||
515 | public function modelForm($model, $method = 'POST', $action = null): Form |
||
521 | |||
522 | /** |
||
523 | * @return $this |
||
524 | */ |
||
525 | public function endModel() |
||
531 | |||
532 | /** |
||
533 | * @return \Illuminate\Contracts\Support\Htmlable |
||
534 | */ |
||
535 | public function closeModelForm(): Htmlable |
||
541 | |||
542 | /** |
||
543 | * @param string $name |
||
544 | * @param mixed $value |
||
545 | * |
||
546 | * @return mixed |
||
547 | */ |
||
548 | protected function old($name, $value = null) |
||
566 | |||
567 | /** |
||
568 | * Retrieve the value from the current session or assigned model. This is |
||
569 | * a public alias for `old`. |
||
570 | * |
||
571 | * @param string $name |
||
572 | * @param mixed $value |
||
573 | * |
||
574 | * @return mixed |
||
575 | */ |
||
576 | public function value($name, $default = null) |
||
580 | |||
581 | /** |
||
582 | * @param string $name |
||
583 | * |
||
584 | * @return string |
||
585 | */ |
||
586 | protected function fieldName($name) |
||
590 | |||
591 | protected function ensureModelIsAvailable() |
||
597 | |||
598 | /** |
||
599 | * @param string $value |
||
600 | * @param string $format DateTime formatting string supported by date_format() |
||
601 | * @return string |
||
602 | */ |
||
603 | protected function formatDateTime($value, $format) |
||
617 | } |
||
618 |