Complex classes like Form 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 Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Form |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * All fields that are added. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $fields = []; |
||
25 | |||
26 | /** |
||
27 | * Model to use. |
||
28 | * |
||
29 | * @var mixed |
||
30 | */ |
||
31 | protected $model = []; |
||
32 | |||
33 | /** |
||
34 | * @var EventDispatcher |
||
35 | */ |
||
36 | protected $eventDispatcher; |
||
37 | |||
38 | /** |
||
39 | * @var FormHelper |
||
40 | */ |
||
41 | protected $formHelper; |
||
42 | |||
43 | /** |
||
44 | * Form options. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $formOptions = [ |
||
49 | 'method' => 'GET', |
||
50 | 'url' => null |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Additional data which can be used to build fields. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $data = []; |
||
59 | |||
60 | /** |
||
61 | * Wether errors for each field should be shown when calling form($form) or form_rest($form). |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $showFieldErrors = true; |
||
66 | |||
67 | /** |
||
68 | * Enable html5 validation. |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $clientValidationEnabled = true; |
||
73 | |||
74 | /** |
||
75 | * Name of the parent form if any. |
||
76 | * |
||
77 | * @var string|null |
||
78 | */ |
||
79 | protected $name = null; |
||
80 | |||
81 | /** |
||
82 | * @var FormBuilder |
||
83 | */ |
||
84 | protected $formBuilder; |
||
85 | |||
86 | /** |
||
87 | * @var ValidatorFactory |
||
88 | */ |
||
89 | protected $validatorFactory; |
||
90 | |||
91 | /** |
||
92 | * @var Validator |
||
93 | */ |
||
94 | protected $validator = null; |
||
95 | |||
96 | /** |
||
97 | * @var Request |
||
98 | */ |
||
99 | protected $request; |
||
100 | |||
101 | /** |
||
102 | * List of fields to not render. |
||
103 | * |
||
104 | * @var array |
||
105 | **/ |
||
106 | protected $exclude = []; |
||
107 | |||
108 | /** |
||
109 | * Wether the form is beign rebuild. |
||
110 | * |
||
111 | * @var bool |
||
112 | */ |
||
113 | protected $rebuilding = false; |
||
114 | |||
115 | /** |
||
116 | * @var string |
||
117 | */ |
||
118 | protected $templatePrefix; |
||
119 | |||
120 | /** |
||
121 | * @var string |
||
122 | */ |
||
123 | protected $languageName; |
||
124 | |||
125 | /** |
||
126 | * Build the form. |
||
127 | * |
||
128 | * @return mixed |
||
129 | */ |
||
130 | 3 | public function buildForm() |
|
133 | |||
134 | /** |
||
135 | * Rebuild the form from scratch. |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | 17 | public function rebuildForm() |
|
157 | |||
158 | /** |
||
159 | * Create the FormField object. |
||
160 | * |
||
161 | * @param string $name |
||
162 | * @param string $type |
||
163 | * @param array $options |
||
164 | * @return FormField |
||
165 | */ |
||
166 | 51 | protected function makeField($name, $type = 'text', array $options = []) |
|
180 | |||
181 | /** |
||
182 | * Create a new field and add it to the form. |
||
183 | * |
||
184 | * @param string $name |
||
185 | * @param string $type |
||
186 | * @param array $options |
||
187 | * @param bool $modify |
||
188 | * @return $this |
||
189 | */ |
||
190 | 53 | public function add($name, $type = 'text', array $options = [], $modify = false) |
|
202 | |||
203 | /** |
||
204 | * Add a FormField to the form's fields. |
||
205 | * |
||
206 | * @param FormField $field |
||
207 | * @return $this |
||
208 | */ |
||
209 | 47 | protected function addField(FormField $field, $modify = false) |
|
224 | |||
225 | /** |
||
226 | * Add field before another field. |
||
227 | * |
||
228 | * @param string $name Name of the field before which new field is added. |
||
229 | * @param string $fieldName Field name which will be added. |
||
230 | * @param string $type |
||
231 | * @param array $options |
||
232 | * @param bool $modify |
||
233 | * @return $this |
||
234 | */ |
||
235 | 1 | public function addBefore($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
250 | |||
251 | /** |
||
252 | * Add field before another field. |
||
253 | * |
||
254 | * @param string $name Name of the field after which new field is added. |
||
255 | * @param string $fieldName Field name which will be added. |
||
256 | * @param string $type |
||
257 | * @param array $options |
||
258 | * @param bool $modify |
||
259 | * @return $this |
||
260 | */ |
||
261 | 1 | public function addAfter($name, $fieldName, $type = 'text', $options = [], $modify = false) |
|
276 | |||
277 | /** |
||
278 | * Take another form and add it's fields directly to this form. |
||
279 | * |
||
280 | * @param mixed $class Form to merge. |
||
281 | * @param array $options |
||
282 | * @param boolean $modify |
||
283 | * @return $this |
||
284 | */ |
||
285 | 1 | public function compose($class, array $options = [], $modify = false) |
|
313 | |||
314 | /** |
||
315 | * Remove field with specified name from the form. |
||
316 | * |
||
317 | * @param $name |
||
318 | * @return $this |
||
319 | */ |
||
320 | 2 | public function remove($name) |
|
328 | |||
329 | /** |
||
330 | * Modify existing field. If it doesn't exist, it is added to form. |
||
331 | * |
||
332 | * @param string $name |
||
333 | * @param string $type |
||
334 | * @param array $options |
||
335 | * @param bool $overwriteOptions |
||
336 | * @return Form |
||
337 | */ |
||
338 | 1 | public function modify($name, $type = 'text', array $options = [], $overwriteOptions = false) |
|
350 | |||
351 | /** |
||
352 | * Render full form. |
||
353 | * |
||
354 | * @param array $options |
||
355 | * @param bool $showStart |
||
356 | * @param bool $showFields |
||
357 | * @param bool $showEnd |
||
358 | * @return string |
||
359 | */ |
||
360 | 7 | public function renderForm(array $options = [], $showStart = true, $showFields = true, $showEnd = true) |
|
364 | |||
365 | /** |
||
366 | * Render rest of the form. |
||
367 | * |
||
368 | * @param bool $showFormEnd |
||
369 | * @param bool $showFields |
||
370 | * @return string |
||
371 | */ |
||
372 | 1 | public function renderRest($showFormEnd = true, $showFields = true) |
|
378 | |||
379 | /** |
||
380 | * Renders the rest of the form up until the specified field name. |
||
381 | * |
||
382 | * @param string $field_name |
||
383 | * @param bool $showFormEnd |
||
384 | * @param bool $showFields |
||
385 | * @return string |
||
386 | */ |
||
387 | 2 | public function renderUntil($field_name, $showFormEnd = true, $showFields = true) |
|
407 | |||
408 | /** |
||
409 | * Get single field instance from form object. |
||
410 | * |
||
411 | * @param string $name |
||
412 | * @return FormField |
||
413 | */ |
||
414 | 25 | public function getField($name) |
|
422 | |||
423 | /** |
||
424 | * Check if form has field. |
||
425 | * |
||
426 | * @param string $name |
||
427 | * @return bool |
||
428 | */ |
||
429 | 47 | public function has($name) |
|
433 | |||
434 | /** |
||
435 | * Check if form has custom field |
||
436 | * |
||
437 | * @param $name |
||
438 | * @return bool |
||
439 | */ |
||
440 | public function hasCustom($name) |
||
444 | |||
445 | /** |
||
446 | * Get all form options. |
||
447 | * |
||
448 | * @return array |
||
449 | */ |
||
450 | 2 | public function getFormOptions() |
|
454 | |||
455 | /** |
||
456 | * Get single form option. |
||
457 | * |
||
458 | * @param string $option |
||
459 | * @param mixed|null $default |
||
460 | * @return mixed |
||
461 | */ |
||
462 | 107 | public function getFormOption($option, $default = null) |
|
466 | |||
467 | /** |
||
468 | * Set single form option on form. |
||
469 | * |
||
470 | * @param string $option |
||
471 | * @param mixed $value |
||
472 | * |
||
473 | * @return $this |
||
474 | */ |
||
475 | 2 | public function setFormOption($option, $value) |
|
481 | |||
482 | /** |
||
483 | * Set form options. |
||
484 | * |
||
485 | * @param array $formOptions |
||
486 | * @return $this |
||
487 | */ |
||
488 | 107 | public function setFormOptions(array $formOptions) |
|
501 | |||
502 | /** |
||
503 | * Get an option from provided options and call method with that value. |
||
504 | * |
||
505 | * @param string $name |
||
506 | * @param string $method |
||
507 | */ |
||
508 | 107 | protected function pullFromOptions($name, $method) |
|
514 | |||
515 | /** |
||
516 | * Get form http method. |
||
517 | * |
||
518 | * @return string |
||
519 | */ |
||
520 | 3 | public function getMethod() |
|
524 | |||
525 | /** |
||
526 | * Set form http method. |
||
527 | * |
||
528 | * @param string $method |
||
529 | * @return $this |
||
530 | */ |
||
531 | 1 | public function setMethod($method) |
|
537 | |||
538 | /** |
||
539 | * Get form action url. |
||
540 | * |
||
541 | * @return string |
||
542 | */ |
||
543 | 3 | public function getUrl() |
|
547 | |||
548 | /** |
||
549 | * Set form action url. |
||
550 | * |
||
551 | * @param string $url |
||
552 | * @return $this |
||
553 | */ |
||
554 | 1 | public function setUrl($url) |
|
560 | |||
561 | /** |
||
562 | * Returns the name of the form. |
||
563 | * |
||
564 | * @return string|null |
||
565 | */ |
||
566 | 52 | public function getName() |
|
570 | |||
571 | /** |
||
572 | * Set the name of the form. |
||
573 | * |
||
574 | * @param string $name |
||
575 | * @param bool $rebuild |
||
576 | * @return $this |
||
577 | */ |
||
578 | 10 | public function setName($name, $rebuild = true) |
|
588 | |||
589 | /** |
||
590 | * Get model that is bind to form object. |
||
591 | * |
||
592 | * @return mixed |
||
593 | */ |
||
594 | 81 | public function getModel() |
|
598 | |||
599 | /** |
||
600 | * Set model to form object. |
||
601 | * |
||
602 | * @param mixed $model |
||
603 | * @return $this |
||
604 | * @deprecated deprecated since 1.6.31, will be removed in 1.7 - pass model as option when creating a form |
||
605 | */ |
||
606 | 15 | public function setModel($model) |
|
614 | |||
615 | /** |
||
616 | * Setup model for form, add namespace if needed for child forms. |
||
617 | * |
||
618 | * @return $this |
||
619 | */ |
||
620 | 12 | protected function setupModel($model) |
|
626 | |||
627 | /** |
||
628 | * Get all fields. |
||
629 | * |
||
630 | * @return FormField[] |
||
631 | */ |
||
632 | 29 | public function getFields() |
|
636 | |||
637 | /** |
||
638 | * Get field dynamically. |
||
639 | * |
||
640 | * @param string $name |
||
641 | * @return FormField |
||
642 | */ |
||
643 | 19 | public function __get($name) |
|
649 | |||
650 | /** |
||
651 | * Check if field exists when fetched using magic methods. |
||
652 | * |
||
653 | * @param string $name |
||
654 | * @return bool |
||
655 | */ |
||
656 | public function __isset($name) |
||
660 | |||
661 | /** |
||
662 | * Set the Event Dispatcher to fire Laravel events. |
||
663 | * |
||
664 | * @param EventDispatcher $eventDispatcher |
||
665 | * @return $this |
||
666 | */ |
||
667 | 107 | public function setEventDispatcher(EventDispatcher $eventDispatcher) |
|
673 | |||
674 | /** |
||
675 | * Set the form helper only on first instantiation. |
||
676 | * |
||
677 | * @param FormHelper $formHelper |
||
678 | * @return $this |
||
679 | */ |
||
680 | 107 | public function setFormHelper(FormHelper $formHelper) |
|
686 | |||
687 | /** |
||
688 | * Get form helper. |
||
689 | * |
||
690 | * @return FormHelper |
||
691 | */ |
||
692 | 85 | public function getFormHelper() |
|
696 | |||
697 | /** |
||
698 | * Add custom field. |
||
699 | * |
||
700 | * @param $name |
||
701 | * @param $class |
||
702 | */ |
||
703 | 2 | public function addCustomField($name, $class) |
|
711 | |||
712 | /** |
||
713 | * Returns wether form errors should be shown under every field. |
||
714 | * |
||
715 | * @return bool |
||
716 | */ |
||
717 | 85 | public function haveErrorsEnabled() |
|
721 | |||
722 | /** |
||
723 | * Enable or disable showing errors under fields |
||
724 | * |
||
725 | * @param bool $enabled |
||
726 | * @return $this |
||
727 | */ |
||
728 | 1 | public function setErrorsEnabled($enabled) |
|
734 | |||
735 | /** |
||
736 | * Is client validation enabled? |
||
737 | * |
||
738 | * @return bool |
||
739 | */ |
||
740 | 21 | public function clientValidationEnabled() |
|
744 | |||
745 | /** |
||
746 | * Enable/disable client validation. |
||
747 | * |
||
748 | * @param bool $enable |
||
749 | * @return $this |
||
750 | */ |
||
751 | 2 | public function setClientValidationEnabled($enable) |
|
757 | |||
758 | /** |
||
759 | * Add any aditional data that field needs (ex. array of choices). |
||
760 | * |
||
761 | * @deprecated deprecated since 1.6.20, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data |
||
762 | * will be switched to protected in 1.7. |
||
763 | * @param string $name |
||
764 | * @param mixed $data |
||
765 | */ |
||
766 | 1 | public function setData($name, $data) |
|
770 | |||
771 | /** |
||
772 | * Get single additional data. |
||
773 | * |
||
774 | * @param string $name |
||
775 | * @param null $default |
||
776 | * @return mixed |
||
777 | */ |
||
778 | 18 | public function getData($name = null, $default = null) |
|
786 | |||
787 | /** |
||
788 | * Add multiple peices of data at once. |
||
789 | * |
||
790 | * @deprecated deprecated since 1.6.12, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data |
||
791 | * will be switched to protected in 1.7. |
||
792 | * @param $data |
||
793 | * @return $this |
||
794 | **/ |
||
795 | 107 | public function addData(array $data) |
|
803 | |||
804 | /** |
||
805 | * Get current request. |
||
806 | * |
||
807 | * @return \Illuminate\Http\Request |
||
808 | */ |
||
809 | 85 | public function getRequest() |
|
813 | |||
814 | /** |
||
815 | * Set request on form. |
||
816 | * |
||
817 | * @param Request $request |
||
818 | * @return $this |
||
819 | */ |
||
820 | 107 | public function setRequest(Request $request) |
|
826 | |||
827 | /** |
||
828 | * Get template prefix that is prepended to all template paths. |
||
829 | * |
||
830 | * @return string |
||
831 | */ |
||
832 | 37 | public function getTemplatePrefix() |
|
840 | |||
841 | /** |
||
842 | * Set a template prefix for the form and its fields. |
||
843 | * |
||
844 | * @param string $prefix |
||
845 | * @return $this |
||
846 | */ |
||
847 | 4 | public function setTemplatePrefix($prefix) |
|
853 | |||
854 | /** |
||
855 | * Get the language name. |
||
856 | * |
||
857 | * @return string |
||
858 | */ |
||
859 | 83 | public function getLanguageName() |
|
863 | |||
864 | /** |
||
865 | * Set a language name, used as prefix for translated strings. |
||
866 | * |
||
867 | * @param string $prefix |
||
868 | * @return $this |
||
869 | */ |
||
870 | 11 | public function setLanguageName($prefix) |
|
876 | |||
877 | /** |
||
878 | * Render the form. |
||
879 | * |
||
880 | * @param array $options |
||
881 | * @param string $fields |
||
882 | * @param bool $showStart |
||
883 | * @param bool $showFields |
||
884 | * @param bool $showEnd |
||
885 | * @return string |
||
886 | */ |
||
887 | 9 | protected function render($options, $fields, $showStart, $showFields, $showEnd) |
|
903 | |||
904 | /** |
||
905 | * Get template from options if provided, otherwise fallback to config. |
||
906 | * |
||
907 | * @return mixed |
||
908 | */ |
||
909 | 9 | protected function getTemplate() |
|
913 | |||
914 | /** |
||
915 | * Get all fields that are not rendered. |
||
916 | * |
||
917 | * @return array |
||
918 | */ |
||
919 | 2 | protected function getUnrenderedFields() |
|
932 | |||
933 | /** |
||
934 | * Prevent adding fields with same name. |
||
935 | * |
||
936 | * @param string $name |
||
937 | * @throws \InvalidArgumentException |
||
938 | * @return void |
||
939 | */ |
||
940 | 47 | protected function preventDuplicate($name) |
|
946 | |||
947 | /** |
||
948 | * Returns and checks the type of the field. |
||
949 | * |
||
950 | * @param string $type |
||
951 | * @return string |
||
952 | */ |
||
953 | 51 | protected function getFieldType($type) |
|
959 | |||
960 | /** |
||
961 | * Check if form is named form. |
||
962 | * |
||
963 | * @return void |
||
964 | */ |
||
965 | 107 | protected function checkIfNamedForm() |
|
971 | |||
972 | /** |
||
973 | * Set up options on single field depending on form options. |
||
974 | * |
||
975 | * @param string $name |
||
976 | * @param $options |
||
977 | */ |
||
978 | 51 | protected function setupFieldOptions($name, &$options) |
|
982 | |||
983 | /** |
||
984 | * Set namespace to model if form is named so the data is bound properly. |
||
985 | * Returns true if model is changed, otherwise false. |
||
986 | * |
||
987 | * @return bool |
||
988 | */ |
||
989 | 9 | protected function setupNamedModel() |
|
1008 | |||
1009 | |||
1010 | /** |
||
1011 | * Set form builder instance on helper so we can use it later. |
||
1012 | * |
||
1013 | * @param FormBuilder $formBuilder |
||
1014 | * @return $this |
||
1015 | */ |
||
1016 | 107 | public function setFormBuilder(FormBuilder $formBuilder) |
|
1022 | |||
1023 | /** |
||
1024 | * Returns the instance of the FormBuilder. |
||
1025 | * |
||
1026 | * @return FormBuilder |
||
1027 | */ |
||
1028 | 12 | public function getFormBuilder() |
|
1032 | |||
1033 | /** |
||
1034 | * Set the Validator instance on this so we can use it later. |
||
1035 | * |
||
1036 | * @param ValidatorFactory $validator |
||
1037 | * @return $this |
||
1038 | */ |
||
1039 | 107 | public function setValidator(ValidatorFactory $validator) |
|
1045 | |||
1046 | /** |
||
1047 | * Returns the validator instance. |
||
1048 | * |
||
1049 | * @return Validator |
||
1050 | */ |
||
1051 | 1 | public function getValidator() |
|
1055 | |||
1056 | /** |
||
1057 | * Exclude some fields from rendering. |
||
1058 | * |
||
1059 | * @return $this |
||
1060 | */ |
||
1061 | public function exclude(array $fields) |
||
1067 | |||
1068 | |||
1069 | /** |
||
1070 | * If form is named form, modify names to be contained in single key (parent[child_field_name]). |
||
1071 | * |
||
1072 | * @param string $name |
||
1073 | * @return string |
||
1074 | */ |
||
1075 | 51 | protected function getFieldName($name) |
|
1092 | |||
1093 | /** |
||
1094 | * Disable all fields in a form. |
||
1095 | */ |
||
1096 | 1 | public function disableFields() |
|
1102 | |||
1103 | /** |
||
1104 | * Enable all fields in a form. |
||
1105 | */ |
||
1106 | 1 | public function enableFields() |
|
1112 | |||
1113 | /** |
||
1114 | * Validate the form. |
||
1115 | * |
||
1116 | * @param array $validationRules |
||
1117 | * @param array $messages |
||
1118 | * @return Validator |
||
1119 | */ |
||
1120 | 8 | public function validate($validationRules = [], $messages = []) |
|
1133 | |||
1134 | /** |
||
1135 | * Get validation rules for the form. |
||
1136 | * |
||
1137 | * @param array $overrideRules |
||
1138 | * @return array |
||
1139 | */ |
||
1140 | 1 | public function getRules($overrideRules = []) |
|
1146 | |||
1147 | /** |
||
1148 | * Redirects to a destination when form is invalid. |
||
1149 | * |
||
1150 | * @param string|null $destination The target url. |
||
1151 | * @return HttpResponseException |
||
1152 | */ |
||
1153 | 2 | public function redirectIfNotValid($destination = null) |
|
1167 | |||
1168 | /** |
||
1169 | * Get all form field attributes, including child forms, in a flat array. |
||
1170 | * |
||
1171 | * @return array |
||
1172 | */ |
||
1173 | 3 | public function getAllAttributes() |
|
1177 | |||
1178 | /** |
||
1179 | * Check if the form is valid. |
||
1180 | * |
||
1181 | * @return bool |
||
1182 | */ |
||
1183 | 8 | public function isValid() |
|
1197 | |||
1198 | /** |
||
1199 | * Optionally change the validation result, and/or add error messages. |
||
1200 | * |
||
1201 | * @param Form $mainForm |
||
1202 | * @param bool $isValid |
||
1203 | * @return void|array |
||
1204 | */ |
||
1205 | 8 | public function alterValid(Form $mainForm, &$isValid) |
|
1209 | |||
1210 | /** |
||
1211 | * Get validation errors. |
||
1212 | * |
||
1213 | * @return array |
||
1214 | */ |
||
1215 | 7 | public function getErrors() |
|
1228 | |||
1229 | /** |
||
1230 | * Get all Request values from all fields, and nothing else. |
||
1231 | * |
||
1232 | * @param bool $with_nulls |
||
1233 | * @return array |
||
1234 | */ |
||
1235 | 3 | public function getFieldValues($with_nulls = true) |
|
1258 | |||
1259 | /** |
||
1260 | * Optionally mess with this form's $values before it's returned from getFieldValues(). |
||
1261 | * |
||
1262 | * @param array $values |
||
1263 | * @return void |
||
1264 | */ |
||
1265 | 3 | public function alterFieldValues(array &$values) |
|
1268 | |||
1269 | /** |
||
1270 | * Throw an exception indicating a field does not exist on the class. |
||
1271 | * |
||
1272 | * @param string $name |
||
1273 | * @throws \InvalidArgumentException |
||
1274 | * @return void |
||
1275 | */ |
||
1276 | 2 | protected function fieldDoesNotExist($name) |
|
1280 | } |
||
1281 |
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: