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; |
||
16 | class FormBuilder implements FormBuilderInterface |
||
17 | { |
||
18 | /* ------------------------------------------------------------------------------------------------ |
||
19 | | Traits |
||
20 | | ------------------------------------------------------------------------------------------------ |
||
21 | */ |
||
22 | use Macroable; |
||
23 | |||
24 | /* ------------------------------------------------------------------------------------------------ |
||
25 | | Properties |
||
26 | | ------------------------------------------------------------------------------------------------ |
||
27 | */ |
||
28 | /** |
||
29 | * The HTML builder instance. |
||
30 | * |
||
31 | * @var \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface |
||
32 | */ |
||
33 | protected $html; |
||
34 | |||
35 | /** |
||
36 | * The URL generator instance. |
||
37 | * |
||
38 | * @var \Illuminate\Contracts\Routing\UrlGenerator |
||
39 | */ |
||
40 | protected $url; |
||
41 | |||
42 | /** |
||
43 | * The CSRF token used by the form builder. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $csrfToken; |
||
48 | |||
49 | /** |
||
50 | * The session store implementation. |
||
51 | * |
||
52 | * @var \Illuminate\Session\SessionInterface |
||
53 | */ |
||
54 | protected $session; |
||
55 | |||
56 | /** |
||
57 | * The current model instance for the form. |
||
58 | * |
||
59 | * @var mixed |
||
60 | */ |
||
61 | protected $model; |
||
62 | |||
63 | /** |
||
64 | * An array of label names we've created. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $labels = []; |
||
69 | |||
70 | /** |
||
71 | * The reserved form open attributes. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $reserved = ['method', 'url', 'route', 'action', 'files']; |
||
76 | |||
77 | /** |
||
78 | * The form methods that should be spoofed, in uppercase. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT']; |
||
83 | |||
84 | /** |
||
85 | * The types of inputs to not fill values on by default. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio']; |
||
90 | |||
91 | /* ------------------------------------------------------------------------------------------------ |
||
92 | | Constructor |
||
93 | | ------------------------------------------------------------------------------------------------ |
||
94 | */ |
||
95 | /** |
||
96 | * Create a new form builder instance. |
||
97 | * |
||
98 | * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
||
99 | * @param \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface $html |
||
100 | * @param string $csrfToken |
||
101 | */ |
||
102 | 276 | public function __construct( |
|
111 | |||
112 | /* ------------------------------------------------------------------------------------------------ |
||
113 | | Getters & Setters |
||
114 | | ------------------------------------------------------------------------------------------------ |
||
115 | */ |
||
116 | /** |
||
117 | * Get the session store implementation. |
||
118 | * |
||
119 | * @return \Illuminate\Session\SessionInterface |
||
120 | */ |
||
121 | 3 | public function getSessionStore() |
|
125 | |||
126 | /** |
||
127 | * Set the session store implementation. |
||
128 | * |
||
129 | * @param \Illuminate\Session\SessionInterface $session |
||
130 | * |
||
131 | * @return self |
||
132 | */ |
||
133 | 45 | public function setSessionStore(Session $session) |
|
139 | |||
140 | /** |
||
141 | * Set the model instance on the form builder. |
||
142 | * |
||
143 | * @param mixed $model |
||
144 | * |
||
145 | * @return self |
||
146 | */ |
||
147 | 21 | public function setModel($model) |
|
153 | |||
154 | /** |
||
155 | * Get the ID attribute for a field name. |
||
156 | * |
||
157 | * @param string $name |
||
158 | * @param array $attributes |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 237 | public function getIdAttribute($name, array $attributes) |
|
174 | |||
175 | /** |
||
176 | * Get the value that should be assigned to the field. |
||
177 | * |
||
178 | * @param string $name |
||
179 | * @param mixed $value |
||
180 | * |
||
181 | * @return mixed |
||
182 | */ |
||
183 | 210 | public function getValueAttribute($name, $value = null) |
|
203 | |||
204 | /** |
||
205 | * Get the model value that should be assigned to the field. |
||
206 | * |
||
207 | * @param string $name |
||
208 | * |
||
209 | * @return mixed |
||
210 | */ |
||
211 | 24 | private function getModelValueAttribute($name) |
|
215 | |||
216 | /** |
||
217 | * Get a value from the session's old input. |
||
218 | * |
||
219 | * @param string $name |
||
220 | * |
||
221 | * @return mixed |
||
222 | */ |
||
223 | 210 | public function old($name) |
|
229 | |||
230 | /** |
||
231 | * Transform key from array to dot syntax. |
||
232 | * |
||
233 | * @param string $key |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 36 | private function transformKey($key) |
|
241 | |||
242 | /** |
||
243 | * Determine if the old input is empty. |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | 9 | public function oldInputIsEmpty() |
|
251 | |||
252 | /** |
||
253 | * Parse the form action method. |
||
254 | * |
||
255 | * @param string $method |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | 48 | private function getMethod($method) |
|
265 | |||
266 | /* ------------------------------------------------------------------------------------------------ |
||
267 | | Main Functions |
||
268 | | ------------------------------------------------------------------------------------------------ |
||
269 | */ |
||
270 | /** |
||
271 | * Open up a new HTML form. |
||
272 | * |
||
273 | * @param array $options |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | 48 | public function open(array $options = []) |
|
312 | |||
313 | /** |
||
314 | * Create a new model based form builder. |
||
315 | * |
||
316 | * @param mixed $model |
||
317 | * @param array $options |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | 15 | public function model($model, array $options = []) |
|
327 | |||
328 | /** |
||
329 | * Close the current form. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | 6 | public function close() |
|
340 | |||
341 | /** |
||
342 | * Generate a hidden field with the current CSRF token. |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | 21 | public function token() |
|
354 | |||
355 | /** |
||
356 | * Create a form label element. |
||
357 | * |
||
358 | * @param string $name |
||
359 | * @param string $value |
||
360 | * @param array $options |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | 9 | public function label($name, $value = null, array $options = []) |
|
370 | |||
371 | /** |
||
372 | * Create a form input field. |
||
373 | * |
||
374 | * @param string $type |
||
375 | * @param string $name |
||
376 | * @param string $value |
||
377 | * @param array $options |
||
378 | * |
||
379 | * @return string |
||
380 | */ |
||
381 | 186 | public function input($type, $name, $value = null, array $options = []) |
|
403 | |||
404 | /** |
||
405 | * Create a text input field. |
||
406 | * |
||
407 | * @param string $name |
||
408 | * @param string $value |
||
409 | * @param array $options |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | 18 | public function text($name, $value = null, array $options = []) |
|
417 | |||
418 | /** |
||
419 | * Create a password input field. |
||
420 | * |
||
421 | * @param string $name |
||
422 | * @param array $options |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | 9 | public function password($name, array $options = []) |
|
430 | |||
431 | /** |
||
432 | * Create a hidden input field. |
||
433 | * |
||
434 | * @param string $name |
||
435 | * @param string $value |
||
436 | * @param array $options |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | 30 | public function hidden($name, $value = null, array $options = []) |
|
444 | |||
445 | /** |
||
446 | * Create an e-mail input field. |
||
447 | * |
||
448 | * @param string $name |
||
449 | * @param string $value |
||
450 | * @param array $options |
||
451 | * |
||
452 | * @return string |
||
453 | */ |
||
454 | 9 | public function email($name, $value = null, array $options = []) |
|
458 | |||
459 | /** |
||
460 | * Create a tel input field. |
||
461 | * |
||
462 | * @param string $name |
||
463 | * @param string $value |
||
464 | * @param array $options |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | 9 | public function tel($name, $value = null, array $options = []) |
|
472 | |||
473 | /** |
||
474 | * Create a number input field. |
||
475 | * |
||
476 | * @param string $name |
||
477 | * @param string $value |
||
478 | * @param array $options |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | 9 | public function number($name, $value = null, array $options = []) |
|
486 | |||
487 | /** |
||
488 | * Create a date input field. |
||
489 | * |
||
490 | * @param string $name |
||
491 | * @param string $value |
||
492 | * @param array $options |
||
493 | * |
||
494 | * @return string |
||
495 | */ |
||
496 | 12 | public function date($name, $value = null, array $options = []) |
|
504 | |||
505 | /** |
||
506 | * Create a datetime input field. |
||
507 | * |
||
508 | * @param string $name |
||
509 | * @param string $value |
||
510 | * @param array $options |
||
511 | * |
||
512 | * @return string |
||
513 | */ |
||
514 | 12 | public function datetime($name, $value = null, array $options = []) |
|
522 | |||
523 | /** |
||
524 | * Create a datetime-local input field. |
||
525 | * |
||
526 | * @param string $name |
||
527 | * @param string $value |
||
528 | * @param array $options |
||
529 | * |
||
530 | * @return string |
||
531 | */ |
||
532 | 12 | public function datetimeLocal($name, $value = null, array $options = []) |
|
540 | |||
541 | /** |
||
542 | * Create a time input field. |
||
543 | * |
||
544 | * @param string $name |
||
545 | * @param string $value |
||
546 | * @param array $options |
||
547 | * |
||
548 | * @return string |
||
549 | */ |
||
550 | 9 | public function time($name, $value = null, array $options = []) |
|
554 | |||
555 | /** |
||
556 | * Create a url input field. |
||
557 | * |
||
558 | * @param string $name |
||
559 | * @param string $value |
||
560 | * @param array $options |
||
561 | * |
||
562 | * @return string |
||
563 | */ |
||
564 | 6 | public function url($name, $value = null, array $options = []) |
|
568 | |||
569 | /** |
||
570 | * Create a file input field. |
||
571 | * |
||
572 | * @param string $name |
||
573 | * @param array $options |
||
574 | * |
||
575 | * @return string |
||
576 | */ |
||
577 | 9 | public function file($name, array $options = []) |
|
581 | |||
582 | /** |
||
583 | * Create a textarea input field. |
||
584 | * |
||
585 | * @param string $name |
||
586 | * @param string $value |
||
587 | * @param array $options |
||
588 | * |
||
589 | * @return string |
||
590 | */ |
||
591 | 12 | public function textarea($name, $value = null, array $options = []) |
|
613 | |||
614 | /** |
||
615 | * Set the text area size on the attributes. |
||
616 | * |
||
617 | * @param array $options |
||
618 | * |
||
619 | * @return array |
||
620 | */ |
||
621 | 12 | private function setTextAreaSize(array $options) |
|
635 | |||
636 | /** |
||
637 | * Set the text area size using the quick "size" attribute. |
||
638 | * |
||
639 | * @param array $options |
||
640 | * |
||
641 | * @return array |
||
642 | */ |
||
643 | 3 | protected function setQuickTextAreaSize(array $options) |
|
652 | |||
653 | /** |
||
654 | * Create a select box field. |
||
655 | * |
||
656 | * @param string $name |
||
657 | * @param array $list |
||
658 | * @param string $selected |
||
659 | * @param array $options |
||
660 | * |
||
661 | * @return string |
||
662 | */ |
||
663 | 39 | public function select($name, $list = [], $selected = null, array $options = []) |
|
702 | |||
703 | /** |
||
704 | * Create a select range field. |
||
705 | * |
||
706 | * @param string $name |
||
707 | * @param string $begin |
||
708 | * @param string $end |
||
709 | * @param string $selected |
||
710 | * @param array $options |
||
711 | * |
||
712 | * @return string |
||
713 | */ |
||
714 | 6 | public function selectRange($name, $begin, $end, $selected = null, array $options = []) |
|
720 | |||
721 | /** |
||
722 | * Create a select year field. |
||
723 | * |
||
724 | * @param string $name |
||
725 | * @param string $begin |
||
726 | * @param string $end |
||
727 | * @param string $selected |
||
728 | * @param array $options |
||
729 | * |
||
730 | * @return string |
||
731 | */ |
||
732 | 3 | public function selectYear($name, $begin, $end, $selected = null, array $options = []) |
|
739 | |||
740 | /** |
||
741 | * Create a select month field. |
||
742 | * |
||
743 | * @param string $name |
||
744 | * @param string $selected |
||
745 | * @param array $options |
||
746 | * @param string $format |
||
747 | * |
||
748 | * @return string |
||
749 | */ |
||
750 | 3 | public function selectMonth($name, $selected = null, array $options = [], $format = '%B') |
|
760 | |||
761 | /** |
||
762 | * Get the select option for the given value. |
||
763 | * |
||
764 | * @param string $display |
||
765 | * @param string $value |
||
766 | * @param string $selected |
||
767 | * |
||
768 | * @return string |
||
769 | */ |
||
770 | 36 | private function getSelectOption($display, $value, $selected) |
|
778 | |||
779 | /** |
||
780 | * Create an option group form element. |
||
781 | * |
||
782 | * @param array $list |
||
783 | * @param string $label |
||
784 | * @param string $selected |
||
785 | * |
||
786 | * @return string |
||
787 | */ |
||
788 | 3 | private function optionGroup(array $list, $label, $selected) |
|
798 | |||
799 | /** |
||
800 | * Create a select element option. |
||
801 | * |
||
802 | * @param string $display |
||
803 | * @param string $value |
||
804 | * @param string $selected |
||
805 | * |
||
806 | * @return string |
||
807 | */ |
||
808 | 36 | private function option($display, $value, $selected) |
|
815 | |||
816 | /** |
||
817 | * Create a placeholder select element option. |
||
818 | * |
||
819 | * @param string $display |
||
820 | * @param string $selected |
||
821 | * |
||
822 | * @return string |
||
823 | */ |
||
824 | 3 | private function placeholderOption($display, $selected) |
|
832 | |||
833 | /** |
||
834 | * Determine if the value is selected. |
||
835 | * |
||
836 | * @param string $value |
||
837 | * @param string $selected |
||
838 | * |
||
839 | * @return string|null |
||
840 | */ |
||
841 | 36 | private function getSelectedValue($value, $selected) |
|
849 | |||
850 | /** |
||
851 | * Create a checkbox input field. |
||
852 | * |
||
853 | * @param string $name |
||
854 | * @param mixed $value |
||
855 | * @param bool|null $checked |
||
856 | * @param array $options |
||
857 | * |
||
858 | * @return string |
||
859 | */ |
||
860 | 12 | public function checkbox($name, $value = 1, $checked = null, array $options = []) |
|
864 | |||
865 | /** |
||
866 | * Create a radio button input field. |
||
867 | * |
||
868 | * @param string $name |
||
869 | * @param mixed $value |
||
870 | * @param bool $checked |
||
871 | * @param array $options |
||
872 | * |
||
873 | * @return string |
||
874 | */ |
||
875 | 6 | public function radio($name, $value = null, $checked = null, array $options = []) |
|
883 | |||
884 | /** |
||
885 | * Create a checkable input field. |
||
886 | * |
||
887 | * @param string $type |
||
888 | * @param string $name |
||
889 | * @param mixed $value |
||
890 | * @param bool|null $checked |
||
891 | * @param array $options |
||
892 | * |
||
893 | * @return string |
||
894 | */ |
||
895 | 21 | protected function checkable($type, $name, $value, $checked, array $options) |
|
905 | |||
906 | /** |
||
907 | * Get the check state for a checkable input. |
||
908 | * |
||
909 | * @param string $type |
||
910 | * @param string $name |
||
911 | * @param mixed $value |
||
912 | * @param bool|null $checked |
||
913 | * |
||
914 | * @return bool |
||
915 | */ |
||
916 | 21 | private function getCheckedState($type, $name, $value, $checked) |
|
929 | |||
930 | /** |
||
931 | * Get the check state for a checkbox input. |
||
932 | * |
||
933 | * @param string $name |
||
934 | * @param mixed $value |
||
935 | * @param bool|null $checked |
||
936 | * |
||
937 | * @return bool |
||
938 | */ |
||
939 | 12 | private function getCheckboxCheckedState($name, $value, $checked) |
|
965 | |||
966 | /** |
||
967 | * Get the check state for a radio input. |
||
968 | * |
||
969 | * @param string $name |
||
970 | * @param mixed $value |
||
971 | * @param bool|null $checked |
||
972 | * |
||
973 | * @return bool |
||
974 | */ |
||
975 | 6 | private function getRadioCheckedState($name, $value, $checked) |
|
981 | |||
982 | /** |
||
983 | * Determine if old input or model input exists for a key. |
||
984 | * |
||
985 | * @param string $name |
||
986 | * |
||
987 | * @return bool |
||
988 | */ |
||
989 | 18 | private function missingOldAndModel($name) |
|
993 | |||
994 | /** |
||
995 | * Create a HTML reset input element. |
||
996 | * |
||
997 | * @param string $value |
||
998 | * @param array $attributes |
||
999 | * |
||
1000 | * @return string |
||
1001 | */ |
||
1002 | 3 | public function reset($value, array $attributes = []) |
|
1006 | |||
1007 | /** |
||
1008 | * Create a HTML image input element. |
||
1009 | * |
||
1010 | * @param string $url |
||
1011 | * @param string $name |
||
1012 | * @param array $attributes |
||
1013 | * |
||
1014 | * @return string |
||
1015 | */ |
||
1016 | 3 | public function image($url, $name = null, array $attributes = []) |
|
1022 | |||
1023 | /** |
||
1024 | * Create a submit button element. |
||
1025 | * |
||
1026 | * @param string $value |
||
1027 | * @param array $options |
||
1028 | * |
||
1029 | * @return string |
||
1030 | */ |
||
1031 | 3 | public function submit($value = null, array $options = []) |
|
1035 | |||
1036 | /** |
||
1037 | * Create a button element. |
||
1038 | * |
||
1039 | * @param string $value |
||
1040 | * @param array $options |
||
1041 | * |
||
1042 | * @return string |
||
1043 | */ |
||
1044 | 3 | public function button($value = null, array $options = []) |
|
1052 | |||
1053 | /** |
||
1054 | * Create a color input field. |
||
1055 | * |
||
1056 | * @param string $name |
||
1057 | * @param string $value |
||
1058 | * @param array $options |
||
1059 | * |
||
1060 | * @return string |
||
1061 | */ |
||
1062 | 9 | public function color($name, $value = null, array $options = []) |
|
1066 | |||
1067 | /* ------------------------------------------------------------------------------------------------ |
||
1068 | | Other Functions |
||
1069 | | ------------------------------------------------------------------------------------------------ |
||
1070 | */ |
||
1071 | /** |
||
1072 | * Get the form action from the options. |
||
1073 | * |
||
1074 | * @param array $options |
||
1075 | * |
||
1076 | * @return string |
||
1077 | */ |
||
1078 | 48 | private function getAction(array $options) |
|
1098 | |||
1099 | /** |
||
1100 | * Get the action for a "url" option. |
||
1101 | * |
||
1102 | * @param array|string $options |
||
1103 | * |
||
1104 | * @return string |
||
1105 | */ |
||
1106 | 15 | private function getUrlAction($options) |
|
1112 | |||
1113 | /** |
||
1114 | * Get the action for a "route" option. |
||
1115 | * |
||
1116 | * @param array|string $options |
||
1117 | * |
||
1118 | * @return string |
||
1119 | */ |
||
1120 | 3 | private function getRouteAction($options) |
|
1126 | |||
1127 | /** |
||
1128 | * Get the action for an "action" option. |
||
1129 | * |
||
1130 | * @param array|string $options |
||
1131 | * |
||
1132 | * @return string |
||
1133 | */ |
||
1134 | 3 | private function getControllerAction($options) |
|
1140 | |||
1141 | /** |
||
1142 | * Get the form appendage for the given method. |
||
1143 | * |
||
1144 | * @param string $method |
||
1145 | * |
||
1146 | * @return string |
||
1147 | */ |
||
1148 | 48 | private function getAppendage($method) |
|
1168 | } |
||
1169 |