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) |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Prepare input data for update. |
||
| 787 | * |
||
| 788 | * @param array $updates |
||
| 789 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
| 790 | * |
||
| 791 | * @return array |
||
| 792 | */ |
||
| 793 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
| 825 | |||
| 826 | /** |
||
| 827 | * @param string|array $columns |
||
| 828 | * @param bool $oneToOneRelation |
||
| 829 | * |
||
| 830 | * @return bool |
||
| 831 | */ |
||
| 832 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Prepare input data for insert. |
||
| 846 | * |
||
| 847 | * @param $inserts |
||
| 848 | * |
||
| 849 | * @return array |
||
| 850 | */ |
||
| 851 | protected function prepareInsert($inserts) |
||
| 874 | |||
| 875 | /** |
||
| 876 | * Is input data is has-one relation. |
||
| 877 | * |
||
| 878 | * @param array $inserts |
||
| 879 | * |
||
| 880 | * @return bool |
||
| 881 | */ |
||
| 882 | protected function isHasOneRelation($inserts) |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Set submitted callback. |
||
| 899 | * |
||
| 900 | * @param Closure $callback |
||
| 901 | * |
||
| 902 | * @return void |
||
| 903 | */ |
||
| 904 | public function submitted(Closure $callback) |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Set saving callback. |
||
| 911 | * |
||
| 912 | * @param Closure $callback |
||
| 913 | * |
||
| 914 | * @return void |
||
| 915 | */ |
||
| 916 | public function saving(Closure $callback) |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Set saved callback. |
||
| 923 | * |
||
| 924 | * @param Closure $callback |
||
| 925 | * |
||
| 926 | * @return void |
||
| 927 | */ |
||
| 928 | public function saved(Closure $callback) |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Ignore fields to save. |
||
| 935 | * |
||
| 936 | * @param string|array $fields |
||
| 937 | * |
||
| 938 | * @return $this |
||
| 939 | */ |
||
| 940 | public function ignore($fields) |
||
| 946 | |||
| 947 | /** |
||
| 948 | * @param array $data |
||
| 949 | * @param string|array $columns |
||
| 950 | * |
||
| 951 | * @return array|mixed |
||
| 952 | */ |
||
| 953 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
| 971 | |||
| 972 | /** |
||
| 973 | * Find field object by column. |
||
| 974 | * |
||
| 975 | * @param $column |
||
| 976 | * |
||
| 977 | * @return mixed |
||
| 978 | */ |
||
| 979 | protected function getFieldByColumn($column) |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Set original data for each field. |
||
| 994 | * |
||
| 995 | * @return void |
||
| 996 | */ |
||
| 997 | protected function setFieldOriginalValue() |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Set all fields value in form. |
||
| 1010 | * |
||
| 1011 | * @param $id |
||
| 1012 | * |
||
| 1013 | * @return void |
||
| 1014 | */ |
||
| 1015 | protected function setFieldValue($id) |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Don't snake case attributes. |
||
| 1034 | * |
||
| 1035 | * @param Model $model |
||
| 1036 | * |
||
| 1037 | * @return void |
||
| 1038 | */ |
||
| 1039 | protected static function doNotSnakeAttributes(Model $model) |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Get validation messages. |
||
| 1048 | * |
||
| 1049 | * @param array $input |
||
| 1050 | * |
||
| 1051 | * @return MessageBag|bool |
||
| 1052 | */ |
||
| 1053 | protected function validationMessages($input) |
||
| 1072 | |||
| 1073 | /** |
||
| 1074 | * Merge validation messages from input validators. |
||
| 1075 | * |
||
| 1076 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 1077 | * |
||
| 1078 | * @return MessageBag |
||
| 1079 | */ |
||
| 1080 | protected function mergeValidationMessages($validators) |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Get all relations of model from callable. |
||
| 1093 | * |
||
| 1094 | * @return array |
||
| 1095 | */ |
||
| 1096 | public function getRelations() |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Set action for form. |
||
| 1126 | * |
||
| 1127 | * @param string $action |
||
| 1128 | * |
||
| 1129 | * @return $this |
||
| 1130 | */ |
||
| 1131 | public function setAction($action) |
||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * Set field and label width in current form. |
||
| 1140 | * |
||
| 1141 | * @param int $fieldWidth |
||
| 1142 | * @param int $labelWidth |
||
| 1143 | * |
||
| 1144 | * @return $this |
||
| 1145 | */ |
||
| 1146 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Set view for form. |
||
| 1160 | * |
||
| 1161 | * @param string $view |
||
| 1162 | * |
||
| 1163 | * @return $this |
||
| 1164 | */ |
||
| 1165 | public function setView($view) |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Set title for form. |
||
| 1174 | * |
||
| 1175 | * @param string $title |
||
| 1176 | * |
||
| 1177 | * @return $this |
||
| 1178 | */ |
||
| 1179 | public function setTitle($title = '') |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Add a row in form. |
||
| 1188 | * |
||
| 1189 | * @param Closure $callback |
||
| 1190 | * |
||
| 1191 | * @return $this |
||
| 1192 | */ |
||
| 1193 | public function row(Closure $callback) |
||
| 1199 | |||
| 1200 | /** |
||
| 1201 | * Tools setting for form. |
||
| 1202 | * |
||
| 1203 | * @param Closure $callback |
||
| 1204 | */ |
||
| 1205 | public function tools(Closure $callback) |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Disable form submit. |
||
| 1212 | * |
||
| 1213 | * @return $this |
||
| 1214 | * |
||
| 1215 | * @deprecated |
||
| 1216 | */ |
||
| 1217 | public function disableSubmit() |
||
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Disable form reset. |
||
| 1226 | * |
||
| 1227 | * @return $this |
||
| 1228 | * |
||
| 1229 | * @deprecated |
||
| 1230 | */ |
||
| 1231 | public function disableReset() |
||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * Disable View Checkbox on footer. |
||
| 1240 | * |
||
| 1241 | * @return $this |
||
| 1242 | */ |
||
| 1243 | public function disableViewCheck() |
||
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Disable Editing Checkbox on footer. |
||
| 1252 | * |
||
| 1253 | * @return $this |
||
| 1254 | */ |
||
| 1255 | public function disableEditingCheck() |
||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * Footer setting for form. |
||
| 1264 | * |
||
| 1265 | * @param Closure $callback |
||
| 1266 | */ |
||
| 1267 | public function footer(Closure $callback) |
||
| 1271 | |||
| 1272 | /** |
||
| 1273 | * Get current resource route url. |
||
| 1274 | * |
||
| 1275 | * @param int $slice |
||
| 1276 | * |
||
| 1277 | * @return string |
||
| 1278 | */ |
||
| 1279 | public function resource($slice = -2) |
||
| 1293 | |||
| 1294 | /** |
||
| 1295 | * Render the form contents. |
||
| 1296 | * |
||
| 1297 | * @return string |
||
| 1298 | */ |
||
| 1299 | public function render() |
||
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Get or set input data. |
||
| 1310 | * |
||
| 1311 | * @param string $key |
||
| 1312 | * @param null $value |
||
| 1313 | * |
||
| 1314 | * @return array|mixed |
||
| 1315 | */ |
||
| 1316 | public function input($key, $value = null) |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Register builtin fields. |
||
| 1327 | * |
||
| 1328 | * @return void |
||
| 1329 | */ |
||
| 1330 | public static function registerBuiltinFields() |
||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * Register custom field. |
||
| 1388 | * |
||
| 1389 | * @param string $abstract |
||
| 1390 | * @param string $class |
||
| 1391 | * |
||
| 1392 | * @return void |
||
| 1393 | */ |
||
| 1394 | public static function extend($abstract, $class) |
||
| 1398 | |||
| 1399 | /** |
||
| 1400 | * Set form field alias. |
||
| 1401 | * |
||
| 1402 | * @param string $field |
||
| 1403 | * @param string $alias |
||
| 1404 | * |
||
| 1405 | * @return void |
||
| 1406 | */ |
||
| 1407 | public static function alias($field, $alias) |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * Remove registered field. |
||
| 1414 | * |
||
| 1415 | * @param array|string $abstract |
||
| 1416 | */ |
||
| 1417 | public static function forget($abstract) |
||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * Find field class. |
||
| 1424 | * |
||
| 1425 | * @param string $method |
||
| 1426 | * |
||
| 1427 | * @return bool|mixed |
||
| 1428 | */ |
||
| 1429 | public static function findFieldClass($method) |
||
| 1444 | |||
| 1445 | /** |
||
| 1446 | * Collect assets required by registered field. |
||
| 1447 | * |
||
| 1448 | * @return array |
||
| 1449 | */ |
||
| 1450 | public static function collectFieldAssets() |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | * Getter. |
||
| 1478 | * |
||
| 1479 | * @param string $name |
||
| 1480 | * |
||
| 1481 | * @return array|mixed |
||
| 1482 | */ |
||
| 1483 | public function __get($name) |
||
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Setter. |
||
| 1490 | * |
||
| 1491 | * @param string $name |
||
| 1492 | * @param $value |
||
| 1493 | */ |
||
| 1494 | public function __set($name, $value) |
||
| 1498 | |||
| 1499 | /** |
||
| 1500 | * Generate a Field object and add to form builder if Field exists. |
||
| 1501 | * |
||
| 1502 | * @param string $method |
||
| 1503 | * @param array $arguments |
||
| 1504 | * |
||
| 1505 | * @return Field |
||
| 1506 | */ |
||
| 1507 | public function __call($method, $arguments) |
||
| 1523 | } |
||
| 1524 |
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: