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) |
||
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 editing callbacks. |
||
450 | * |
||
451 | * @return void |
||
452 | */ |
||
453 | protected function callEditing() |
||
459 | |||
460 | /** |
||
461 | * Call submitted callback. |
||
462 | * |
||
463 | * @return mixed |
||
464 | */ |
||
465 | protected function callSubmitted() |
||
473 | |||
474 | /** |
||
475 | * Call saving callback. |
||
476 | * |
||
477 | * @return mixed |
||
478 | */ |
||
479 | protected function callSaving() |
||
487 | |||
488 | /** |
||
489 | * Callback after saving a Model. |
||
490 | * |
||
491 | * @return mixed|null |
||
492 | */ |
||
493 | protected function callSaved() |
||
501 | |||
502 | /** |
||
503 | * Handle update. |
||
504 | * |
||
505 | * @param int $id |
||
506 | * |
||
507 | * @return \Symfony\Component\HttpFoundation\Response |
||
508 | */ |
||
509 | public function update($id, $data = null) |
||
567 | |||
568 | /** |
||
569 | * Get RedirectResponse after store. |
||
570 | * |
||
571 | * @return \Illuminate\Http\RedirectResponse |
||
572 | */ |
||
573 | protected function redirectAfterStore() |
||
581 | |||
582 | /** |
||
583 | * Get RedirectResponse after update. |
||
584 | * |
||
585 | * @param mixed $key |
||
586 | * |
||
587 | * @return \Illuminate\Http\RedirectResponse |
||
588 | */ |
||
589 | protected function redirectAfterUpdate($key) |
||
595 | |||
596 | /** |
||
597 | * Get RedirectResponse after data saving. |
||
598 | * |
||
599 | * @param string $resourcesPath |
||
600 | * @param string $key |
||
601 | * |
||
602 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
603 | */ |
||
604 | protected function redirectAfterSaving($resourcesPath, $key) |
||
620 | |||
621 | /** |
||
622 | * Check if request is from editable. |
||
623 | * |
||
624 | * @param array $input |
||
625 | * |
||
626 | * @return bool |
||
627 | */ |
||
628 | protected function isEditable(array $input = []) |
||
632 | |||
633 | /** |
||
634 | * Handle editable update. |
||
635 | * |
||
636 | * @param array $input |
||
637 | * |
||
638 | * @return array |
||
639 | */ |
||
640 | protected function handleEditable(array $input = []) |
||
652 | |||
653 | /** |
||
654 | * @param array $input |
||
655 | * |
||
656 | * @return array |
||
657 | */ |
||
658 | protected function handleFileDelete(array $input = []) |
||
669 | |||
670 | /** |
||
671 | * Handle orderable update. |
||
672 | * |
||
673 | * @param int $id |
||
674 | * @param array $input |
||
675 | * |
||
676 | * @return bool |
||
677 | */ |
||
678 | protected function handleOrderable($id, array $input = []) |
||
692 | |||
693 | /** |
||
694 | * Update relation data. |
||
695 | * |
||
696 | * @param array $relationsData |
||
697 | * |
||
698 | * @return void |
||
699 | */ |
||
700 | protected function updateRelation($relationsData) |
||
803 | |||
804 | /** |
||
805 | * Prepare input data for update. |
||
806 | * |
||
807 | * @param array $updates |
||
808 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
809 | * |
||
810 | * @return array |
||
811 | */ |
||
812 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
844 | |||
845 | /** |
||
846 | * @param string|array $columns |
||
847 | * @param bool $oneToOneRelation |
||
848 | * |
||
849 | * @return bool |
||
850 | */ |
||
851 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
862 | |||
863 | /** |
||
864 | * Prepare input data for insert. |
||
865 | * |
||
866 | * @param $inserts |
||
867 | * |
||
868 | * @return array |
||
869 | */ |
||
870 | protected function prepareInsert($inserts) |
||
893 | |||
894 | /** |
||
895 | * Is input data is has-one relation. |
||
896 | * |
||
897 | * @param array $inserts |
||
898 | * |
||
899 | * @return bool |
||
900 | */ |
||
901 | protected function isHasOneRelation($inserts) |
||
915 | |||
916 | /** |
||
917 | * Set after getting editing model callback. |
||
918 | * |
||
919 | * @param Closure $callback |
||
920 | * |
||
921 | * @return void |
||
922 | */ |
||
923 | public function editing(Closure $callback) |
||
927 | |||
928 | /** |
||
929 | * Set submitted callback. |
||
930 | * |
||
931 | * @param Closure $callback |
||
932 | * |
||
933 | * @return void |
||
934 | */ |
||
935 | public function submitted(Closure $callback) |
||
939 | |||
940 | /** |
||
941 | * Set saving callback. |
||
942 | * |
||
943 | * @param Closure $callback |
||
944 | * |
||
945 | * @return void |
||
946 | */ |
||
947 | public function saving(Closure $callback) |
||
951 | |||
952 | /** |
||
953 | * Set saved callback. |
||
954 | * |
||
955 | * @param Closure $callback |
||
956 | * |
||
957 | * @return void |
||
958 | */ |
||
959 | public function saved(Closure $callback) |
||
963 | |||
964 | /** |
||
965 | * Ignore fields to save. |
||
966 | * |
||
967 | * @param string|array $fields |
||
968 | * |
||
969 | * @return $this |
||
970 | */ |
||
971 | public function ignore($fields) |
||
977 | |||
978 | /** |
||
979 | * @param array $data |
||
980 | * @param string|array $columns |
||
981 | * |
||
982 | * @return array|mixed |
||
983 | */ |
||
984 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
1002 | |||
1003 | /** |
||
1004 | * Find field object by column. |
||
1005 | * |
||
1006 | * @param $column |
||
1007 | * |
||
1008 | * @return mixed |
||
1009 | */ |
||
1010 | protected function getFieldByColumn($column) |
||
1022 | |||
1023 | /** |
||
1024 | * Set original data for each field. |
||
1025 | * |
||
1026 | * @return void |
||
1027 | */ |
||
1028 | protected function setFieldOriginalValue() |
||
1038 | |||
1039 | /** |
||
1040 | * Set all fields value in form. |
||
1041 | * |
||
1042 | * @param $id |
||
1043 | * |
||
1044 | * @return void |
||
1045 | */ |
||
1046 | protected function setFieldValue($id) |
||
1064 | |||
1065 | /** |
||
1066 | * Don't snake case attributes. |
||
1067 | * |
||
1068 | * @param Model $model |
||
1069 | * |
||
1070 | * @return void |
||
1071 | */ |
||
1072 | protected static function doNotSnakeAttributes(Model $model) |
||
1078 | |||
1079 | /** |
||
1080 | * Get validation messages. |
||
1081 | * |
||
1082 | * @param array $input |
||
1083 | * |
||
1084 | * @return MessageBag|bool |
||
1085 | */ |
||
1086 | public function validationMessages($input) |
||
1105 | |||
1106 | /** |
||
1107 | * Merge validation messages from input validators. |
||
1108 | * |
||
1109 | * @param \Illuminate\Validation\Validator[] $validators |
||
1110 | * |
||
1111 | * @return MessageBag |
||
1112 | */ |
||
1113 | protected function mergeValidationMessages($validators) |
||
1123 | |||
1124 | /** |
||
1125 | * Get all relations of model from callable. |
||
1126 | * |
||
1127 | * @return array |
||
1128 | */ |
||
1129 | public function getRelations() |
||
1156 | |||
1157 | /** |
||
1158 | * Set action for form. |
||
1159 | * |
||
1160 | * @param string $action |
||
1161 | * |
||
1162 | * @return $this |
||
1163 | */ |
||
1164 | public function setAction($action) |
||
1170 | |||
1171 | /** |
||
1172 | * Set field and label width in current form. |
||
1173 | * |
||
1174 | * @param int $fieldWidth |
||
1175 | * @param int $labelWidth |
||
1176 | * |
||
1177 | * @return $this |
||
1178 | */ |
||
1179 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1190 | |||
1191 | /** |
||
1192 | * Set view for form. |
||
1193 | * |
||
1194 | * @param string $view |
||
1195 | * |
||
1196 | * @return $this |
||
1197 | */ |
||
1198 | public function setView($view) |
||
1204 | |||
1205 | /** |
||
1206 | * Set title for form. |
||
1207 | * |
||
1208 | * @param string $title |
||
1209 | * |
||
1210 | * @return $this |
||
1211 | */ |
||
1212 | public function setTitle($title = '') |
||
1218 | |||
1219 | /** |
||
1220 | * Add a row in form. |
||
1221 | * |
||
1222 | * @param Closure $callback |
||
1223 | * |
||
1224 | * @return $this |
||
1225 | */ |
||
1226 | public function row(Closure $callback) |
||
1232 | |||
1233 | /** |
||
1234 | * Tools setting for form. |
||
1235 | * |
||
1236 | * @param Closure $callback |
||
1237 | */ |
||
1238 | public function tools(Closure $callback) |
||
1242 | |||
1243 | /** |
||
1244 | * Disable form submit. |
||
1245 | * |
||
1246 | * @return $this |
||
1247 | * |
||
1248 | * @deprecated |
||
1249 | */ |
||
1250 | public function disableSubmit() |
||
1256 | |||
1257 | /** |
||
1258 | * Disable form reset. |
||
1259 | * |
||
1260 | * @return $this |
||
1261 | * |
||
1262 | * @deprecated |
||
1263 | */ |
||
1264 | public function disableReset() |
||
1270 | |||
1271 | /** |
||
1272 | * Disable View Checkbox on footer. |
||
1273 | * |
||
1274 | * @return $this |
||
1275 | */ |
||
1276 | public function disableViewCheck() |
||
1282 | |||
1283 | /** |
||
1284 | * Disable Editing Checkbox on footer. |
||
1285 | * |
||
1286 | * @return $this |
||
1287 | */ |
||
1288 | public function disableEditingCheck() |
||
1294 | |||
1295 | /** |
||
1296 | * Footer setting for form. |
||
1297 | * |
||
1298 | * @param Closure $callback |
||
1299 | */ |
||
1300 | public function footer(Closure $callback) |
||
1304 | |||
1305 | /** |
||
1306 | * Get current resource route url. |
||
1307 | * |
||
1308 | * @param int $slice |
||
1309 | * |
||
1310 | * @return string |
||
1311 | */ |
||
1312 | public function resource($slice = -2) |
||
1326 | |||
1327 | /** |
||
1328 | * Render the form contents. |
||
1329 | * |
||
1330 | * @return string |
||
1331 | */ |
||
1332 | public function render() |
||
1340 | |||
1341 | /** |
||
1342 | * Get or set input data. |
||
1343 | * |
||
1344 | * @param string $key |
||
1345 | * @param null $value |
||
1346 | * |
||
1347 | * @return array|mixed |
||
1348 | */ |
||
1349 | public function input($key, $value = null) |
||
1357 | |||
1358 | /** |
||
1359 | * Register builtin fields. |
||
1360 | * |
||
1361 | * @return void |
||
1362 | */ |
||
1363 | public static function registerBuiltinFields() |
||
1418 | |||
1419 | /** |
||
1420 | * Register custom field. |
||
1421 | * |
||
1422 | * @param string $abstract |
||
1423 | * @param string $class |
||
1424 | * |
||
1425 | * @return void |
||
1426 | */ |
||
1427 | public static function extend($abstract, $class) |
||
1431 | |||
1432 | /** |
||
1433 | * Set form field alias. |
||
1434 | * |
||
1435 | * @param string $field |
||
1436 | * @param string $alias |
||
1437 | * |
||
1438 | * @return void |
||
1439 | */ |
||
1440 | public static function alias($field, $alias) |
||
1444 | |||
1445 | /** |
||
1446 | * Remove registered field. |
||
1447 | * |
||
1448 | * @param array|string $abstract |
||
1449 | */ |
||
1450 | public static function forget($abstract) |
||
1454 | |||
1455 | /** |
||
1456 | * Find field class. |
||
1457 | * |
||
1458 | * @param string $method |
||
1459 | * |
||
1460 | * @return bool|mixed |
||
1461 | */ |
||
1462 | public static function findFieldClass($method) |
||
1477 | |||
1478 | /** |
||
1479 | * Collect assets required by registered field. |
||
1480 | * |
||
1481 | * @return array |
||
1482 | */ |
||
1483 | public static function collectFieldAssets() |
||
1508 | |||
1509 | /** |
||
1510 | * Getter. |
||
1511 | * |
||
1512 | * @param string $name |
||
1513 | * |
||
1514 | * @return array|mixed |
||
1515 | */ |
||
1516 | public function __get($name) |
||
1520 | |||
1521 | /** |
||
1522 | * Setter. |
||
1523 | * |
||
1524 | * @param string $name |
||
1525 | * @param $value |
||
1526 | */ |
||
1527 | public function __set($name, $value) |
||
1531 | |||
1532 | /** |
||
1533 | * Generate a Field object and add to form builder if Field exists. |
||
1534 | * |
||
1535 | * @param string $method |
||
1536 | * @param array $arguments |
||
1537 | * |
||
1538 | * @return Field |
||
1539 | */ |
||
1540 | public function __call($method, $arguments) |
||
1556 | } |
||
1557 |
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: