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 | * Use tab to split form. |
||
236 | * |
||
237 | * @param string $title |
||
238 | * @param Closure $content |
||
239 | * |
||
240 | * @return $this |
||
241 | */ |
||
242 | public function tab($title, Closure $content, $active = false) |
||
248 | |||
249 | /** |
||
250 | * Get Tab instance. |
||
251 | * |
||
252 | * @return Tab |
||
253 | */ |
||
254 | public function getTab() |
||
262 | |||
263 | /** |
||
264 | * Destroy data entity and remove files. |
||
265 | * |
||
266 | * @param $id |
||
267 | * |
||
268 | * @return mixed |
||
269 | */ |
||
270 | public function destroy($id) |
||
284 | |||
285 | /** |
||
286 | * Remove files or images in record. |
||
287 | * |
||
288 | * @param $id |
||
289 | */ |
||
290 | protected function deleteFilesAndImages($id) |
||
303 | |||
304 | /** |
||
305 | * Store a new record. |
||
306 | * |
||
307 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
308 | */ |
||
309 | public function store() |
||
344 | |||
345 | /** |
||
346 | * Get ajax response. |
||
347 | * |
||
348 | * @param string $message |
||
349 | * |
||
350 | * @return bool|\Illuminate\Http\JsonResponse |
||
351 | */ |
||
352 | protected function ajaxResponse($message) |
||
366 | |||
367 | /** |
||
368 | * Prepare input data for insert or update. |
||
369 | * |
||
370 | * @param array $data |
||
371 | * |
||
372 | * @return mixed |
||
373 | */ |
||
374 | protected function prepare($data = []) |
||
390 | |||
391 | /** |
||
392 | * Remove ignored fields from input. |
||
393 | * |
||
394 | * @param array $input |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | protected function removeIgnoredFields($input) |
||
404 | |||
405 | /** |
||
406 | * Get inputs for relations. |
||
407 | * |
||
408 | * @param array $inputs |
||
409 | * |
||
410 | * @return array |
||
411 | */ |
||
412 | protected function getRelationInputs($inputs = []) |
||
428 | |||
429 | /** |
||
430 | * Call submitted callback. |
||
431 | * |
||
432 | * @return mixed |
||
433 | */ |
||
434 | protected function callSubmitted() |
||
440 | |||
441 | /** |
||
442 | * Call saving callback. |
||
443 | * |
||
444 | * @return mixed |
||
445 | */ |
||
446 | protected function callSaving() |
||
452 | |||
453 | /** |
||
454 | * Callback after saving a Model. |
||
455 | * |
||
456 | * @param Closure|null $callback |
||
457 | * |
||
458 | * @return mixed|null |
||
459 | */ |
||
460 | protected function complete(Closure $callback = null) |
||
466 | |||
467 | /** |
||
468 | * Handle update. |
||
469 | * |
||
470 | * @param int $id |
||
471 | * |
||
472 | * @return \Symfony\Component\HttpFoundation\Response |
||
473 | */ |
||
474 | public function update($id, $data = null) |
||
532 | |||
533 | /** |
||
534 | * Get RedirectResponse after store. |
||
535 | * |
||
536 | * @return \Illuminate\Http\RedirectResponse |
||
537 | */ |
||
538 | protected function redirectAfterStore() |
||
546 | |||
547 | /** |
||
548 | * Get RedirectResponse after update. |
||
549 | * |
||
550 | * @param mixed $key |
||
551 | * |
||
552 | * @return \Illuminate\Http\RedirectResponse |
||
553 | */ |
||
554 | protected function redirectAfterUpdate($key) |
||
560 | |||
561 | /** |
||
562 | * Get RedirectResponse after data saving. |
||
563 | * |
||
564 | * @param string $resourcesPath |
||
565 | * @param string $key |
||
566 | * |
||
567 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
568 | */ |
||
569 | protected function redirectAfterSaving($resourcesPath, $key) |
||
585 | |||
586 | /** |
||
587 | * Check if request is from editable. |
||
588 | * |
||
589 | * @param array $input |
||
590 | * |
||
591 | * @return bool |
||
592 | */ |
||
593 | protected function isEditable(array $input = []) |
||
597 | |||
598 | /** |
||
599 | * Handle editable update. |
||
600 | * |
||
601 | * @param array $input |
||
602 | * |
||
603 | * @return array |
||
604 | */ |
||
605 | protected function handleEditable(array $input = []) |
||
617 | |||
618 | /** |
||
619 | * @param array $input |
||
620 | * |
||
621 | * @return array |
||
622 | */ |
||
623 | protected function handleFileDelete(array $input = []) |
||
634 | |||
635 | /** |
||
636 | * Handle orderable update. |
||
637 | * |
||
638 | * @param int $id |
||
639 | * @param array $input |
||
640 | * |
||
641 | * @return bool |
||
642 | */ |
||
643 | protected function handleOrderable($id, array $input = []) |
||
657 | |||
658 | /** |
||
659 | * Update relation data. |
||
660 | * |
||
661 | * @param array $relationsData |
||
662 | * |
||
663 | * @return void |
||
664 | */ |
||
665 | protected function updateRelation($relationsData) |
||
746 | |||
747 | /** |
||
748 | * Prepare input data for update. |
||
749 | * |
||
750 | * @param array $updates |
||
751 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
752 | * |
||
753 | * @return array |
||
754 | */ |
||
755 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
787 | |||
788 | /** |
||
789 | * @param string|array $columns |
||
790 | * @param bool $oneToOneRelation |
||
791 | * |
||
792 | * @return bool |
||
793 | */ |
||
794 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
805 | |||
806 | /** |
||
807 | * Prepare input data for insert. |
||
808 | * |
||
809 | * @param $inserts |
||
810 | * |
||
811 | * @return array |
||
812 | */ |
||
813 | protected function prepareInsert($inserts) |
||
836 | |||
837 | /** |
||
838 | * Is input data is has-one relation. |
||
839 | * |
||
840 | * @param array $inserts |
||
841 | * |
||
842 | * @return bool |
||
843 | */ |
||
844 | protected function isHasOneRelation($inserts) |
||
858 | |||
859 | /** |
||
860 | * Set submitted callback. |
||
861 | * |
||
862 | * @param Closure $callback |
||
863 | * |
||
864 | * @return void |
||
865 | */ |
||
866 | public function submitted(Closure $callback) |
||
870 | |||
871 | /** |
||
872 | * Set saving callback. |
||
873 | * |
||
874 | * @param Closure $callback |
||
875 | * |
||
876 | * @return void |
||
877 | */ |
||
878 | public function saving(Closure $callback) |
||
882 | |||
883 | /** |
||
884 | * Set saved callback. |
||
885 | * |
||
886 | * @param Closure $callback |
||
887 | * |
||
888 | * @return void |
||
889 | */ |
||
890 | public function saved(Closure $callback) |
||
894 | |||
895 | /** |
||
896 | * Ignore fields to save. |
||
897 | * |
||
898 | * @param string|array $fields |
||
899 | * |
||
900 | * @return $this |
||
901 | */ |
||
902 | public function ignore($fields) |
||
908 | |||
909 | /** |
||
910 | * @param array $data |
||
911 | * @param string|array $columns |
||
912 | * |
||
913 | * @return array|mixed |
||
914 | */ |
||
915 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
933 | |||
934 | /** |
||
935 | * Find field object by column. |
||
936 | * |
||
937 | * @param $column |
||
938 | * |
||
939 | * @return mixed |
||
940 | */ |
||
941 | protected function getFieldByColumn($column) |
||
953 | |||
954 | /** |
||
955 | * Set original data for each field. |
||
956 | * |
||
957 | * @return void |
||
958 | */ |
||
959 | protected function setFieldOriginalValue() |
||
969 | |||
970 | /** |
||
971 | * Set all fields value in form. |
||
972 | * |
||
973 | * @param $id |
||
974 | * |
||
975 | * @return void |
||
976 | */ |
||
977 | protected function setFieldValue($id) |
||
993 | |||
994 | /** |
||
995 | * Don't snake case attributes. |
||
996 | * |
||
997 | * @param Model $model |
||
998 | * |
||
999 | * @return void |
||
1000 | */ |
||
1001 | protected static function doNotSnakeAttributes(Model $model) |
||
1007 | |||
1008 | /** |
||
1009 | * Get validation messages. |
||
1010 | * |
||
1011 | * @param array $input |
||
1012 | * |
||
1013 | * @return MessageBag|bool |
||
1014 | */ |
||
1015 | protected function validationMessages($input) |
||
1034 | |||
1035 | /** |
||
1036 | * Merge validation messages from input validators. |
||
1037 | * |
||
1038 | * @param \Illuminate\Validation\Validator[] $validators |
||
1039 | * |
||
1040 | * @return MessageBag |
||
1041 | */ |
||
1042 | protected function mergeValidationMessages($validators) |
||
1052 | |||
1053 | /** |
||
1054 | * Get all relations of model from callable. |
||
1055 | * |
||
1056 | * @return array |
||
1057 | */ |
||
1058 | public function getRelations() |
||
1085 | |||
1086 | /** |
||
1087 | * Set action for form. |
||
1088 | * |
||
1089 | * @param string $action |
||
1090 | * |
||
1091 | * @return $this |
||
1092 | */ |
||
1093 | public function setAction($action) |
||
1099 | |||
1100 | /** |
||
1101 | * Set field and label width in current form. |
||
1102 | * |
||
1103 | * @param int $fieldWidth |
||
1104 | * @param int $labelWidth |
||
1105 | * |
||
1106 | * @return $this |
||
1107 | */ |
||
1108 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1119 | |||
1120 | /** |
||
1121 | * Set view for form. |
||
1122 | * |
||
1123 | * @param string $view |
||
1124 | * |
||
1125 | * @return $this |
||
1126 | */ |
||
1127 | public function setView($view) |
||
1133 | |||
1134 | /** |
||
1135 | * Set title for form. |
||
1136 | * |
||
1137 | * @param string $title |
||
1138 | * |
||
1139 | * @return $this |
||
1140 | */ |
||
1141 | public function setTitle($title = '') |
||
1147 | |||
1148 | /** |
||
1149 | * Add a row in form. |
||
1150 | * |
||
1151 | * @param Closure $callback |
||
1152 | * |
||
1153 | * @return $this |
||
1154 | */ |
||
1155 | public function row(Closure $callback) |
||
1161 | |||
1162 | /** |
||
1163 | * Tools setting for form. |
||
1164 | * |
||
1165 | * @param Closure $callback |
||
1166 | */ |
||
1167 | public function tools(Closure $callback) |
||
1171 | |||
1172 | /** |
||
1173 | * Disable form submit. |
||
1174 | * |
||
1175 | * @return $this |
||
1176 | * |
||
1177 | * @deprecated |
||
1178 | */ |
||
1179 | public function disableSubmit() |
||
1185 | |||
1186 | /** |
||
1187 | * Disable form reset. |
||
1188 | * |
||
1189 | * @return $this |
||
1190 | * |
||
1191 | * @deprecated |
||
1192 | */ |
||
1193 | public function disableReset() |
||
1199 | |||
1200 | /** |
||
1201 | * Disable View Checkbox on footer. |
||
1202 | * |
||
1203 | * @return $this |
||
1204 | */ |
||
1205 | public function disableViewCheck() |
||
1211 | |||
1212 | /** |
||
1213 | * Disable Editing Checkbox on footer. |
||
1214 | * |
||
1215 | * @return $this |
||
1216 | */ |
||
1217 | public function disableEditingCheck() |
||
1223 | |||
1224 | /** |
||
1225 | * Footer setting for form. |
||
1226 | * |
||
1227 | * @param Closure $callback |
||
1228 | */ |
||
1229 | public function footer(Closure $callback) |
||
1233 | |||
1234 | /** |
||
1235 | * Get current resource route url. |
||
1236 | * |
||
1237 | * @param int $slice |
||
1238 | * |
||
1239 | * @return string |
||
1240 | */ |
||
1241 | public function resource($slice = -2) |
||
1255 | |||
1256 | /** |
||
1257 | * Render the form contents. |
||
1258 | * |
||
1259 | * @return string |
||
1260 | */ |
||
1261 | public function render() |
||
1269 | |||
1270 | /** |
||
1271 | * Get or set input data. |
||
1272 | * |
||
1273 | * @param string $key |
||
1274 | * @param null $value |
||
1275 | * |
||
1276 | * @return array|mixed |
||
1277 | */ |
||
1278 | public function input($key, $value = null) |
||
1286 | |||
1287 | /** |
||
1288 | * Register builtin fields. |
||
1289 | * |
||
1290 | * @return void |
||
1291 | */ |
||
1292 | public static function registerBuiltinFields() |
||
1347 | |||
1348 | /** |
||
1349 | * Register custom field. |
||
1350 | * |
||
1351 | * @param string $abstract |
||
1352 | * @param string $class |
||
1353 | * |
||
1354 | * @return void |
||
1355 | */ |
||
1356 | public static function extend($abstract, $class) |
||
1360 | |||
1361 | /** |
||
1362 | * Remove registered field. |
||
1363 | * |
||
1364 | * @param array|string $abstract |
||
1365 | */ |
||
1366 | public static function forget($abstract) |
||
1370 | |||
1371 | /** |
||
1372 | * Find field class. |
||
1373 | * |
||
1374 | * @param string $method |
||
1375 | * |
||
1376 | * @return bool|mixed |
||
1377 | */ |
||
1378 | View Code Duplication | public static function findFieldClass($method) |
|
1388 | |||
1389 | /** |
||
1390 | * Collect assets required by registered field. |
||
1391 | * |
||
1392 | * @return array |
||
1393 | */ |
||
1394 | public static function collectFieldAssets() |
||
1419 | |||
1420 | /** |
||
1421 | * Getter. |
||
1422 | * |
||
1423 | * @param string $name |
||
1424 | * |
||
1425 | * @return array|mixed |
||
1426 | */ |
||
1427 | public function __get($name) |
||
1431 | |||
1432 | /** |
||
1433 | * Setter. |
||
1434 | * |
||
1435 | * @param string $name |
||
1436 | * @param $value |
||
1437 | */ |
||
1438 | public function __set($name, $value) |
||
1442 | |||
1443 | /** |
||
1444 | * Generate a Field object and add to form builder if Field exists. |
||
1445 | * |
||
1446 | * @param string $method |
||
1447 | * @param array $arguments |
||
1448 | * |
||
1449 | * @return Field|void |
||
1450 | */ |
||
1451 | public function __call($method, $arguments) |
||
1463 | } |
||
1464 |
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: