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 |
||
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) |
||
184 | |||
185 | /** |
||
186 | * @param Field $field |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function pushField(Field $field) |
||
198 | |||
199 | /** |
||
200 | * @return Model |
||
201 | */ |
||
202 | public function model() |
||
206 | |||
207 | /** |
||
208 | * @return Builder |
||
209 | */ |
||
210 | public function builder() |
||
214 | |||
215 | /** |
||
216 | * Generate a edit form. |
||
217 | * |
||
218 | * @param $id |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function edit($id) |
||
231 | |||
232 | /** |
||
233 | * @param $id |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function view($id) |
||
246 | |||
247 | /** |
||
248 | * Use tab to split form. |
||
249 | * |
||
250 | * @param string $title |
||
251 | * @param Closure $content |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function tab($title, Closure $content, $active = false) |
||
261 | |||
262 | /** |
||
263 | * Get Tab instance. |
||
264 | * |
||
265 | * @return Tab |
||
266 | */ |
||
267 | public function getTab() |
||
275 | |||
276 | /** |
||
277 | * Destroy data entity and remove files. |
||
278 | * |
||
279 | * @param $id |
||
280 | * |
||
281 | * @return mixed |
||
282 | */ |
||
283 | public function destroy($id) |
||
297 | |||
298 | /** |
||
299 | * Remove files or images in record. |
||
300 | * |
||
301 | * @param $id |
||
302 | */ |
||
303 | protected function deleteFilesAndImages($id) |
||
316 | |||
317 | /** |
||
318 | * Store a new record. |
||
319 | * |
||
320 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
321 | */ |
||
322 | public function store() |
||
357 | |||
358 | /** |
||
359 | * Get RedirectResponse after store. |
||
360 | * |
||
361 | * @return \Illuminate\Http\RedirectResponse |
||
362 | */ |
||
363 | View Code Duplication | protected function redirectAfterStore() |
|
371 | |||
372 | /** |
||
373 | * Get ajax response. |
||
374 | * |
||
375 | * @param string $message |
||
376 | * |
||
377 | * @return bool|\Illuminate\Http\JsonResponse |
||
378 | */ |
||
379 | protected function ajaxResponse($message) |
||
393 | |||
394 | /** |
||
395 | * Prepare input data for insert or update. |
||
396 | * |
||
397 | * @param array $data |
||
398 | * |
||
399 | * @return mixed |
||
400 | */ |
||
401 | protected function prepare($data = []) |
||
417 | |||
418 | /** |
||
419 | * Remove ignored fields from input. |
||
420 | * |
||
421 | * @param array $input |
||
422 | * |
||
423 | * @return array |
||
424 | */ |
||
425 | protected function removeIgnoredFields($input) |
||
431 | |||
432 | /** |
||
433 | * Get inputs for relations. |
||
434 | * |
||
435 | * @param array $inputs |
||
436 | * |
||
437 | * @return array |
||
438 | */ |
||
439 | protected function getRelationInputs($inputs = []) |
||
455 | |||
456 | /** |
||
457 | * Call submitted callback. |
||
458 | * |
||
459 | * @return mixed |
||
460 | */ |
||
461 | protected function callSubmitted() |
||
467 | |||
468 | /** |
||
469 | * Call saving callback. |
||
470 | * |
||
471 | * @return mixed |
||
472 | */ |
||
473 | protected function callSaving() |
||
479 | |||
480 | /** |
||
481 | * Callback after saving a Model. |
||
482 | * |
||
483 | * @param Closure|null $callback |
||
484 | * |
||
485 | * @return mixed|null |
||
486 | */ |
||
487 | protected function complete(Closure $callback = null) |
||
493 | |||
494 | /** |
||
495 | * Handle update. |
||
496 | * |
||
497 | * @param int $id |
||
498 | * |
||
499 | * @return \Symfony\Component\HttpFoundation\Response |
||
500 | */ |
||
501 | public function update($id, $data = null) |
||
559 | |||
560 | /** |
||
561 | * Get RedirectResponse after update. |
||
562 | * |
||
563 | * @return \Illuminate\Http\RedirectResponse |
||
564 | */ |
||
565 | View Code Duplication | protected function redirectAfterUpdate() |
|
573 | |||
574 | /** |
||
575 | * Check if request is from editable. |
||
576 | * |
||
577 | * @param array $input |
||
578 | * |
||
579 | * @return bool |
||
580 | */ |
||
581 | protected function isEditable(array $input = []) |
||
585 | |||
586 | /** |
||
587 | * Handle editable update. |
||
588 | * |
||
589 | * @param array $input |
||
590 | * |
||
591 | * @return array |
||
592 | */ |
||
593 | protected function handleEditable(array $input = []) |
||
605 | |||
606 | /** |
||
607 | * @param array $input |
||
608 | * |
||
609 | * @return array |
||
610 | */ |
||
611 | protected function handleFileDelete(array $input = []) |
||
622 | |||
623 | /** |
||
624 | * Handle orderable update. |
||
625 | * |
||
626 | * @param int $id |
||
627 | * @param array $input |
||
628 | * |
||
629 | * @return bool |
||
630 | */ |
||
631 | protected function handleOrderable($id, array $input = []) |
||
645 | |||
646 | /** |
||
647 | * Update relation data. |
||
648 | * |
||
649 | * @param array $relationsData |
||
650 | * |
||
651 | * @return void |
||
652 | */ |
||
653 | protected function updateRelation($relationsData) |
||
731 | |||
732 | /** |
||
733 | * Prepare input data for update. |
||
734 | * |
||
735 | * @param array $updates |
||
736 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
737 | * |
||
738 | * @return array |
||
739 | */ |
||
740 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
771 | |||
772 | /** |
||
773 | * @param string|array $columns |
||
774 | * @param bool $oneToOneRelation |
||
775 | * |
||
776 | * @return bool |
||
777 | */ |
||
778 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
789 | |||
790 | /** |
||
791 | * Prepare input data for insert. |
||
792 | * |
||
793 | * @param $inserts |
||
794 | * |
||
795 | * @return array |
||
796 | */ |
||
797 | protected function prepareInsert($inserts) |
||
820 | |||
821 | /** |
||
822 | * Is input data is has-one relation. |
||
823 | * |
||
824 | * @param array $inserts |
||
825 | * |
||
826 | * @return bool |
||
827 | */ |
||
828 | protected function isHasOneRelation($inserts) |
||
842 | |||
843 | /** |
||
844 | * Set submitted callback. |
||
845 | * |
||
846 | * @param Closure $callback |
||
847 | * |
||
848 | * @return void |
||
849 | */ |
||
850 | public function submitted(Closure $callback) |
||
854 | |||
855 | /** |
||
856 | * Set saving callback. |
||
857 | * |
||
858 | * @param Closure $callback |
||
859 | * |
||
860 | * @return void |
||
861 | */ |
||
862 | public function saving(Closure $callback) |
||
866 | |||
867 | /** |
||
868 | * Set saved callback. |
||
869 | * |
||
870 | * @param callable $callback |
||
871 | * |
||
872 | * @return void |
||
873 | */ |
||
874 | public function saved(Closure $callback) |
||
878 | |||
879 | /** |
||
880 | * Ignore fields to save. |
||
881 | * |
||
882 | * @param string|array $fields |
||
883 | * |
||
884 | * @return $this |
||
885 | */ |
||
886 | public function ignore($fields) |
||
892 | |||
893 | /** |
||
894 | * @param array $data |
||
895 | * @param string|array $columns |
||
896 | * |
||
897 | * @return array|mixed |
||
898 | */ |
||
899 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
917 | |||
918 | /** |
||
919 | * Find field object by column. |
||
920 | * |
||
921 | * @param $column |
||
922 | * |
||
923 | * @return mixed |
||
924 | */ |
||
925 | protected function getFieldByColumn($column) |
||
937 | |||
938 | /** |
||
939 | * Set original data for each field. |
||
940 | * |
||
941 | * @return void |
||
942 | */ |
||
943 | protected function setFieldOriginalValue() |
||
953 | |||
954 | /** |
||
955 | * Set all fields value in form. |
||
956 | * |
||
957 | * @param $id |
||
958 | * |
||
959 | * @return void |
||
960 | */ |
||
961 | protected function setFieldValue($id) |
||
977 | |||
978 | /** |
||
979 | * Don't snake case attributes. |
||
980 | * |
||
981 | * @param Model $model |
||
982 | * |
||
983 | * @return void |
||
984 | */ |
||
985 | protected static function doNotSnakeAttributes(Model $model) |
||
991 | |||
992 | /** |
||
993 | * Get validation messages. |
||
994 | * |
||
995 | * @param array $input |
||
996 | * |
||
997 | * @return MessageBag|bool |
||
998 | */ |
||
999 | protected function validationMessages($input) |
||
1017 | |||
1018 | /** |
||
1019 | * Merge validation messages from input validators. |
||
1020 | * |
||
1021 | * @param \Illuminate\Validation\Validator[] $validators |
||
1022 | * |
||
1023 | * @return MessageBag |
||
1024 | */ |
||
1025 | protected function mergeValidationMessages($validators) |
||
1035 | |||
1036 | /** |
||
1037 | * Get all relations of model from callable. |
||
1038 | * |
||
1039 | * @return array |
||
1040 | */ |
||
1041 | public function getRelations() |
||
1067 | |||
1068 | /** |
||
1069 | * Set action for form. |
||
1070 | * |
||
1071 | * @param string $action |
||
1072 | * |
||
1073 | * @return $this |
||
1074 | */ |
||
1075 | public function setAction($action) |
||
1081 | |||
1082 | /** |
||
1083 | * Set field and label width in current form. |
||
1084 | * |
||
1085 | * @param int $fieldWidth |
||
1086 | * @param int $labelWidth |
||
1087 | * |
||
1088 | * @return $this |
||
1089 | */ |
||
1090 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1101 | |||
1102 | /** |
||
1103 | * Set view for form. |
||
1104 | * |
||
1105 | * @param string $view |
||
1106 | * |
||
1107 | * @return $this |
||
1108 | */ |
||
1109 | public function setView($view) |
||
1115 | |||
1116 | /** |
||
1117 | * Set title for form. |
||
1118 | * |
||
1119 | * @param string $title |
||
1120 | * |
||
1121 | * @return $this |
||
1122 | */ |
||
1123 | public function setTitle($title = '') |
||
1129 | |||
1130 | /** |
||
1131 | * Add a row in form. |
||
1132 | * |
||
1133 | * @param Closure $callback |
||
1134 | * |
||
1135 | * @return $this |
||
1136 | */ |
||
1137 | public function row(Closure $callback) |
||
1143 | |||
1144 | /** |
||
1145 | * Tools setting for form. |
||
1146 | * |
||
1147 | * @param Closure $callback |
||
1148 | */ |
||
1149 | public function tools(Closure $callback) |
||
1153 | |||
1154 | /** |
||
1155 | * Disable form submit. |
||
1156 | * |
||
1157 | * @return $this |
||
1158 | */ |
||
1159 | public function disableSubmit() |
||
1165 | |||
1166 | /** |
||
1167 | * Disable form reset. |
||
1168 | * |
||
1169 | * @return $this |
||
1170 | */ |
||
1171 | public function disableReset() |
||
1177 | |||
1178 | /** |
||
1179 | * Disable form remove reserved fields. |
||
1180 | * |
||
1181 | * @return $this |
||
1182 | */ |
||
1183 | public function disableRemoveReservedFields() |
||
1189 | |||
1190 | /** |
||
1191 | * Get current resource route url. |
||
1192 | * |
||
1193 | * @param int $slice |
||
1194 | * |
||
1195 | * @return string |
||
1196 | */ |
||
1197 | public function resource($slice = -2) |
||
1211 | |||
1212 | /** |
||
1213 | * Render the form contents. |
||
1214 | * |
||
1215 | * @return string |
||
1216 | */ |
||
1217 | public function render() |
||
1225 | |||
1226 | /** |
||
1227 | * Get or set input data. |
||
1228 | * |
||
1229 | * @param string $key |
||
1230 | * @param null $value |
||
1231 | * |
||
1232 | * @return array|mixed |
||
1233 | */ |
||
1234 | public function input($key, $value = null) |
||
1242 | |||
1243 | /** |
||
1244 | * Register builtin fields. |
||
1245 | * |
||
1246 | * @return void |
||
1247 | */ |
||
1248 | public static function registerBuiltinFields() |
||
1303 | |||
1304 | /** |
||
1305 | * Register custom field. |
||
1306 | * |
||
1307 | * @param string $abstract |
||
1308 | * @param string $class |
||
1309 | * |
||
1310 | * @return void |
||
1311 | */ |
||
1312 | public static function extend($abstract, $class) |
||
1316 | |||
1317 | /** |
||
1318 | * Remove registered field. |
||
1319 | * |
||
1320 | * @param array|string $abstract |
||
1321 | */ |
||
1322 | public static function forget($abstract) |
||
1326 | |||
1327 | /** |
||
1328 | * Find field class. |
||
1329 | * |
||
1330 | * @param string $method |
||
1331 | * |
||
1332 | * @return bool|mixed |
||
1333 | */ |
||
1334 | View Code Duplication | public static function findFieldClass($method) |
|
1344 | |||
1345 | /** |
||
1346 | * Collect assets required by registered field. |
||
1347 | * |
||
1348 | * @return array |
||
1349 | */ |
||
1350 | public static function collectFieldAssets() |
||
1375 | |||
1376 | /** |
||
1377 | * Getter. |
||
1378 | * |
||
1379 | * @param string $name |
||
1380 | * |
||
1381 | * @return array|mixed |
||
1382 | */ |
||
1383 | public function __get($name) |
||
1387 | |||
1388 | /** |
||
1389 | * Setter. |
||
1390 | * |
||
1391 | * @param string $name |
||
1392 | * @param $value |
||
1393 | */ |
||
1394 | public function __set($name, $value) |
||
1398 | |||
1399 | /** |
||
1400 | * Generate a Field object and add to form builder if Field exists. |
||
1401 | * |
||
1402 | * @param string $method |
||
1403 | * @param array $arguments |
||
1404 | * |
||
1405 | * @return Field|void |
||
1406 | */ |
||
1407 | View Code Duplication | public function __call($method, $arguments) |
|
1419 | |||
1420 | /** |
||
1421 | * Render the contents of the form when casting to string. |
||
1422 | * |
||
1423 | * @return string |
||
1424 | */ |
||
1425 | public function __toString() |
||
1429 | } |
||
1430 |
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: