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() |
||
442 | |||
443 | /** |
||
444 | * Call saving callback. |
||
445 | * |
||
446 | * @return mixed |
||
447 | */ |
||
448 | protected function callSaving() |
||
456 | |||
457 | /** |
||
458 | * Callback after saving a Model. |
||
459 | * |
||
460 | * @return mixed|null |
||
461 | */ |
||
462 | protected function callSaved() |
||
470 | |||
471 | /** |
||
472 | * Handle update. |
||
473 | * |
||
474 | * @param int $id |
||
475 | * |
||
476 | * @return \Symfony\Component\HttpFoundation\Response |
||
477 | */ |
||
478 | public function update($id, $data = null) |
||
536 | |||
537 | /** |
||
538 | * Get RedirectResponse after store. |
||
539 | * |
||
540 | * @return \Illuminate\Http\RedirectResponse |
||
541 | */ |
||
542 | protected function redirectAfterStore() |
||
550 | |||
551 | /** |
||
552 | * Get RedirectResponse after update. |
||
553 | * |
||
554 | * @param mixed $key |
||
555 | * |
||
556 | * @return \Illuminate\Http\RedirectResponse |
||
557 | */ |
||
558 | protected function redirectAfterUpdate($key) |
||
564 | |||
565 | /** |
||
566 | * Get RedirectResponse after data saving. |
||
567 | * |
||
568 | * @param string $resourcesPath |
||
569 | * @param string $key |
||
570 | * |
||
571 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
572 | */ |
||
573 | protected function redirectAfterSaving($resourcesPath, $key) |
||
589 | |||
590 | /** |
||
591 | * Check if request is from editable. |
||
592 | * |
||
593 | * @param array $input |
||
594 | * |
||
595 | * @return bool |
||
596 | */ |
||
597 | protected function isEditable(array $input = []) |
||
601 | |||
602 | /** |
||
603 | * Handle editable update. |
||
604 | * |
||
605 | * @param array $input |
||
606 | * |
||
607 | * @return array |
||
608 | */ |
||
609 | protected function handleEditable(array $input = []) |
||
621 | |||
622 | /** |
||
623 | * @param array $input |
||
624 | * |
||
625 | * @return array |
||
626 | */ |
||
627 | protected function handleFileDelete(array $input = []) |
||
638 | |||
639 | /** |
||
640 | * Handle orderable update. |
||
641 | * |
||
642 | * @param int $id |
||
643 | * @param array $input |
||
644 | * |
||
645 | * @return bool |
||
646 | */ |
||
647 | protected function handleOrderable($id, array $input = []) |
||
661 | |||
662 | /** |
||
663 | * Update relation data. |
||
664 | * |
||
665 | * @param array $relationsData |
||
666 | * |
||
667 | * @return void |
||
668 | */ |
||
669 | protected function updateRelation($relationsData) |
||
750 | |||
751 | /** |
||
752 | * Prepare input data for update. |
||
753 | * |
||
754 | * @param array $updates |
||
755 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
756 | * |
||
757 | * @return array |
||
758 | */ |
||
759 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
791 | |||
792 | /** |
||
793 | * @param string|array $columns |
||
794 | * @param bool $oneToOneRelation |
||
795 | * |
||
796 | * @return bool |
||
797 | */ |
||
798 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
809 | |||
810 | /** |
||
811 | * Prepare input data for insert. |
||
812 | * |
||
813 | * @param $inserts |
||
814 | * |
||
815 | * @return array |
||
816 | */ |
||
817 | protected function prepareInsert($inserts) |
||
840 | |||
841 | /** |
||
842 | * Is input data is has-one relation. |
||
843 | * |
||
844 | * @param array $inserts |
||
845 | * |
||
846 | * @return bool |
||
847 | */ |
||
848 | protected function isHasOneRelation($inserts) |
||
862 | |||
863 | /** |
||
864 | * Set submitted callback. |
||
865 | * |
||
866 | * @param Closure $callback |
||
867 | * |
||
868 | * @return void |
||
869 | */ |
||
870 | public function submitted(Closure $callback) |
||
874 | |||
875 | /** |
||
876 | * Set saving callback. |
||
877 | * |
||
878 | * @param Closure $callback |
||
879 | * |
||
880 | * @return void |
||
881 | */ |
||
882 | public function saving(Closure $callback) |
||
886 | |||
887 | /** |
||
888 | * Set saved callback. |
||
889 | * |
||
890 | * @param Closure $callback |
||
891 | * |
||
892 | * @return void |
||
893 | */ |
||
894 | public function saved(Closure $callback) |
||
898 | |||
899 | /** |
||
900 | * Ignore fields to save. |
||
901 | * |
||
902 | * @param string|array $fields |
||
903 | * |
||
904 | * @return $this |
||
905 | */ |
||
906 | public function ignore($fields) |
||
912 | |||
913 | /** |
||
914 | * @param array $data |
||
915 | * @param string|array $columns |
||
916 | * |
||
917 | * @return array|mixed |
||
918 | */ |
||
919 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
937 | |||
938 | /** |
||
939 | * Find field object by column. |
||
940 | * |
||
941 | * @param $column |
||
942 | * |
||
943 | * @return mixed |
||
944 | */ |
||
945 | protected function getFieldByColumn($column) |
||
957 | |||
958 | /** |
||
959 | * Set original data for each field. |
||
960 | * |
||
961 | * @return void |
||
962 | */ |
||
963 | protected function setFieldOriginalValue() |
||
973 | |||
974 | /** |
||
975 | * Set all fields value in form. |
||
976 | * |
||
977 | * @param $id |
||
978 | * |
||
979 | * @return void |
||
980 | */ |
||
981 | protected function setFieldValue($id) |
||
997 | |||
998 | /** |
||
999 | * Don't snake case attributes. |
||
1000 | * |
||
1001 | * @param Model $model |
||
1002 | * |
||
1003 | * @return void |
||
1004 | */ |
||
1005 | protected static function doNotSnakeAttributes(Model $model) |
||
1011 | |||
1012 | /** |
||
1013 | * Get validation messages. |
||
1014 | * |
||
1015 | * @param array $input |
||
1016 | * |
||
1017 | * @return MessageBag|bool |
||
1018 | */ |
||
1019 | protected function validationMessages($input) |
||
1038 | |||
1039 | /** |
||
1040 | * Merge validation messages from input validators. |
||
1041 | * |
||
1042 | * @param \Illuminate\Validation\Validator[] $validators |
||
1043 | * |
||
1044 | * @return MessageBag |
||
1045 | */ |
||
1046 | protected function mergeValidationMessages($validators) |
||
1056 | |||
1057 | /** |
||
1058 | * Get all relations of model from callable. |
||
1059 | * |
||
1060 | * @return array |
||
1061 | */ |
||
1062 | public function getRelations() |
||
1089 | |||
1090 | /** |
||
1091 | * Set action for form. |
||
1092 | * |
||
1093 | * @param string $action |
||
1094 | * |
||
1095 | * @return $this |
||
1096 | */ |
||
1097 | public function setAction($action) |
||
1103 | |||
1104 | /** |
||
1105 | * Set field and label width in current form. |
||
1106 | * |
||
1107 | * @param int $fieldWidth |
||
1108 | * @param int $labelWidth |
||
1109 | * |
||
1110 | * @return $this |
||
1111 | */ |
||
1112 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1123 | |||
1124 | /** |
||
1125 | * Set view for form. |
||
1126 | * |
||
1127 | * @param string $view |
||
1128 | * |
||
1129 | * @return $this |
||
1130 | */ |
||
1131 | public function setView($view) |
||
1137 | |||
1138 | /** |
||
1139 | * Set title for form. |
||
1140 | * |
||
1141 | * @param string $title |
||
1142 | * |
||
1143 | * @return $this |
||
1144 | */ |
||
1145 | public function setTitle($title = '') |
||
1151 | |||
1152 | /** |
||
1153 | * Add a row in form. |
||
1154 | * |
||
1155 | * @param Closure $callback |
||
1156 | * |
||
1157 | * @return $this |
||
1158 | */ |
||
1159 | public function row(Closure $callback) |
||
1165 | |||
1166 | /** |
||
1167 | * Tools setting for form. |
||
1168 | * |
||
1169 | * @param Closure $callback |
||
1170 | */ |
||
1171 | public function tools(Closure $callback) |
||
1175 | |||
1176 | /** |
||
1177 | * Disable form submit. |
||
1178 | * |
||
1179 | * @return $this |
||
1180 | * |
||
1181 | * @deprecated |
||
1182 | */ |
||
1183 | public function disableSubmit() |
||
1189 | |||
1190 | /** |
||
1191 | * Disable form reset. |
||
1192 | * |
||
1193 | * @return $this |
||
1194 | * |
||
1195 | * @deprecated |
||
1196 | */ |
||
1197 | public function disableReset() |
||
1203 | |||
1204 | /** |
||
1205 | * Footer setting for form. |
||
1206 | * |
||
1207 | * @param Closure $callback |
||
1208 | */ |
||
1209 | public function footer(Closure $callback) |
||
1213 | |||
1214 | /** |
||
1215 | * Get current resource route url. |
||
1216 | * |
||
1217 | * @param int $slice |
||
1218 | * |
||
1219 | * @return string |
||
1220 | */ |
||
1221 | public function resource($slice = -2) |
||
1235 | |||
1236 | /** |
||
1237 | * Render the form contents. |
||
1238 | * |
||
1239 | * @return string |
||
1240 | */ |
||
1241 | public function render() |
||
1249 | |||
1250 | /** |
||
1251 | * Get or set input data. |
||
1252 | * |
||
1253 | * @param string $key |
||
1254 | * @param null $value |
||
1255 | * |
||
1256 | * @return array|mixed |
||
1257 | */ |
||
1258 | public function input($key, $value = null) |
||
1266 | |||
1267 | /** |
||
1268 | * Register builtin fields. |
||
1269 | * |
||
1270 | * @return void |
||
1271 | */ |
||
1272 | public static function registerBuiltinFields() |
||
1327 | |||
1328 | /** |
||
1329 | * Register custom field. |
||
1330 | * |
||
1331 | * @param string $abstract |
||
1332 | * @param string $class |
||
1333 | * |
||
1334 | * @return void |
||
1335 | */ |
||
1336 | public static function extend($abstract, $class) |
||
1340 | |||
1341 | /** |
||
1342 | * Remove registered field. |
||
1343 | * |
||
1344 | * @param array|string $abstract |
||
1345 | */ |
||
1346 | public static function forget($abstract) |
||
1350 | |||
1351 | /** |
||
1352 | * Find field class. |
||
1353 | * |
||
1354 | * @param string $method |
||
1355 | * |
||
1356 | * @return bool|mixed |
||
1357 | */ |
||
1358 | View Code Duplication | public static function findFieldClass($method) |
|
1368 | |||
1369 | /** |
||
1370 | * Collect assets required by registered field. |
||
1371 | * |
||
1372 | * @return array |
||
1373 | */ |
||
1374 | public static function collectFieldAssets() |
||
1399 | |||
1400 | /** |
||
1401 | * Getter. |
||
1402 | * |
||
1403 | * @param string $name |
||
1404 | * |
||
1405 | * @return array|mixed |
||
1406 | */ |
||
1407 | public function __get($name) |
||
1411 | |||
1412 | /** |
||
1413 | * Setter. |
||
1414 | * |
||
1415 | * @param string $name |
||
1416 | * @param $value |
||
1417 | */ |
||
1418 | public function __set($name, $value) |
||
1422 | |||
1423 | /** |
||
1424 | * Generate a Field object and add to form builder if Field exists. |
||
1425 | * |
||
1426 | * @param string $method |
||
1427 | * @param array $arguments |
||
1428 | * |
||
1429 | * @return Field|void |
||
1430 | */ |
||
1431 | public function __call($method, $arguments) |
||
1443 | } |
||
1444 |
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: