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 |
||
71 | class Form implements Renderable |
||
72 | { |
||
73 | /** |
||
74 | * Eloquent model of the form. |
||
75 | * |
||
76 | * @var Model |
||
77 | */ |
||
78 | protected $model; |
||
79 | |||
80 | /** |
||
81 | * @var \Illuminate\Validation\Validator |
||
82 | */ |
||
83 | protected $validator; |
||
84 | |||
85 | /** |
||
86 | * @var Builder |
||
87 | */ |
||
88 | protected $builder; |
||
89 | |||
90 | /** |
||
91 | * Submitted callback. |
||
92 | * |
||
93 | * @var Closure |
||
94 | */ |
||
95 | protected $submitted; |
||
96 | |||
97 | /** |
||
98 | * Saving callback. |
||
99 | * |
||
100 | * @var Closure |
||
101 | */ |
||
102 | protected $saving; |
||
103 | |||
104 | /** |
||
105 | * Saved callback. |
||
106 | * |
||
107 | * @var Closure |
||
108 | */ |
||
109 | protected $saved; |
||
110 | |||
111 | /** |
||
112 | * Data for save to current model from input. |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | protected $updates = []; |
||
117 | |||
118 | /** |
||
119 | * Data for save to model's relations from input. |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | protected $relations = []; |
||
124 | |||
125 | /** |
||
126 | * Input data. |
||
127 | * |
||
128 | * @var array |
||
129 | */ |
||
130 | protected $inputs = []; |
||
131 | |||
132 | /** |
||
133 | * Available fields. |
||
134 | * |
||
135 | * @var array |
||
136 | */ |
||
137 | public static $availableFields = []; |
||
138 | |||
139 | /** |
||
140 | * Ignored saving fields. |
||
141 | * |
||
142 | * @var array |
||
143 | */ |
||
144 | protected $ignored = []; |
||
145 | |||
146 | /** |
||
147 | * Collected field assets. |
||
148 | * |
||
149 | * @var array |
||
150 | */ |
||
151 | protected static $collectedAssets = []; |
||
152 | |||
153 | /** |
||
154 | * @var Form\Tab |
||
155 | */ |
||
156 | protected $tab = null; |
||
157 | |||
158 | /** |
||
159 | * Remove flag in `has many` form. |
||
160 | */ |
||
161 | const REMOVE_FLAG_NAME = '_remove_'; |
||
162 | |||
163 | /** |
||
164 | * Field rows in form. |
||
165 | * |
||
166 | * @var array |
||
167 | */ |
||
168 | public $rows = []; |
||
169 | |||
170 | /** |
||
171 | * Create a new form instance. |
||
172 | * |
||
173 | * @param $model |
||
174 | * @param \Closure $callback |
||
175 | */ |
||
176 | public function __construct($model, Closure $callback = null) |
||
186 | |||
187 | /** |
||
188 | * @param Field $field |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function pushField(Field $field) |
||
200 | |||
201 | /** |
||
202 | * @return Model |
||
203 | */ |
||
204 | public function model() |
||
208 | |||
209 | /** |
||
210 | * @return Builder |
||
211 | */ |
||
212 | public function builder() |
||
216 | |||
217 | /** |
||
218 | * Generate a edit form. |
||
219 | * |
||
220 | * @param $id |
||
221 | * |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function edit($id) |
||
233 | |||
234 | /** |
||
235 | * @param $id |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function view($id) |
||
248 | |||
249 | /** |
||
250 | * Use tab to split form. |
||
251 | * |
||
252 | * @param string $title |
||
253 | * @param Closure $content |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function tab($title, Closure $content, $active = false) |
||
263 | |||
264 | /** |
||
265 | * Get Tab instance. |
||
266 | * |
||
267 | * @return Tab |
||
268 | */ |
||
269 | public function getTab() |
||
277 | |||
278 | /** |
||
279 | * Destroy data entity and remove files. |
||
280 | * |
||
281 | * @param $id |
||
282 | * |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function destroy($id) |
||
299 | |||
300 | /** |
||
301 | * Remove files or images in record. |
||
302 | * |
||
303 | * @param $id |
||
304 | */ |
||
305 | protected function deleteFilesAndImages($id) |
||
318 | |||
319 | /** |
||
320 | * Store a new record. |
||
321 | * |
||
322 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
323 | */ |
||
324 | public function store() |
||
359 | |||
360 | /** |
||
361 | * Get ajax response. |
||
362 | * |
||
363 | * @param string $message |
||
364 | * |
||
365 | * @return bool|\Illuminate\Http\JsonResponse |
||
366 | */ |
||
367 | protected function ajaxResponse($message) |
||
381 | |||
382 | /** |
||
383 | * Prepare input data for insert or update. |
||
384 | * |
||
385 | * @param array $data |
||
386 | * |
||
387 | * @return mixed |
||
388 | */ |
||
389 | protected function prepare($data = []) |
||
405 | |||
406 | /** |
||
407 | * Remove ignored fields from input. |
||
408 | * |
||
409 | * @param array $input |
||
410 | * |
||
411 | * @return array |
||
412 | */ |
||
413 | protected function removeIgnoredFields($input) |
||
419 | |||
420 | /** |
||
421 | * Get inputs for relations. |
||
422 | * |
||
423 | * @param array $inputs |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | protected function getRelationInputs($inputs = []) |
||
443 | |||
444 | /** |
||
445 | * Call submitted callback. |
||
446 | * |
||
447 | * @return mixed |
||
448 | */ |
||
449 | protected function callSubmitted() |
||
455 | |||
456 | /** |
||
457 | * Call saving callback. |
||
458 | * |
||
459 | * @return mixed |
||
460 | */ |
||
461 | protected function callSaving() |
||
467 | |||
468 | /** |
||
469 | * Callback after saving a Model. |
||
470 | * |
||
471 | * @param Closure|null $callback |
||
472 | * |
||
473 | * @return mixed|null |
||
474 | */ |
||
475 | protected function complete(Closure $callback = null) |
||
481 | |||
482 | /** |
||
483 | * Handle update. |
||
484 | * |
||
485 | * @param int $id |
||
486 | * |
||
487 | * @return \Symfony\Component\HttpFoundation\Response |
||
488 | */ |
||
489 | public function update($id, $data = null) |
||
547 | |||
548 | /** |
||
549 | * Get RedirectResponse after store. |
||
550 | * |
||
551 | * @return \Illuminate\Http\RedirectResponse |
||
552 | */ |
||
553 | protected function redirectAfterStore() |
||
561 | |||
562 | /** |
||
563 | * Get RedirectResponse after update. |
||
564 | * |
||
565 | * @param mixed $key |
||
566 | * |
||
567 | * @return \Illuminate\Http\RedirectResponse |
||
568 | */ |
||
569 | protected function redirectAfterUpdate($key) |
||
575 | |||
576 | /** |
||
577 | * Get RedirectResponse after data saving. |
||
578 | * |
||
579 | * @param string $resourcesPath |
||
580 | * @param string $key |
||
581 | * |
||
582 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
583 | */ |
||
584 | protected function redirectAfterSaving($resourcesPath, $key) |
||
600 | |||
601 | /** |
||
602 | * Check if request is from editable. |
||
603 | * |
||
604 | * @param array $input |
||
605 | * |
||
606 | * @return bool |
||
607 | */ |
||
608 | protected function isEditable(array $input = []) |
||
612 | |||
613 | /** |
||
614 | * Handle editable update. |
||
615 | * |
||
616 | * @param array $input |
||
617 | * |
||
618 | * @return array |
||
619 | */ |
||
620 | protected function handleEditable(array $input = []) |
||
632 | |||
633 | /** |
||
634 | * @param array $input |
||
635 | * |
||
636 | * @return array |
||
637 | */ |
||
638 | protected function handleFileDelete(array $input = []) |
||
649 | |||
650 | /** |
||
651 | * Handle orderable update. |
||
652 | * |
||
653 | * @param int $id |
||
654 | * @param array $input |
||
655 | * |
||
656 | * @return bool |
||
657 | */ |
||
658 | protected function handleOrderable($id, array $input = []) |
||
672 | |||
673 | /** |
||
674 | * Update relation data. |
||
675 | * |
||
676 | * @param array $relationsData |
||
677 | * |
||
678 | * @return void |
||
679 | */ |
||
680 | protected function updateRelation($relationsData) |
||
759 | |||
760 | /** |
||
761 | * Prepare input data for update. |
||
762 | * |
||
763 | * @param array $updates |
||
764 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
765 | * |
||
766 | * @return array |
||
767 | */ |
||
768 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
800 | |||
801 | /** |
||
802 | * @param string|array $columns |
||
803 | * @param bool $oneToOneRelation |
||
804 | * |
||
805 | * @return bool |
||
806 | */ |
||
807 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
818 | |||
819 | /** |
||
820 | * Prepare input data for insert. |
||
821 | * |
||
822 | * @param $inserts |
||
823 | * |
||
824 | * @return array |
||
825 | */ |
||
826 | protected function prepareInsert($inserts) |
||
849 | |||
850 | /** |
||
851 | * Is input data is has-one relation. |
||
852 | * |
||
853 | * @param array $inserts |
||
854 | * |
||
855 | * @return bool |
||
856 | */ |
||
857 | protected function isHasOneRelation($inserts) |
||
871 | |||
872 | /** |
||
873 | * Set submitted callback. |
||
874 | * |
||
875 | * @param Closure $callback |
||
876 | * |
||
877 | * @return void |
||
878 | */ |
||
879 | public function submitted(Closure $callback) |
||
883 | |||
884 | /** |
||
885 | * Set saving callback. |
||
886 | * |
||
887 | * @param Closure $callback |
||
888 | * |
||
889 | * @return void |
||
890 | */ |
||
891 | public function saving(Closure $callback) |
||
895 | |||
896 | /** |
||
897 | * Set saved callback. |
||
898 | * |
||
899 | * @param Closure $callback |
||
900 | * |
||
901 | * @return void |
||
902 | */ |
||
903 | public function saved(Closure $callback) |
||
907 | |||
908 | /** |
||
909 | * Ignore fields to save. |
||
910 | * |
||
911 | * @param string|array $fields |
||
912 | * |
||
913 | * @return $this |
||
914 | */ |
||
915 | public function ignore($fields) |
||
921 | |||
922 | /** |
||
923 | * @param array $data |
||
924 | * @param string|array $columns |
||
925 | * |
||
926 | * @return array|mixed |
||
927 | */ |
||
928 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
946 | |||
947 | /** |
||
948 | * Find field object by column. |
||
949 | * |
||
950 | * @param $column |
||
951 | * |
||
952 | * @return mixed |
||
953 | */ |
||
954 | protected function getFieldByColumn($column) |
||
966 | |||
967 | /** |
||
968 | * Set original data for each field. |
||
969 | * |
||
970 | * @return void |
||
971 | */ |
||
972 | protected function setFieldOriginalValue() |
||
982 | |||
983 | /** |
||
984 | * Set all fields value in form. |
||
985 | * |
||
986 | * @param $id |
||
987 | * |
||
988 | * @return void |
||
989 | */ |
||
990 | protected function setFieldValue($id) |
||
1006 | |||
1007 | /** |
||
1008 | * Don't snake case attributes. |
||
1009 | * |
||
1010 | * @param Model $model |
||
1011 | * |
||
1012 | * @return void |
||
1013 | */ |
||
1014 | protected static function doNotSnakeAttributes(Model $model) |
||
1020 | |||
1021 | /** |
||
1022 | * Get validation messages. |
||
1023 | * |
||
1024 | * @param array $input |
||
1025 | * |
||
1026 | * @return MessageBag|bool |
||
1027 | */ |
||
1028 | protected function validationMessages($input) |
||
1047 | |||
1048 | /** |
||
1049 | * Merge validation messages from input validators. |
||
1050 | * |
||
1051 | * @param \Illuminate\Validation\Validator[] $validators |
||
1052 | * |
||
1053 | * @return MessageBag |
||
1054 | */ |
||
1055 | protected function mergeValidationMessages($validators) |
||
1065 | |||
1066 | /** |
||
1067 | * Get all relations of model from callable. |
||
1068 | * |
||
1069 | * @return array |
||
1070 | */ |
||
1071 | public function getRelations() |
||
1098 | |||
1099 | /** |
||
1100 | * Set action for form. |
||
1101 | * |
||
1102 | * @param string $action |
||
1103 | * |
||
1104 | * @return $this |
||
1105 | */ |
||
1106 | public function setAction($action) |
||
1112 | |||
1113 | /** |
||
1114 | * Set field and label width in current form. |
||
1115 | * |
||
1116 | * @param int $fieldWidth |
||
1117 | * @param int $labelWidth |
||
1118 | * |
||
1119 | * @return $this |
||
1120 | */ |
||
1121 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1132 | |||
1133 | /** |
||
1134 | * Set view for form. |
||
1135 | * |
||
1136 | * @param string $view |
||
1137 | * |
||
1138 | * @return $this |
||
1139 | */ |
||
1140 | public function setView($view) |
||
1146 | |||
1147 | /** |
||
1148 | * Set title for form. |
||
1149 | * |
||
1150 | * @param string $title |
||
1151 | * |
||
1152 | * @return $this |
||
1153 | */ |
||
1154 | public function setTitle($title = '') |
||
1160 | |||
1161 | /** |
||
1162 | * Add a row in form. |
||
1163 | * |
||
1164 | * @param Closure $callback |
||
1165 | * |
||
1166 | * @return $this |
||
1167 | */ |
||
1168 | public function row(Closure $callback) |
||
1174 | |||
1175 | /** |
||
1176 | * Tools setting for form. |
||
1177 | * |
||
1178 | * @param Closure $callback |
||
1179 | */ |
||
1180 | public function tools(Closure $callback) |
||
1184 | |||
1185 | /** |
||
1186 | * Disable form submit. |
||
1187 | * |
||
1188 | * @return $this |
||
1189 | * |
||
1190 | * @deprecated |
||
1191 | */ |
||
1192 | public function disableSubmit() |
||
1198 | |||
1199 | /** |
||
1200 | * Disable form reset. |
||
1201 | * |
||
1202 | * @return $this |
||
1203 | * |
||
1204 | * @deprecated |
||
1205 | */ |
||
1206 | public function disableReset() |
||
1212 | |||
1213 | /** |
||
1214 | * Disable form remove reserved fields. |
||
1215 | * |
||
1216 | * @return $this |
||
1217 | */ |
||
1218 | public function disableRemoveReservedFields() |
||
1224 | |||
1225 | /** |
||
1226 | * Footer setting for form. |
||
1227 | * |
||
1228 | * @param Closure $callback |
||
1229 | */ |
||
1230 | public function footer(Closure $callback) |
||
1234 | |||
1235 | /** |
||
1236 | * Get current resource route url. |
||
1237 | * |
||
1238 | * @param int $slice |
||
1239 | * |
||
1240 | * @return string |
||
1241 | */ |
||
1242 | public function resource($slice = -2) |
||
1256 | |||
1257 | /** |
||
1258 | * Render the form contents. |
||
1259 | * |
||
1260 | * @return string |
||
1261 | */ |
||
1262 | public function render() |
||
1270 | |||
1271 | /** |
||
1272 | * Get or set input data. |
||
1273 | * |
||
1274 | * @param string $key |
||
1275 | * @param null $value |
||
1276 | * |
||
1277 | * @return array|mixed |
||
1278 | */ |
||
1279 | public function input($key, $value = null) |
||
1287 | |||
1288 | /** |
||
1289 | * Register builtin fields. |
||
1290 | * |
||
1291 | * @return void |
||
1292 | */ |
||
1293 | public static function registerBuiltinFields() |
||
1348 | |||
1349 | /** |
||
1350 | * Register custom field. |
||
1351 | * |
||
1352 | * @param string $abstract |
||
1353 | * @param string $class |
||
1354 | * |
||
1355 | * @return void |
||
1356 | */ |
||
1357 | public static function extend($abstract, $class) |
||
1361 | |||
1362 | /** |
||
1363 | * Remove registered field. |
||
1364 | * |
||
1365 | * @param array|string $abstract |
||
1366 | */ |
||
1367 | public static function forget($abstract) |
||
1371 | |||
1372 | /** |
||
1373 | * Find field class. |
||
1374 | * |
||
1375 | * @param string $method |
||
1376 | * |
||
1377 | * @return bool|mixed |
||
1378 | */ |
||
1379 | View Code Duplication | public static function findFieldClass($method) |
|
1389 | |||
1390 | /** |
||
1391 | * Collect assets required by registered field. |
||
1392 | * |
||
1393 | * @return array |
||
1394 | */ |
||
1395 | public static function collectFieldAssets() |
||
1420 | |||
1421 | /** |
||
1422 | * Getter. |
||
1423 | * |
||
1424 | * @param string $name |
||
1425 | * |
||
1426 | * @return array|mixed |
||
1427 | */ |
||
1428 | public function __get($name) |
||
1432 | |||
1433 | /** |
||
1434 | * Setter. |
||
1435 | * |
||
1436 | * @param string $name |
||
1437 | * @param $value |
||
1438 | */ |
||
1439 | public function __set($name, $value) |
||
1443 | |||
1444 | /** |
||
1445 | * Generate a Field object and add to form builder if Field exists. |
||
1446 | * |
||
1447 | * @param string $method |
||
1448 | * @param array $arguments |
||
1449 | * |
||
1450 | * @return Field|void |
||
1451 | */ |
||
1452 | public function __call($method, $arguments) |
||
1464 | } |
||
1465 |
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: