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 extends Builder implements FormBuilderContract |
||
20 | { |
||
21 | /* ----------------------------------------------------------------- |
||
22 | | Properties |
||
23 | | ----------------------------------------------------------------- |
||
24 | */ |
||
25 | |||
26 | /** |
||
27 | * The HTML builder instance. |
||
28 | * |
||
29 | * @var \Arcanedev\LaravelHtml\Contracts\HtmlBuilder |
||
30 | */ |
||
31 | protected $html; |
||
32 | |||
33 | /** |
||
34 | * The URL generator instance. |
||
35 | * |
||
36 | * @var \Illuminate\Contracts\Routing\UrlGenerator |
||
37 | */ |
||
38 | protected $url; |
||
39 | |||
40 | /** |
||
41 | * The CSRF token used by the form builder. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $csrfToken; |
||
46 | |||
47 | /** |
||
48 | * The session store implementation. |
||
49 | * |
||
50 | * @var \Illuminate\Contracts\Session\Session|\Illuminate\Session\Store |
||
51 | */ |
||
52 | protected $session; |
||
53 | |||
54 | /** |
||
55 | * The current model instance for the form. |
||
56 | * |
||
57 | * @var \Illuminate\Database\Eloquent\Model |
||
58 | */ |
||
59 | protected $model; |
||
60 | |||
61 | /** |
||
62 | * An array of label names we've created. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $labels = []; |
||
67 | |||
68 | /** |
||
69 | * The reserved form open attributes. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $reserved = ['method', 'url', 'route', 'action', 'files']; |
||
74 | |||
75 | /** |
||
76 | * The form methods that should be spoofed, in uppercase. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT']; |
||
81 | |||
82 | /** |
||
83 | * The types of inputs to not fill values on by default. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio']; |
||
88 | |||
89 | /* ----------------------------------------------------------------- |
||
90 | | Constructor |
||
91 | | ----------------------------------------------------------------- |
||
92 | */ |
||
93 | |||
94 | /** |
||
95 | * Create a new form builder instance. |
||
96 | * |
||
97 | * @param \Arcanedev\LaravelHtml\Contracts\HtmlBuilder $html |
||
98 | * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
||
99 | * @param \Illuminate\Contracts\Session\Session $session |
||
100 | */ |
||
101 | 321 | public function __construct(Contracts\HtmlBuilder $html, UrlGenerator $url, Session $session) |
|
109 | |||
110 | /* ----------------------------------------------------------------- |
||
111 | | Getters & Setters |
||
112 | | ----------------------------------------------------------------- |
||
113 | */ |
||
114 | |||
115 | /** |
||
116 | * Get the session store implementation. |
||
117 | * |
||
118 | * @return \Illuminate\Contracts\Session\Session |
||
119 | */ |
||
120 | 3 | public function getSessionStore() |
|
124 | |||
125 | /** |
||
126 | * Set the session store implementation. |
||
127 | * |
||
128 | * @param \Illuminate\Contracts\Session\Session $session |
||
129 | * |
||
130 | * @return self |
||
131 | */ |
||
132 | 321 | public function setSessionStore(Session $session) |
|
138 | |||
139 | /** |
||
140 | * Set the model instance on the form builder. |
||
141 | * |
||
142 | * @param \Illuminate\Database\Eloquent\Model $model |
||
143 | * |
||
144 | * @return self |
||
145 | */ |
||
146 | 36 | public function setModel($model) |
|
152 | |||
153 | /** |
||
154 | * Get the model instance on the form builder. |
||
155 | * |
||
156 | * @return \Illuminate\Database\Eloquent\Model |
||
157 | */ |
||
158 | 138 | public function getModel() |
|
162 | |||
163 | /** |
||
164 | * Get the ID attribute for a field name. |
||
165 | * |
||
166 | * @param string $name |
||
167 | * @param array $attributes |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 261 | public function getIdAttribute($name, array $attributes) |
|
181 | |||
182 | /** |
||
183 | * Get the value that should be assigned to the field. |
||
184 | * |
||
185 | * @param string $name |
||
186 | * @param mixed $value |
||
187 | * |
||
188 | * @return mixed |
||
189 | */ |
||
190 | 237 | public function getValueAttribute($name, $value = null) |
|
205 | |||
206 | /** |
||
207 | * Get the model value that should be assigned to the field. |
||
208 | * |
||
209 | * @param string $name |
||
210 | * @param \Illuminate\Database\Eloquent\Model $model |
||
211 | * |
||
212 | * @return mixed |
||
213 | */ |
||
214 | 27 | private function getModelValueAttribute($name, $model = null) |
|
232 | |||
233 | /** |
||
234 | * Get a value from the session's old input. |
||
235 | * |
||
236 | * @param string $name |
||
237 | * |
||
238 | * @return mixed |
||
239 | */ |
||
240 | 237 | public function old($name) |
|
246 | |||
247 | /** |
||
248 | * Transform key from array to dot syntax. |
||
249 | * |
||
250 | * @param string $key |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | 237 | private function transformKey($key) |
|
262 | |||
263 | /** |
||
264 | * Determine if the old input is empty. |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | 12 | public function oldInputIsEmpty() |
|
273 | |||
274 | /* ----------------------------------------------------------------- |
||
275 | | Main Methods |
||
276 | | ----------------------------------------------------------------- |
||
277 | */ |
||
278 | |||
279 | /** |
||
280 | * Open up a new HTML form. |
||
281 | * |
||
282 | * @param array $attributes |
||
283 | * |
||
284 | * @return \Illuminate\Support\HtmlString |
||
285 | */ |
||
286 | 57 | public function open(array $attributes = []) |
|
309 | |||
310 | /** |
||
311 | * Create a new model based form builder. |
||
312 | * |
||
313 | * @param mixed $model |
||
314 | * @param array $attributes |
||
315 | * |
||
316 | * @return \Illuminate\Support\HtmlString |
||
317 | */ |
||
318 | 15 | public function model($model, array $attributes = []) |
|
323 | |||
324 | /** |
||
325 | * Close the current form. |
||
326 | * |
||
327 | * @return \Illuminate\Support\HtmlString |
||
328 | */ |
||
329 | 6 | public function close() |
|
336 | |||
337 | /** |
||
338 | * Generate a hidden field with the current CSRF token. |
||
339 | * |
||
340 | * @return \Illuminate\Support\HtmlString |
||
341 | */ |
||
342 | 30 | public function token() |
|
349 | |||
350 | /** |
||
351 | * Create a form label element. |
||
352 | * |
||
353 | * @param string $name |
||
354 | * @param string|mixed $value |
||
355 | * @param array $attributes |
||
356 | * @param bool $escaped |
||
357 | * |
||
358 | * @return \Illuminate\Support\HtmlString |
||
359 | */ |
||
360 | 15 | public function label($name, $value = null, array $attributes = [], $escaped = true) |
|
372 | |||
373 | /** |
||
374 | * Create a form input field. |
||
375 | * |
||
376 | * @param string $type |
||
377 | * @param string $name |
||
378 | * @param string|mixed $value |
||
379 | * @param array $attributes |
||
380 | * |
||
381 | * @return \Illuminate\Support\HtmlString |
||
382 | */ |
||
383 | 198 | public function input($type, $name, $value = null, array $attributes = []) |
|
398 | |||
399 | /** |
||
400 | * Create a text input field. |
||
401 | * |
||
402 | * @param string $name |
||
403 | * @param string|mixed $value |
||
404 | * @param array $attributes |
||
405 | * |
||
406 | * @return \Illuminate\Support\HtmlString |
||
407 | */ |
||
408 | 21 | public function text($name, $value = null, array $attributes = []) |
|
412 | |||
413 | /** |
||
414 | * Create a password input field. |
||
415 | * |
||
416 | * @param string $name |
||
417 | * @param array $attributes |
||
418 | * |
||
419 | * @return \Illuminate\Support\HtmlString |
||
420 | */ |
||
421 | 9 | public function password($name, array $attributes = []) |
|
425 | |||
426 | /** |
||
427 | * Create a hidden input field. |
||
428 | * |
||
429 | * @param string $name |
||
430 | * @param string|mixed $value |
||
431 | * @param array $attributes |
||
432 | * |
||
433 | * @return \Illuminate\Support\HtmlString |
||
434 | */ |
||
435 | 39 | public function hidden($name, $value = null, array $attributes = []) |
|
439 | |||
440 | /** |
||
441 | * Create an e-mail input field. |
||
442 | * |
||
443 | * @param string $name |
||
444 | * @param string|mixed $value |
||
445 | * @param array $attributes |
||
446 | * |
||
447 | * @return \Illuminate\Support\HtmlString |
||
448 | */ |
||
449 | 9 | public function email($name, $value = null, array $attributes = []) |
|
453 | |||
454 | /** |
||
455 | * Create a tel input field. |
||
456 | * |
||
457 | * @param string $name |
||
458 | * @param string|mixed $value |
||
459 | * @param array $attributes |
||
460 | * |
||
461 | * @return \Illuminate\Support\HtmlString |
||
462 | */ |
||
463 | 9 | public function tel($name, $value = null, array $attributes = []) |
|
467 | |||
468 | /** |
||
469 | * Create a number input field. |
||
470 | * |
||
471 | * @param string $name |
||
472 | * @param string|mixed $value |
||
473 | * @param array $attributes |
||
474 | * |
||
475 | * @return \Illuminate\Support\HtmlString |
||
476 | */ |
||
477 | 9 | public function number($name, $value = null, array $attributes = []) |
|
481 | |||
482 | /** |
||
483 | * Create a date input field. |
||
484 | * |
||
485 | * @param string $name |
||
486 | * @param string $value |
||
487 | * @param array $attributes |
||
488 | * |
||
489 | * @return \Illuminate\Support\HtmlString |
||
490 | */ |
||
491 | 12 | public function date($name, $value = null, array $attributes = []) |
|
498 | |||
499 | /** |
||
500 | * Create a datetime input field. |
||
501 | * |
||
502 | * @param string $name |
||
503 | * @param string|mixed $value |
||
504 | * @param array $attributes |
||
505 | * |
||
506 | * @return \Illuminate\Support\HtmlString |
||
507 | */ |
||
508 | 12 | public function datetime($name, $value = null, array $attributes = []) |
|
515 | |||
516 | /** |
||
517 | * Create a datetime-local input field. |
||
518 | * |
||
519 | * @param string $name |
||
520 | * @param string|mixed $value |
||
521 | * @param array $attributes |
||
522 | * |
||
523 | * @return \Illuminate\Support\HtmlString |
||
524 | */ |
||
525 | 12 | public function datetimeLocal($name, $value = null, array $attributes = []) |
|
532 | |||
533 | /** |
||
534 | * Create a time input field. |
||
535 | * |
||
536 | * @param string $name |
||
537 | * @param string|mixed $value |
||
538 | * @param array $options |
||
539 | * |
||
540 | * @return \Illuminate\Support\HtmlString |
||
541 | */ |
||
542 | 9 | public function time($name, $value = null, array $options = []) |
|
546 | |||
547 | /** |
||
548 | * Create a url input field. |
||
549 | * |
||
550 | * @param string $name |
||
551 | * @param string $value |
||
552 | * @param array $options |
||
553 | * |
||
554 | * @return \Illuminate\Support\HtmlString |
||
555 | */ |
||
556 | 6 | public function url($name, $value = null, array $options = []) |
|
560 | |||
561 | /** |
||
562 | * Create a file input field. |
||
563 | * |
||
564 | * @param string $name |
||
565 | * @param array $options |
||
566 | * |
||
567 | * @return \Illuminate\Support\HtmlString |
||
568 | */ |
||
569 | 9 | public function file($name, array $options = []) |
|
573 | |||
574 | /** |
||
575 | * Create a textarea input field. |
||
576 | * |
||
577 | * @param string $name |
||
578 | * @param string $value |
||
579 | * @param array $attributes |
||
580 | * |
||
581 | * @return \Illuminate\Support\HtmlString |
||
582 | */ |
||
583 | 18 | public function textarea($name, $value = null, array $attributes = []) |
|
601 | |||
602 | /** |
||
603 | * Set the text area size on the attributes. |
||
604 | * |
||
605 | * @param array $attributes |
||
606 | * |
||
607 | * @return array |
||
608 | */ |
||
609 | 18 | private function setTextAreaSize(array $attributes) |
|
622 | |||
623 | /** |
||
624 | * Set the text area size using the quick "size" attribute. |
||
625 | * |
||
626 | * @param array $attributes |
||
627 | * |
||
628 | * @return array |
||
629 | */ |
||
630 | 9 | protected function setQuickTextAreaSize(array $attributes) |
|
636 | |||
637 | /** |
||
638 | * Create a select box field. |
||
639 | * |
||
640 | * @param string $name |
||
641 | * @param array|\Illuminate\Support\Collection $list |
||
642 | * @param string|bool $selected |
||
643 | * @param array $attributes |
||
644 | * @param array $optionsAttributes |
||
645 | * @param array $optgroupsAttributes |
||
646 | * |
||
647 | * @return \Illuminate\Support\HtmlString |
||
648 | */ |
||
649 | 45 | public function select( |
|
673 | |||
674 | /** |
||
675 | * Create a select range field. |
||
676 | * |
||
677 | * @param string $name |
||
678 | * @param string $begin |
||
679 | * @param string $end |
||
680 | * @param string $selected |
||
681 | * @param array $attributes |
||
682 | * |
||
683 | * @return \Illuminate\Support\HtmlString |
||
684 | */ |
||
685 | 6 | public function selectRange($name, $begin, $end, $selected = null, array $attributes = []) |
|
691 | |||
692 | /** |
||
693 | * Create a select year field. |
||
694 | * |
||
695 | * @param string $name |
||
696 | * @param string $begin |
||
697 | * @param string $end |
||
698 | * @param string $selected |
||
699 | * @param array $attributes |
||
700 | * |
||
701 | * @return \Illuminate\Support\HtmlString |
||
702 | */ |
||
703 | 3 | public function selectYear($name, $begin, $end, $selected = null, array $attributes = []) |
|
707 | |||
708 | /** |
||
709 | * Create a select month field. |
||
710 | * |
||
711 | * @param string $name |
||
712 | * @param string $selected |
||
713 | * @param array $attributes |
||
714 | * @param string $format |
||
715 | * |
||
716 | * @return \Illuminate\Support\HtmlString |
||
717 | */ |
||
718 | 3 | public function selectMonth($name, $selected = null, array $attributes = [], $format = '%B') |
|
728 | |||
729 | /** |
||
730 | * Create a checkbox input field. |
||
731 | * |
||
732 | * @param string $name |
||
733 | * @param mixed $value |
||
734 | * @param bool|null $checked |
||
735 | * @param array $attributes |
||
736 | * |
||
737 | * @return \Illuminate\Support\HtmlString |
||
738 | */ |
||
739 | 12 | public function checkbox($name, $value = 1, $checked = null, array $attributes = []) |
|
743 | |||
744 | /** |
||
745 | * Create a radio button input field. |
||
746 | * |
||
747 | * @param string $name |
||
748 | * @param mixed $value |
||
749 | * @param bool $checked |
||
750 | * @param array $options |
||
751 | * |
||
752 | * @return \Illuminate\Support\HtmlString |
||
753 | */ |
||
754 | 6 | public function radio($name, $value = null, $checked = null, array $options = []) |
|
758 | |||
759 | /** |
||
760 | * Create a HTML reset input element. |
||
761 | * |
||
762 | * @param string|mixed $value |
||
763 | * @param array $attributes |
||
764 | * |
||
765 | * @return \Illuminate\Support\HtmlString |
||
766 | */ |
||
767 | 3 | public function reset($value, array $attributes = []) |
|
771 | |||
772 | /** |
||
773 | * Create a HTML image input element. |
||
774 | * |
||
775 | * @param string $url |
||
776 | * @param string|null $name |
||
777 | * @param array $attributes |
||
778 | * |
||
779 | * @return \Illuminate\Support\HtmlString |
||
780 | */ |
||
781 | 3 | public function image($url, $name = null, array $attributes = []) |
|
787 | |||
788 | /** |
||
789 | * Create a submit button element. |
||
790 | * |
||
791 | * @param string|mixed $value |
||
792 | * @param array $attributes |
||
793 | * |
||
794 | * @return \Illuminate\Support\HtmlString |
||
795 | */ |
||
796 | 3 | public function submit($value = null, array $attributes = []) |
|
800 | |||
801 | /** |
||
802 | * Create a button element. |
||
803 | * |
||
804 | * @param string|mixed $value |
||
805 | * @param array $attributes |
||
806 | * |
||
807 | * @return \Illuminate\Support\HtmlString |
||
808 | */ |
||
809 | 3 | public function button($value = null, array $attributes = []) |
|
817 | |||
818 | /** |
||
819 | * Create a color input field. |
||
820 | * |
||
821 | * @param string $name |
||
822 | * @param string|mixed $value |
||
823 | * @param array $attributes |
||
824 | * |
||
825 | * @return \Illuminate\Support\HtmlString |
||
826 | */ |
||
827 | 9 | public function color($name, $value = null, array $attributes = []) |
|
831 | |||
832 | /* ----------------------------------------------------------------- |
||
833 | | Other Methods |
||
834 | | ----------------------------------------------------------------- |
||
835 | */ |
||
836 | |||
837 | /** |
||
838 | * Create a checkable input field. |
||
839 | * |
||
840 | * @param string $type |
||
841 | * @param string $name |
||
842 | * @param mixed $value |
||
843 | * @param bool|null $checked |
||
844 | * @param array $attributes |
||
845 | * |
||
846 | * @return \Illuminate\Support\HtmlString |
||
847 | */ |
||
848 | 21 | protected function checkable($type, $name, $value, $checked, array $attributes) |
|
857 | |||
858 | /** |
||
859 | * Get the check state for a checkable input. |
||
860 | * |
||
861 | * @param string $type |
||
862 | * @param string $name |
||
863 | * @param mixed $value |
||
864 | * @param bool|null $checked |
||
865 | * |
||
866 | * @return bool |
||
867 | */ |
||
868 | 21 | private function getCheckedState($type, $name, $value, $checked) |
|
881 | |||
882 | /** |
||
883 | * Get the check state for a checkbox input. |
||
884 | * |
||
885 | * @param string $name |
||
886 | * @param mixed $value |
||
887 | * @param bool|null $checked |
||
888 | * |
||
889 | * @return bool |
||
890 | */ |
||
891 | 12 | private function getCheckboxCheckedState($name, $value, $checked) |
|
913 | |||
914 | /** |
||
915 | * Get the check state for a radio input. |
||
916 | * |
||
917 | * @param string $name |
||
918 | * @param mixed $value |
||
919 | * @param bool|null $checked |
||
920 | * |
||
921 | * @return bool |
||
922 | */ |
||
923 | 6 | private function getRadioCheckedState($name, $value, $checked) |
|
929 | |||
930 | /** |
||
931 | * Determine if old input or model input exists for a key. |
||
932 | * |
||
933 | * @param string $name |
||
934 | * |
||
935 | * @return bool |
||
936 | */ |
||
937 | 18 | private function missingOldAndModel($name) |
|
941 | |||
942 | /** |
||
943 | * Get the form action from the options. |
||
944 | * |
||
945 | * @param array $attributes |
||
946 | * |
||
947 | * @return string |
||
948 | */ |
||
949 | 57 | private function getAction(array $attributes) |
|
962 | |||
963 | /** |
||
964 | * Get the action for a "url" option. |
||
965 | * |
||
966 | * @param array|string $attribute |
||
967 | * |
||
968 | * @return string |
||
969 | */ |
||
970 | 18 | private function getUrlAction($attribute) |
|
976 | |||
977 | /** |
||
978 | * Get the action for a "route" option. |
||
979 | * |
||
980 | * @param array|string $attribute |
||
981 | * |
||
982 | * @return string |
||
983 | */ |
||
984 | 6 | private function getRouteAction($attribute) |
|
990 | |||
991 | /** |
||
992 | * Get the action for an "action" option. |
||
993 | * |
||
994 | * @param array|string $attribute |
||
995 | * |
||
996 | * @return string |
||
997 | */ |
||
998 | 6 | private function getControllerAction($attribute) |
|
1004 | } |
||
1005 |
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: