Complex classes like FormBuilder 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 FormBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanedev\LaravelHtml; |
||
19 | class FormBuilder implements FormBuilderInterface |
||
20 | { |
||
21 | /* ------------------------------------------------------------------------------------------------ |
||
22 | | Traits |
||
23 | | ------------------------------------------------------------------------------------------------ |
||
24 | */ |
||
25 | use Macroable, Componentable { |
||
26 | Macroable::__call as macroCall; |
||
27 | Componentable::__call as componentCall; |
||
28 | } |
||
29 | |||
30 | /* ------------------------------------------------------------------------------------------------ |
||
31 | | Properties |
||
32 | | ------------------------------------------------------------------------------------------------ |
||
33 | */ |
||
34 | /** |
||
35 | * The HTML builder instance. |
||
36 | * |
||
37 | * @var \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface |
||
38 | */ |
||
39 | protected $html; |
||
40 | |||
41 | /** |
||
42 | * The URL generator instance. |
||
43 | * |
||
44 | * @var \Illuminate\Contracts\Routing\UrlGenerator |
||
45 | */ |
||
46 | protected $url; |
||
47 | |||
48 | /** |
||
49 | * The CSRF token used by the form builder. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $csrfToken; |
||
54 | |||
55 | /** |
||
56 | * The session store implementation. |
||
57 | * |
||
58 | * @var \Illuminate\Session\SessionInterface |
||
59 | */ |
||
60 | protected $session; |
||
61 | |||
62 | /** |
||
63 | * The current model instance for the form. |
||
64 | * |
||
65 | * @var mixed |
||
66 | */ |
||
67 | protected $model; |
||
68 | |||
69 | /** |
||
70 | * An array of label names we've created. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $labels = []; |
||
75 | |||
76 | /** |
||
77 | * The reserved form open attributes. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $reserved = ['method', 'url', 'route', 'action', 'files']; |
||
82 | |||
83 | /** |
||
84 | * The form methods that should be spoofed, in uppercase. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT']; |
||
89 | |||
90 | /** |
||
91 | * The types of inputs to not fill values on by default. |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio']; |
||
96 | |||
97 | /* ------------------------------------------------------------------------------------------------ |
||
98 | | Constructor |
||
99 | | ------------------------------------------------------------------------------------------------ |
||
100 | */ |
||
101 | /** |
||
102 | * Create a new form builder instance. |
||
103 | * |
||
104 | * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
||
105 | * @param \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface $html |
||
106 | * @param string $csrfToken |
||
107 | */ |
||
108 | 760 | public function __construct( |
|
117 | |||
118 | /* ------------------------------------------------------------------------------------------------ |
||
119 | | Getters & Setters |
||
120 | | ------------------------------------------------------------------------------------------------ |
||
121 | */ |
||
122 | /** |
||
123 | * Get the session store implementation. |
||
124 | * |
||
125 | * @return \Illuminate\Session\SessionInterface |
||
126 | */ |
||
127 | 8 | public function getSessionStore() |
|
131 | |||
132 | /** |
||
133 | * Set the session store implementation. |
||
134 | * |
||
135 | * @param \Illuminate\Session\SessionInterface $session |
||
136 | * |
||
137 | * @return self |
||
138 | */ |
||
139 | 120 | public function setSessionStore(Session $session) |
|
145 | |||
146 | /** |
||
147 | * Set the model instance on the form builder. |
||
148 | * |
||
149 | * @param mixed $model |
||
150 | * |
||
151 | * @return self |
||
152 | */ |
||
153 | 80 | public function setModel($model) |
|
159 | |||
160 | /** |
||
161 | * Get the ID attribute for a field name. |
||
162 | * |
||
163 | * @param string $name |
||
164 | * @param array $attributes |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 632 | public function getIdAttribute($name, array $attributes) |
|
180 | |||
181 | /** |
||
182 | * Get the value that should be assigned to the field. |
||
183 | * |
||
184 | * @param string $name |
||
185 | * @param mixed $value |
||
186 | * |
||
187 | * @return mixed |
||
188 | */ |
||
189 | 560 | public function getValueAttribute($name, $value = null) |
|
209 | |||
210 | /** |
||
211 | * Get the model value that should be assigned to the field. |
||
212 | * |
||
213 | * @param string $name |
||
214 | * |
||
215 | * @return mixed |
||
216 | */ |
||
217 | 64 | private function getModelValueAttribute($name) |
|
221 | |||
222 | /** |
||
223 | * Get a value from the session's old input. |
||
224 | * |
||
225 | * @param string $name |
||
226 | * |
||
227 | * @return mixed |
||
228 | */ |
||
229 | 560 | public function old($name) |
|
235 | |||
236 | /** |
||
237 | * Transform key from array to dot syntax. |
||
238 | * |
||
239 | * @param string $key |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | 96 | private function transformKey($key) |
|
247 | |||
248 | /** |
||
249 | * Determine if the old input is empty. |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | 24 | public function oldInputIsEmpty() |
|
257 | |||
258 | /** |
||
259 | * Parse the form action method. |
||
260 | * |
||
261 | * @param string $method |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | 128 | private function getMethod($method) |
|
271 | |||
272 | /* ------------------------------------------------------------------------------------------------ |
||
273 | | Main Functions |
||
274 | | ------------------------------------------------------------------------------------------------ |
||
275 | */ |
||
276 | /** |
||
277 | * Open up a new HTML form. |
||
278 | * |
||
279 | * @param array $options |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | 128 | public function open(array $options = []) |
|
318 | |||
319 | /** |
||
320 | * Create a new model based form builder. |
||
321 | * |
||
322 | * @param mixed $model |
||
323 | * @param array $options |
||
324 | * |
||
325 | * @return \Illuminate\Support\HtmlString |
||
326 | */ |
||
327 | 40 | public function model($model, array $options = []) |
|
333 | |||
334 | /** |
||
335 | * Close the current form. |
||
336 | * |
||
337 | * @return \Illuminate\Support\HtmlString |
||
338 | */ |
||
339 | 16 | public function close() |
|
346 | |||
347 | /** |
||
348 | * Generate a hidden field with the current CSRF token. |
||
349 | * |
||
350 | * @return \Illuminate\Support\HtmlString |
||
351 | */ |
||
352 | 56 | public function token() |
|
360 | |||
361 | /** |
||
362 | * Create a form label element. |
||
363 | * |
||
364 | * @param string $name |
||
365 | * @param string $value |
||
366 | * @param array $options |
||
367 | * |
||
368 | * @return \Illuminate\Support\HtmlString |
||
369 | */ |
||
370 | 24 | public function label($name, $value = null, array $options = []) |
|
378 | |||
379 | /** |
||
380 | * Create a form input field. |
||
381 | * |
||
382 | * @param string $type |
||
383 | * @param string $name |
||
384 | * @param string $value |
||
385 | * @param array $options |
||
386 | * |
||
387 | * @return \Illuminate\Support\HtmlString |
||
388 | */ |
||
389 | 496 | public function input($type, $name, $value = null, array $options = []) |
|
413 | |||
414 | /** |
||
415 | * Create a text input field. |
||
416 | * |
||
417 | * @param string $name |
||
418 | * @param string $value |
||
419 | * @param array $options |
||
420 | * |
||
421 | * @return \Illuminate\Support\HtmlString |
||
422 | */ |
||
423 | 54 | public function text($name, $value = null, array $options = []) |
|
427 | |||
428 | /** |
||
429 | * Create a password input field. |
||
430 | * |
||
431 | * @param string $name |
||
432 | * @param array $options |
||
433 | * |
||
434 | * @return \Illuminate\Support\HtmlString |
||
435 | */ |
||
436 | 24 | public function password($name, array $options = []) |
|
440 | |||
441 | /** |
||
442 | * Create a hidden input field. |
||
443 | * |
||
444 | * @param string $name |
||
445 | * @param string $value |
||
446 | * @param array $options |
||
447 | * |
||
448 | * @return \Illuminate\Support\HtmlString |
||
449 | */ |
||
450 | 80 | public function hidden($name, $value = null, array $options = []) |
|
454 | |||
455 | /** |
||
456 | * Create an e-mail input field. |
||
457 | * |
||
458 | * @param string $name |
||
459 | * @param string $value |
||
460 | * @param array $options |
||
461 | * |
||
462 | * @return \Illuminate\Support\HtmlString |
||
463 | */ |
||
464 | 24 | public function email($name, $value = null, array $options = []) |
|
468 | |||
469 | /** |
||
470 | * Create a tel input field. |
||
471 | * |
||
472 | * @param string $name |
||
473 | * @param string $value |
||
474 | * @param array $options |
||
475 | * |
||
476 | * @return \Illuminate\Support\HtmlString |
||
477 | */ |
||
478 | 24 | public function tel($name, $value = null, array $options = []) |
|
482 | |||
483 | /** |
||
484 | * Create a number input field. |
||
485 | * |
||
486 | * @param string $name |
||
487 | * @param string $value |
||
488 | * @param array $options |
||
489 | * |
||
490 | * @return \Illuminate\Support\HtmlString |
||
491 | */ |
||
492 | 24 | public function number($name, $value = null, array $options = []) |
|
496 | |||
497 | /** |
||
498 | * Create a date input field. |
||
499 | * |
||
500 | * @param string $name |
||
501 | * @param string $value |
||
502 | * @param array $options |
||
503 | * |
||
504 | * @return \Illuminate\Support\HtmlString |
||
505 | */ |
||
506 | 32 | public function date($name, $value = null, array $options = []) |
|
514 | |||
515 | /** |
||
516 | * Create a datetime input field. |
||
517 | * |
||
518 | * @param string $name |
||
519 | * @param string $value |
||
520 | * @param array $options |
||
521 | * |
||
522 | * @return \Illuminate\Support\HtmlString |
||
523 | */ |
||
524 | 32 | public function datetime($name, $value = null, array $options = []) |
|
532 | |||
533 | /** |
||
534 | * Create a datetime-local input field. |
||
535 | * |
||
536 | * @param string $name |
||
537 | * @param string $value |
||
538 | * @param array $options |
||
539 | * |
||
540 | * @return \Illuminate\Support\HtmlString |
||
541 | */ |
||
542 | 32 | public function datetimeLocal($name, $value = null, array $options = []) |
|
550 | |||
551 | /** |
||
552 | * Create a time input field. |
||
553 | * |
||
554 | * @param string $name |
||
555 | * @param string $value |
||
556 | * @param array $options |
||
557 | * |
||
558 | * @return \Illuminate\Support\HtmlString |
||
559 | */ |
||
560 | 24 | public function time($name, $value = null, array $options = []) |
|
564 | |||
565 | /** |
||
566 | * Create a url input field. |
||
567 | * |
||
568 | * @param string $name |
||
569 | * @param string $value |
||
570 | * @param array $options |
||
571 | * |
||
572 | * @return \Illuminate\Support\HtmlString |
||
573 | */ |
||
574 | 16 | public function url($name, $value = null, array $options = []) |
|
578 | |||
579 | /** |
||
580 | * Create a file input field. |
||
581 | * |
||
582 | * @param string $name |
||
583 | * @param array $options |
||
584 | * |
||
585 | * @return \Illuminate\Support\HtmlString |
||
586 | */ |
||
587 | 24 | public function file($name, array $options = []) |
|
591 | |||
592 | /** |
||
593 | * Create a textarea input field. |
||
594 | * |
||
595 | * @param string $name |
||
596 | * @param string $value |
||
597 | * @param array $options |
||
598 | * |
||
599 | * @return \Illuminate\Support\HtmlString |
||
600 | */ |
||
601 | 32 | public function textarea($name, $value = null, array $options = []) |
|
625 | |||
626 | /** |
||
627 | * Set the text area size on the attributes. |
||
628 | * |
||
629 | * @param array $options |
||
630 | * |
||
631 | * @return array |
||
632 | */ |
||
633 | 32 | private function setTextAreaSize(array $options) |
|
647 | |||
648 | /** |
||
649 | * Set the text area size using the quick "size" attribute. |
||
650 | * |
||
651 | * @param array $options |
||
652 | * |
||
653 | * @return array |
||
654 | */ |
||
655 | 8 | protected function setQuickTextAreaSize(array $options) |
|
664 | |||
665 | /** |
||
666 | * Create a select box field. |
||
667 | * |
||
668 | * @param string $name |
||
669 | * @param array $list |
||
670 | * @param string $selected |
||
671 | * @param array $options |
||
672 | * |
||
673 | * @return \Illuminate\Support\HtmlString |
||
674 | */ |
||
675 | 104 | public function select($name, $list = [], $selected = null, array $options = []) |
|
716 | |||
717 | /** |
||
718 | * Create a select range field. |
||
719 | * |
||
720 | * @param string $name |
||
721 | * @param string $begin |
||
722 | * @param string $end |
||
723 | * @param string $selected |
||
724 | * @param array $options |
||
725 | * |
||
726 | * @return \Illuminate\Support\HtmlString |
||
727 | */ |
||
728 | 16 | public function selectRange($name, $begin, $end, $selected = null, array $options = []) |
|
734 | |||
735 | /** |
||
736 | * Create a select year field. |
||
737 | * |
||
738 | * @param string $name |
||
739 | * @param string $begin |
||
740 | * @param string $end |
||
741 | * @param string $selected |
||
742 | * @param array $options |
||
743 | * |
||
744 | * @return \Illuminate\Support\HtmlString |
||
745 | */ |
||
746 | 8 | public function selectYear($name, $begin, $end, $selected = null, array $options = []) |
|
753 | |||
754 | /** |
||
755 | * Create a select month field. |
||
756 | * |
||
757 | * @param string $name |
||
758 | * @param string $selected |
||
759 | * @param array $options |
||
760 | * @param string $format |
||
761 | * |
||
762 | * @return \Illuminate\Support\HtmlString |
||
763 | */ |
||
764 | 8 | public function selectMonth($name, $selected = null, array $options = [], $format = '%B') |
|
774 | |||
775 | /** |
||
776 | * Get the select option for the given value. |
||
777 | * |
||
778 | * @param string $display |
||
779 | * @param string $value |
||
780 | * @param string $selected |
||
781 | * |
||
782 | * @return string |
||
783 | */ |
||
784 | 96 | private function getSelectOption($display, $value, $selected) |
|
792 | |||
793 | /** |
||
794 | * Create an option group form element. |
||
795 | * |
||
796 | * @param array $list |
||
797 | * @param string $label |
||
798 | * @param string $selected |
||
799 | * |
||
800 | * @return string |
||
801 | */ |
||
802 | 8 | private function optionGroup(array $list, $label, $selected) |
|
812 | |||
813 | /** |
||
814 | * Create a select element option. |
||
815 | * |
||
816 | * @param string $display |
||
817 | * @param string $value |
||
818 | * @param string $selected |
||
819 | * |
||
820 | * @return string |
||
821 | */ |
||
822 | 96 | private function option($display, $value, $selected) |
|
829 | |||
830 | /** |
||
831 | * Create a placeholder select element option. |
||
832 | * |
||
833 | * @param string $display |
||
834 | * @param string $selected |
||
835 | * |
||
836 | * @return string |
||
837 | */ |
||
838 | 8 | private function placeholderOption($display, $selected) |
|
846 | |||
847 | /** |
||
848 | * Determine if the value is selected. |
||
849 | * |
||
850 | * @param string $value |
||
851 | * @param string $selected |
||
852 | * |
||
853 | * @return string|null |
||
854 | */ |
||
855 | 96 | private function getSelectedValue($value, $selected) |
|
863 | |||
864 | /** |
||
865 | * Create a checkbox input field. |
||
866 | * |
||
867 | * @param string $name |
||
868 | * @param mixed $value |
||
869 | * @param bool|null $checked |
||
870 | * @param array $options |
||
871 | * |
||
872 | * @return \Illuminate\Support\HtmlString |
||
873 | */ |
||
874 | 32 | public function checkbox($name, $value = 1, $checked = null, array $options = []) |
|
878 | |||
879 | /** |
||
880 | * Create a radio button input field. |
||
881 | * |
||
882 | * @param string $name |
||
883 | * @param mixed $value |
||
884 | * @param bool $checked |
||
885 | * @param array $options |
||
886 | * |
||
887 | * @return \Illuminate\Support\HtmlString |
||
888 | */ |
||
889 | 16 | public function radio($name, $value = null, $checked = null, array $options = []) |
|
897 | |||
898 | /** |
||
899 | * Create a checkable input field. |
||
900 | * |
||
901 | * @param string $type |
||
902 | * @param string $name |
||
903 | * @param mixed $value |
||
904 | * @param bool|null $checked |
||
905 | * @param array $options |
||
906 | * |
||
907 | * @return \Illuminate\Support\HtmlString |
||
908 | */ |
||
909 | 56 | protected function checkable($type, $name, $value, $checked, array $options) |
|
919 | |||
920 | /** |
||
921 | * Get the check state for a checkable input. |
||
922 | * |
||
923 | * @param string $type |
||
924 | * @param string $name |
||
925 | * @param mixed $value |
||
926 | * @param bool|null $checked |
||
927 | * |
||
928 | * @return bool |
||
929 | */ |
||
930 | 56 | private function getCheckedState($type, $name, $value, $checked) |
|
943 | |||
944 | /** |
||
945 | * Get the check state for a checkbox input. |
||
946 | * |
||
947 | * @param string $name |
||
948 | * @param mixed $value |
||
949 | * @param bool|null $checked |
||
950 | * |
||
951 | * @return bool |
||
952 | */ |
||
953 | 32 | private function getCheckboxCheckedState($name, $value, $checked) |
|
979 | |||
980 | /** |
||
981 | * Get the check state for a radio input. |
||
982 | * |
||
983 | * @param string $name |
||
984 | * @param mixed $value |
||
985 | * @param bool|null $checked |
||
986 | * |
||
987 | * @return bool |
||
988 | */ |
||
989 | 16 | private function getRadioCheckedState($name, $value, $checked) |
|
995 | |||
996 | /** |
||
997 | * Determine if old input or model input exists for a key. |
||
998 | * |
||
999 | * @param string $name |
||
1000 | * |
||
1001 | * @return bool |
||
1002 | */ |
||
1003 | 48 | private function missingOldAndModel($name) |
|
1007 | |||
1008 | /** |
||
1009 | * Create a HTML reset input element. |
||
1010 | * |
||
1011 | * @param string $value |
||
1012 | * @param array $attributes |
||
1013 | * |
||
1014 | * @return \Illuminate\Support\HtmlString |
||
1015 | */ |
||
1016 | 8 | public function reset($value, array $attributes = []) |
|
1020 | |||
1021 | /** |
||
1022 | * Create a HTML image input element. |
||
1023 | * |
||
1024 | * @param string $url |
||
1025 | * @param string $name |
||
1026 | * @param array $attributes |
||
1027 | * |
||
1028 | * @return \Illuminate\Support\HtmlString |
||
1029 | */ |
||
1030 | 8 | public function image($url, $name = null, array $attributes = []) |
|
1036 | |||
1037 | /** |
||
1038 | * Create a submit button element. |
||
1039 | * |
||
1040 | * @param string $value |
||
1041 | * @param array $options |
||
1042 | * |
||
1043 | * @return \Illuminate\Support\HtmlString |
||
1044 | */ |
||
1045 | 8 | public function submit($value = null, array $options = []) |
|
1049 | |||
1050 | /** |
||
1051 | * Create a button element. |
||
1052 | * |
||
1053 | * @param string $value |
||
1054 | * @param array $options |
||
1055 | * |
||
1056 | * @return \Illuminate\Support\HtmlString |
||
1057 | */ |
||
1058 | 8 | public function button($value = null, array $options = []) |
|
1068 | |||
1069 | /** |
||
1070 | * Create a color input field. |
||
1071 | * |
||
1072 | * @param string $name |
||
1073 | * @param string $value |
||
1074 | * @param array $options |
||
1075 | * |
||
1076 | * @return string |
||
1077 | */ |
||
1078 | 24 | public function color($name, $value = null, array $options = []) |
|
1082 | |||
1083 | /** |
||
1084 | * Dynamically handle calls to the class. |
||
1085 | * |
||
1086 | * @param string $method |
||
1087 | * @param array $parameters |
||
1088 | * |
||
1089 | * @return \Illuminate\Contracts\View\View|mixed |
||
1090 | * |
||
1091 | * @throws \BadMethodCallException |
||
1092 | */ |
||
1093 | public function __call($method, $parameters) |
||
1104 | |||
1105 | /* ------------------------------------------------------------------------------------------------ |
||
1106 | | Other Functions |
||
1107 | | ------------------------------------------------------------------------------------------------ |
||
1108 | */ |
||
1109 | /** |
||
1110 | * Get the form action from the options. |
||
1111 | * |
||
1112 | * @param array $options |
||
1113 | * |
||
1114 | * @return string |
||
1115 | */ |
||
1116 | 128 | private function getAction(array $options) |
|
1136 | |||
1137 | /** |
||
1138 | * Get the action for a "url" option. |
||
1139 | * |
||
1140 | * @param array|string $options |
||
1141 | * |
||
1142 | * @return string |
||
1143 | */ |
||
1144 | 40 | private function getUrlAction($options) |
|
1150 | |||
1151 | /** |
||
1152 | * Get the action for a "route" option. |
||
1153 | * |
||
1154 | * @param array|string $options |
||
1155 | * |
||
1156 | * @return string |
||
1157 | */ |
||
1158 | 8 | private function getRouteAction($options) |
|
1164 | |||
1165 | /** |
||
1166 | * Get the action for an "action" option. |
||
1167 | * |
||
1168 | * @param array|string $options |
||
1169 | * |
||
1170 | * @return string |
||
1171 | */ |
||
1172 | 8 | private function getControllerAction($options) |
|
1178 | |||
1179 | /** |
||
1180 | * Get the form appendage for the given method. |
||
1181 | * |
||
1182 | * @param string $method |
||
1183 | * |
||
1184 | * @return string |
||
1185 | */ |
||
1186 | 128 | private function getAppendage($method) |
|
1206 | |||
1207 | /** |
||
1208 | * Transform the string to an Html serializable object |
||
1209 | * |
||
1210 | * @param $html |
||
1211 | * |
||
1212 | * @return \Illuminate\Support\HtmlString |
||
1213 | */ |
||
1214 | 720 | protected function toHtmlString($html) |
|
1218 | } |
||
1219 |