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 namespace Anomaly\Streams\Platform\Support; |
||
12 | class Html |
||
13 | { |
||
14 | use Macroable, Componentable { |
||
15 | Macroable::__call as macroCall; |
||
16 | Componentable::__call as componentCall; |
||
17 | } |
||
18 | |||
19 | /** |
||
20 | * The HTML builder instance. |
||
21 | * |
||
22 | * @var \Collective\Html\HtmlBuilder |
||
23 | */ |
||
24 | protected $html; |
||
25 | |||
26 | /** |
||
27 | * The URL generator instance. |
||
28 | * |
||
29 | * @var \Illuminate\Contracts\Routing\UrlGenerator |
||
30 | */ |
||
31 | protected $url; |
||
32 | |||
33 | /** |
||
34 | * The View factory instance. |
||
35 | * |
||
36 | * @var \Illuminate\Contracts\View\Factory |
||
37 | */ |
||
38 | protected $view; |
||
39 | |||
40 | /** |
||
41 | * The CSRF token used by the form builder. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $csrfToken; |
||
46 | |||
47 | /** |
||
48 | * The session store implementation. |
||
49 | * |
||
50 | * @var \Illuminate\Session\SessionInterface |
||
51 | */ |
||
52 | protected $session; |
||
53 | |||
54 | /** |
||
55 | * The current model instance for the form. |
||
56 | * |
||
57 | * @var mixed |
||
58 | */ |
||
59 | protected $model; |
||
60 | |||
61 | /** |
||
62 | * An array of label names we've created. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $labels = []; |
||
67 | |||
68 | /** |
||
69 | * The reserved form open attributes. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $reserved = ['method', 'url', 'route', 'action', 'files']; |
||
74 | |||
75 | /** |
||
76 | * The form methods that should be spoofed, in uppercase. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT']; |
||
81 | |||
82 | /** |
||
83 | * The types of inputs to not fill values on by default. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio']; |
||
88 | |||
89 | /** |
||
90 | * Create a new form builder instance. |
||
91 | * |
||
92 | * @param \Collective\Html\HtmlBuilder $html |
||
93 | * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
||
94 | * @param \Illuminate\Contracts\View\Factory $view |
||
95 | * @param string $csrfToken |
||
96 | */ |
||
97 | public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken) |
||
104 | |||
105 | /** |
||
106 | * Open up a new HTML form. |
||
107 | * |
||
108 | * @param array $options |
||
109 | * |
||
110 | * @return \Illuminate\Support\HtmlString |
||
111 | */ |
||
112 | public function open(array $options = []) |
||
150 | |||
151 | /** |
||
152 | * Create a new model based form builder. |
||
153 | * |
||
154 | * @param mixed $model |
||
155 | * @param array $options |
||
156 | * |
||
157 | * @return \Illuminate\Support\HtmlString |
||
158 | */ |
||
159 | public function model($model, array $options = []) |
||
165 | |||
166 | /** |
||
167 | * Set the model instance on the form builder. |
||
168 | * |
||
169 | * @param mixed $model |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | public function setModel($model) |
||
177 | |||
178 | /** |
||
179 | * Close the current form. |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | public function close() |
||
191 | |||
192 | /** |
||
193 | * Generate a hidden field with the current CSRF token. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function token() |
||
203 | |||
204 | /** |
||
205 | * Create a form label element. |
||
206 | * |
||
207 | * @param string $name |
||
208 | * @param string $value |
||
209 | * @param array $options |
||
210 | * @param bool $escape_html |
||
211 | * |
||
212 | * @return \Illuminate\Support\HtmlString |
||
213 | */ |
||
214 | public function label($name, $value = null, $options = [], $escape_html = true) |
||
228 | |||
229 | /** |
||
230 | * Format the label value. |
||
231 | * |
||
232 | * @param string $name |
||
233 | * @param string|null $value |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | protected function formatLabel($name, $value) |
||
241 | |||
242 | /** |
||
243 | * Create a form input field. |
||
244 | * |
||
245 | * @param string $type |
||
246 | * @param string $name |
||
247 | * @param string $value |
||
248 | * @param array $options |
||
249 | * |
||
250 | * @return \Illuminate\Support\HtmlString |
||
251 | */ |
||
252 | public function input($type, $name, $value = null, $options = []) |
||
276 | |||
277 | /** |
||
278 | * Create a text input field. |
||
279 | * |
||
280 | * @param string $name |
||
281 | * @param string $value |
||
282 | * @param array $options |
||
283 | * |
||
284 | * @return \Illuminate\Support\HtmlString |
||
285 | */ |
||
286 | public function text($name, $value = null, $options = []) |
||
290 | |||
291 | /** |
||
292 | * Create a password input field. |
||
293 | * |
||
294 | * @param string $name |
||
295 | * @param array $options |
||
296 | * |
||
297 | * @return \Illuminate\Support\HtmlString |
||
298 | */ |
||
299 | public function password($name, $options = []) |
||
303 | |||
304 | /** |
||
305 | * Create a hidden input field. |
||
306 | * |
||
307 | * @param string $name |
||
308 | * @param string $value |
||
309 | * @param array $options |
||
310 | * |
||
311 | * @return \Illuminate\Support\HtmlString |
||
312 | */ |
||
313 | public function hidden($name, $value = null, $options = []) |
||
317 | |||
318 | /** |
||
319 | * Create an e-mail input field. |
||
320 | * |
||
321 | * @param string $name |
||
322 | * @param string $value |
||
323 | * @param array $options |
||
324 | * |
||
325 | * @return \Illuminate\Support\HtmlString |
||
326 | */ |
||
327 | public function email($name, $value = null, $options = []) |
||
331 | |||
332 | /** |
||
333 | * Create a tel input field. |
||
334 | * |
||
335 | * @param string $name |
||
336 | * @param string $value |
||
337 | * @param array $options |
||
338 | * |
||
339 | * @return \Illuminate\Support\HtmlString |
||
340 | */ |
||
341 | public function tel($name, $value = null, $options = []) |
||
345 | |||
346 | /** |
||
347 | * Create a number input field. |
||
348 | * |
||
349 | * @param string $name |
||
350 | * @param string $value |
||
351 | * @param array $options |
||
352 | * |
||
353 | * @return \Illuminate\Support\HtmlString |
||
354 | */ |
||
355 | public function number($name, $value = null, $options = []) |
||
359 | |||
360 | /** |
||
361 | * Create a date input field. |
||
362 | * |
||
363 | * @param string $name |
||
364 | * @param string $value |
||
365 | * @param array $options |
||
366 | * |
||
367 | * @return \Illuminate\Support\HtmlString |
||
368 | */ |
||
369 | View Code Duplication | public function date($name, $value = null, $options = []) |
|
377 | |||
378 | /** |
||
379 | * Create a datetime input field. |
||
380 | * |
||
381 | * @param string $name |
||
382 | * @param string $value |
||
383 | * @param array $options |
||
384 | * |
||
385 | * @return \Illuminate\Support\HtmlString |
||
386 | */ |
||
387 | View Code Duplication | public function datetime($name, $value = null, $options = []) |
|
395 | |||
396 | /** |
||
397 | * Create a datetime-local input field. |
||
398 | * |
||
399 | * @param string $name |
||
400 | * @param string $value |
||
401 | * @param array $options |
||
402 | * |
||
403 | * @return \Illuminate\Support\HtmlString |
||
404 | */ |
||
405 | View Code Duplication | public function datetimeLocal($name, $value = null, $options = []) |
|
413 | |||
414 | /** |
||
415 | * Create a time input field. |
||
416 | * |
||
417 | * @param string $name |
||
418 | * @param string $value |
||
419 | * @param array $options |
||
420 | * |
||
421 | * @return \Illuminate\Support\HtmlString |
||
422 | */ |
||
423 | public function time($name, $value = null, $options = []) |
||
427 | |||
428 | /** |
||
429 | * Create a url input field. |
||
430 | * |
||
431 | * @param string $name |
||
432 | * @param string $value |
||
433 | * @param array $options |
||
434 | * |
||
435 | * @return \Illuminate\Support\HtmlString |
||
436 | */ |
||
437 | public function url($name, $value = null, $options = []) |
||
441 | |||
442 | /** |
||
443 | * Create a file input field. |
||
444 | * |
||
445 | * @param string $name |
||
446 | * @param array $options |
||
447 | * |
||
448 | * @return \Illuminate\Support\HtmlString |
||
449 | */ |
||
450 | public function file($name, $options = []) |
||
454 | |||
455 | /** |
||
456 | * Create a textarea input field. |
||
457 | * |
||
458 | * @param string $name |
||
459 | * @param string $value |
||
460 | * @param array $options |
||
461 | * |
||
462 | * @return \Illuminate\Support\HtmlString |
||
463 | */ |
||
464 | public function textarea($name, $value = null, $options = []) |
||
488 | |||
489 | /** |
||
490 | * Set the text area size on the attributes. |
||
491 | * |
||
492 | * @param array $options |
||
493 | * |
||
494 | * @return array |
||
495 | */ |
||
496 | protected function setTextAreaSize($options) |
||
511 | |||
512 | /** |
||
513 | * Set the text area size using the quick "size" attribute. |
||
514 | * |
||
515 | * @param array $options |
||
516 | * |
||
517 | * @return array |
||
518 | */ |
||
519 | protected function setQuickTextAreaSize($options) |
||
525 | |||
526 | /** |
||
527 | * Create a select box field. |
||
528 | * |
||
529 | * @param string $name |
||
530 | * @param array $list |
||
531 | * @param string $selected |
||
532 | * @param array $options |
||
533 | * |
||
534 | * @return \Illuminate\Support\HtmlString |
||
535 | */ |
||
536 | public function select($name, $list = [], $selected = null, $options = []) |
||
572 | |||
573 | /** |
||
574 | * Create a select range field. |
||
575 | * |
||
576 | * @param string $name |
||
577 | * @param string $begin |
||
578 | * @param string $end |
||
579 | * @param string $selected |
||
580 | * @param array $options |
||
581 | * |
||
582 | * @return \Illuminate\Support\HtmlString |
||
583 | */ |
||
584 | public function selectRange($name, $begin, $end, $selected = null, $options = []) |
||
590 | |||
591 | /** |
||
592 | * Create a select year field. |
||
593 | * |
||
594 | * @param string $name |
||
595 | * @param string $begin |
||
596 | * @param string $end |
||
597 | * @param string $selected |
||
598 | * @param array $options |
||
599 | * |
||
600 | * @return mixed |
||
601 | */ |
||
602 | public function selectYear() |
||
606 | |||
607 | /** |
||
608 | * Create a select month field. |
||
609 | * |
||
610 | * @param string $name |
||
611 | * @param string $selected |
||
612 | * @param array $options |
||
613 | * @param string $format |
||
614 | * |
||
615 | * @return \Illuminate\Support\HtmlString |
||
616 | */ |
||
617 | public function selectMonth($name, $selected = null, $options = [], $format = '%B') |
||
627 | |||
628 | /** |
||
629 | * Get the select option for the given value. |
||
630 | * |
||
631 | * @param string $display |
||
632 | * @param string $value |
||
633 | * @param string $selected |
||
634 | * |
||
635 | * @return \Illuminate\Support\HtmlString |
||
636 | */ |
||
637 | public function getSelectOption($display, $value, $selected) |
||
645 | |||
646 | /** |
||
647 | * Create an option group form element. |
||
648 | * |
||
649 | * @param array $list |
||
650 | * @param string $label |
||
651 | * @param string $selected |
||
652 | * |
||
653 | * @return \Illuminate\Support\HtmlString |
||
654 | */ |
||
655 | protected function optionGroup($list, $label, $selected) |
||
665 | |||
666 | /** |
||
667 | * Create a select element option. |
||
668 | * |
||
669 | * @param string $display |
||
670 | * @param string $value |
||
671 | * @param string $selected |
||
672 | * |
||
673 | * @return \Illuminate\Support\HtmlString |
||
674 | */ |
||
675 | protected function option($display, $value, $selected) |
||
683 | |||
684 | /** |
||
685 | * Create a placeholder select element option. |
||
686 | * |
||
687 | * @param $display |
||
688 | * @param $selected |
||
689 | * |
||
690 | * @return \Illuminate\Support\HtmlString |
||
691 | */ |
||
692 | protected function placeholderOption($display, $selected) |
||
701 | |||
702 | /** |
||
703 | * Determine if the value is selected. |
||
704 | * |
||
705 | * @param string $value |
||
706 | * @param string $selected |
||
707 | * |
||
708 | * @return null|string |
||
709 | */ |
||
710 | protected function getSelectedValue($value, $selected) |
||
718 | |||
719 | /** |
||
720 | * Create a checkbox input field. |
||
721 | * |
||
722 | * @param string $name |
||
723 | * @param mixed $value |
||
724 | * @param bool $checked |
||
725 | * @param array $options |
||
726 | * |
||
727 | * @return \Illuminate\Support\HtmlString |
||
728 | */ |
||
729 | public function checkbox($name, $value = 1, $checked = null, $options = []) |
||
733 | |||
734 | /** |
||
735 | * Create a radio button input field. |
||
736 | * |
||
737 | * @param string $name |
||
738 | * @param mixed $value |
||
739 | * @param bool $checked |
||
740 | * @param array $options |
||
741 | * |
||
742 | * @return \Illuminate\Support\HtmlString |
||
743 | */ |
||
744 | public function radio($name, $value = null, $checked = null, $options = []) |
||
752 | |||
753 | /** |
||
754 | * Create a checkable input field. |
||
755 | * |
||
756 | * @param string $type |
||
757 | * @param string $name |
||
758 | * @param mixed $value |
||
759 | * @param bool $checked |
||
760 | * @param array $options |
||
761 | * |
||
762 | * @return \Illuminate\Support\HtmlString |
||
763 | */ |
||
764 | protected function checkable($type, $name, $value, $checked, $options) |
||
774 | |||
775 | /** |
||
776 | * Get the check state for a checkable input. |
||
777 | * |
||
778 | * @param string $type |
||
779 | * @param string $name |
||
780 | * @param mixed $value |
||
781 | * @param bool $checked |
||
782 | * |
||
783 | * @return bool |
||
784 | */ |
||
785 | protected function getCheckedState($type, $name, $value, $checked) |
||
798 | |||
799 | /** |
||
800 | * Get the check state for a checkbox input. |
||
801 | * |
||
802 | * @param string $name |
||
803 | * @param mixed $value |
||
804 | * @param bool $checked |
||
805 | * |
||
806 | * @return bool |
||
807 | */ |
||
808 | protected function getCheckboxCheckedState($name, $value, $checked) |
||
828 | |||
829 | /** |
||
830 | * Get the check state for a radio input. |
||
831 | * |
||
832 | * @param string $name |
||
833 | * @param mixed $value |
||
834 | * @param bool $checked |
||
835 | * |
||
836 | * @return bool |
||
837 | */ |
||
838 | protected function getRadioCheckedState($name, $value, $checked) |
||
846 | |||
847 | /** |
||
848 | * Determine if old input or model input exists for a key. |
||
849 | * |
||
850 | * @param string $name |
||
851 | * |
||
852 | * @return bool |
||
853 | */ |
||
854 | protected function missingOldAndModel($name) |
||
858 | |||
859 | /** |
||
860 | * Create a HTML reset input element. |
||
861 | * |
||
862 | * @param string $value |
||
863 | * @param array $attributes |
||
864 | * |
||
865 | * @return \Illuminate\Support\HtmlString |
||
866 | */ |
||
867 | public function reset($value, $attributes = []) |
||
871 | |||
872 | /** |
||
873 | * Create a HTML image input element. |
||
874 | * |
||
875 | * @param string $url |
||
876 | * @param string $name |
||
877 | * @param array $attributes |
||
878 | * |
||
879 | * @return \Illuminate\Support\HtmlString |
||
880 | */ |
||
881 | public function image($url, $name = null, $attributes = []) |
||
887 | |||
888 | /** |
||
889 | * Create a color input field. |
||
890 | * |
||
891 | * @param string $name |
||
892 | * @param string $value |
||
893 | * @param array $options |
||
894 | * |
||
895 | * @return \Illuminate\Support\HtmlString |
||
896 | */ |
||
897 | public function color($name, $value = null, $options = []) |
||
901 | |||
902 | /** |
||
903 | * Create a submit button element. |
||
904 | * |
||
905 | * @param string $value |
||
906 | * @param array $options |
||
907 | * |
||
908 | * @return \Illuminate\Support\HtmlString |
||
909 | */ |
||
910 | public function submit($value = null, $options = []) |
||
914 | |||
915 | /** |
||
916 | * Create a button element. |
||
917 | * |
||
918 | * @param string $value |
||
919 | * @param array $options |
||
920 | * |
||
921 | * @return \Illuminate\Support\HtmlString |
||
922 | */ |
||
923 | public function button($value = null, $options = []) |
||
931 | |||
932 | /** |
||
933 | * Parse the form action method. |
||
934 | * |
||
935 | * @param string $method |
||
936 | * |
||
937 | * @return string |
||
938 | */ |
||
939 | protected function getMethod($method) |
||
945 | |||
946 | /** |
||
947 | * Get the form action from the options. |
||
948 | * |
||
949 | * @param array $options |
||
950 | * |
||
951 | * @return string |
||
952 | */ |
||
953 | protected function getAction(array $options) |
||
975 | |||
976 | /** |
||
977 | * Get the action for a "url" option. |
||
978 | * |
||
979 | * @param array|string $options |
||
980 | * |
||
981 | * @return string |
||
982 | */ |
||
983 | protected function getUrlAction($options) |
||
991 | |||
992 | /** |
||
993 | * Get the action for a "route" option. |
||
994 | * |
||
995 | * @param array|string $options |
||
996 | * |
||
997 | * @return string |
||
998 | */ |
||
999 | View Code Duplication | protected function getRouteAction($options) |
|
1007 | |||
1008 | /** |
||
1009 | * Get the action for an "action" option. |
||
1010 | * |
||
1011 | * @param array|string $options |
||
1012 | * |
||
1013 | * @return string |
||
1014 | */ |
||
1015 | View Code Duplication | protected function getControllerAction($options) |
|
1023 | |||
1024 | /** |
||
1025 | * Get the form appendage for the given method. |
||
1026 | * |
||
1027 | * @param string $method |
||
1028 | * |
||
1029 | * @return string |
||
1030 | */ |
||
1031 | protected function getAppendage($method) |
||
1051 | |||
1052 | /** |
||
1053 | * Get the ID attribute for a field name. |
||
1054 | * |
||
1055 | * @param string $name |
||
1056 | * @param array $attributes |
||
1057 | * |
||
1058 | * @return string |
||
1059 | */ |
||
1060 | public function getIdAttribute($name, $attributes) |
||
1070 | |||
1071 | /** |
||
1072 | * Get the value that should be assigned to the field. |
||
1073 | * |
||
1074 | * @param string $name |
||
1075 | * @param string $value |
||
1076 | * |
||
1077 | * @return mixed |
||
1078 | */ |
||
1079 | public function getValueAttribute($name, $value = null) |
||
1097 | |||
1098 | /** |
||
1099 | * Get the model value that should be assigned to the field. |
||
1100 | * |
||
1101 | * @param string $name |
||
1102 | * |
||
1103 | * @return mixed |
||
1104 | */ |
||
1105 | protected function getModelValueAttribute($name) |
||
1113 | |||
1114 | /** |
||
1115 | * Get a value from the session's old input. |
||
1116 | * |
||
1117 | * @param string $name |
||
1118 | * |
||
1119 | * @return mixed |
||
1120 | */ |
||
1121 | public function old($name) |
||
1127 | |||
1128 | /** |
||
1129 | * Determine if the old input is empty. |
||
1130 | * |
||
1131 | * @return bool |
||
1132 | */ |
||
1133 | public function oldInputIsEmpty() |
||
1137 | |||
1138 | /** |
||
1139 | * Transform key from array to dot syntax. |
||
1140 | * |
||
1141 | * @param string $key |
||
1142 | * |
||
1143 | * @return mixed |
||
1144 | */ |
||
1145 | protected function transformKey($key) |
||
1149 | |||
1150 | /** |
||
1151 | * Transform the string to an Html serializable object |
||
1152 | * |
||
1153 | * @param $html |
||
1154 | * |
||
1155 | * @return \Illuminate\Support\HtmlString |
||
1156 | */ |
||
1157 | protected function toHtmlString($html) |
||
1161 | |||
1162 | /** |
||
1163 | * Get the session store implementation. |
||
1164 | * |
||
1165 | * @return \Illuminate\Session\SessionInterface $session |
||
1166 | */ |
||
1167 | public function getSessionStore() |
||
1171 | |||
1172 | /** |
||
1173 | * Set the session store implementation. |
||
1174 | * |
||
1175 | * @param \Illuminate\Session\SessionInterface $session |
||
1176 | * |
||
1177 | * @return $this |
||
1178 | */ |
||
1179 | public function setSessionStore(SessionInterface $session) |
||
1185 | |||
1186 | /** |
||
1187 | * Dynamically handle calls to the class. |
||
1188 | * |
||
1189 | * @param string $method |
||
1190 | * @param array $parameters |
||
1191 | * |
||
1192 | * @return \Illuminate\Contracts\View\View|mixed |
||
1193 | * |
||
1194 | * @throws \BadMethodCallException |
||
1195 | */ |
||
1196 | public function __call($method, $parameters) |
||
1212 | } |
||
1213 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: