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 |
||
28 | class Html |
||
29 | { |
||
30 | use Macroable; |
||
31 | |||
32 | const HTML_DATE_FORMAT = 'Y-m-d'; |
||
33 | const HTML_TIME_FORMAT = 'H:i:s'; |
||
34 | |||
35 | /** @var \Illuminate\Http\Request */ |
||
36 | protected $request; |
||
37 | |||
38 | /** @var \ArrayAccess|array */ |
||
39 | protected $model; |
||
40 | |||
41 | public function __construct(Request $request) |
||
45 | |||
46 | /** |
||
47 | * @param string|null $href |
||
48 | * @param string|null $text |
||
49 | * |
||
50 | * @return \Spatie\Html\Elements\A |
||
51 | */ |
||
52 | public function a($href = null, $contents = null) |
||
58 | |||
59 | /** |
||
60 | * @param string|null $href |
||
61 | * @param string|null $text |
||
62 | * |
||
63 | * @return \Spatie\Html\Elements\I |
||
64 | */ |
||
65 | public function i($contents = null) |
||
70 | |||
71 | /** |
||
72 | * @param string|null $type |
||
73 | * @param string|null $text |
||
74 | * |
||
75 | * @return \Spatie\Html\Elements\Button |
||
76 | */ |
||
77 | public function button($contents = null, $type = null, $name = null) |
||
84 | |||
85 | /** |
||
86 | * @param \Illuminate\Support\Collection|iterable|string $classes |
||
87 | * |
||
88 | * @return \Illuminate\Contracts\Support\Htmlable |
||
89 | */ |
||
90 | public function class($classes): Htmlable |
||
103 | |||
104 | /** |
||
105 | * @param string|null $name |
||
106 | * @param bool $checked |
||
107 | * @param string|null $value |
||
108 | * |
||
109 | * @return \Spatie\Html\Elements\Input |
||
110 | */ |
||
111 | public function checkbox($name = null, $checked = null, $value = '1') |
||
117 | |||
118 | /** |
||
119 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
120 | * |
||
121 | * @return \Spatie\Html\Elements\Div |
||
122 | */ |
||
123 | public function div($contents = null) |
||
127 | |||
128 | /** |
||
129 | * @param string|null $name |
||
130 | * @param string|null $value |
||
131 | * |
||
132 | * @return \Spatie\Html\Elements\Input |
||
133 | */ |
||
134 | public function email($name = null, $value = null) |
||
138 | |||
139 | /** |
||
140 | * @param string|null $name |
||
141 | * @param string|null $value |
||
142 | * @param bool $format |
||
143 | * |
||
144 | * @return \Spatie\Html\Elements\Input |
||
145 | */ |
||
146 | View Code Duplication | public function date($name = '', $value = null, $format = true) |
|
156 | |||
157 | /** |
||
158 | * @param string|null $name |
||
159 | * @param string|null $value |
||
160 | * @param bool $format |
||
161 | * |
||
162 | * @return \Spatie\Html\Elements\Input |
||
163 | */ |
||
164 | View Code Duplication | public function time($name = '', $value = null, $format = true) |
|
174 | |||
175 | /** |
||
176 | * @param string $tag |
||
177 | * |
||
178 | * @return \Spatie\Html\Elements\Element |
||
179 | */ |
||
180 | public function element($tag) |
||
184 | |||
185 | /** |
||
186 | * @param string|null $type |
||
187 | * @param string|null $name |
||
188 | * @param string|null $value |
||
189 | * |
||
190 | * @return \Spatie\Html\Elements\Input |
||
191 | */ |
||
192 | public function input($type = null, $name = null, $value = null) |
||
193 | { |
||
194 | $hasValue = $name && ($type !== 'password' && ! is_null($this->old($name, $value)) || ! is_null($value)); |
||
195 | |||
196 | return Input::create() |
||
197 | ->attributeIf($type, 'type', $type) |
||
198 | ->attributeIf($name, 'name', $this->fieldName($name)) |
||
199 | ->attributeIf($name, 'id', $this->fieldName($name)) |
||
200 | ->attributeIf($hasValue, 'value', $this->old($name, $value)); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param \Spatie\Html\HtmlElement|string|null $legend |
||
205 | * |
||
206 | * @return \Spatie\Html\Elements\Fieldset |
||
207 | */ |
||
208 | public function fieldset($legend = null) |
||
214 | |||
215 | /** |
||
216 | * @param string $method |
||
217 | * @param string|null $action |
||
218 | * |
||
219 | * @return \Spatie\Html\Elements\Form |
||
220 | */ |
||
221 | public function form($method = 'POST', $action = null) |
||
241 | |||
242 | /** |
||
243 | * @param string|null $name |
||
244 | * @param string|null $value |
||
245 | * |
||
246 | * @return \Spatie\Html\Elements\Input |
||
247 | */ |
||
248 | public function hidden($name = null, $value = null) |
||
252 | |||
253 | /** |
||
254 | * @param string|null $src |
||
255 | * @param string|null $alt |
||
256 | * |
||
257 | * @return \Spatie\Html\Elements\Img |
||
258 | */ |
||
259 | public function img($src = null, $alt = null) |
||
265 | |||
266 | /** |
||
267 | * @param \Spatie\Html\HtmlElement|iterable|string|null $contents |
||
268 | * @param string|null $for |
||
269 | * |
||
270 | * @return \Spatie\Html\Elements\Label |
||
271 | */ |
||
272 | public function label($contents = null, $for = null) |
||
278 | |||
279 | /** |
||
280 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
281 | * |
||
282 | * @return \Spatie\Html\Elements\Legend |
||
283 | */ |
||
284 | public function legend($contents = null) |
||
288 | |||
289 | /** |
||
290 | * @param string $email |
||
291 | * @param string|null $text |
||
292 | * |
||
293 | * @return \Spatie\Html\Elements\A |
||
294 | */ |
||
295 | public function mailto($email, $text = null) |
||
299 | |||
300 | /** |
||
301 | * @param string|null $name |
||
302 | * @param iterable $options |
||
303 | * @param string|iterable|null $value |
||
304 | * |
||
305 | * @return \Spatie\Html\Elements\Select |
||
306 | */ |
||
307 | View Code Duplication | public function multiselect($name = null, $options = [], $value = null) |
|
316 | |||
317 | /** |
||
318 | * @param string|null $text |
||
319 | * @param string|null $value |
||
320 | * @param bool $selected |
||
321 | * |
||
322 | * @return \Spatie\Html\Elements\Option |
||
323 | */ |
||
324 | public function option($text = null, $value = null, $selected = false) |
||
331 | |||
332 | /** |
||
333 | * @param string|null $value |
||
334 | * |
||
335 | * @return \Spatie\Html\Elements\Input |
||
336 | */ |
||
337 | public function password($name = null) |
||
341 | |||
342 | /** |
||
343 | * @param string|null $name |
||
344 | * @param bool $checked |
||
345 | * @param string|null $value |
||
346 | * |
||
347 | * @return \Spatie\Html\Elements\Input |
||
348 | */ |
||
349 | public function radio($name = null, $checked = null, $value = null) |
||
356 | |||
357 | /** |
||
358 | * @param string|null $name |
||
359 | * @param iterable $options |
||
360 | * @param string|iterable|null $value |
||
361 | * |
||
362 | * @return \Spatie\Html\Elements\Select |
||
363 | */ |
||
364 | View Code Duplication | public function select($name = null, $options = [], $value = null) |
|
372 | |||
373 | /** |
||
374 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
375 | * |
||
376 | * @return \Spatie\Html\Elements\Span |
||
377 | */ |
||
378 | public function span($contents = null) |
||
382 | |||
383 | /** |
||
384 | * @param string|null $text |
||
385 | * |
||
386 | * @return \Spatie\Html\Elements\Button |
||
387 | */ |
||
388 | public function submit($text = null) |
||
392 | |||
393 | /** |
||
394 | * @param string|null $text |
||
395 | * |
||
396 | * @return \Spatie\Html\Elements\Button |
||
397 | */ |
||
398 | public function reset($text = null) |
||
402 | |||
403 | /** |
||
404 | * @param string $number |
||
405 | * @param string|null $text |
||
406 | * |
||
407 | * @return \Spatie\Html\Elements\A |
||
408 | */ |
||
409 | public function tel($number, $text = null) |
||
413 | |||
414 | /** |
||
415 | * @param string|null $name |
||
416 | * @param string|null $value |
||
417 | * |
||
418 | * @return \Spatie\Html\Elements\Input |
||
419 | */ |
||
420 | public function text($name = null, $value = null) |
||
424 | |||
425 | /** |
||
426 | * @param string|null $name |
||
427 | * |
||
428 | * @return \Spatie\Html\Elements\File |
||
429 | */ |
||
430 | public function file($name = null) |
||
436 | |||
437 | /** |
||
438 | * @param string|null $name |
||
439 | * @param string|null $value |
||
440 | * |
||
441 | * @return \Spatie\Html\Elements\Textarea |
||
442 | */ |
||
443 | public function textarea($name = null, $value = null) |
||
450 | |||
451 | /** |
||
452 | * @return \Spatie\Html\Elements\Input |
||
453 | */ |
||
454 | public function token() |
||
461 | |||
462 | /** |
||
463 | * @param \ArrayAccess|array $model |
||
464 | * |
||
465 | * @return $this |
||
466 | */ |
||
467 | public function model($model) |
||
473 | |||
474 | /** |
||
475 | * @param \ArrayAccess|array $model |
||
476 | * @param string|null $method |
||
477 | * @param string|null $action |
||
478 | * |
||
479 | * @return \Spatie\Html\Elements\Form |
||
480 | */ |
||
481 | public function modelForm($model, $method = 'POST', $action = null): Form |
||
487 | |||
488 | /** |
||
489 | * @return $this |
||
490 | */ |
||
491 | public function endModel() |
||
497 | |||
498 | /** |
||
499 | * @return \Illuminate\Contracts\Support\Htmlable |
||
500 | */ |
||
501 | public function closeModelForm(): Htmlable |
||
507 | |||
508 | /** |
||
509 | * @param string $name |
||
510 | * @param mixed $value |
||
511 | * |
||
512 | * @return mixed |
||
513 | */ |
||
514 | protected function old($name, $value = null) |
||
532 | |||
533 | /** |
||
534 | * Retrieve the value from the current session or assigned model. This is |
||
535 | * a public alias for `old`. |
||
536 | * |
||
537 | * @param string $name |
||
538 | * @param mixed $value |
||
539 | * |
||
540 | * @return mixed |
||
541 | */ |
||
542 | public function value($name, $default = null) |
||
546 | |||
547 | /** |
||
548 | * @param string $name |
||
549 | * |
||
550 | * @return string |
||
551 | */ |
||
552 | protected function fieldName($name) |
||
556 | |||
557 | protected function ensureModelIsAvailable() |
||
563 | |||
564 | /** |
||
565 | * @param string $value |
||
566 | * @param string $format DateTime formatting string supported by date_format() |
||
567 | * @return string |
||
568 | */ |
||
569 | protected function formatDateTime($value, $format) |
||
583 | } |
||
584 |