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; |
||
18 | class FormBuilder implements FormBuilderInterface |
||
19 | { |
||
20 | /* ------------------------------------------------------------------------------------------------ |
||
21 | | Traits |
||
22 | | ------------------------------------------------------------------------------------------------ |
||
23 | */ |
||
24 | use Macroable, Componentable { |
||
25 | Macroable::__call as macroCall; |
||
26 | Componentable::__call as componentCall; |
||
27 | } |
||
28 | |||
29 | /* ------------------------------------------------------------------------------------------------ |
||
30 | | Properties |
||
31 | | ------------------------------------------------------------------------------------------------ |
||
32 | */ |
||
33 | /** |
||
34 | * The HTML builder instance. |
||
35 | * |
||
36 | * @var \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface |
||
37 | */ |
||
38 | protected $html; |
||
39 | |||
40 | /** |
||
41 | * The URL generator instance. |
||
42 | * |
||
43 | * @var \Illuminate\Contracts\Routing\UrlGenerator |
||
44 | */ |
||
45 | protected $url; |
||
46 | |||
47 | /** |
||
48 | * The CSRF token used by the form builder. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $csrfToken; |
||
53 | |||
54 | /** |
||
55 | * The session store implementation. |
||
56 | * |
||
57 | * @var \Illuminate\Session\SessionInterface |
||
58 | */ |
||
59 | protected $session; |
||
60 | |||
61 | /** |
||
62 | * The current model instance for the form. |
||
63 | * |
||
64 | * @var mixed |
||
65 | */ |
||
66 | protected $model; |
||
67 | |||
68 | /** |
||
69 | * An array of label names we've created. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $labels = []; |
||
74 | |||
75 | /** |
||
76 | * The reserved form open attributes. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $reserved = ['method', 'url', 'route', 'action', 'files']; |
||
81 | |||
82 | /** |
||
83 | * The form methods that should be spoofed, in uppercase. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT']; |
||
88 | |||
89 | /** |
||
90 | * The types of inputs to not fill values on by default. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio']; |
||
95 | |||
96 | /* ------------------------------------------------------------------------------------------------ |
||
97 | | Constructor |
||
98 | | ------------------------------------------------------------------------------------------------ |
||
99 | */ |
||
100 | /** |
||
101 | * Create a new form builder instance. |
||
102 | * |
||
103 | * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
||
104 | * @param \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface $html |
||
105 | * @param string $csrfToken |
||
106 | */ |
||
107 | 760 | public function __construct( |
|
116 | |||
117 | /* ------------------------------------------------------------------------------------------------ |
||
118 | | Getters & Setters |
||
119 | | ------------------------------------------------------------------------------------------------ |
||
120 | */ |
||
121 | /** |
||
122 | * Get the session store implementation. |
||
123 | * |
||
124 | * @return \Illuminate\Session\SessionInterface |
||
125 | */ |
||
126 | 8 | public function getSessionStore() |
|
130 | |||
131 | /** |
||
132 | * Set the session store implementation. |
||
133 | * |
||
134 | * @param \Illuminate\Session\SessionInterface $session |
||
135 | * |
||
136 | * @return self |
||
137 | */ |
||
138 | 120 | public function setSessionStore(Session $session) |
|
144 | |||
145 | /** |
||
146 | * Set the model instance on the form builder. |
||
147 | * |
||
148 | * @param mixed $model |
||
149 | * |
||
150 | * @return self |
||
151 | */ |
||
152 | 80 | public function setModel($model) |
|
158 | |||
159 | /** |
||
160 | * Get the ID attribute for a field name. |
||
161 | * |
||
162 | * @param string $name |
||
163 | * @param array $attributes |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | 632 | public function getIdAttribute($name, array $attributes) |
|
179 | |||
180 | /** |
||
181 | * Get the value that should be assigned to the field. |
||
182 | * |
||
183 | * @param string $name |
||
184 | * @param mixed $value |
||
185 | * |
||
186 | * @return mixed |
||
187 | */ |
||
188 | 560 | public function getValueAttribute($name, $value = null) |
|
208 | |||
209 | /** |
||
210 | * Get the model value that should be assigned to the field. |
||
211 | * |
||
212 | * @param string $name |
||
213 | * |
||
214 | * @return mixed |
||
215 | */ |
||
216 | 64 | private function getModelValueAttribute($name) |
|
220 | |||
221 | /** |
||
222 | * Get a value from the session's old input. |
||
223 | * |
||
224 | * @param string $name |
||
225 | * |
||
226 | * @return mixed |
||
227 | */ |
||
228 | 560 | public function old($name) |
|
234 | |||
235 | /** |
||
236 | * Transform key from array to dot syntax. |
||
237 | * |
||
238 | * @param string $key |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | 96 | private function transformKey($key) |
|
246 | |||
247 | /** |
||
248 | * Determine if the old input is empty. |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | 24 | public function oldInputIsEmpty() |
|
256 | |||
257 | /** |
||
258 | * Parse the form action method. |
||
259 | * |
||
260 | * @param string $method |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | 128 | private function getMethod($method) |
|
270 | |||
271 | /* ------------------------------------------------------------------------------------------------ |
||
272 | | Main Functions |
||
273 | | ------------------------------------------------------------------------------------------------ |
||
274 | */ |
||
275 | /** |
||
276 | * Open up a new HTML form. |
||
277 | * |
||
278 | * @param array $options |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | 128 | public function open(array $options = []) |
|
317 | |||
318 | /** |
||
319 | * Create a new model based form builder. |
||
320 | * |
||
321 | * @param mixed $model |
||
322 | * @param array $options |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | 40 | public function model($model, array $options = []) |
|
332 | |||
333 | /** |
||
334 | * Close the current form. |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | 16 | public function close() |
|
345 | |||
346 | /** |
||
347 | * Generate a hidden field with the current CSRF token. |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | 56 | public function token() |
|
359 | |||
360 | /** |
||
361 | * Create a form label element. |
||
362 | * |
||
363 | * @param string $name |
||
364 | * @param string $value |
||
365 | * @param array $options |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | 24 | public function label($name, $value = null, array $options = []) |
|
375 | |||
376 | /** |
||
377 | * Create a form input field. |
||
378 | * |
||
379 | * @param string $type |
||
380 | * @param string $name |
||
381 | * @param string $value |
||
382 | * @param array $options |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | 496 | public function input($type, $name, $value = null, array $options = []) |
|
408 | |||
409 | /** |
||
410 | * Create a text input field. |
||
411 | * |
||
412 | * @param string $name |
||
413 | * @param string $value |
||
414 | * @param array $options |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | 48 | public function text($name, $value = null, array $options = []) |
|
422 | |||
423 | /** |
||
424 | * Create a password input field. |
||
425 | * |
||
426 | * @param string $name |
||
427 | * @param array $options |
||
428 | * |
||
429 | * @return string |
||
430 | */ |
||
431 | 24 | public function password($name, array $options = []) |
|
435 | |||
436 | /** |
||
437 | * Create a hidden input field. |
||
438 | * |
||
439 | * @param string $name |
||
440 | * @param string $value |
||
441 | * @param array $options |
||
442 | * |
||
443 | * @return string |
||
444 | */ |
||
445 | 80 | public function hidden($name, $value = null, array $options = []) |
|
449 | |||
450 | /** |
||
451 | * Create an e-mail input field. |
||
452 | * |
||
453 | * @param string $name |
||
454 | * @param string $value |
||
455 | * @param array $options |
||
456 | * |
||
457 | * @return string |
||
458 | */ |
||
459 | 24 | public function email($name, $value = null, array $options = []) |
|
463 | |||
464 | /** |
||
465 | * Create a tel input field. |
||
466 | * |
||
467 | * @param string $name |
||
468 | * @param string $value |
||
469 | * @param array $options |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | 24 | public function tel($name, $value = null, array $options = []) |
|
477 | |||
478 | /** |
||
479 | * Create a number input field. |
||
480 | * |
||
481 | * @param string $name |
||
482 | * @param string $value |
||
483 | * @param array $options |
||
484 | * |
||
485 | * @return string |
||
486 | */ |
||
487 | 24 | public function number($name, $value = null, array $options = []) |
|
491 | |||
492 | /** |
||
493 | * Create a date input field. |
||
494 | * |
||
495 | * @param string $name |
||
496 | * @param string $value |
||
497 | * @param array $options |
||
498 | * |
||
499 | * @return string |
||
500 | */ |
||
501 | 32 | public function date($name, $value = null, array $options = []) |
|
509 | |||
510 | /** |
||
511 | * Create a datetime input field. |
||
512 | * |
||
513 | * @param string $name |
||
514 | * @param string $value |
||
515 | * @param array $options |
||
516 | * |
||
517 | * @return string |
||
518 | */ |
||
519 | 32 | public function datetime($name, $value = null, array $options = []) |
|
527 | |||
528 | /** |
||
529 | * Create a datetime-local input field. |
||
530 | * |
||
531 | * @param string $name |
||
532 | * @param string $value |
||
533 | * @param array $options |
||
534 | * |
||
535 | * @return string |
||
536 | */ |
||
537 | 32 | public function datetimeLocal($name, $value = null, array $options = []) |
|
545 | |||
546 | /** |
||
547 | * Create a time input field. |
||
548 | * |
||
549 | * @param string $name |
||
550 | * @param string $value |
||
551 | * @param array $options |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | 24 | public function time($name, $value = null, array $options = []) |
|
559 | |||
560 | /** |
||
561 | * Create a url input field. |
||
562 | * |
||
563 | * @param string $name |
||
564 | * @param string $value |
||
565 | * @param array $options |
||
566 | * |
||
567 | * @return string |
||
568 | */ |
||
569 | 16 | public function url($name, $value = null, array $options = []) |
|
573 | |||
574 | /** |
||
575 | * Create a file input field. |
||
576 | * |
||
577 | * @param string $name |
||
578 | * @param array $options |
||
579 | * |
||
580 | * @return string |
||
581 | */ |
||
582 | 24 | public function file($name, array $options = []) |
|
586 | |||
587 | /** |
||
588 | * Create a textarea input field. |
||
589 | * |
||
590 | * @param string $name |
||
591 | * @param string $value |
||
592 | * @param array $options |
||
593 | * |
||
594 | * @return string |
||
595 | */ |
||
596 | 32 | public function textarea($name, $value = null, array $options = []) |
|
618 | |||
619 | /** |
||
620 | * Set the text area size on the attributes. |
||
621 | * |
||
622 | * @param array $options |
||
623 | * |
||
624 | * @return array |
||
625 | */ |
||
626 | 32 | private function setTextAreaSize(array $options) |
|
640 | |||
641 | /** |
||
642 | * Set the text area size using the quick "size" attribute. |
||
643 | * |
||
644 | * @param array $options |
||
645 | * |
||
646 | * @return array |
||
647 | */ |
||
648 | 8 | protected function setQuickTextAreaSize(array $options) |
|
657 | |||
658 | /** |
||
659 | * Create a select box field. |
||
660 | * |
||
661 | * @param string $name |
||
662 | * @param array $list |
||
663 | * @param string $selected |
||
664 | * @param array $options |
||
665 | * |
||
666 | * @return string |
||
667 | */ |
||
668 | 104 | public function select($name, $list = [], $selected = null, array $options = []) |
|
707 | |||
708 | /** |
||
709 | * Create a select range field. |
||
710 | * |
||
711 | * @param string $name |
||
712 | * @param string $begin |
||
713 | * @param string $end |
||
714 | * @param string $selected |
||
715 | * @param array $options |
||
716 | * |
||
717 | * @return string |
||
718 | */ |
||
719 | 16 | public function selectRange($name, $begin, $end, $selected = null, array $options = []) |
|
725 | |||
726 | /** |
||
727 | * Create a select year field. |
||
728 | * |
||
729 | * @param string $name |
||
730 | * @param string $begin |
||
731 | * @param string $end |
||
732 | * @param string $selected |
||
733 | * @param array $options |
||
734 | * |
||
735 | * @return string |
||
736 | */ |
||
737 | 8 | public function selectYear($name, $begin, $end, $selected = null, array $options = []) |
|
744 | |||
745 | /** |
||
746 | * Create a select month field. |
||
747 | * |
||
748 | * @param string $name |
||
749 | * @param string $selected |
||
750 | * @param array $options |
||
751 | * @param string $format |
||
752 | * |
||
753 | * @return string |
||
754 | */ |
||
755 | 8 | public function selectMonth($name, $selected = null, array $options = [], $format = '%B') |
|
765 | |||
766 | /** |
||
767 | * Get the select option for the given value. |
||
768 | * |
||
769 | * @param string $display |
||
770 | * @param string $value |
||
771 | * @param string $selected |
||
772 | * |
||
773 | * @return string |
||
774 | */ |
||
775 | 96 | private function getSelectOption($display, $value, $selected) |
|
783 | |||
784 | /** |
||
785 | * Create an option group form element. |
||
786 | * |
||
787 | * @param array $list |
||
788 | * @param string $label |
||
789 | * @param string $selected |
||
790 | * |
||
791 | * @return string |
||
792 | */ |
||
793 | 8 | private function optionGroup(array $list, $label, $selected) |
|
803 | |||
804 | /** |
||
805 | * Create a select element option. |
||
806 | * |
||
807 | * @param string $display |
||
808 | * @param string $value |
||
809 | * @param string $selected |
||
810 | * |
||
811 | * @return string |
||
812 | */ |
||
813 | 96 | private function option($display, $value, $selected) |
|
820 | |||
821 | /** |
||
822 | * Create a placeholder select element option. |
||
823 | * |
||
824 | * @param string $display |
||
825 | * @param string $selected |
||
826 | * |
||
827 | * @return string |
||
828 | */ |
||
829 | 8 | private function placeholderOption($display, $selected) |
|
837 | |||
838 | /** |
||
839 | * Determine if the value is selected. |
||
840 | * |
||
841 | * @param string $value |
||
842 | * @param string $selected |
||
843 | * |
||
844 | * @return string|null |
||
845 | */ |
||
846 | 96 | private function getSelectedValue($value, $selected) |
|
854 | |||
855 | /** |
||
856 | * Create a checkbox input field. |
||
857 | * |
||
858 | * @param string $name |
||
859 | * @param mixed $value |
||
860 | * @param bool|null $checked |
||
861 | * @param array $options |
||
862 | * |
||
863 | * @return string |
||
864 | */ |
||
865 | 32 | public function checkbox($name, $value = 1, $checked = null, array $options = []) |
|
869 | |||
870 | /** |
||
871 | * Create a radio button input field. |
||
872 | * |
||
873 | * @param string $name |
||
874 | * @param mixed $value |
||
875 | * @param bool $checked |
||
876 | * @param array $options |
||
877 | * |
||
878 | * @return string |
||
879 | */ |
||
880 | 16 | public function radio($name, $value = null, $checked = null, array $options = []) |
|
888 | |||
889 | /** |
||
890 | * Create a checkable input field. |
||
891 | * |
||
892 | * @param string $type |
||
893 | * @param string $name |
||
894 | * @param mixed $value |
||
895 | * @param bool|null $checked |
||
896 | * @param array $options |
||
897 | * |
||
898 | * @return string |
||
899 | */ |
||
900 | 56 | protected function checkable($type, $name, $value, $checked, array $options) |
|
910 | |||
911 | /** |
||
912 | * Get the check state for a checkable input. |
||
913 | * |
||
914 | * @param string $type |
||
915 | * @param string $name |
||
916 | * @param mixed $value |
||
917 | * @param bool|null $checked |
||
918 | * |
||
919 | * @return bool |
||
920 | */ |
||
921 | 56 | private function getCheckedState($type, $name, $value, $checked) |
|
934 | |||
935 | /** |
||
936 | * Get the check state for a checkbox input. |
||
937 | * |
||
938 | * @param string $name |
||
939 | * @param mixed $value |
||
940 | * @param bool|null $checked |
||
941 | * |
||
942 | * @return bool |
||
943 | */ |
||
944 | 32 | private function getCheckboxCheckedState($name, $value, $checked) |
|
970 | |||
971 | /** |
||
972 | * Get the check state for a radio input. |
||
973 | * |
||
974 | * @param string $name |
||
975 | * @param mixed $value |
||
976 | * @param bool|null $checked |
||
977 | * |
||
978 | * @return bool |
||
979 | */ |
||
980 | 16 | private function getRadioCheckedState($name, $value, $checked) |
|
986 | |||
987 | /** |
||
988 | * Determine if old input or model input exists for a key. |
||
989 | * |
||
990 | * @param string $name |
||
991 | * |
||
992 | * @return bool |
||
993 | */ |
||
994 | 48 | private function missingOldAndModel($name) |
|
998 | |||
999 | /** |
||
1000 | * Create a HTML reset input element. |
||
1001 | * |
||
1002 | * @param string $value |
||
1003 | * @param array $attributes |
||
1004 | * |
||
1005 | * @return string |
||
1006 | */ |
||
1007 | 8 | public function reset($value, array $attributes = []) |
|
1011 | |||
1012 | /** |
||
1013 | * Create a HTML image input element. |
||
1014 | * |
||
1015 | * @param string $url |
||
1016 | * @param string $name |
||
1017 | * @param array $attributes |
||
1018 | * |
||
1019 | * @return string |
||
1020 | */ |
||
1021 | 8 | public function image($url, $name = null, array $attributes = []) |
|
1027 | |||
1028 | /** |
||
1029 | * Create a submit button element. |
||
1030 | * |
||
1031 | * @param string $value |
||
1032 | * @param array $options |
||
1033 | * |
||
1034 | * @return string |
||
1035 | */ |
||
1036 | 8 | public function submit($value = null, array $options = []) |
|
1040 | |||
1041 | /** |
||
1042 | * Create a button element. |
||
1043 | * |
||
1044 | * @param string $value |
||
1045 | * @param array $options |
||
1046 | * |
||
1047 | * @return string |
||
1048 | */ |
||
1049 | 8 | public function button($value = null, array $options = []) |
|
1057 | |||
1058 | /** |
||
1059 | * Create a color input field. |
||
1060 | * |
||
1061 | * @param string $name |
||
1062 | * @param string $value |
||
1063 | * @param array $options |
||
1064 | * |
||
1065 | * @return string |
||
1066 | */ |
||
1067 | 24 | public function color($name, $value = null, array $options = []) |
|
1071 | |||
1072 | /** |
||
1073 | * Dynamically handle calls to the class. |
||
1074 | * |
||
1075 | * @param string $method |
||
1076 | * @param array $parameters |
||
1077 | * |
||
1078 | * @return \Illuminate\Contracts\View\View|mixed |
||
1079 | * |
||
1080 | * @throws \BadMethodCallException |
||
1081 | */ |
||
1082 | public function __call($method, $parameters) |
||
1093 | |||
1094 | /* ------------------------------------------------------------------------------------------------ |
||
1095 | | Other Functions |
||
1096 | | ------------------------------------------------------------------------------------------------ |
||
1097 | */ |
||
1098 | /** |
||
1099 | * Get the form action from the options. |
||
1100 | * |
||
1101 | * @param array $options |
||
1102 | * |
||
1103 | * @return string |
||
1104 | */ |
||
1105 | 128 | private function getAction(array $options) |
|
1125 | |||
1126 | /** |
||
1127 | * Get the action for a "url" option. |
||
1128 | * |
||
1129 | * @param array|string $options |
||
1130 | * |
||
1131 | * @return string |
||
1132 | */ |
||
1133 | 40 | private function getUrlAction($options) |
|
1139 | |||
1140 | /** |
||
1141 | * Get the action for a "route" option. |
||
1142 | * |
||
1143 | * @param array|string $options |
||
1144 | * |
||
1145 | * @return string |
||
1146 | */ |
||
1147 | 8 | private function getRouteAction($options) |
|
1153 | |||
1154 | /** |
||
1155 | * Get the action for an "action" option. |
||
1156 | * |
||
1157 | * @param array|string $options |
||
1158 | * |
||
1159 | * @return string |
||
1160 | */ |
||
1161 | 8 | private function getControllerAction($options) |
|
1167 | |||
1168 | /** |
||
1169 | * Get the form appendage for the given method. |
||
1170 | * |
||
1171 | * @param string $method |
||
1172 | * |
||
1173 | * @return string |
||
1174 | */ |
||
1175 | 128 | private function getAppendage($method) |
|
1195 | } |
||
1196 |