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 extends Builder implements FormBuilderInterface |
||
17 | { |
||
18 | /* ------------------------------------------------------------------------------------------------ |
||
19 | | Properties |
||
20 | | ------------------------------------------------------------------------------------------------ |
||
21 | */ |
||
22 | /** |
||
23 | * The HTML builder instance. |
||
24 | * |
||
25 | * @var \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface |
||
26 | */ |
||
27 | protected $html; |
||
28 | |||
29 | /** |
||
30 | * The URL generator instance. |
||
31 | * |
||
32 | * @var \Illuminate\Contracts\Routing\UrlGenerator |
||
33 | */ |
||
34 | protected $url; |
||
35 | |||
36 | /** |
||
37 | * The CSRF token used by the form builder. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $csrfToken; |
||
42 | |||
43 | /** |
||
44 | * The session store implementation. |
||
45 | * |
||
46 | * @var \Illuminate\Session\SessionInterface |
||
47 | */ |
||
48 | protected $session; |
||
49 | |||
50 | /** |
||
51 | * The current model instance for the form. |
||
52 | * |
||
53 | * @var \Illuminate\Database\Eloquent\Model |
||
54 | */ |
||
55 | protected $model; |
||
56 | |||
57 | /** |
||
58 | * An array of label names we've created. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $labels = []; |
||
63 | |||
64 | /** |
||
65 | * The reserved form open attributes. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $reserved = ['method', 'url', 'route', 'action', 'files']; |
||
70 | |||
71 | /** |
||
72 | * The form methods that should be spoofed, in uppercase. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT']; |
||
77 | |||
78 | /** |
||
79 | * The types of inputs to not fill values on by default. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio']; |
||
84 | |||
85 | /* ------------------------------------------------------------------------------------------------ |
||
86 | | Constructor |
||
87 | | ------------------------------------------------------------------------------------------------ |
||
88 | */ |
||
89 | /** |
||
90 | * Create a new form builder instance. |
||
91 | * |
||
92 | * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
||
93 | * @param \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface $html |
||
94 | * @param string $csrfToken |
||
95 | */ |
||
96 | 784 | public function __construct( |
|
97 | Contracts\HtmlBuilderInterface $html, |
||
98 | UrlGenerator $url, |
||
99 | $csrfToken |
||
100 | ) { |
||
101 | 784 | $this->url = $url; |
|
102 | 784 | $this->html = $html; |
|
103 | 784 | $this->csrfToken = $csrfToken; |
|
104 | 784 | } |
|
105 | |||
106 | /* ------------------------------------------------------------------------------------------------ |
||
107 | | Getters & Setters |
||
108 | | ------------------------------------------------------------------------------------------------ |
||
109 | */ |
||
110 | /** |
||
111 | * Get the session store implementation. |
||
112 | * |
||
113 | * @return \Illuminate\Session\SessionInterface |
||
114 | */ |
||
115 | 8 | public function getSessionStore() |
|
119 | |||
120 | /** |
||
121 | * Set the session store implementation. |
||
122 | * |
||
123 | * @param \Illuminate\Session\SessionInterface $session |
||
124 | * |
||
125 | * @return self |
||
126 | */ |
||
127 | 128 | public function setSessionStore(Session $session) |
|
133 | |||
134 | /** |
||
135 | * Set the model instance on the form builder. |
||
136 | * |
||
137 | * @param \Illuminate\Database\Eloquent\Model $model |
||
138 | * |
||
139 | * @return self |
||
140 | */ |
||
141 | 80 | public function setModel($model) |
|
147 | |||
148 | /** |
||
149 | * Get the ID attribute for a field name. |
||
150 | * |
||
151 | * @param string $name |
||
152 | * @param array $attributes |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | 648 | public function getIdAttribute($name, array $attributes) |
|
168 | |||
169 | /** |
||
170 | * Get the value that should be assigned to the field. |
||
171 | * |
||
172 | * @param string $name |
||
173 | * @param mixed $value |
||
174 | * |
||
175 | * @return mixed |
||
176 | */ |
||
177 | 576 | public function getValueAttribute($name, $value = null) |
|
195 | |||
196 | /** |
||
197 | * Get the model value that should be assigned to the field. |
||
198 | * |
||
199 | * @param string $name |
||
200 | * |
||
201 | * @return mixed |
||
202 | */ |
||
203 | 64 | private function getModelValueAttribute($name) |
|
207 | |||
208 | /** |
||
209 | * Get a value from the session's old input. |
||
210 | * |
||
211 | * @param string $name |
||
212 | * |
||
213 | * @return mixed |
||
214 | */ |
||
215 | 576 | public function old($name) |
|
221 | |||
222 | /** |
||
223 | * Transform key from array to dot syntax. |
||
224 | * |
||
225 | * @param string $key |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 104 | private function transformKey($key) |
|
233 | |||
234 | /** |
||
235 | * Determine if the old input is empty. |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | 24 | public function oldInputIsEmpty() |
|
243 | |||
244 | /** |
||
245 | * Parse the form action method. |
||
246 | * |
||
247 | * @param string $method |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 128 | private function getMethod($method) |
|
257 | |||
258 | /* ------------------------------------------------------------------------------------------------ |
||
259 | | Main Functions |
||
260 | | ------------------------------------------------------------------------------------------------ |
||
261 | */ |
||
262 | /** |
||
263 | * Open up a new HTML form. |
||
264 | * |
||
265 | * @param array $options |
||
266 | * |
||
267 | * @return \Illuminate\Support\HtmlString |
||
268 | */ |
||
269 | 128 | public function open(array $options = []) |
|
304 | |||
305 | /** |
||
306 | * Create a new model based form builder. |
||
307 | * |
||
308 | * @param mixed $model |
||
309 | * @param array $options |
||
310 | * |
||
311 | * @return \Illuminate\Support\HtmlString |
||
312 | */ |
||
313 | 40 | public function model($model, array $options = []) |
|
319 | |||
320 | /** |
||
321 | * Close the current form. |
||
322 | * |
||
323 | * @return \Illuminate\Support\HtmlString |
||
324 | */ |
||
325 | 16 | public function close() |
|
332 | |||
333 | /** |
||
334 | * Generate a hidden field with the current CSRF token. |
||
335 | * |
||
336 | * @return \Illuminate\Support\HtmlString |
||
337 | */ |
||
338 | 56 | public function token() |
|
346 | |||
347 | /** |
||
348 | * Create a form label element. |
||
349 | * |
||
350 | * @param string $name |
||
351 | * @param string $value |
||
352 | * @param array $options |
||
353 | * @param bool $escaped |
||
354 | * |
||
355 | * @return \Illuminate\Support\HtmlString |
||
356 | */ |
||
357 | 46 | public function label($name, $value = null, array $options = [], $escaped = true) |
|
365 | |||
366 | /** |
||
367 | * Create a form input field. |
||
368 | * |
||
369 | * @param string $type |
||
370 | * @param string $name |
||
371 | * @param string $value |
||
372 | * @param array $options |
||
373 | * |
||
374 | * @return \Illuminate\Support\HtmlString |
||
375 | */ |
||
376 | 504 | public function input($type, $name, $value = null, array $options = []) |
|
398 | |||
399 | /** |
||
400 | * Create a text input field. |
||
401 | * |
||
402 | * @param string $name |
||
403 | * @param string $value |
||
404 | * @param array $options |
||
405 | * |
||
406 | * @return \Illuminate\Support\HtmlString |
||
407 | */ |
||
408 | 56 | public function text($name, $value = null, array $options = []) |
|
412 | |||
413 | /** |
||
414 | * Create a password input field. |
||
415 | * |
||
416 | * @param string $name |
||
417 | * @param array $options |
||
418 | * |
||
419 | * @return \Illuminate\Support\HtmlString |
||
420 | */ |
||
421 | 24 | public function password($name, array $options = []) |
|
425 | |||
426 | /** |
||
427 | * Create a hidden input field. |
||
428 | * |
||
429 | * @param string $name |
||
430 | * @param string $value |
||
431 | * @param array $options |
||
432 | * |
||
433 | * @return \Illuminate\Support\HtmlString |
||
434 | */ |
||
435 | 80 | public function hidden($name, $value = null, array $options = []) |
|
439 | |||
440 | /** |
||
441 | * Create an e-mail input field. |
||
442 | * |
||
443 | * @param string $name |
||
444 | * @param string $value |
||
445 | * @param array $options |
||
446 | * |
||
447 | * @return \Illuminate\Support\HtmlString |
||
448 | */ |
||
449 | 24 | public function email($name, $value = null, array $options = []) |
|
453 | |||
454 | /** |
||
455 | * Create a tel input field. |
||
456 | * |
||
457 | * @param string $name |
||
458 | * @param string $value |
||
459 | * @param array $options |
||
460 | * |
||
461 | * @return \Illuminate\Support\HtmlString |
||
462 | */ |
||
463 | 24 | public function tel($name, $value = null, array $options = []) |
|
467 | |||
468 | /** |
||
469 | * Create a number input field. |
||
470 | * |
||
471 | * @param string $name |
||
472 | * @param string $value |
||
473 | * @param array $options |
||
474 | * |
||
475 | * @return \Illuminate\Support\HtmlString |
||
476 | */ |
||
477 | 24 | public function number($name, $value = null, array $options = []) |
|
481 | |||
482 | /** |
||
483 | * Create a date input field. |
||
484 | * |
||
485 | * @param string $name |
||
486 | * @param string $value |
||
487 | * @param array $options |
||
488 | * |
||
489 | * @return \Illuminate\Support\HtmlString |
||
490 | */ |
||
491 | 32 | public function date($name, $value = null, array $options = []) |
|
499 | |||
500 | /** |
||
501 | * Create a datetime input field. |
||
502 | * |
||
503 | * @param string $name |
||
504 | * @param string $value |
||
505 | * @param array $options |
||
506 | * |
||
507 | * @return \Illuminate\Support\HtmlString |
||
508 | */ |
||
509 | 32 | public function datetime($name, $value = null, array $options = []) |
|
517 | |||
518 | /** |
||
519 | * Create a datetime-local input field. |
||
520 | * |
||
521 | * @param string $name |
||
522 | * @param string $value |
||
523 | * @param array $options |
||
524 | * |
||
525 | * @return \Illuminate\Support\HtmlString |
||
526 | */ |
||
527 | 32 | public function datetimeLocal($name, $value = null, array $options = []) |
|
535 | |||
536 | /** |
||
537 | * Create a time input field. |
||
538 | * |
||
539 | * @param string $name |
||
540 | * @param string $value |
||
541 | * @param array $options |
||
542 | * |
||
543 | * @return \Illuminate\Support\HtmlString |
||
544 | */ |
||
545 | 24 | public function time($name, $value = null, array $options = []) |
|
549 | |||
550 | /** |
||
551 | * Create a url input field. |
||
552 | * |
||
553 | * @param string $name |
||
554 | * @param string $value |
||
555 | * @param array $options |
||
556 | * |
||
557 | * @return \Illuminate\Support\HtmlString |
||
558 | */ |
||
559 | 16 | public function url($name, $value = null, array $options = []) |
|
563 | |||
564 | /** |
||
565 | * Create a file input field. |
||
566 | * |
||
567 | * @param string $name |
||
568 | * @param array $options |
||
569 | * |
||
570 | * @return \Illuminate\Support\HtmlString |
||
571 | */ |
||
572 | 24 | public function file($name, array $options = []) |
|
576 | |||
577 | /** |
||
578 | * Create a textarea input field. |
||
579 | * |
||
580 | * @param string $name |
||
581 | * @param string $value |
||
582 | * @param array $options |
||
583 | * |
||
584 | * @return \Illuminate\Support\HtmlString |
||
585 | */ |
||
586 | 32 | public function textarea($name, $value = null, array $options = []) |
|
608 | |||
609 | /** |
||
610 | * Set the text area size on the attributes. |
||
611 | * |
||
612 | * @param array $options |
||
613 | * |
||
614 | * @return array |
||
615 | */ |
||
616 | 32 | private function setTextAreaSize(array $options) |
|
630 | |||
631 | /** |
||
632 | * Set the text area size using the quick "size" attribute. |
||
633 | * |
||
634 | * @param array $options |
||
635 | * |
||
636 | * @return array |
||
637 | */ |
||
638 | 8 | protected function setQuickTextAreaSize(array $options) |
|
647 | |||
648 | /** |
||
649 | * Create a select box field. |
||
650 | * |
||
651 | * @param string $name |
||
652 | * @param array $list |
||
653 | * @param string $selected |
||
654 | * @param array $options |
||
655 | * |
||
656 | * @return \Illuminate\Support\HtmlString |
||
657 | */ |
||
658 | 112 | public function select($name, $list = [], $selected = null, array $options = []) |
|
697 | |||
698 | /** |
||
699 | * Create a select range field. |
||
700 | * |
||
701 | * @param string $name |
||
702 | * @param string $begin |
||
703 | * @param string $end |
||
704 | * @param string $selected |
||
705 | * @param array $options |
||
706 | * |
||
707 | * @return \Illuminate\Support\HtmlString |
||
708 | */ |
||
709 | 16 | public function selectRange($name, $begin, $end, $selected = null, array $options = []) |
|
715 | |||
716 | /** |
||
717 | * Create a select year field. |
||
718 | * |
||
719 | * @param string $name |
||
720 | * @param string $begin |
||
721 | * @param string $end |
||
722 | * @param string $selected |
||
723 | * @param array $options |
||
724 | * |
||
725 | * @return \Illuminate\Support\HtmlString |
||
726 | */ |
||
727 | 8 | public function selectYear($name, $begin, $end, $selected = null, array $options = []) |
|
734 | |||
735 | /** |
||
736 | * Create a select month field. |
||
737 | * |
||
738 | * @param string $name |
||
739 | * @param string $selected |
||
740 | * @param array $options |
||
741 | * @param string $format |
||
742 | * |
||
743 | * @return \Illuminate\Support\HtmlString |
||
744 | */ |
||
745 | 8 | public function selectMonth($name, $selected = null, array $options = [], $format = '%B') |
|
755 | |||
756 | /** |
||
757 | * Get the select option for the given value. |
||
758 | * |
||
759 | * @param string $display |
||
760 | * @param string $value |
||
761 | * @param string $selected |
||
762 | * |
||
763 | * @return string |
||
764 | */ |
||
765 | 104 | private function getSelectOption($display, $value, $selected) |
|
773 | |||
774 | /** |
||
775 | * Create an option group form element. |
||
776 | * |
||
777 | * @param array $list |
||
778 | * @param string $label |
||
779 | * @param string $selected |
||
780 | * |
||
781 | * @return string |
||
782 | */ |
||
783 | 16 | private function optionGroup(array $list, $label, $selected) |
|
793 | |||
794 | /** |
||
795 | * Create a select element option. |
||
796 | * |
||
797 | * @param string $display |
||
798 | * @param string $value |
||
799 | * @param string $selected |
||
800 | * |
||
801 | * @return string |
||
802 | */ |
||
803 | 104 | private function option($display, $value, $selected) |
|
810 | |||
811 | /** |
||
812 | * Create a placeholder select element option. |
||
813 | * |
||
814 | * @param string $display |
||
815 | * @param string $selected |
||
816 | * |
||
817 | * @return string |
||
818 | */ |
||
819 | 8 | private function placeholderOption($display, $selected) |
|
827 | |||
828 | /** |
||
829 | * Determine if the value is selected. |
||
830 | * |
||
831 | * @param string $value |
||
832 | * @param string $selected |
||
833 | * |
||
834 | * @return string|null |
||
835 | */ |
||
836 | 104 | private function getSelectedValue($value, $selected) |
|
844 | |||
845 | /** |
||
846 | * Create a checkbox input field. |
||
847 | * |
||
848 | * @param string $name |
||
849 | * @param mixed $value |
||
850 | * @param bool|null $checked |
||
851 | * @param array $options |
||
852 | * |
||
853 | * @return \Illuminate\Support\HtmlString |
||
854 | */ |
||
855 | 32 | public function checkbox($name, $value = 1, $checked = null, array $options = []) |
|
859 | |||
860 | /** |
||
861 | * Create a radio button input field. |
||
862 | * |
||
863 | * @param string $name |
||
864 | * @param mixed $value |
||
865 | * @param bool $checked |
||
866 | * @param array $options |
||
867 | * |
||
868 | * @return \Illuminate\Support\HtmlString |
||
869 | */ |
||
870 | 16 | public function radio($name, $value = null, $checked = null, array $options = []) |
|
878 | |||
879 | /** |
||
880 | * Create a checkable input field. |
||
881 | * |
||
882 | * @param string $type |
||
883 | * @param string $name |
||
884 | * @param mixed $value |
||
885 | * @param bool|null $checked |
||
886 | * @param array $options |
||
887 | * |
||
888 | * @return \Illuminate\Support\HtmlString |
||
889 | */ |
||
890 | 56 | protected function checkable($type, $name, $value, $checked, array $options) |
|
900 | |||
901 | /** |
||
902 | * Get the check state for a checkable input. |
||
903 | * |
||
904 | * @param string $type |
||
905 | * @param string $name |
||
906 | * @param mixed $value |
||
907 | * @param bool|null $checked |
||
908 | * |
||
909 | * @return bool |
||
910 | */ |
||
911 | 56 | private function getCheckedState($type, $name, $value, $checked) |
|
924 | |||
925 | /** |
||
926 | * Get the check state for a checkbox input. |
||
927 | * |
||
928 | * @param string $name |
||
929 | * @param mixed $value |
||
930 | * @param bool|null $checked |
||
931 | * |
||
932 | * @return bool |
||
933 | */ |
||
934 | 32 | private function getCheckboxCheckedState($name, $value, $checked) |
|
960 | |||
961 | /** |
||
962 | * Get the check state for a radio input. |
||
963 | * |
||
964 | * @param string $name |
||
965 | * @param mixed $value |
||
966 | * @param bool|null $checked |
||
967 | * |
||
968 | * @return bool |
||
969 | */ |
||
970 | 16 | private function getRadioCheckedState($name, $value, $checked) |
|
976 | |||
977 | /** |
||
978 | * Determine if old input or model input exists for a key. |
||
979 | * |
||
980 | * @param string $name |
||
981 | * |
||
982 | * @return bool |
||
983 | */ |
||
984 | 48 | private function missingOldAndModel($name) |
|
988 | |||
989 | /** |
||
990 | * Create a HTML reset input element. |
||
991 | * |
||
992 | * @param string $value |
||
993 | * @param array $attributes |
||
994 | * |
||
995 | * @return \Illuminate\Support\HtmlString |
||
996 | */ |
||
997 | 8 | public function reset($value, array $attributes = []) |
|
1001 | |||
1002 | /** |
||
1003 | * Create a HTML image input element. |
||
1004 | * |
||
1005 | * @param string $url |
||
1006 | * @param string $name |
||
1007 | * @param array $attributes |
||
1008 | * |
||
1009 | * @return \Illuminate\Support\HtmlString |
||
1010 | */ |
||
1011 | 8 | public function image($url, $name = null, array $attributes = []) |
|
1017 | |||
1018 | /** |
||
1019 | * Create a submit button element. |
||
1020 | * |
||
1021 | * @param string $value |
||
1022 | * @param array $options |
||
1023 | * |
||
1024 | * @return \Illuminate\Support\HtmlString |
||
1025 | */ |
||
1026 | 8 | public function submit($value = null, array $options = []) |
|
1030 | |||
1031 | /** |
||
1032 | * Create a button element. |
||
1033 | * |
||
1034 | * @param string $value |
||
1035 | * @param array $options |
||
1036 | * |
||
1037 | * @return \Illuminate\Support\HtmlString |
||
1038 | */ |
||
1039 | 8 | public function button($value = null, array $options = []) |
|
1049 | |||
1050 | /** |
||
1051 | * Create a color input field. |
||
1052 | * |
||
1053 | * @param string $name |
||
1054 | * @param string $value |
||
1055 | * @param array $options |
||
1056 | * |
||
1057 | * @return \Illuminate\Support\HtmlString |
||
1058 | */ |
||
1059 | 24 | public function color($name, $value = null, array $options = []) |
|
1063 | |||
1064 | /* ------------------------------------------------------------------------------------------------ |
||
1065 | | Other Functions |
||
1066 | | ------------------------------------------------------------------------------------------------ |
||
1067 | */ |
||
1068 | /** |
||
1069 | * Get the form action from the options. |
||
1070 | * |
||
1071 | * @param array $options |
||
1072 | * |
||
1073 | * @return string |
||
1074 | */ |
||
1075 | 128 | private function getAction(array $options) |
|
1095 | |||
1096 | /** |
||
1097 | * Get the action for a "url" option. |
||
1098 | * |
||
1099 | * @param array|string $options |
||
1100 | * |
||
1101 | * @return string |
||
1102 | */ |
||
1103 | 40 | private function getUrlAction($options) |
|
1109 | |||
1110 | /** |
||
1111 | * Get the action for a "route" option. |
||
1112 | * |
||
1113 | * @param array|string $options |
||
1114 | * |
||
1115 | * @return string |
||
1116 | */ |
||
1117 | 8 | private function getRouteAction($options) |
|
1123 | |||
1124 | /** |
||
1125 | * Get the action for an "action" option. |
||
1126 | * |
||
1127 | * @param array|string $options |
||
1128 | * |
||
1129 | * @return string |
||
1130 | */ |
||
1131 | 8 | private function getControllerAction($options) |
|
1137 | |||
1138 | /** |
||
1139 | * Get the form appendage for the given method. |
||
1140 | * |
||
1141 | * @param string $method |
||
1142 | * |
||
1143 | * @return string |
||
1144 | */ |
||
1145 | 128 | private function getAppendage($method) |
|
1165 | } |
||
1166 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: