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 $editingModel = []; |
||
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) |
||
192 | { |
||
193 | $this->model = $model; |
||
194 | |||
195 | $this->builder = new Builder($this); |
||
196 | |||
197 | if ($callback instanceof Closure) { |
||
198 | $callback($this); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param Field $field |
||
204 | * |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function pushField(Field $field) |
||
208 | { |
||
209 | $field->setForm($this); |
||
210 | |||
211 | $this->builder->fields()->push($field); |
||
212 | |||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return Model |
||
218 | */ |
||
219 | public function model() |
||
220 | { |
||
221 | return $this->model; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @return Builder |
||
226 | */ |
||
227 | public function builder() |
||
228 | { |
||
229 | return $this->builder; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Generate a edit form. |
||
234 | * |
||
235 | * @param $id |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function edit($id) |
||
240 | { |
||
241 | $this->builder->setMode(Builder::MODE_EDIT); |
||
242 | $this->builder->setResourceId($id); |
||
243 | |||
244 | $this->setFieldValue($id); |
||
245 | |||
246 | return $this; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Use tab to split form. |
||
251 | * |
||
252 | * @param string $title |
||
253 | * @param Closure $content |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function tab($title, Closure $content, $active = false) |
||
263 | |||
264 | /** |
||
265 | * Get Tab instance. |
||
266 | * |
||
267 | * @return Tab |
||
268 | */ |
||
269 | public function getTab() |
||
277 | |||
278 | /** |
||
279 | * Destroy data entity and remove files. |
||
280 | * |
||
281 | * @param $id |
||
282 | * |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function destroy($id) |
||
296 | |||
297 | /** |
||
298 | * Remove files in record. |
||
299 | * |
||
300 | * @param $id |
||
301 | */ |
||
302 | protected function deleteFiles($id) |
||
322 | |||
323 | /** |
||
324 | * Store a new record. |
||
325 | * |
||
326 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
327 | */ |
||
328 | public function store() |
||
363 | |||
364 | /** |
||
365 | * Get ajax response. |
||
366 | * |
||
367 | * @param string $message |
||
368 | * |
||
369 | * @return bool|\Illuminate\Http\JsonResponse |
||
370 | */ |
||
371 | protected function ajaxResponse($message) |
||
385 | |||
386 | /** |
||
387 | * Prepare input data for insert or update. |
||
388 | * |
||
389 | * @param array $data |
||
390 | * |
||
391 | * @return mixed |
||
392 | */ |
||
393 | protected function prepare($data = []) |
||
409 | |||
410 | /** |
||
411 | * Remove ignored fields from input. |
||
412 | * |
||
413 | * @param array $input |
||
414 | * |
||
415 | * @return array |
||
416 | */ |
||
417 | protected function removeIgnoredFields($input) |
||
423 | |||
424 | /** |
||
425 | * Get inputs for relations. |
||
426 | * |
||
427 | * @param array $inputs |
||
428 | * |
||
429 | * @return array |
||
430 | */ |
||
431 | protected function getRelationInputs($inputs = []) |
||
447 | |||
448 | /** |
||
449 | * Call submitted callback. |
||
450 | * |
||
451 | * @return mixed |
||
452 | */ |
||
453 | protected function callSubmitted() |
||
461 | |||
462 | /** |
||
463 | * Call saving callback. |
||
464 | * |
||
465 | * @return mixed |
||
466 | */ |
||
467 | protected function callSaving() |
||
475 | |||
476 | /** |
||
477 | * Callback after saving a Model. |
||
478 | * |
||
479 | * @return mixed|null |
||
480 | */ |
||
481 | protected function callSaved() |
||
489 | |||
490 | /** |
||
491 | * Handle update. |
||
492 | * |
||
493 | * @param int $id |
||
494 | * |
||
495 | * @return \Symfony\Component\HttpFoundation\Response |
||
496 | */ |
||
497 | public function update($id, $data = null) |
||
555 | |||
556 | /** |
||
557 | * Get RedirectResponse after store. |
||
558 | * |
||
559 | * @return \Illuminate\Http\RedirectResponse |
||
560 | */ |
||
561 | protected function redirectAfterStore() |
||
569 | |||
570 | /** |
||
571 | * Get RedirectResponse after update. |
||
572 | * |
||
573 | * @param mixed $key |
||
574 | * |
||
575 | * @return \Illuminate\Http\RedirectResponse |
||
576 | */ |
||
577 | protected function redirectAfterUpdate($key) |
||
583 | |||
584 | /** |
||
585 | * Get RedirectResponse after data saving. |
||
586 | * |
||
587 | * @param string $resourcesPath |
||
588 | * @param string $key |
||
589 | * |
||
590 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
591 | */ |
||
592 | protected function redirectAfterSaving($resourcesPath, $key) |
||
608 | |||
609 | /** |
||
610 | * Check if request is from editable. |
||
611 | * |
||
612 | * @param array $input |
||
613 | * |
||
614 | * @return bool |
||
615 | */ |
||
616 | protected function isEditable(array $input = []) |
||
620 | |||
621 | /** |
||
622 | * Handle editable update. |
||
623 | * |
||
624 | * @param array $input |
||
625 | * |
||
626 | * @return array |
||
627 | */ |
||
628 | protected function handleEditable(array $input = []) |
||
640 | |||
641 | /** |
||
642 | * @param array $input |
||
643 | * |
||
644 | * @return array |
||
645 | */ |
||
646 | protected function handleFileDelete(array $input = []) |
||
657 | |||
658 | /** |
||
659 | * Handle orderable update. |
||
660 | * |
||
661 | * @param int $id |
||
662 | * @param array $input |
||
663 | * |
||
664 | * @return bool |
||
665 | */ |
||
666 | protected function handleOrderable($id, array $input = []) |
||
680 | |||
681 | /** |
||
682 | * Update relation data. |
||
683 | * |
||
684 | * @param array $relationsData |
||
685 | * |
||
686 | * @return void |
||
687 | */ |
||
688 | protected function updateRelation($relationsData) |
||
791 | |||
792 | /** |
||
793 | * Prepare input data for update. |
||
794 | * |
||
795 | * @param array $updates |
||
796 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
797 | * |
||
798 | * @return array |
||
799 | */ |
||
800 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
832 | |||
833 | /** |
||
834 | * @param string|array $columns |
||
835 | * @param bool $oneToOneRelation |
||
836 | * |
||
837 | * @return bool |
||
838 | */ |
||
839 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
850 | |||
851 | /** |
||
852 | * Prepare input data for insert. |
||
853 | * |
||
854 | * @param $inserts |
||
855 | * |
||
856 | * @return array |
||
857 | */ |
||
858 | protected function prepareInsert($inserts) |
||
881 | |||
882 | /** |
||
883 | * Is input data is has-one relation. |
||
884 | * |
||
885 | * @param array $inserts |
||
886 | * |
||
887 | * @return bool |
||
888 | */ |
||
889 | protected function isHasOneRelation($inserts) |
||
903 | |||
904 | /** |
||
905 | * Set after getting editing model callback. |
||
906 | * |
||
907 | * @param Closure $callback |
||
908 | * |
||
909 | * @return void |
||
910 | */ |
||
911 | public function editingModel(Closure $callback) |
||
915 | |||
916 | /** |
||
917 | * Set submitted callback. |
||
918 | * |
||
919 | * @param Closure $callback |
||
920 | * |
||
921 | * @return void |
||
922 | */ |
||
923 | public function submitted(Closure $callback) |
||
927 | |||
928 | /** |
||
929 | * Set saving callback. |
||
930 | * |
||
931 | * @param Closure $callback |
||
932 | * |
||
933 | * @return void |
||
934 | */ |
||
935 | public function saving(Closure $callback) |
||
939 | |||
940 | /** |
||
941 | * Set saved callback. |
||
942 | * |
||
943 | * @param Closure $callback |
||
944 | * |
||
945 | * @return void |
||
946 | */ |
||
947 | public function saved(Closure $callback) |
||
951 | |||
952 | /** |
||
953 | * Ignore fields to save. |
||
954 | * |
||
955 | * @param string|array $fields |
||
956 | * |
||
957 | * @return $this |
||
958 | */ |
||
959 | public function ignore($fields) |
||
965 | |||
966 | /** |
||
967 | * @param array $data |
||
968 | * @param string|array $columns |
||
969 | * |
||
970 | * @return array|mixed |
||
971 | */ |
||
972 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
990 | |||
991 | /** |
||
992 | * Find field object by column. |
||
993 | * |
||
994 | * @param $column |
||
995 | * |
||
996 | * @return mixed |
||
997 | */ |
||
998 | protected function getFieldByColumn($column) |
||
1010 | |||
1011 | /** |
||
1012 | * Set original data for each field. |
||
1013 | * |
||
1014 | * @return void |
||
1015 | */ |
||
1016 | protected function setFieldOriginalValue() |
||
1026 | |||
1027 | /** |
||
1028 | * Set all fields value in form. |
||
1029 | * |
||
1030 | * @param $id |
||
1031 | * |
||
1032 | * @return void |
||
1033 | */ |
||
1034 | protected function setFieldValue($id) |
||
1054 | |||
1055 | /** |
||
1056 | * Don't snake case attributes. |
||
1057 | * |
||
1058 | * @param Model $model |
||
1059 | * |
||
1060 | * @return void |
||
1061 | */ |
||
1062 | protected static function doNotSnakeAttributes(Model $model) |
||
1068 | |||
1069 | /** |
||
1070 | * Get validation messages. |
||
1071 | * |
||
1072 | * @param array $input |
||
1073 | * |
||
1074 | * @return MessageBag|bool |
||
1075 | */ |
||
1076 | public function validationMessages($input) |
||
1095 | |||
1096 | /** |
||
1097 | * Merge validation messages from input validators. |
||
1098 | * |
||
1099 | * @param \Illuminate\Validation\Validator[] $validators |
||
1100 | * |
||
1101 | * @return MessageBag |
||
1102 | */ |
||
1103 | protected function mergeValidationMessages($validators) |
||
1113 | |||
1114 | /** |
||
1115 | * Get all relations of model from callable. |
||
1116 | * |
||
1117 | * @return array |
||
1118 | */ |
||
1119 | public function getRelations() |
||
1146 | |||
1147 | /** |
||
1148 | * Set action for form. |
||
1149 | * |
||
1150 | * @param string $action |
||
1151 | * |
||
1152 | * @return $this |
||
1153 | */ |
||
1154 | public function setAction($action) |
||
1160 | |||
1161 | /** |
||
1162 | * Set field and label width in current form. |
||
1163 | * |
||
1164 | * @param int $fieldWidth |
||
1165 | * @param int $labelWidth |
||
1166 | * |
||
1167 | * @return $this |
||
1168 | */ |
||
1169 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1180 | |||
1181 | /** |
||
1182 | * Set view for form. |
||
1183 | * |
||
1184 | * @param string $view |
||
1185 | * |
||
1186 | * @return $this |
||
1187 | */ |
||
1188 | public function setView($view) |
||
1194 | |||
1195 | /** |
||
1196 | * Set title for form. |
||
1197 | * |
||
1198 | * @param string $title |
||
1199 | * |
||
1200 | * @return $this |
||
1201 | */ |
||
1202 | public function setTitle($title = '') |
||
1208 | |||
1209 | /** |
||
1210 | * Add a row in form. |
||
1211 | * |
||
1212 | * @param Closure $callback |
||
1213 | * |
||
1214 | * @return $this |
||
1215 | */ |
||
1216 | public function row(Closure $callback) |
||
1222 | |||
1223 | /** |
||
1224 | * Tools setting for form. |
||
1225 | * |
||
1226 | * @param Closure $callback |
||
1227 | */ |
||
1228 | public function tools(Closure $callback) |
||
1232 | |||
1233 | /** |
||
1234 | * Disable form submit. |
||
1235 | * |
||
1236 | * @return $this |
||
1237 | * |
||
1238 | * @deprecated |
||
1239 | */ |
||
1240 | public function disableSubmit() |
||
1246 | |||
1247 | /** |
||
1248 | * Disable form reset. |
||
1249 | * |
||
1250 | * @return $this |
||
1251 | * |
||
1252 | * @deprecated |
||
1253 | */ |
||
1254 | public function disableReset() |
||
1260 | |||
1261 | /** |
||
1262 | * Disable View Checkbox on footer. |
||
1263 | * |
||
1264 | * @return $this |
||
1265 | */ |
||
1266 | public function disableViewCheck() |
||
1272 | |||
1273 | /** |
||
1274 | * Disable Editing Checkbox on footer. |
||
1275 | * |
||
1276 | * @return $this |
||
1277 | */ |
||
1278 | public function disableEditingCheck() |
||
1284 | |||
1285 | /** |
||
1286 | * Footer setting for form. |
||
1287 | * |
||
1288 | * @param Closure $callback |
||
1289 | */ |
||
1290 | public function footer(Closure $callback) |
||
1294 | |||
1295 | /** |
||
1296 | * Get current resource route url. |
||
1297 | * |
||
1298 | * @param int $slice |
||
1299 | * |
||
1300 | * @return string |
||
1301 | */ |
||
1302 | public function resource($slice = -2) |
||
1316 | |||
1317 | /** |
||
1318 | * Render the form contents. |
||
1319 | * |
||
1320 | * @return string |
||
1321 | */ |
||
1322 | public function render() |
||
1330 | |||
1331 | /** |
||
1332 | * Get or set input data. |
||
1333 | * |
||
1334 | * @param string $key |
||
1335 | * @param null $value |
||
1336 | * |
||
1337 | * @return array|mixed |
||
1338 | */ |
||
1339 | public function input($key, $value = null) |
||
1347 | |||
1348 | /** |
||
1349 | * Register builtin fields. |
||
1350 | * |
||
1351 | * @return void |
||
1352 | */ |
||
1353 | public static function registerBuiltinFields() |
||
1408 | |||
1409 | /** |
||
1410 | * Register custom field. |
||
1411 | * |
||
1412 | * @param string $abstract |
||
1413 | * @param string $class |
||
1414 | * |
||
1415 | * @return void |
||
1416 | */ |
||
1417 | public static function extend($abstract, $class) |
||
1421 | |||
1422 | /** |
||
1423 | * Set form field alias. |
||
1424 | * |
||
1425 | * @param string $field |
||
1426 | * @param string $alias |
||
1427 | * |
||
1428 | * @return void |
||
1429 | */ |
||
1430 | public static function alias($field, $alias) |
||
1434 | |||
1435 | /** |
||
1436 | * Remove registered field. |
||
1437 | * |
||
1438 | * @param array|string $abstract |
||
1439 | */ |
||
1440 | public static function forget($abstract) |
||
1444 | |||
1445 | /** |
||
1446 | * Find field class. |
||
1447 | * |
||
1448 | * @param string $method |
||
1449 | * |
||
1450 | * @return bool|mixed |
||
1451 | */ |
||
1452 | public static function findFieldClass($method) |
||
1467 | |||
1468 | /** |
||
1469 | * Collect assets required by registered field. |
||
1470 | * |
||
1471 | * @return array |
||
1472 | */ |
||
1473 | public static function collectFieldAssets() |
||
1498 | |||
1499 | /** |
||
1500 | * Getter. |
||
1501 | * |
||
1502 | * @param string $name |
||
1503 | * |
||
1504 | * @return array|mixed |
||
1505 | */ |
||
1506 | public function __get($name) |
||
1510 | |||
1511 | /** |
||
1512 | * Setter. |
||
1513 | * |
||
1514 | * @param string $name |
||
1515 | * @param $value |
||
1516 | */ |
||
1517 | public function __set($name, $value) |
||
1521 | |||
1522 | /** |
||
1523 | * Generate a Field object and add to form builder if Field exists. |
||
1524 | * |
||
1525 | * @param string $method |
||
1526 | * @param array $arguments |
||
1527 | * |
||
1528 | * @return Field |
||
1529 | */ |
||
1530 | public function __call($method, $arguments) |
||
1546 | } |
||
1547 |
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: