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 | * Callbacks after getting editing model. |
||
114 | * |
||
115 | * @var Closure[] |
||
116 | */ |
||
117 | protected $editing = []; |
||
118 | |||
119 | /** |
||
120 | * Data for save to current model from input. |
||
121 | * |
||
122 | * @var array |
||
123 | */ |
||
124 | protected $updates = []; |
||
125 | |||
126 | /** |
||
127 | * Data for save to model's relations from input. |
||
128 | * |
||
129 | * @var array |
||
130 | */ |
||
131 | protected $relations = []; |
||
132 | |||
133 | /** |
||
134 | * Input data. |
||
135 | * |
||
136 | * @var array |
||
137 | */ |
||
138 | protected $inputs = []; |
||
139 | |||
140 | /** |
||
141 | * Available fields. |
||
142 | * |
||
143 | * @var array |
||
144 | */ |
||
145 | public static $availableFields = []; |
||
146 | |||
147 | /** |
||
148 | * Form field alias. |
||
149 | * |
||
150 | * @var array |
||
151 | */ |
||
152 | public static $fieldAlias = []; |
||
153 | |||
154 | /** |
||
155 | * Ignored saving fields. |
||
156 | * |
||
157 | * @var array |
||
158 | */ |
||
159 | protected $ignored = []; |
||
160 | |||
161 | /** |
||
162 | * Collected field assets. |
||
163 | * |
||
164 | * @var array |
||
165 | */ |
||
166 | protected static $collectedAssets = []; |
||
167 | |||
168 | /** |
||
169 | * @var Form\Tab |
||
170 | */ |
||
171 | protected $tab = null; |
||
172 | |||
173 | /** |
||
174 | * Remove flag in `has many` form. |
||
175 | */ |
||
176 | const REMOVE_FLAG_NAME = '_remove_'; |
||
177 | |||
178 | /** |
||
179 | * Field rows in form. |
||
180 | * |
||
181 | * @var array |
||
182 | */ |
||
183 | public $rows = []; |
||
184 | |||
185 | /** |
||
186 | * Create a new form instance. |
||
187 | * |
||
188 | * @param $model |
||
189 | * @param \Closure $callback |
||
190 | */ |
||
191 | public function __construct($model, Closure $callback = null) |
||
201 | |||
202 | /** |
||
203 | * @param Field $field |
||
204 | * |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function pushField(Field $field) |
||
215 | |||
216 | /** |
||
217 | * @return Model |
||
218 | */ |
||
219 | public function model() |
||
227 | |||
228 | /** |
||
229 | * @return Builder |
||
230 | */ |
||
231 | public function builder() |
||
235 | |||
236 | /** |
||
237 | * Generate a edit form. |
||
238 | * |
||
239 | * @param $id |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function edit($id) |
||
252 | |||
253 | /** |
||
254 | * Use tab to split form. |
||
255 | * |
||
256 | * @param string $title |
||
257 | * @param Closure $content |
||
258 | * |
||
259 | * @return $this |
||
260 | */ |
||
261 | public function tab($title, Closure $content, $active = false) |
||
267 | |||
268 | /** |
||
269 | * Get Tab instance. |
||
270 | * |
||
271 | * @return Tab |
||
272 | */ |
||
273 | public function getTab() |
||
281 | |||
282 | /** |
||
283 | * If is a soft-deletes model. |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | protected function isSoftDeletes() |
||
291 | |||
292 | /** |
||
293 | * Destroy data entity and remove files. |
||
294 | * |
||
295 | * @param $id |
||
296 | * |
||
297 | * @return mixed |
||
298 | */ |
||
299 | public function destroy($id) |
||
318 | |||
319 | /** |
||
320 | * Remove files in record. |
||
321 | * |
||
322 | * @param mixed $id |
||
323 | * @param bool $forceDelete |
||
324 | */ |
||
325 | protected function deleteFiles($id, $forceDelete = false) |
||
345 | |||
346 | /** |
||
347 | * Store a new record. |
||
348 | * |
||
349 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
350 | */ |
||
351 | public function store() |
||
386 | |||
387 | /** |
||
388 | * Get ajax response. |
||
389 | * |
||
390 | * @param string $message |
||
391 | * |
||
392 | * @return bool|\Illuminate\Http\JsonResponse |
||
393 | */ |
||
394 | protected function ajaxResponse($message) |
||
408 | |||
409 | /** |
||
410 | * Prepare input data for insert or update. |
||
411 | * |
||
412 | * @param array $data |
||
413 | * |
||
414 | * @return mixed |
||
415 | */ |
||
416 | protected function prepare($data = []) |
||
432 | |||
433 | /** |
||
434 | * Remove ignored fields from input. |
||
435 | * |
||
436 | * @param array $input |
||
437 | * |
||
438 | * @return array |
||
439 | */ |
||
440 | protected function removeIgnoredFields($input) |
||
446 | |||
447 | /** |
||
448 | * Get inputs for relations. |
||
449 | * |
||
450 | * @param array $inputs |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | protected function getRelationInputs($inputs = []) |
||
470 | |||
471 | /** |
||
472 | * Call editing callbacks. |
||
473 | * |
||
474 | * @return void |
||
475 | */ |
||
476 | protected function callEditing() |
||
482 | |||
483 | /** |
||
484 | * Call submitted callback. |
||
485 | * |
||
486 | * @return mixed |
||
487 | */ |
||
488 | protected function callSubmitted() |
||
496 | |||
497 | /** |
||
498 | * Call saving callback. |
||
499 | * |
||
500 | * @return mixed |
||
501 | */ |
||
502 | protected function callSaving() |
||
510 | |||
511 | /** |
||
512 | * Callback after saving a Model. |
||
513 | * |
||
514 | * @return mixed|null |
||
515 | */ |
||
516 | protected function callSaved() |
||
524 | |||
525 | /** |
||
526 | * Handle update. |
||
527 | * |
||
528 | * @param int $id |
||
529 | * |
||
530 | * @return \Symfony\Component\HttpFoundation\Response |
||
531 | */ |
||
532 | public function update($id, $data = null) |
||
590 | |||
591 | /** |
||
592 | * Get RedirectResponse after store. |
||
593 | * |
||
594 | * @return \Illuminate\Http\RedirectResponse |
||
595 | */ |
||
596 | protected function redirectAfterStore() |
||
604 | |||
605 | /** |
||
606 | * Get RedirectResponse after update. |
||
607 | * |
||
608 | * @param mixed $key |
||
609 | * |
||
610 | * @return \Illuminate\Http\RedirectResponse |
||
611 | */ |
||
612 | protected function redirectAfterUpdate($key) |
||
618 | |||
619 | /** |
||
620 | * Get RedirectResponse after data saving. |
||
621 | * |
||
622 | * @param string $resourcesPath |
||
623 | * @param string $key |
||
624 | * |
||
625 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
626 | */ |
||
627 | protected function redirectAfterSaving($resourcesPath, $key) |
||
643 | |||
644 | /** |
||
645 | * Check if request is from editable. |
||
646 | * |
||
647 | * @param array $input |
||
648 | * |
||
649 | * @return bool |
||
650 | */ |
||
651 | protected function isEditable(array $input = []) |
||
655 | |||
656 | /** |
||
657 | * Handle editable update. |
||
658 | * |
||
659 | * @param array $input |
||
660 | * |
||
661 | * @return array |
||
662 | */ |
||
663 | protected function handleEditable(array $input = []) |
||
675 | |||
676 | /** |
||
677 | * @param array $input |
||
678 | * |
||
679 | * @return array |
||
680 | */ |
||
681 | protected function handleFileDelete(array $input = []) |
||
692 | |||
693 | /** |
||
694 | * Handle orderable update. |
||
695 | * |
||
696 | * @param int $id |
||
697 | * @param array $input |
||
698 | * |
||
699 | * @return bool |
||
700 | */ |
||
701 | protected function handleOrderable($id, array $input = []) |
||
715 | |||
716 | /** |
||
717 | * Update relation data. |
||
718 | * |
||
719 | * @param array $relationsData |
||
720 | * |
||
721 | * @return void |
||
722 | */ |
||
723 | protected function updateRelation($relationsData) |
||
826 | |||
827 | /** |
||
828 | * Prepare input data for update. |
||
829 | * |
||
830 | * @param array $updates |
||
831 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
832 | * |
||
833 | * @return array |
||
834 | */ |
||
835 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
867 | |||
868 | /** |
||
869 | * @param string|array $columns |
||
870 | * @param bool $oneToOneRelation |
||
871 | * |
||
872 | * @return bool |
||
873 | */ |
||
874 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
885 | |||
886 | /** |
||
887 | * Prepare input data for insert. |
||
888 | * |
||
889 | * @param $inserts |
||
890 | * |
||
891 | * @return array |
||
892 | */ |
||
893 | protected function prepareInsert($inserts) |
||
916 | |||
917 | /** |
||
918 | * Is input data is has-one relation. |
||
919 | * |
||
920 | * @param array $inserts |
||
921 | * |
||
922 | * @return bool |
||
923 | */ |
||
924 | protected function isHasOneRelation($inserts) |
||
938 | |||
939 | /** |
||
940 | * Set after getting editing model callback. |
||
941 | * |
||
942 | * @param Closure $callback |
||
943 | * |
||
944 | * @return void |
||
945 | */ |
||
946 | public function editing(Closure $callback) |
||
950 | |||
951 | /** |
||
952 | * Set submitted callback. |
||
953 | * |
||
954 | * @param Closure $callback |
||
955 | * |
||
956 | * @return void |
||
957 | */ |
||
958 | public function submitted(Closure $callback) |
||
962 | |||
963 | /** |
||
964 | * Set saving callback. |
||
965 | * |
||
966 | * @param Closure $callback |
||
967 | * |
||
968 | * @return void |
||
969 | */ |
||
970 | public function saving(Closure $callback) |
||
974 | |||
975 | /** |
||
976 | * Set saved callback. |
||
977 | * |
||
978 | * @param Closure $callback |
||
979 | * |
||
980 | * @return void |
||
981 | */ |
||
982 | public function saved(Closure $callback) |
||
986 | |||
987 | /** |
||
988 | * Ignore fields to save. |
||
989 | * |
||
990 | * @param string|array $fields |
||
991 | * |
||
992 | * @return $this |
||
993 | */ |
||
994 | public function ignore($fields) |
||
1000 | |||
1001 | /** |
||
1002 | * @param array $data |
||
1003 | * @param string|array $columns |
||
1004 | * |
||
1005 | * @return array|mixed |
||
1006 | */ |
||
1007 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
1025 | |||
1026 | /** |
||
1027 | * Find field object by column. |
||
1028 | * |
||
1029 | * @param $column |
||
1030 | * |
||
1031 | * @return mixed |
||
1032 | */ |
||
1033 | protected function getFieldByColumn($column) |
||
1045 | |||
1046 | /** |
||
1047 | * Set original data for each field. |
||
1048 | * |
||
1049 | * @return void |
||
1050 | */ |
||
1051 | protected function setFieldOriginalValue() |
||
1061 | |||
1062 | /** |
||
1063 | * Set all fields value in form. |
||
1064 | * |
||
1065 | * @param $id |
||
1066 | * |
||
1067 | * @return void |
||
1068 | */ |
||
1069 | protected function setFieldValue($id) |
||
1087 | |||
1088 | /** |
||
1089 | * Don't snake case attributes. |
||
1090 | * |
||
1091 | * @param Model $model |
||
1092 | * |
||
1093 | * @return void |
||
1094 | */ |
||
1095 | protected static function doNotSnakeAttributes(Model $model) |
||
1101 | |||
1102 | /** |
||
1103 | * Get validation messages. |
||
1104 | * |
||
1105 | * @param array $input |
||
1106 | * |
||
1107 | * @return MessageBag|bool |
||
1108 | */ |
||
1109 | public function validationMessages($input) |
||
1128 | |||
1129 | /** |
||
1130 | * Merge validation messages from input validators. |
||
1131 | * |
||
1132 | * @param \Illuminate\Validation\Validator[] $validators |
||
1133 | * |
||
1134 | * @return MessageBag |
||
1135 | */ |
||
1136 | protected function mergeValidationMessages($validators) |
||
1146 | |||
1147 | /** |
||
1148 | * Get all relations of model from callable. |
||
1149 | * |
||
1150 | * @return array |
||
1151 | */ |
||
1152 | public function getRelations() |
||
1179 | |||
1180 | /** |
||
1181 | * Set action for form. |
||
1182 | * |
||
1183 | * @param string $action |
||
1184 | * |
||
1185 | * @return $this |
||
1186 | */ |
||
1187 | public function setAction($action) |
||
1193 | |||
1194 | /** |
||
1195 | * Set field and label width in current form. |
||
1196 | * |
||
1197 | * @param int $fieldWidth |
||
1198 | * @param int $labelWidth |
||
1199 | * |
||
1200 | * @return $this |
||
1201 | */ |
||
1202 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1213 | |||
1214 | /** |
||
1215 | * Set view for form. |
||
1216 | * |
||
1217 | * @param string $view |
||
1218 | * |
||
1219 | * @return $this |
||
1220 | */ |
||
1221 | public function setView($view) |
||
1227 | |||
1228 | /** |
||
1229 | * Set title for form. |
||
1230 | * |
||
1231 | * @param string $title |
||
1232 | * |
||
1233 | * @return $this |
||
1234 | */ |
||
1235 | public function setTitle($title = '') |
||
1241 | |||
1242 | /** |
||
1243 | * Add a row in form. |
||
1244 | * |
||
1245 | * @param Closure $callback |
||
1246 | * |
||
1247 | * @return $this |
||
1248 | */ |
||
1249 | public function row(Closure $callback) |
||
1255 | |||
1256 | /** |
||
1257 | * Tools setting for form. |
||
1258 | * |
||
1259 | * @param Closure $callback |
||
1260 | */ |
||
1261 | public function tools(Closure $callback) |
||
1265 | |||
1266 | /** |
||
1267 | * Disable form submit. |
||
1268 | * |
||
1269 | * @return $this |
||
1270 | * |
||
1271 | * @deprecated |
||
1272 | */ |
||
1273 | public function disableSubmit() |
||
1279 | |||
1280 | /** |
||
1281 | * Disable form reset. |
||
1282 | * |
||
1283 | * @return $this |
||
1284 | * |
||
1285 | * @deprecated |
||
1286 | */ |
||
1287 | public function disableReset() |
||
1293 | |||
1294 | /** |
||
1295 | * Disable View Checkbox on footer. |
||
1296 | * |
||
1297 | * @return $this |
||
1298 | */ |
||
1299 | public function disableViewCheck() |
||
1305 | |||
1306 | /** |
||
1307 | * Disable Editing Checkbox on footer. |
||
1308 | * |
||
1309 | * @return $this |
||
1310 | */ |
||
1311 | public function disableEditingCheck() |
||
1317 | |||
1318 | /** |
||
1319 | * Footer setting for form. |
||
1320 | * |
||
1321 | * @param Closure $callback |
||
1322 | */ |
||
1323 | public function footer(Closure $callback) |
||
1327 | |||
1328 | /** |
||
1329 | * Get current resource route url. |
||
1330 | * |
||
1331 | * @param int $slice |
||
1332 | * |
||
1333 | * @return string |
||
1334 | */ |
||
1335 | public function resource($slice = -2) |
||
1349 | |||
1350 | /** |
||
1351 | * Render the form contents. |
||
1352 | * |
||
1353 | * @return string |
||
1354 | */ |
||
1355 | public function render() |
||
1363 | |||
1364 | /** |
||
1365 | * Get or set input data. |
||
1366 | * |
||
1367 | * @param string $key |
||
1368 | * @param null $value |
||
1369 | * |
||
1370 | * @return array|mixed |
||
1371 | */ |
||
1372 | public function input($key, $value = null) |
||
1380 | |||
1381 | /** |
||
1382 | * Register builtin fields. |
||
1383 | * |
||
1384 | * @return void |
||
1385 | */ |
||
1386 | public static function registerBuiltinFields() |
||
1441 | |||
1442 | /** |
||
1443 | * Register custom field. |
||
1444 | * |
||
1445 | * @param string $abstract |
||
1446 | * @param string $class |
||
1447 | * |
||
1448 | * @return void |
||
1449 | */ |
||
1450 | public static function extend($abstract, $class) |
||
1454 | |||
1455 | /** |
||
1456 | * Set form field alias. |
||
1457 | * |
||
1458 | * @param string $field |
||
1459 | * @param string $alias |
||
1460 | * |
||
1461 | * @return void |
||
1462 | */ |
||
1463 | public static function alias($field, $alias) |
||
1467 | |||
1468 | /** |
||
1469 | * Remove registered field. |
||
1470 | * |
||
1471 | * @param array|string $abstract |
||
1472 | */ |
||
1473 | public static function forget($abstract) |
||
1477 | |||
1478 | /** |
||
1479 | * Find field class. |
||
1480 | * |
||
1481 | * @param string $method |
||
1482 | * |
||
1483 | * @return bool|mixed |
||
1484 | */ |
||
1485 | public static function findFieldClass($method) |
||
1500 | |||
1501 | /** |
||
1502 | * Collect assets required by registered field. |
||
1503 | * |
||
1504 | * @return array |
||
1505 | */ |
||
1506 | public static function collectFieldAssets() |
||
1531 | |||
1532 | /** |
||
1533 | * Getter. |
||
1534 | * |
||
1535 | * @param string $name |
||
1536 | * |
||
1537 | * @return array|mixed |
||
1538 | */ |
||
1539 | public function __get($name) |
||
1543 | |||
1544 | /** |
||
1545 | * Setter. |
||
1546 | * |
||
1547 | * @param string $name |
||
1548 | * @param $value |
||
1549 | */ |
||
1550 | public function __set($name, $value) |
||
1554 | |||
1555 | /** |
||
1556 | * Generate a Field object and add to form builder if Field exists. |
||
1557 | * |
||
1558 | * @param string $method |
||
1559 | * @param array $arguments |
||
1560 | * |
||
1561 | * @return Field |
||
1562 | */ |
||
1563 | public function __call($method, $arguments) |
||
1579 | } |
||
1580 |
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: