Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
72 | class Form implements Renderable |
||
73 | { |
||
74 | /** |
||
75 | * Eloquent model of the form. |
||
76 | * |
||
77 | * @var Model |
||
78 | */ |
||
79 | protected $model; |
||
80 | |||
81 | /** |
||
82 | * @var \Illuminate\Validation\Validator |
||
83 | */ |
||
84 | protected $validator; |
||
85 | |||
86 | /** |
||
87 | * @var Builder |
||
88 | */ |
||
89 | protected $builder; |
||
90 | |||
91 | /** |
||
92 | * Submitted callback. |
||
93 | * |
||
94 | * @var Closure[] |
||
95 | */ |
||
96 | protected $submitted = []; |
||
97 | |||
98 | /** |
||
99 | * Saving callback. |
||
100 | * |
||
101 | * @var Closure[] |
||
102 | */ |
||
103 | protected $saving = []; |
||
104 | |||
105 | /** |
||
106 | * Saved callback. |
||
107 | * |
||
108 | * @var Closure[] |
||
109 | */ |
||
110 | protected $saved = []; |
||
111 | |||
112 | /** |
||
113 | * Data for save to current model from input. |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $updates = []; |
||
118 | |||
119 | /** |
||
120 | * Data for save to model's relations from input. |
||
121 | * |
||
122 | * @var array |
||
123 | */ |
||
124 | protected $relations = []; |
||
125 | |||
126 | /** |
||
127 | * Input data. |
||
128 | * |
||
129 | * @var array |
||
130 | */ |
||
131 | protected $inputs = []; |
||
132 | |||
133 | /** |
||
134 | * Available fields. |
||
135 | * |
||
136 | * @var array |
||
137 | */ |
||
138 | public static $availableFields = []; |
||
139 | |||
140 | /** |
||
141 | * Ignored saving fields. |
||
142 | * |
||
143 | * @var array |
||
144 | */ |
||
145 | protected $ignored = []; |
||
146 | |||
147 | /** |
||
148 | * Collected field assets. |
||
149 | * |
||
150 | * @var array |
||
151 | */ |
||
152 | protected static $collectedAssets = []; |
||
153 | |||
154 | /** |
||
155 | * @var Form\Tab |
||
156 | */ |
||
157 | protected $tab = null; |
||
158 | |||
159 | /** |
||
160 | * Remove flag in `has many` form. |
||
161 | */ |
||
162 | const REMOVE_FLAG_NAME = '_remove_'; |
||
163 | |||
164 | /** |
||
165 | * Field rows in form. |
||
166 | * |
||
167 | * @var array |
||
168 | */ |
||
169 | public $rows = []; |
||
170 | |||
171 | /** |
||
172 | * Create a new form instance. |
||
173 | * |
||
174 | * @param $model |
||
175 | * @param \Closure $callback |
||
176 | */ |
||
177 | public function __construct($model, Closure $callback = null) |
||
187 | |||
188 | /** |
||
189 | * @param Field $field |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function pushField(Field $field) |
||
201 | |||
202 | /** |
||
203 | * @return Model |
||
204 | */ |
||
205 | public function model() |
||
209 | |||
210 | /** |
||
211 | * @return Builder |
||
212 | */ |
||
213 | public function builder() |
||
217 | |||
218 | /** |
||
219 | * Generate a edit form. |
||
220 | * |
||
221 | * @param $id |
||
222 | * |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function edit($id) |
||
234 | |||
235 | /** |
||
236 | * Use tab to split form. |
||
237 | * |
||
238 | * @param string $title |
||
239 | * @param Closure $content |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function tab($title, Closure $content, $active = false) |
||
249 | |||
250 | /** |
||
251 | * Get Tab instance. |
||
252 | * |
||
253 | * @return Tab |
||
254 | */ |
||
255 | public function getTab() |
||
263 | |||
264 | /** |
||
265 | * Destroy data entity and remove files. |
||
266 | * |
||
267 | * @param $id |
||
268 | * |
||
269 | * @return mixed |
||
270 | */ |
||
271 | public function destroy($id) |
||
282 | |||
283 | /** |
||
284 | * Remove files in record. |
||
285 | * |
||
286 | * @param $id |
||
287 | */ |
||
288 | protected function deleteFiles($id) |
||
308 | |||
309 | /** |
||
310 | * Store a new record. |
||
311 | * |
||
312 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
313 | */ |
||
314 | public function store() |
||
349 | |||
350 | /** |
||
351 | * Get ajax response. |
||
352 | * |
||
353 | * @param string $message |
||
354 | * |
||
355 | * @return bool|\Illuminate\Http\JsonResponse |
||
356 | */ |
||
357 | protected function ajaxResponse($message) |
||
371 | |||
372 | /** |
||
373 | * Prepare input data for insert or update. |
||
374 | * |
||
375 | * @param array $data |
||
376 | * |
||
377 | * @return mixed |
||
378 | */ |
||
379 | protected function prepare($data = []) |
||
395 | |||
396 | /** |
||
397 | * Remove ignored fields from input. |
||
398 | * |
||
399 | * @param array $input |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | protected function removeIgnoredFields($input) |
||
409 | |||
410 | /** |
||
411 | * Get inputs for relations. |
||
412 | * |
||
413 | * @param array $inputs |
||
414 | * |
||
415 | * @return array |
||
416 | */ |
||
417 | protected function getRelationInputs($inputs = []) |
||
433 | |||
434 | /** |
||
435 | * Call submitted callback. |
||
436 | * |
||
437 | * @return mixed |
||
438 | */ |
||
439 | protected function callSubmitted() |
||
447 | |||
448 | /** |
||
449 | * Call saving callback. |
||
450 | * |
||
451 | * @return mixed |
||
452 | */ |
||
453 | protected function callSaving() |
||
461 | |||
462 | /** |
||
463 | * Callback after saving a Model. |
||
464 | * |
||
465 | * @return mixed|null |
||
466 | */ |
||
467 | protected function callSaved() |
||
475 | |||
476 | /** |
||
477 | * Handle update. |
||
478 | * |
||
479 | * @param int $id |
||
480 | * |
||
481 | * @return \Symfony\Component\HttpFoundation\Response |
||
482 | */ |
||
483 | public function update($id, $data = null) |
||
541 | |||
542 | /** |
||
543 | * Get RedirectResponse after store. |
||
544 | * |
||
545 | * @return \Illuminate\Http\RedirectResponse |
||
546 | */ |
||
547 | protected function redirectAfterStore() |
||
555 | |||
556 | /** |
||
557 | * Get RedirectResponse after update. |
||
558 | * |
||
559 | * @param mixed $key |
||
560 | * |
||
561 | * @return \Illuminate\Http\RedirectResponse |
||
562 | */ |
||
563 | protected function redirectAfterUpdate($key) |
||
569 | |||
570 | /** |
||
571 | * Get RedirectResponse after data saving. |
||
572 | * |
||
573 | * @param string $resourcesPath |
||
574 | * @param string $key |
||
575 | * |
||
576 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
577 | */ |
||
578 | protected function redirectAfterSaving($resourcesPath, $key) |
||
594 | |||
595 | /** |
||
596 | * Check if request is from editable. |
||
597 | * |
||
598 | * @param array $input |
||
599 | * |
||
600 | * @return bool |
||
601 | */ |
||
602 | protected function isEditable(array $input = []) |
||
606 | |||
607 | /** |
||
608 | * Handle editable update. |
||
609 | * |
||
610 | * @param array $input |
||
611 | * |
||
612 | * @return array |
||
613 | */ |
||
614 | protected function handleEditable(array $input = []) |
||
626 | |||
627 | /** |
||
628 | * @param array $input |
||
629 | * |
||
630 | * @return array |
||
631 | */ |
||
632 | protected function handleFileDelete(array $input = []) |
||
643 | |||
644 | /** |
||
645 | * Handle orderable update. |
||
646 | * |
||
647 | * @param int $id |
||
648 | * @param array $input |
||
649 | * |
||
650 | * @return bool |
||
651 | */ |
||
652 | protected function handleOrderable($id, array $input = []) |
||
666 | |||
667 | /** |
||
668 | * Update relation data. |
||
669 | * |
||
670 | * @param array $relationsData |
||
671 | * |
||
672 | * @return void |
||
673 | */ |
||
674 | protected function updateRelation($relationsData) |
||
755 | |||
756 | /** |
||
757 | * Prepare input data for update. |
||
758 | * |
||
759 | * @param array $updates |
||
760 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
761 | * |
||
762 | * @return array |
||
763 | */ |
||
764 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
796 | |||
797 | /** |
||
798 | * @param string|array $columns |
||
799 | * @param bool $oneToOneRelation |
||
800 | * |
||
801 | * @return bool |
||
802 | */ |
||
803 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
814 | |||
815 | /** |
||
816 | * Prepare input data for insert. |
||
817 | * |
||
818 | * @param $inserts |
||
819 | * |
||
820 | * @return array |
||
821 | */ |
||
822 | protected function prepareInsert($inserts) |
||
845 | |||
846 | /** |
||
847 | * Is input data is has-one relation. |
||
848 | * |
||
849 | * @param array $inserts |
||
850 | * |
||
851 | * @return bool |
||
852 | */ |
||
853 | protected function isHasOneRelation($inserts) |
||
867 | |||
868 | /** |
||
869 | * Set submitted callback. |
||
870 | * |
||
871 | * @param Closure $callback |
||
872 | * |
||
873 | * @return void |
||
874 | */ |
||
875 | public function submitted(Closure $callback) |
||
879 | |||
880 | /** |
||
881 | * Set saving callback. |
||
882 | * |
||
883 | * @param Closure $callback |
||
884 | * |
||
885 | * @return void |
||
886 | */ |
||
887 | public function saving(Closure $callback) |
||
891 | |||
892 | /** |
||
893 | * Set saved callback. |
||
894 | * |
||
895 | * @param Closure $callback |
||
896 | * |
||
897 | * @return void |
||
898 | */ |
||
899 | public function saved(Closure $callback) |
||
903 | |||
904 | /** |
||
905 | * Ignore fields to save. |
||
906 | * |
||
907 | * @param string|array $fields |
||
908 | * |
||
909 | * @return $this |
||
910 | */ |
||
911 | public function ignore($fields) |
||
917 | |||
918 | /** |
||
919 | * @param array $data |
||
920 | * @param string|array $columns |
||
921 | * |
||
922 | * @return array|mixed |
||
923 | */ |
||
924 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
942 | |||
943 | /** |
||
944 | * Find field object by column. |
||
945 | * |
||
946 | * @param $column |
||
947 | * |
||
948 | * @return mixed |
||
949 | */ |
||
950 | protected function getFieldByColumn($column) |
||
962 | |||
963 | /** |
||
964 | * Set original data for each field. |
||
965 | * |
||
966 | * @return void |
||
967 | */ |
||
968 | protected function setFieldOriginalValue() |
||
978 | |||
979 | /** |
||
980 | * Set all fields value in form. |
||
981 | * |
||
982 | * @param $id |
||
983 | * |
||
984 | * @return void |
||
985 | */ |
||
986 | protected function setFieldValue($id) |
||
1002 | |||
1003 | /** |
||
1004 | * Don't snake case attributes. |
||
1005 | * |
||
1006 | * @param Model $model |
||
1007 | * |
||
1008 | * @return void |
||
1009 | */ |
||
1010 | protected static function doNotSnakeAttributes(Model $model) |
||
1016 | |||
1017 | /** |
||
1018 | * Get validation messages. |
||
1019 | * |
||
1020 | * @param array $input |
||
1021 | * |
||
1022 | * @return MessageBag|bool |
||
1023 | */ |
||
1024 | protected function validationMessages($input) |
||
1043 | |||
1044 | /** |
||
1045 | * Merge validation messages from input validators. |
||
1046 | * |
||
1047 | * @param \Illuminate\Validation\Validator[] $validators |
||
1048 | * |
||
1049 | * @return MessageBag |
||
1050 | */ |
||
1051 | protected function mergeValidationMessages($validators) |
||
1061 | |||
1062 | /** |
||
1063 | * Get all relations of model from callable. |
||
1064 | * |
||
1065 | * @return array |
||
1066 | */ |
||
1067 | public function getRelations() |
||
1094 | |||
1095 | /** |
||
1096 | * Set action for form. |
||
1097 | * |
||
1098 | * @param string $action |
||
1099 | * |
||
1100 | * @return $this |
||
1101 | */ |
||
1102 | public function setAction($action) |
||
1108 | |||
1109 | /** |
||
1110 | * Set field and label width in current form. |
||
1111 | * |
||
1112 | * @param int $fieldWidth |
||
1113 | * @param int $labelWidth |
||
1114 | * |
||
1115 | * @return $this |
||
1116 | */ |
||
1117 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1128 | |||
1129 | /** |
||
1130 | * Set view for form. |
||
1131 | * |
||
1132 | * @param string $view |
||
1133 | * |
||
1134 | * @return $this |
||
1135 | */ |
||
1136 | public function setView($view) |
||
1142 | |||
1143 | /** |
||
1144 | * Set title for form. |
||
1145 | * |
||
1146 | * @param string $title |
||
1147 | * |
||
1148 | * @return $this |
||
1149 | */ |
||
1150 | public function setTitle($title = '') |
||
1156 | |||
1157 | /** |
||
1158 | * Add a row in form. |
||
1159 | * |
||
1160 | * @param Closure $callback |
||
1161 | * |
||
1162 | * @return $this |
||
1163 | */ |
||
1164 | public function row(Closure $callback) |
||
1170 | |||
1171 | /** |
||
1172 | * Tools setting for form. |
||
1173 | * |
||
1174 | * @param Closure $callback |
||
1175 | */ |
||
1176 | public function tools(Closure $callback) |
||
1180 | |||
1181 | /** |
||
1182 | * Disable form submit. |
||
1183 | * |
||
1184 | * @return $this |
||
1185 | * |
||
1186 | * @deprecated |
||
1187 | */ |
||
1188 | public function disableSubmit() |
||
1194 | |||
1195 | /** |
||
1196 | * Disable form reset. |
||
1197 | * |
||
1198 | * @return $this |
||
1199 | * |
||
1200 | * @deprecated |
||
1201 | */ |
||
1202 | public function disableReset() |
||
1208 | |||
1209 | /** |
||
1210 | * Disable View Checkbox on footer. |
||
1211 | * |
||
1212 | * @return $this |
||
1213 | */ |
||
1214 | public function disableViewCheck() |
||
1220 | |||
1221 | /** |
||
1222 | * Disable Editing Checkbox on footer. |
||
1223 | * |
||
1224 | * @return $this |
||
1225 | */ |
||
1226 | public function disableEditingCheck() |
||
1232 | |||
1233 | /** |
||
1234 | * Footer setting for form. |
||
1235 | * |
||
1236 | * @param Closure $callback |
||
1237 | */ |
||
1238 | public function footer(Closure $callback) |
||
1242 | |||
1243 | /** |
||
1244 | * Get current resource route url. |
||
1245 | * |
||
1246 | * @param int $slice |
||
1247 | * |
||
1248 | * @return string |
||
1249 | */ |
||
1250 | public function resource($slice = -2) |
||
1264 | |||
1265 | /** |
||
1266 | * Render the form contents. |
||
1267 | * |
||
1268 | * @return string |
||
1269 | */ |
||
1270 | public function render() |
||
1278 | |||
1279 | /** |
||
1280 | * Get or set input data. |
||
1281 | * |
||
1282 | * @param string $key |
||
1283 | * @param null $value |
||
1284 | * |
||
1285 | * @return array|mixed |
||
1286 | */ |
||
1287 | public function input($key, $value = null) |
||
1295 | |||
1296 | /** |
||
1297 | * Register builtin fields. |
||
1298 | * |
||
1299 | * @return void |
||
1300 | */ |
||
1301 | public static function registerBuiltinFields() |
||
1356 | |||
1357 | /** |
||
1358 | * Register custom field. |
||
1359 | * |
||
1360 | * @param string $abstract |
||
1361 | * @param string $class |
||
1362 | * |
||
1363 | * @return void |
||
1364 | */ |
||
1365 | public static function extend($abstract, $class) |
||
1369 | |||
1370 | /** |
||
1371 | * Remove registered field. |
||
1372 | * |
||
1373 | * @param array|string $abstract |
||
1374 | */ |
||
1375 | public static function forget($abstract) |
||
1379 | |||
1380 | /** |
||
1381 | * Find field class. |
||
1382 | * |
||
1383 | * @param string $method |
||
1384 | * |
||
1385 | * @return bool|mixed |
||
1386 | */ |
||
1387 | View Code Duplication | public static function findFieldClass($method) |
|
1397 | |||
1398 | /** |
||
1399 | * Collect assets required by registered field. |
||
1400 | * |
||
1401 | * @return array |
||
1402 | */ |
||
1403 | public static function collectFieldAssets() |
||
1428 | |||
1429 | /** |
||
1430 | * Getter. |
||
1431 | * |
||
1432 | * @param string $name |
||
1433 | * |
||
1434 | * @return array|mixed |
||
1435 | */ |
||
1436 | public function __get($name) |
||
1440 | |||
1441 | /** |
||
1442 | * Setter. |
||
1443 | * |
||
1444 | * @param string $name |
||
1445 | * @param $value |
||
1446 | */ |
||
1447 | public function __set($name, $value) |
||
1451 | |||
1452 | /** |
||
1453 | * Generate a Field object and add to form builder if Field exists. |
||
1454 | * |
||
1455 | * @param string $method |
||
1456 | * @param array $arguments |
||
1457 | * |
||
1458 | * @return Field|void |
||
1459 | */ |
||
1460 | public function __call($method, $arguments) |
||
1472 | } |
||
1473 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: