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 |
||
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) |
||
184 | |||
185 | /** |
||
186 | * @param Field $field |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function pushField(Field $field) |
||
198 | |||
199 | /** |
||
200 | * @return Model |
||
201 | */ |
||
202 | public function model() |
||
206 | |||
207 | /** |
||
208 | * @return Builder |
||
209 | */ |
||
210 | public function builder() |
||
214 | |||
215 | /** |
||
216 | * Generate a edit form. |
||
217 | * |
||
218 | * @param $id |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function edit($id) |
||
231 | |||
232 | /** |
||
233 | * @param $id |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function view($id) |
||
246 | |||
247 | /** |
||
248 | * Use tab to split form. |
||
249 | * |
||
250 | * @param string $title |
||
251 | * @param Closure $content |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function tab($title, Closure $content, $active = false) |
||
261 | |||
262 | /** |
||
263 | * Get Tab instance. |
||
264 | * |
||
265 | * @return Tab |
||
266 | */ |
||
267 | public function getTab() |
||
275 | |||
276 | /** |
||
277 | * Destroy data entity and remove files. |
||
278 | * |
||
279 | * @param $id |
||
280 | * |
||
281 | * @return mixed |
||
282 | */ |
||
283 | public function destroy($id) |
||
297 | |||
298 | /** |
||
299 | * Remove files or images in record. |
||
300 | * |
||
301 | * @param $id |
||
302 | */ |
||
303 | protected function deleteFilesAndImages($id) |
||
316 | |||
317 | /** |
||
318 | * Store a new record. |
||
319 | * |
||
320 | * @param null $inputData |
||
321 | * |
||
322 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
323 | */ |
||
324 | public function store($inputData = null) |
||
359 | |||
360 | /** |
||
361 | * Get RedirectResponse after store. |
||
362 | * |
||
363 | * @return \Illuminate\Http\RedirectResponse |
||
364 | */ |
||
365 | View Code Duplication | protected function redirectAfterStore() |
|
373 | |||
374 | /** |
||
375 | * Get ajax response. |
||
376 | * |
||
377 | * @param string $message |
||
378 | * |
||
379 | * @return bool|\Illuminate\Http\JsonResponse |
||
380 | */ |
||
381 | protected function ajaxResponse($message) |
||
395 | |||
396 | /** |
||
397 | * Prepare input data for insert or update. |
||
398 | * |
||
399 | * @param array $data |
||
400 | * |
||
401 | * @return mixed |
||
402 | */ |
||
403 | protected function prepare($data = []) |
||
419 | |||
420 | /** |
||
421 | * Remove ignored fields from input. |
||
422 | * |
||
423 | * @param array $input |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | protected function removeIgnoredFields($input) |
||
433 | |||
434 | /** |
||
435 | * Get inputs for relations. |
||
436 | * |
||
437 | * @param array $inputs |
||
438 | * |
||
439 | * @return array |
||
440 | */ |
||
441 | protected function getRelationInputs($inputs = []) |
||
457 | |||
458 | /** |
||
459 | * Call submitted callback. |
||
460 | * |
||
461 | * @return mixed |
||
462 | */ |
||
463 | protected function callSubmitted() |
||
469 | |||
470 | /** |
||
471 | * Call saving callback. |
||
472 | * |
||
473 | * @return mixed |
||
474 | */ |
||
475 | protected function callSaving() |
||
481 | |||
482 | /** |
||
483 | * Callback after saving a Model. |
||
484 | * |
||
485 | * @param Closure|null $callback |
||
486 | * |
||
487 | * @return mixed|null |
||
488 | */ |
||
489 | protected function complete(Closure $callback = null) |
||
495 | |||
496 | /** |
||
497 | * Handle update. |
||
498 | * |
||
499 | * @param int $id |
||
500 | * @param array|null $inputData |
||
501 | * |
||
502 | * @return \Symfony\Component\HttpFoundation\Response |
||
503 | */ |
||
504 | public function update($id, $inputData = null) |
||
562 | |||
563 | /** |
||
564 | * Get RedirectResponse after update. |
||
565 | * |
||
566 | * @return \Illuminate\Http\RedirectResponse |
||
567 | */ |
||
568 | View Code Duplication | protected function redirectAfterUpdate() |
|
576 | |||
577 | /** |
||
578 | * Check if request is from editable. |
||
579 | * |
||
580 | * @param array $input |
||
581 | * |
||
582 | * @return bool |
||
583 | */ |
||
584 | protected function isEditable(array $input = []) |
||
588 | |||
589 | /** |
||
590 | * Handle editable update. |
||
591 | * |
||
592 | * @param array $input |
||
593 | * |
||
594 | * @return array |
||
595 | */ |
||
596 | protected function handleEditable(array $input = []) |
||
608 | |||
609 | /** |
||
610 | * @param array $input |
||
611 | * |
||
612 | * @return array |
||
613 | */ |
||
614 | protected function handleFileDelete(array $input = []) |
||
625 | |||
626 | /** |
||
627 | * Handle orderable update. |
||
628 | * |
||
629 | * @param int $id |
||
630 | * @param array $input |
||
631 | * |
||
632 | * @return bool |
||
633 | */ |
||
634 | protected function handleOrderable($id, array $input = []) |
||
648 | |||
649 | /** |
||
650 | * Update relation data. |
||
651 | * |
||
652 | * @param array $relationsData |
||
653 | * |
||
654 | * @return void |
||
655 | */ |
||
656 | protected function updateRelation($relationsData) |
||
734 | |||
735 | /** |
||
736 | * Prepare input data for update. |
||
737 | * |
||
738 | * @param array $updates |
||
739 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
740 | * |
||
741 | * @return array |
||
742 | */ |
||
743 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
774 | |||
775 | /** |
||
776 | * @param string|array $columns |
||
777 | * @param bool $oneToOneRelation |
||
778 | * |
||
779 | * @return bool |
||
780 | */ |
||
781 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
792 | |||
793 | /** |
||
794 | * Prepare input data for insert. |
||
795 | * |
||
796 | * @param $inserts |
||
797 | * |
||
798 | * @return array |
||
799 | */ |
||
800 | protected function prepareInsert($inserts) |
||
823 | |||
824 | /** |
||
825 | * Is input data is has-one relation. |
||
826 | * |
||
827 | * @param array $inserts |
||
828 | * |
||
829 | * @return bool |
||
830 | */ |
||
831 | protected function isHasOneRelation($inserts) |
||
845 | |||
846 | /** |
||
847 | * Set submitted callback. |
||
848 | * |
||
849 | * @param Closure $callback |
||
850 | * |
||
851 | * @return void |
||
852 | */ |
||
853 | public function submitted(Closure $callback) |
||
857 | |||
858 | /** |
||
859 | * Set saving callback. |
||
860 | * |
||
861 | * @param Closure $callback |
||
862 | * |
||
863 | * @return void |
||
864 | */ |
||
865 | public function saving(Closure $callback) |
||
869 | |||
870 | /** |
||
871 | * Set saved callback. |
||
872 | * |
||
873 | * @param callable $callback |
||
874 | * |
||
875 | * @return void |
||
876 | */ |
||
877 | public function saved(Closure $callback) |
||
881 | |||
882 | /** |
||
883 | * Ignore fields to save. |
||
884 | * |
||
885 | * @param string|array $fields |
||
886 | * |
||
887 | * @return $this |
||
888 | */ |
||
889 | public function ignore($fields) |
||
895 | |||
896 | /** |
||
897 | * @param array $data |
||
898 | * @param string|array $columns |
||
899 | * |
||
900 | * @return array|mixed |
||
901 | */ |
||
902 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
920 | |||
921 | /** |
||
922 | * Find field object by column. |
||
923 | * |
||
924 | * @param $column |
||
925 | * |
||
926 | * @return mixed |
||
927 | */ |
||
928 | protected function getFieldByColumn($column) |
||
940 | |||
941 | /** |
||
942 | * Set original data for each field. |
||
943 | * |
||
944 | * @return void |
||
945 | */ |
||
946 | protected function setFieldOriginalValue() |
||
956 | |||
957 | /** |
||
958 | * Set all fields value in form. |
||
959 | * |
||
960 | * @param $id |
||
961 | * |
||
962 | * @return void |
||
963 | */ |
||
964 | protected function setFieldValue($id) |
||
978 | |||
979 | /** |
||
980 | * Don't snake case attributes. |
||
981 | * |
||
982 | * @param Model $model |
||
983 | * |
||
984 | * @return void |
||
985 | */ |
||
986 | protected static function doNotSnakeAttributes(Model $model) |
||
992 | |||
993 | /** |
||
994 | * Get validation messages. |
||
995 | * |
||
996 | * @param array $input |
||
997 | * |
||
998 | * @return MessageBag|bool |
||
999 | */ |
||
1000 | protected function validationMessages($input) |
||
1018 | |||
1019 | /** |
||
1020 | * Merge validation messages from input validators. |
||
1021 | * |
||
1022 | * @param \Illuminate\Validation\Validator[] $validators |
||
1023 | * |
||
1024 | * @return MessageBag |
||
1025 | */ |
||
1026 | protected function mergeValidationMessages($validators) |
||
1036 | |||
1037 | /** |
||
1038 | * Get all relations of model from callable. |
||
1039 | * |
||
1040 | * @return array |
||
1041 | */ |
||
1042 | public function getRelations() |
||
1068 | |||
1069 | /** |
||
1070 | * Set action for form. |
||
1071 | * |
||
1072 | * @param string $action |
||
1073 | * |
||
1074 | * @return $this |
||
1075 | */ |
||
1076 | public function setAction($action) |
||
1082 | |||
1083 | /** |
||
1084 | * Set field and label width in current form. |
||
1085 | * |
||
1086 | * @param int $fieldWidth |
||
1087 | * @param int $labelWidth |
||
1088 | * |
||
1089 | * @return $this |
||
1090 | */ |
||
1091 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1102 | |||
1103 | /** |
||
1104 | * Set view for form. |
||
1105 | * |
||
1106 | * @param string $view |
||
1107 | * |
||
1108 | * @return $this |
||
1109 | */ |
||
1110 | public function setView($view) |
||
1116 | |||
1117 | /** |
||
1118 | * Add a row in form. |
||
1119 | * |
||
1120 | * @param Closure $callback |
||
1121 | * |
||
1122 | * @return $this |
||
1123 | */ |
||
1124 | public function row(Closure $callback) |
||
1130 | |||
1131 | /** |
||
1132 | * Tools setting for form. |
||
1133 | * |
||
1134 | * @param Closure $callback |
||
1135 | */ |
||
1136 | public function tools(Closure $callback) |
||
1140 | |||
1141 | /** |
||
1142 | * Disable form submit. |
||
1143 | * |
||
1144 | * @return $this |
||
1145 | */ |
||
1146 | public function disableSubmit() |
||
1152 | |||
1153 | /** |
||
1154 | * Disable form reset. |
||
1155 | * |
||
1156 | * @return $this |
||
1157 | */ |
||
1158 | public function disableReset() |
||
1164 | |||
1165 | /** |
||
1166 | * Get current resource route url. |
||
1167 | * |
||
1168 | * @param int $slice |
||
1169 | * |
||
1170 | * @return string |
||
1171 | */ |
||
1172 | public function resource($slice = -2) |
||
1182 | |||
1183 | /** |
||
1184 | * Render the form contents. |
||
1185 | * |
||
1186 | * @return string |
||
1187 | */ |
||
1188 | public function render() |
||
1196 | |||
1197 | /** |
||
1198 | * Get or set input data. |
||
1199 | * |
||
1200 | * @param string $key |
||
1201 | * @param null $value |
||
1202 | * |
||
1203 | * @return array|mixed |
||
1204 | */ |
||
1205 | public function input($key, $value = null) |
||
1213 | |||
1214 | /** |
||
1215 | * Register builtin fields. |
||
1216 | * |
||
1217 | * @return void |
||
1218 | */ |
||
1219 | public static function registerBuiltinFields() |
||
1274 | |||
1275 | /** |
||
1276 | * Register custom field. |
||
1277 | * |
||
1278 | * @param string $abstract |
||
1279 | * @param string $class |
||
1280 | * |
||
1281 | * @return void |
||
1282 | */ |
||
1283 | public static function extend($abstract, $class) |
||
1287 | |||
1288 | /** |
||
1289 | * Remove registered field. |
||
1290 | * |
||
1291 | * @param array|string $abstract |
||
1292 | */ |
||
1293 | public static function forget($abstract) |
||
1297 | |||
1298 | /** |
||
1299 | * Find field class. |
||
1300 | * |
||
1301 | * @param string $method |
||
1302 | * |
||
1303 | * @return bool|mixed |
||
1304 | */ |
||
1305 | View Code Duplication | public static function findFieldClass($method) |
|
1315 | |||
1316 | /** |
||
1317 | * Collect assets required by registered field. |
||
1318 | * |
||
1319 | * @return array |
||
1320 | */ |
||
1321 | public static function collectFieldAssets() |
||
1346 | |||
1347 | /** |
||
1348 | * Getter. |
||
1349 | * |
||
1350 | * @param string $name |
||
1351 | * |
||
1352 | * @return array|mixed |
||
1353 | */ |
||
1354 | public function __get($name) |
||
1358 | |||
1359 | /** |
||
1360 | * Setter. |
||
1361 | * |
||
1362 | * @param string $name |
||
1363 | * @param $value |
||
1364 | */ |
||
1365 | public function __set($name, $value) |
||
1369 | |||
1370 | /** |
||
1371 | * Generate a Field object and add to form builder if Field exists. |
||
1372 | * |
||
1373 | * @param string $method |
||
1374 | * @param array $arguments |
||
1375 | * |
||
1376 | * @return Field|void |
||
1377 | */ |
||
1378 | public function __call($method, $arguments) |
||
1390 | |||
1391 | /** |
||
1392 | * Render the contents of the form when casting to string. |
||
1393 | * |
||
1394 | * @return string |
||
1395 | */ |
||
1396 | public function __toString() |
||
1400 | } |
||
1401 |
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: