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; |
||
17 | class FormBuilder extends Builder implements FormBuilderContract |
||
18 | { |
||
19 | /* ----------------------------------------------------------------- |
||
20 | | Properties |
||
21 | | ----------------------------------------------------------------- |
||
22 | */ |
||
23 | |||
24 | /** |
||
25 | * The HTML builder instance. |
||
26 | * |
||
27 | * @var \Arcanedev\LaravelHtml\Contracts\HtmlBuilder |
||
28 | */ |
||
29 | protected $html; |
||
30 | |||
31 | /** |
||
32 | * The URL generator instance. |
||
33 | * |
||
34 | * @var \Illuminate\Contracts\Routing\UrlGenerator |
||
35 | */ |
||
36 | protected $url; |
||
37 | |||
38 | /** |
||
39 | * The CSRF token used by the form builder. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $csrfToken; |
||
44 | |||
45 | /** |
||
46 | * The session store implementation. |
||
47 | * |
||
48 | * @var \Illuminate\Contracts\Session\Session|\Illuminate\Session\Store |
||
49 | */ |
||
50 | protected $session; |
||
51 | |||
52 | /** |
||
53 | * The current model instance for the form. |
||
54 | * |
||
55 | * @var \Illuminate\Database\Eloquent\Model |
||
56 | */ |
||
57 | protected $model; |
||
58 | |||
59 | /** |
||
60 | * An array of label names we've created. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $labels = []; |
||
65 | |||
66 | /** |
||
67 | * The reserved form open attributes. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $reserved = ['method', 'url', 'route', 'action', 'files']; |
||
72 | |||
73 | /** |
||
74 | * The form methods that should be spoofed, in uppercase. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT']; |
||
79 | |||
80 | /** |
||
81 | * The types of inputs to not fill values on by default. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio']; |
||
86 | |||
87 | /* ----------------------------------------------------------------- |
||
88 | | Constructor |
||
89 | | ----------------------------------------------------------------- |
||
90 | */ |
||
91 | |||
92 | /** |
||
93 | * Create a new form builder instance. |
||
94 | * |
||
95 | * @param \Arcanedev\LaravelHtml\Contracts\HtmlBuilder $html |
||
96 | * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
||
97 | * @param \Illuminate\Contracts\Session\Session $session |
||
98 | */ |
||
99 | 321 | public function __construct( |
|
110 | |||
111 | /* ----------------------------------------------------------------- |
||
112 | | Getters & Setters |
||
113 | | ----------------------------------------------------------------- |
||
114 | */ |
||
115 | |||
116 | /** |
||
117 | * Get the session store implementation. |
||
118 | * |
||
119 | * @return \Illuminate\Contracts\Session\Session |
||
120 | */ |
||
121 | 3 | public function getSessionStore() |
|
125 | |||
126 | /** |
||
127 | * Set the session store implementation. |
||
128 | * |
||
129 | * @param \Illuminate\Contracts\Session\Session $session |
||
130 | * |
||
131 | * @return self |
||
132 | */ |
||
133 | 321 | public function setSessionStore(Session $session) |
|
139 | |||
140 | /** |
||
141 | * Set the model instance on the form builder. |
||
142 | * |
||
143 | * @param \Illuminate\Database\Eloquent\Model $model |
||
144 | * |
||
145 | * @return self |
||
146 | */ |
||
147 | 36 | public function setModel($model) |
|
153 | |||
154 | /** |
||
155 | * Get the model instance on the form builder. |
||
156 | * |
||
157 | * @return \Illuminate\Database\Eloquent\Model |
||
158 | */ |
||
159 | 15 | public function getModel() |
|
163 | |||
164 | /** |
||
165 | * Get the ID attribute for a field name. |
||
166 | * |
||
167 | * @param string $name |
||
168 | * @param array $attributes |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 261 | public function getIdAttribute($name, array $attributes) |
|
182 | |||
183 | /** |
||
184 | * Get the value that should be assigned to the field. |
||
185 | * |
||
186 | * @param string $name |
||
187 | * @param mixed $value |
||
188 | * |
||
189 | * @return mixed |
||
190 | */ |
||
191 | 237 | public function getValueAttribute($name, $value = null) |
|
206 | |||
207 | /** |
||
208 | * Get the model value that should be assigned to the field. |
||
209 | * |
||
210 | * @param string $name |
||
211 | * @param \Illuminate\Database\Eloquent\Model $model |
||
212 | * |
||
213 | * @return mixed |
||
214 | */ |
||
215 | 27 | private function getModelValueAttribute($name, $model = null) |
|
234 | |||
235 | /** |
||
236 | * Get a value from the session's old input. |
||
237 | * |
||
238 | * @param string $name |
||
239 | * |
||
240 | * @return mixed |
||
241 | */ |
||
242 | 237 | public function old($name) |
|
248 | |||
249 | /** |
||
250 | * Transform key from array to dot syntax. |
||
251 | * |
||
252 | * @param string $key |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | 237 | private function transformKey($key) |
|
260 | |||
261 | /** |
||
262 | * Determine if the old input is empty. |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | 12 | public function oldInputIsEmpty() |
|
270 | |||
271 | /** |
||
272 | * Parse the form action method. |
||
273 | * |
||
274 | * @param string $method |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 57 | private function getMethod($method) |
|
284 | |||
285 | /* ----------------------------------------------------------------- |
||
286 | | Main Methods |
||
287 | | ----------------------------------------------------------------- |
||
288 | */ |
||
289 | |||
290 | /** |
||
291 | * Open up a new HTML form. |
||
292 | * |
||
293 | * @param array $options |
||
294 | * |
||
295 | * @return \Illuminate\Support\HtmlString |
||
296 | */ |
||
297 | 57 | public function open(array $options = []) |
|
332 | |||
333 | /** |
||
334 | * Create a new model based form builder. |
||
335 | * |
||
336 | * @param mixed $model |
||
337 | * @param array $options |
||
338 | * |
||
339 | * @return \Illuminate\Support\HtmlString |
||
340 | */ |
||
341 | 15 | public function model($model, array $options = []) |
|
347 | |||
348 | /** |
||
349 | * Close the current form. |
||
350 | * |
||
351 | * @return \Illuminate\Support\HtmlString |
||
352 | */ |
||
353 | 6 | public function close() |
|
360 | |||
361 | /** |
||
362 | * Generate a hidden field with the current CSRF token. |
||
363 | * |
||
364 | * @return \Illuminate\Support\HtmlString |
||
365 | */ |
||
366 | 30 | public function token() |
|
373 | |||
374 | /** |
||
375 | * Create a form label element. |
||
376 | * |
||
377 | * @param string $name |
||
378 | * @param string $value |
||
379 | * @param array $options |
||
380 | * @param bool $escaped |
||
381 | * |
||
382 | * @return \Illuminate\Support\HtmlString |
||
383 | */ |
||
384 | 15 | public function label($name, $value = null, array $options = [], $escaped = true) |
|
392 | |||
393 | /** |
||
394 | * Create a form input field. |
||
395 | * |
||
396 | * @param string $type |
||
397 | * @param string $name |
||
398 | * @param string $value |
||
399 | * @param array $options |
||
400 | * |
||
401 | * @return \Illuminate\Support\HtmlString |
||
402 | */ |
||
403 | 198 | public function input($type, $name, $value = null, array $options = []) |
|
423 | |||
424 | /** |
||
425 | * Create a text input field. |
||
426 | * |
||
427 | * @param string $name |
||
428 | * @param string $value |
||
429 | * @param array $options |
||
430 | * |
||
431 | * @return \Illuminate\Support\HtmlString |
||
432 | */ |
||
433 | 21 | public function text($name, $value = null, array $options = []) |
|
437 | |||
438 | /** |
||
439 | * Create a password input field. |
||
440 | * |
||
441 | * @param string $name |
||
442 | * @param array $options |
||
443 | * |
||
444 | * @return \Illuminate\Support\HtmlString |
||
445 | */ |
||
446 | 9 | public function password($name, array $options = []) |
|
450 | |||
451 | /** |
||
452 | * Create a hidden input field. |
||
453 | * |
||
454 | * @param string $name |
||
455 | * @param string $value |
||
456 | * @param array $options |
||
457 | * |
||
458 | * @return \Illuminate\Support\HtmlString |
||
459 | */ |
||
460 | 39 | public function hidden($name, $value = null, array $options = []) |
|
464 | |||
465 | /** |
||
466 | * Create an e-mail input field. |
||
467 | * |
||
468 | * @param string $name |
||
469 | * @param string $value |
||
470 | * @param array $options |
||
471 | * |
||
472 | * @return \Illuminate\Support\HtmlString |
||
473 | */ |
||
474 | 9 | public function email($name, $value = null, array $options = []) |
|
478 | |||
479 | /** |
||
480 | * Create a tel input field. |
||
481 | * |
||
482 | * @param string $name |
||
483 | * @param string $value |
||
484 | * @param array $options |
||
485 | * |
||
486 | * @return \Illuminate\Support\HtmlString |
||
487 | */ |
||
488 | 9 | public function tel($name, $value = null, array $options = []) |
|
492 | |||
493 | /** |
||
494 | * Create a number input field. |
||
495 | * |
||
496 | * @param string $name |
||
497 | * @param string $value |
||
498 | * @param array $options |
||
499 | * |
||
500 | * @return \Illuminate\Support\HtmlString |
||
501 | */ |
||
502 | 9 | public function number($name, $value = null, array $options = []) |
|
506 | |||
507 | /** |
||
508 | * Create a date input field. |
||
509 | * |
||
510 | * @param string $name |
||
511 | * @param string $value |
||
512 | * @param array $options |
||
513 | * |
||
514 | * @return \Illuminate\Support\HtmlString |
||
515 | */ |
||
516 | 12 | public function date($name, $value = null, array $options = []) |
|
523 | |||
524 | /** |
||
525 | * Create a datetime input field. |
||
526 | * |
||
527 | * @param string $name |
||
528 | * @param string $value |
||
529 | * @param array $options |
||
530 | * |
||
531 | * @return \Illuminate\Support\HtmlString |
||
532 | */ |
||
533 | 12 | public function datetime($name, $value = null, array $options = []) |
|
540 | |||
541 | /** |
||
542 | * Create a datetime-local input field. |
||
543 | * |
||
544 | * @param string $name |
||
545 | * @param string $value |
||
546 | * @param array $options |
||
547 | * |
||
548 | * @return \Illuminate\Support\HtmlString |
||
549 | */ |
||
550 | 12 | public function datetimeLocal($name, $value = null, array $options = []) |
|
557 | |||
558 | /** |
||
559 | * Create a time input field. |
||
560 | * |
||
561 | * @param string $name |
||
562 | * @param string $value |
||
563 | * @param array $options |
||
564 | * |
||
565 | * @return \Illuminate\Support\HtmlString |
||
566 | */ |
||
567 | 9 | public function time($name, $value = null, array $options = []) |
|
571 | |||
572 | /** |
||
573 | * Create a url input field. |
||
574 | * |
||
575 | * @param string $name |
||
576 | * @param string $value |
||
577 | * @param array $options |
||
578 | * |
||
579 | * @return \Illuminate\Support\HtmlString |
||
580 | */ |
||
581 | 6 | public function url($name, $value = null, array $options = []) |
|
585 | |||
586 | /** |
||
587 | * Create a file input field. |
||
588 | * |
||
589 | * @param string $name |
||
590 | * @param array $options |
||
591 | * |
||
592 | * @return \Illuminate\Support\HtmlString |
||
593 | */ |
||
594 | 9 | public function file($name, array $options = []) |
|
598 | |||
599 | /** |
||
600 | * Create a textarea input field. |
||
601 | * |
||
602 | * @param string $name |
||
603 | * @param string $value |
||
604 | * @param array $options |
||
605 | * |
||
606 | * @return \Illuminate\Support\HtmlString |
||
607 | */ |
||
608 | 18 | public function textarea($name, $value = null, array $options = []) |
|
629 | |||
630 | /** |
||
631 | * Set the text area size on the attributes. |
||
632 | * |
||
633 | * @param array $options |
||
634 | * |
||
635 | * @return array |
||
636 | */ |
||
637 | 18 | private function setTextAreaSize(array $options) |
|
650 | |||
651 | /** |
||
652 | * Set the text area size using the quick "size" attribute. |
||
653 | * |
||
654 | * @param array $options |
||
655 | * |
||
656 | * @return array |
||
657 | */ |
||
658 | 9 | protected function setQuickTextAreaSize(array $options) |
|
664 | |||
665 | /** |
||
666 | * Create a select box field. |
||
667 | * |
||
668 | * @param string $name |
||
669 | * @param array|\Illuminate\Support\Collection $list |
||
670 | * @param string|bool $selected |
||
671 | * @param array $attributes |
||
672 | * @param array $optionsAttributes |
||
673 | * @param array $optgroupsAttributes |
||
674 | * |
||
675 | * @return \Illuminate\Support\HtmlString |
||
676 | */ |
||
677 | 45 | public function select( |
|
723 | |||
724 | /** |
||
725 | * Create a select range field. |
||
726 | * |
||
727 | * @param string $name |
||
728 | * @param string $begin |
||
729 | * @param string $end |
||
730 | * @param string $selected |
||
731 | * @param array $options |
||
732 | * |
||
733 | * @return \Illuminate\Support\HtmlString |
||
734 | */ |
||
735 | 6 | public function selectRange($name, $begin, $end, $selected = null, array $options = []) |
|
741 | |||
742 | /** |
||
743 | * Create a select year field. |
||
744 | * |
||
745 | * @param string $name |
||
746 | * @param string $begin |
||
747 | * @param string $end |
||
748 | * @param string $selected |
||
749 | * @param array $options |
||
750 | * |
||
751 | * @return \Illuminate\Support\HtmlString |
||
752 | */ |
||
753 | 3 | public function selectYear($name, $begin, $end, $selected = null, array $options = []) |
|
760 | |||
761 | /** |
||
762 | * Create a select month field. |
||
763 | * |
||
764 | * @param string $name |
||
765 | * @param string $selected |
||
766 | * @param array $options |
||
767 | * @param string $format |
||
768 | * |
||
769 | * @return \Illuminate\Support\HtmlString |
||
770 | */ |
||
771 | 3 | public function selectMonth($name, $selected = null, array $options = [], $format = '%B') |
|
781 | |||
782 | /** |
||
783 | * Get the select option for the given value. |
||
784 | * |
||
785 | * @param string $display |
||
786 | * @param string $value |
||
787 | * @param string $selected |
||
788 | * @param array $attributes |
||
789 | * @param array $optgroupAttributes |
||
790 | * |
||
791 | * @return string |
||
792 | */ |
||
793 | 42 | private function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = []) |
|
799 | |||
800 | /** |
||
801 | * Create an option group form element. |
||
802 | * |
||
803 | * @param array $list |
||
804 | * @param string $label |
||
805 | * @param string $selected |
||
806 | * @param array $attributes |
||
807 | * @param array $optionsAttributes |
||
808 | * @param int $level |
||
809 | * |
||
810 | * @return string |
||
811 | */ |
||
812 | 6 | private function optionGroup(array $list, $label, $selected, array $attributes = [], array $optionsAttributes = [], $level = 0) |
|
827 | |||
828 | /** |
||
829 | * Create a select element option. |
||
830 | * |
||
831 | * @param string $display |
||
832 | * @param string $value |
||
833 | * @param string $selected |
||
834 | * @param array $attributes |
||
835 | * |
||
836 | * @return string |
||
837 | */ |
||
838 | 42 | private function option($display, $value, $selected, array $attributes = []) |
|
845 | |||
846 | /** |
||
847 | * Create a placeholder select element option. |
||
848 | * |
||
849 | * @param string $display |
||
850 | * @param string $selected |
||
851 | * |
||
852 | * @return string |
||
853 | */ |
||
854 | 6 | private function placeholderOption($display, $selected) |
|
861 | |||
862 | /** |
||
863 | * Determine if the value is selected. |
||
864 | * |
||
865 | * @param string $value |
||
866 | * @param string $selected |
||
867 | * |
||
868 | * @return string|null |
||
869 | */ |
||
870 | 42 | private function getSelectedValue($value, $selected) |
|
883 | |||
884 | /** |
||
885 | * Create a checkbox input field. |
||
886 | * |
||
887 | * @param string $name |
||
888 | * @param mixed $value |
||
889 | * @param bool|null $checked |
||
890 | * @param array $options |
||
891 | * |
||
892 | * @return \Illuminate\Support\HtmlString |
||
893 | */ |
||
894 | 12 | public function checkbox($name, $value = 1, $checked = null, array $options = []) |
|
898 | |||
899 | /** |
||
900 | * Create a radio button input field. |
||
901 | * |
||
902 | * @param string $name |
||
903 | * @param mixed $value |
||
904 | * @param bool $checked |
||
905 | * @param array $options |
||
906 | * |
||
907 | * @return \Illuminate\Support\HtmlString |
||
908 | */ |
||
909 | 6 | public function radio($name, $value = null, $checked = null, array $options = []) |
|
913 | |||
914 | /** |
||
915 | * Create a checkable input field. |
||
916 | * |
||
917 | * @param string $type |
||
918 | * @param string $name |
||
919 | * @param mixed $value |
||
920 | * @param bool|null $checked |
||
921 | * @param array $options |
||
922 | * |
||
923 | * @return \Illuminate\Support\HtmlString |
||
924 | */ |
||
925 | 21 | protected function checkable($type, $name, $value, $checked, array $options) |
|
934 | |||
935 | /** |
||
936 | * Get the check state for a checkable input. |
||
937 | * |
||
938 | * @param string $type |
||
939 | * @param string $name |
||
940 | * @param mixed $value |
||
941 | * @param bool|null $checked |
||
942 | * |
||
943 | * @return bool |
||
944 | */ |
||
945 | 21 | private function getCheckedState($type, $name, $value, $checked) |
|
958 | |||
959 | /** |
||
960 | * Get the check state for a checkbox input. |
||
961 | * |
||
962 | * @param string $name |
||
963 | * @param mixed $value |
||
964 | * @param bool|null $checked |
||
965 | * |
||
966 | * @return bool |
||
967 | */ |
||
968 | 12 | private function getCheckboxCheckedState($name, $value, $checked) |
|
990 | |||
991 | /** |
||
992 | * Get the check state for a radio input. |
||
993 | * |
||
994 | * @param string $name |
||
995 | * @param mixed $value |
||
996 | * @param bool|null $checked |
||
997 | * |
||
998 | * @return bool |
||
999 | */ |
||
1000 | 6 | private function getRadioCheckedState($name, $value, $checked) |
|
1006 | |||
1007 | /** |
||
1008 | * Determine if old input or model input exists for a key. |
||
1009 | * |
||
1010 | * @param string $name |
||
1011 | * |
||
1012 | * @return bool |
||
1013 | */ |
||
1014 | 18 | private function missingOldAndModel($name) |
|
1018 | |||
1019 | /** |
||
1020 | * Create a HTML reset input element. |
||
1021 | * |
||
1022 | * @param string $value |
||
1023 | * @param array $attributes |
||
1024 | * |
||
1025 | * @return \Illuminate\Support\HtmlString |
||
1026 | */ |
||
1027 | 3 | public function reset($value, array $attributes = []) |
|
1031 | |||
1032 | /** |
||
1033 | * Create a HTML image input element. |
||
1034 | * |
||
1035 | * @param string $url |
||
1036 | * @param string $name |
||
1037 | * @param array $attributes |
||
1038 | * |
||
1039 | * @return \Illuminate\Support\HtmlString |
||
1040 | */ |
||
1041 | 3 | public function image($url, $name = null, array $attributes = []) |
|
1047 | |||
1048 | /** |
||
1049 | * Create a submit button element. |
||
1050 | * |
||
1051 | * @param string $value |
||
1052 | * @param array $options |
||
1053 | * |
||
1054 | * @return \Illuminate\Support\HtmlString |
||
1055 | */ |
||
1056 | 3 | public function submit($value = null, array $options = []) |
|
1060 | |||
1061 | /** |
||
1062 | * Create a button element. |
||
1063 | * |
||
1064 | * @param string $value |
||
1065 | * @param array $options |
||
1066 | * |
||
1067 | * @return \Illuminate\Support\HtmlString |
||
1068 | */ |
||
1069 | 3 | public function button($value = null, array $options = []) |
|
1078 | |||
1079 | /** |
||
1080 | * Create a color input field. |
||
1081 | * |
||
1082 | * @param string $name |
||
1083 | * @param string $value |
||
1084 | * @param array $options |
||
1085 | * |
||
1086 | * @return \Illuminate\Support\HtmlString |
||
1087 | */ |
||
1088 | 9 | public function color($name, $value = null, array $options = []) |
|
1092 | |||
1093 | /* ----------------------------------------------------------------- |
||
1094 | | Other Methods |
||
1095 | | ----------------------------------------------------------------- |
||
1096 | */ |
||
1097 | |||
1098 | /** |
||
1099 | * Get the form action from the options. |
||
1100 | * |
||
1101 | * @param array $options |
||
1102 | * |
||
1103 | * @return string |
||
1104 | */ |
||
1105 | 57 | private function getAction(array $options) |
|
1124 | |||
1125 | /** |
||
1126 | * Get the action for a "url" option. |
||
1127 | * |
||
1128 | * @param array|string $options |
||
1129 | * |
||
1130 | * @return string |
||
1131 | */ |
||
1132 | 18 | private function getUrlAction($options) |
|
1138 | |||
1139 | /** |
||
1140 | * Get the action for a "route" option. |
||
1141 | * |
||
1142 | * @param array|string $options |
||
1143 | * |
||
1144 | * @return string |
||
1145 | */ |
||
1146 | 6 | private function getRouteAction($options) |
|
1152 | |||
1153 | /** |
||
1154 | * Get the action for an "action" option. |
||
1155 | * |
||
1156 | * @param array|string $options |
||
1157 | * |
||
1158 | * @return string |
||
1159 | */ |
||
1160 | 6 | private function getControllerAction($options) |
|
1166 | |||
1167 | /** |
||
1168 | * Get the form appendage for the given method. |
||
1169 | * |
||
1170 | * @param string $method |
||
1171 | * |
||
1172 | * @return string |
||
1173 | */ |
||
1174 | 57 | private function getAppendage($method) |
|
1192 | } |
||
1193 |
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: