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 | * Form field alias. |
||
142 | * |
||
143 | * @var array |
||
144 | */ |
||
145 | public static $fieldAlias = []; |
||
146 | |||
147 | /** |
||
148 | * Ignored saving fields. |
||
149 | * |
||
150 | * @var array |
||
151 | */ |
||
152 | protected $ignored = []; |
||
153 | |||
154 | /** |
||
155 | * Collected field assets. |
||
156 | * |
||
157 | * @var array |
||
158 | */ |
||
159 | protected static $collectedAssets = []; |
||
160 | |||
161 | /** |
||
162 | * @var Form\Tab |
||
163 | */ |
||
164 | protected $tab = null; |
||
165 | |||
166 | /** |
||
167 | * Remove flag in `has many` form. |
||
168 | */ |
||
169 | const REMOVE_FLAG_NAME = '_remove_'; |
||
170 | |||
171 | /** |
||
172 | * Field rows in form. |
||
173 | * |
||
174 | * @var array |
||
175 | */ |
||
176 | public $rows = []; |
||
177 | |||
178 | /** |
||
179 | * Create a new form instance. |
||
180 | * |
||
181 | * @param $model |
||
182 | * @param \Closure $callback |
||
183 | */ |
||
184 | public function __construct($model, Closure $callback = null) |
||
194 | |||
195 | /** |
||
196 | * @param Field $field |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function pushField(Field $field) |
||
208 | |||
209 | /** |
||
210 | * @return Model |
||
211 | */ |
||
212 | public function model() |
||
216 | |||
217 | /** |
||
218 | * @return Builder |
||
219 | */ |
||
220 | public function builder() |
||
224 | |||
225 | /** |
||
226 | * Generate a edit form. |
||
227 | * |
||
228 | * @param $id |
||
229 | * |
||
230 | * @return $this |
||
231 | */ |
||
232 | public function edit($id) |
||
241 | |||
242 | /** |
||
243 | * Use tab to split form. |
||
244 | * |
||
245 | * @param string $title |
||
246 | * @param Closure $content |
||
247 | * |
||
248 | * @return $this |
||
249 | */ |
||
250 | public function tab($title, Closure $content, $active = false) |
||
256 | |||
257 | /** |
||
258 | * Get Tab instance. |
||
259 | * |
||
260 | * @return Tab |
||
261 | */ |
||
262 | public function getTab() |
||
270 | |||
271 | /** |
||
272 | * Destroy data entity and remove files. |
||
273 | * |
||
274 | * @param $id |
||
275 | * |
||
276 | * @return mixed |
||
277 | */ |
||
278 | public function destroy($id) |
||
289 | |||
290 | /** |
||
291 | * Remove files in record. |
||
292 | * |
||
293 | * @param $id |
||
294 | */ |
||
295 | protected function deleteFiles($id) |
||
315 | |||
316 | /** |
||
317 | * Store a new record. |
||
318 | * |
||
319 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
320 | */ |
||
321 | public function store() |
||
356 | |||
357 | /** |
||
358 | * Get ajax response. |
||
359 | * |
||
360 | * @param string $message |
||
361 | * |
||
362 | * @return bool|\Illuminate\Http\JsonResponse |
||
363 | */ |
||
364 | protected function ajaxResponse($message) |
||
378 | |||
379 | /** |
||
380 | * Prepare input data for insert or update. |
||
381 | * |
||
382 | * @param array $data |
||
383 | * |
||
384 | * @return mixed |
||
385 | */ |
||
386 | protected function prepare($data = []) |
||
402 | |||
403 | /** |
||
404 | * Remove ignored fields from input. |
||
405 | * |
||
406 | * @param array $input |
||
407 | * |
||
408 | * @return array |
||
409 | */ |
||
410 | protected function removeIgnoredFields($input) |
||
416 | |||
417 | /** |
||
418 | * Get inputs for relations. |
||
419 | * |
||
420 | * @param array $inputs |
||
421 | * |
||
422 | * @return array |
||
423 | */ |
||
424 | protected function getRelationInputs($inputs = []) |
||
440 | |||
441 | /** |
||
442 | * Call submitted callback. |
||
443 | * |
||
444 | * @return mixed |
||
445 | */ |
||
446 | protected function callSubmitted() |
||
454 | |||
455 | /** |
||
456 | * Call saving callback. |
||
457 | * |
||
458 | * @return mixed |
||
459 | */ |
||
460 | protected function callSaving() |
||
468 | |||
469 | /** |
||
470 | * Callback after saving a Model. |
||
471 | * |
||
472 | * @return mixed|null |
||
473 | */ |
||
474 | protected function callSaved() |
||
482 | |||
483 | /** |
||
484 | * Handle update. |
||
485 | * |
||
486 | * @param int $id |
||
487 | * |
||
488 | * @return \Symfony\Component\HttpFoundation\Response |
||
489 | */ |
||
490 | public function update($id, $data = null) |
||
548 | |||
549 | /** |
||
550 | * Get RedirectResponse after store. |
||
551 | * |
||
552 | * @return \Illuminate\Http\RedirectResponse |
||
553 | */ |
||
554 | protected function redirectAfterStore() |
||
562 | |||
563 | /** |
||
564 | * Get RedirectResponse after update. |
||
565 | * |
||
566 | * @param mixed $key |
||
567 | * |
||
568 | * @return \Illuminate\Http\RedirectResponse |
||
569 | */ |
||
570 | protected function redirectAfterUpdate($key) |
||
576 | |||
577 | /** |
||
578 | * Get RedirectResponse after data saving. |
||
579 | * |
||
580 | * @param string $resourcesPath |
||
581 | * @param string $key |
||
582 | * |
||
583 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
584 | */ |
||
585 | protected function redirectAfterSaving($resourcesPath, $key) |
||
601 | |||
602 | /** |
||
603 | * Check if request is from editable. |
||
604 | * |
||
605 | * @param array $input |
||
606 | * |
||
607 | * @return bool |
||
608 | */ |
||
609 | protected function isEditable(array $input = []) |
||
613 | |||
614 | /** |
||
615 | * Handle editable update. |
||
616 | * |
||
617 | * @param array $input |
||
618 | * |
||
619 | * @return array |
||
620 | */ |
||
621 | protected function handleEditable(array $input = []) |
||
633 | |||
634 | /** |
||
635 | * @param array $input |
||
636 | * |
||
637 | * @return array |
||
638 | */ |
||
639 | protected function handleFileDelete(array $input = []) |
||
650 | |||
651 | /** |
||
652 | * Handle orderable update. |
||
653 | * |
||
654 | * @param int $id |
||
655 | * @param array $input |
||
656 | * |
||
657 | * @return bool |
||
658 | */ |
||
659 | protected function handleOrderable($id, array $input = []) |
||
673 | |||
674 | /** |
||
675 | * Update relation data. |
||
676 | * |
||
677 | * @param array $relationsData |
||
678 | * |
||
679 | * @return void |
||
680 | */ |
||
681 | protected function updateRelation($relationsData) |
||
762 | |||
763 | /** |
||
764 | * Prepare input data for update. |
||
765 | * |
||
766 | * @param array $updates |
||
767 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
768 | * |
||
769 | * @return array |
||
770 | */ |
||
771 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
803 | |||
804 | /** |
||
805 | * @param string|array $columns |
||
806 | * @param bool $oneToOneRelation |
||
807 | * |
||
808 | * @return bool |
||
809 | */ |
||
810 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
821 | |||
822 | /** |
||
823 | * Prepare input data for insert. |
||
824 | * |
||
825 | * @param $inserts |
||
826 | * |
||
827 | * @return array |
||
828 | */ |
||
829 | protected function prepareInsert($inserts) |
||
852 | |||
853 | /** |
||
854 | * Is input data is has-one relation. |
||
855 | * |
||
856 | * @param array $inserts |
||
857 | * |
||
858 | * @return bool |
||
859 | */ |
||
860 | protected function isHasOneRelation($inserts) |
||
874 | |||
875 | /** |
||
876 | * Set submitted callback. |
||
877 | * |
||
878 | * @param Closure $callback |
||
879 | * |
||
880 | * @return void |
||
881 | */ |
||
882 | public function submitted(Closure $callback) |
||
886 | |||
887 | /** |
||
888 | * Set saving callback. |
||
889 | * |
||
890 | * @param Closure $callback |
||
891 | * |
||
892 | * @return void |
||
893 | */ |
||
894 | public function saving(Closure $callback) |
||
898 | |||
899 | /** |
||
900 | * Set saved callback. |
||
901 | * |
||
902 | * @param Closure $callback |
||
903 | * |
||
904 | * @return void |
||
905 | */ |
||
906 | public function saved(Closure $callback) |
||
910 | |||
911 | /** |
||
912 | * Ignore fields to save. |
||
913 | * |
||
914 | * @param string|array $fields |
||
915 | * |
||
916 | * @return $this |
||
917 | */ |
||
918 | public function ignore($fields) |
||
924 | |||
925 | /** |
||
926 | * @param array $data |
||
927 | * @param string|array $columns |
||
928 | * |
||
929 | * @return array|mixed |
||
930 | */ |
||
931 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
949 | |||
950 | /** |
||
951 | * Find field object by column. |
||
952 | * |
||
953 | * @param $column |
||
954 | * |
||
955 | * @return mixed |
||
956 | */ |
||
957 | protected function getFieldByColumn($column) |
||
969 | |||
970 | /** |
||
971 | * Set original data for each field. |
||
972 | * |
||
973 | * @return void |
||
974 | */ |
||
975 | protected function setFieldOriginalValue() |
||
985 | |||
986 | /** |
||
987 | * Set all fields value in form. |
||
988 | * |
||
989 | * @param $id |
||
990 | * |
||
991 | * @return void |
||
992 | */ |
||
993 | protected function setFieldValue($id) |
||
1009 | |||
1010 | /** |
||
1011 | * Don't snake case attributes. |
||
1012 | * |
||
1013 | * @param Model $model |
||
1014 | * |
||
1015 | * @return void |
||
1016 | */ |
||
1017 | protected static function doNotSnakeAttributes(Model $model) |
||
1023 | |||
1024 | /** |
||
1025 | * Get validation messages. |
||
1026 | * |
||
1027 | * @param array $input |
||
1028 | * |
||
1029 | * @return MessageBag|bool |
||
1030 | */ |
||
1031 | protected function validationMessages($input) |
||
1050 | |||
1051 | /** |
||
1052 | * Merge validation messages from input validators. |
||
1053 | * |
||
1054 | * @param \Illuminate\Validation\Validator[] $validators |
||
1055 | * |
||
1056 | * @return MessageBag |
||
1057 | */ |
||
1058 | protected function mergeValidationMessages($validators) |
||
1068 | |||
1069 | /** |
||
1070 | * Get all relations of model from callable. |
||
1071 | * |
||
1072 | * @return array |
||
1073 | */ |
||
1074 | public function getRelations() |
||
1101 | |||
1102 | /** |
||
1103 | * Set action for form. |
||
1104 | * |
||
1105 | * @param string $action |
||
1106 | * |
||
1107 | * @return $this |
||
1108 | */ |
||
1109 | public function setAction($action) |
||
1115 | |||
1116 | /** |
||
1117 | * Set field and label width in current form. |
||
1118 | * |
||
1119 | * @param int $fieldWidth |
||
1120 | * @param int $labelWidth |
||
1121 | * |
||
1122 | * @return $this |
||
1123 | */ |
||
1124 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1135 | |||
1136 | /** |
||
1137 | * Set view for form. |
||
1138 | * |
||
1139 | * @param string $view |
||
1140 | * |
||
1141 | * @return $this |
||
1142 | */ |
||
1143 | public function setView($view) |
||
1149 | |||
1150 | /** |
||
1151 | * Set title for form. |
||
1152 | * |
||
1153 | * @param string $title |
||
1154 | * |
||
1155 | * @return $this |
||
1156 | */ |
||
1157 | public function setTitle($title = '') |
||
1163 | |||
1164 | /** |
||
1165 | * Add a row in form. |
||
1166 | * |
||
1167 | * @param Closure $callback |
||
1168 | * |
||
1169 | * @return $this |
||
1170 | */ |
||
1171 | public function row(Closure $callback) |
||
1177 | |||
1178 | /** |
||
1179 | * Tools setting for form. |
||
1180 | * |
||
1181 | * @param Closure $callback |
||
1182 | */ |
||
1183 | public function tools(Closure $callback) |
||
1187 | |||
1188 | /** |
||
1189 | * Disable form submit. |
||
1190 | * |
||
1191 | * @return $this |
||
1192 | * |
||
1193 | * @deprecated |
||
1194 | */ |
||
1195 | public function disableSubmit() |
||
1201 | |||
1202 | /** |
||
1203 | * Disable form reset. |
||
1204 | * |
||
1205 | * @return $this |
||
1206 | * |
||
1207 | * @deprecated |
||
1208 | */ |
||
1209 | public function disableReset() |
||
1215 | |||
1216 | /** |
||
1217 | * Disable View Checkbox on footer. |
||
1218 | * |
||
1219 | * @return $this |
||
1220 | */ |
||
1221 | public function disableViewCheck() |
||
1227 | |||
1228 | /** |
||
1229 | * Disable Editing Checkbox on footer. |
||
1230 | * |
||
1231 | * @return $this |
||
1232 | */ |
||
1233 | public function disableEditingCheck() |
||
1239 | |||
1240 | /** |
||
1241 | * Footer setting for form. |
||
1242 | * |
||
1243 | * @param Closure $callback |
||
1244 | */ |
||
1245 | public function footer(Closure $callback) |
||
1249 | |||
1250 | /** |
||
1251 | * Get current resource route url. |
||
1252 | * |
||
1253 | * @param int $slice |
||
1254 | * |
||
1255 | * @return string |
||
1256 | */ |
||
1257 | public function resource($slice = -2) |
||
1271 | |||
1272 | /** |
||
1273 | * Render the form contents. |
||
1274 | * |
||
1275 | * @return string |
||
1276 | */ |
||
1277 | public function render() |
||
1285 | |||
1286 | /** |
||
1287 | * Get or set input data. |
||
1288 | * |
||
1289 | * @param string $key |
||
1290 | * @param null $value |
||
1291 | * |
||
1292 | * @return array|mixed |
||
1293 | */ |
||
1294 | public function input($key, $value = null) |
||
1302 | |||
1303 | /** |
||
1304 | * Register builtin fields. |
||
1305 | * |
||
1306 | * @return void |
||
1307 | */ |
||
1308 | public static function registerBuiltinFields() |
||
1363 | |||
1364 | /** |
||
1365 | * Register custom field. |
||
1366 | * |
||
1367 | * @param string $abstract |
||
1368 | * @param string $class |
||
1369 | * |
||
1370 | * @return void |
||
1371 | */ |
||
1372 | public static function extend($abstract, $class) |
||
1376 | |||
1377 | /** |
||
1378 | * Set form field alias. |
||
1379 | * |
||
1380 | * @param string $field |
||
1381 | * @param string $alias |
||
1382 | * |
||
1383 | * @return void |
||
1384 | */ |
||
1385 | public static function alias($field, $alias) |
||
1389 | |||
1390 | /** |
||
1391 | * Remove registered field. |
||
1392 | * |
||
1393 | * @param array|string $abstract |
||
1394 | */ |
||
1395 | public static function forget($abstract) |
||
1399 | |||
1400 | /** |
||
1401 | * Find field class. |
||
1402 | * |
||
1403 | * @param string $method |
||
1404 | * |
||
1405 | * @return bool|mixed |
||
1406 | */ |
||
1407 | public static function findFieldClass($method) |
||
1422 | |||
1423 | /** |
||
1424 | * Collect assets required by registered field. |
||
1425 | * |
||
1426 | * @return array |
||
1427 | */ |
||
1428 | public static function collectFieldAssets() |
||
1453 | |||
1454 | /** |
||
1455 | * Getter. |
||
1456 | * |
||
1457 | * @param string $name |
||
1458 | * |
||
1459 | * @return array|mixed |
||
1460 | */ |
||
1461 | public function __get($name) |
||
1465 | |||
1466 | /** |
||
1467 | * Setter. |
||
1468 | * |
||
1469 | * @param string $name |
||
1470 | * @param $value |
||
1471 | */ |
||
1472 | public function __set($name, $value) |
||
1476 | |||
1477 | /** |
||
1478 | * Generate a Field object and add to form builder if Field exists. |
||
1479 | * |
||
1480 | * @param string $method |
||
1481 | * @param array $arguments |
||
1482 | * |
||
1483 | * @return Field |
||
1484 | */ |
||
1485 | public function __call($method, $arguments) |
||
1501 | } |
||
1502 |
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: