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() |
||
440 | |||
441 | /** |
||
442 | * Call saving callback. |
||
443 | * |
||
444 | * @return mixed |
||
445 | */ |
||
446 | protected function callSaving() |
||
452 | |||
453 | /** |
||
454 | * Callback after saving a Model. |
||
455 | * |
||
456 | * @param Closure|null $callback |
||
457 | * |
||
458 | * @return mixed|null |
||
459 | */ |
||
460 | protected function complete(Closure $callback = null) |
||
466 | |||
467 | /** |
||
468 | * Handle update. |
||
469 | * |
||
470 | * @param int $id |
||
471 | * |
||
472 | * @return \Symfony\Component\HttpFoundation\Response |
||
473 | */ |
||
474 | public function update($id, $data = null) |
||
532 | |||
533 | /** |
||
534 | * Get RedirectResponse after store. |
||
535 | * |
||
536 | * @return \Illuminate\Http\RedirectResponse |
||
537 | */ |
||
538 | protected function redirectAfterStore() |
||
546 | |||
547 | /** |
||
548 | * Get RedirectResponse after update. |
||
549 | * |
||
550 | * @param mixed $key |
||
551 | * @return \Illuminate\Http\RedirectResponse |
||
552 | */ |
||
553 | protected function redirectAfterUpdate($key) |
||
559 | |||
560 | /** |
||
561 | * Get RedirectResponse after data saving. |
||
562 | * |
||
563 | * @param string $resourcesPath |
||
564 | * @param string $key |
||
565 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
566 | */ |
||
567 | protected function redirectAfterSaving($resourcesPath, $key) |
||
583 | |||
584 | /** |
||
585 | * Check if request is from editable. |
||
586 | * |
||
587 | * @param array $input |
||
588 | * |
||
589 | * @return bool |
||
590 | */ |
||
591 | protected function isEditable(array $input = []) |
||
595 | |||
596 | /** |
||
597 | * Handle editable update. |
||
598 | * |
||
599 | * @param array $input |
||
600 | * |
||
601 | * @return array |
||
602 | */ |
||
603 | protected function handleEditable(array $input = []) |
||
615 | |||
616 | /** |
||
617 | * @param array $input |
||
618 | * |
||
619 | * @return array |
||
620 | */ |
||
621 | protected function handleFileDelete(array $input = []) |
||
632 | |||
633 | /** |
||
634 | * Handle orderable update. |
||
635 | * |
||
636 | * @param int $id |
||
637 | * @param array $input |
||
638 | * |
||
639 | * @return bool |
||
640 | */ |
||
641 | protected function handleOrderable($id, array $input = []) |
||
655 | |||
656 | /** |
||
657 | * Update relation data. |
||
658 | * |
||
659 | * @param array $relationsData |
||
660 | * |
||
661 | * @return void |
||
662 | */ |
||
663 | protected function updateRelation($relationsData) |
||
742 | |||
743 | /** |
||
744 | * Prepare input data for update. |
||
745 | * |
||
746 | * @param array $updates |
||
747 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
748 | * |
||
749 | * @return array |
||
750 | */ |
||
751 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
783 | |||
784 | /** |
||
785 | * @param string|array $columns |
||
786 | * @param bool $oneToOneRelation |
||
787 | * |
||
788 | * @return bool |
||
789 | */ |
||
790 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
801 | |||
802 | /** |
||
803 | * Prepare input data for insert. |
||
804 | * |
||
805 | * @param $inserts |
||
806 | * |
||
807 | * @return array |
||
808 | */ |
||
809 | protected function prepareInsert($inserts) |
||
832 | |||
833 | /** |
||
834 | * Is input data is has-one relation. |
||
835 | * |
||
836 | * @param array $inserts |
||
837 | * |
||
838 | * @return bool |
||
839 | */ |
||
840 | protected function isHasOneRelation($inserts) |
||
854 | |||
855 | /** |
||
856 | * Set submitted callback. |
||
857 | * |
||
858 | * @param Closure $callback |
||
859 | * |
||
860 | * @return void |
||
861 | */ |
||
862 | public function submitted(Closure $callback) |
||
866 | |||
867 | /** |
||
868 | * Set saving callback. |
||
869 | * |
||
870 | * @param Closure $callback |
||
871 | * |
||
872 | * @return void |
||
873 | */ |
||
874 | public function saving(Closure $callback) |
||
878 | |||
879 | /** |
||
880 | * Set saved callback. |
||
881 | * |
||
882 | * @param Closure $callback |
||
883 | * |
||
884 | * @return void |
||
885 | */ |
||
886 | public function saved(Closure $callback) |
||
890 | |||
891 | /** |
||
892 | * Ignore fields to save. |
||
893 | * |
||
894 | * @param string|array $fields |
||
895 | * |
||
896 | * @return $this |
||
897 | */ |
||
898 | public function ignore($fields) |
||
904 | |||
905 | /** |
||
906 | * @param array $data |
||
907 | * @param string|array $columns |
||
908 | * |
||
909 | * @return array|mixed |
||
910 | */ |
||
911 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
929 | |||
930 | /** |
||
931 | * Find field object by column. |
||
932 | * |
||
933 | * @param $column |
||
934 | * |
||
935 | * @return mixed |
||
936 | */ |
||
937 | protected function getFieldByColumn($column) |
||
949 | |||
950 | /** |
||
951 | * Set original data for each field. |
||
952 | * |
||
953 | * @return void |
||
954 | */ |
||
955 | protected function setFieldOriginalValue() |
||
965 | |||
966 | /** |
||
967 | * Set all fields value in form. |
||
968 | * |
||
969 | * @param $id |
||
970 | * |
||
971 | * @return void |
||
972 | */ |
||
973 | protected function setFieldValue($id) |
||
989 | |||
990 | /** |
||
991 | * Don't snake case attributes. |
||
992 | * |
||
993 | * @param Model $model |
||
994 | * |
||
995 | * @return void |
||
996 | */ |
||
997 | protected static function doNotSnakeAttributes(Model $model) |
||
1003 | |||
1004 | /** |
||
1005 | * Get validation messages. |
||
1006 | * |
||
1007 | * @param array $input |
||
1008 | * |
||
1009 | * @return MessageBag|bool |
||
1010 | */ |
||
1011 | protected function validationMessages($input) |
||
1030 | |||
1031 | /** |
||
1032 | * Merge validation messages from input validators. |
||
1033 | * |
||
1034 | * @param \Illuminate\Validation\Validator[] $validators |
||
1035 | * |
||
1036 | * @return MessageBag |
||
1037 | */ |
||
1038 | protected function mergeValidationMessages($validators) |
||
1048 | |||
1049 | /** |
||
1050 | * Get all relations of model from callable. |
||
1051 | * |
||
1052 | * @return array |
||
1053 | */ |
||
1054 | public function getRelations() |
||
1081 | |||
1082 | /** |
||
1083 | * Set action for form. |
||
1084 | * |
||
1085 | * @param string $action |
||
1086 | * |
||
1087 | * @return $this |
||
1088 | */ |
||
1089 | public function setAction($action) |
||
1095 | |||
1096 | /** |
||
1097 | * Set field and label width in current form. |
||
1098 | * |
||
1099 | * @param int $fieldWidth |
||
1100 | * @param int $labelWidth |
||
1101 | * |
||
1102 | * @return $this |
||
1103 | */ |
||
1104 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1115 | |||
1116 | /** |
||
1117 | * Set view for form. |
||
1118 | * |
||
1119 | * @param string $view |
||
1120 | * |
||
1121 | * @return $this |
||
1122 | */ |
||
1123 | public function setView($view) |
||
1129 | |||
1130 | /** |
||
1131 | * Set title for form. |
||
1132 | * |
||
1133 | * @param string $title |
||
1134 | * |
||
1135 | * @return $this |
||
1136 | */ |
||
1137 | public function setTitle($title = '') |
||
1143 | |||
1144 | /** |
||
1145 | * Add a row in form. |
||
1146 | * |
||
1147 | * @param Closure $callback |
||
1148 | * |
||
1149 | * @return $this |
||
1150 | */ |
||
1151 | public function row(Closure $callback) |
||
1157 | |||
1158 | /** |
||
1159 | * Tools setting for form. |
||
1160 | * |
||
1161 | * @param Closure $callback |
||
1162 | */ |
||
1163 | public function tools(Closure $callback) |
||
1167 | |||
1168 | /** |
||
1169 | * Disable form submit. |
||
1170 | * |
||
1171 | * @return $this |
||
1172 | * |
||
1173 | * @deprecated |
||
1174 | */ |
||
1175 | public function disableSubmit() |
||
1181 | |||
1182 | /** |
||
1183 | * Disable form reset. |
||
1184 | * |
||
1185 | * @return $this |
||
1186 | * |
||
1187 | * @deprecated |
||
1188 | */ |
||
1189 | public function disableReset() |
||
1195 | |||
1196 | /** |
||
1197 | * Footer setting for form. |
||
1198 | * |
||
1199 | * @param Closure $callback |
||
1200 | */ |
||
1201 | public function footer(Closure $callback) |
||
1205 | |||
1206 | /** |
||
1207 | * Get current resource route url. |
||
1208 | * |
||
1209 | * @param int $slice |
||
1210 | * |
||
1211 | * @return string |
||
1212 | */ |
||
1213 | public function resource($slice = -2) |
||
1227 | |||
1228 | /** |
||
1229 | * Render the form contents. |
||
1230 | * |
||
1231 | * @return string |
||
1232 | */ |
||
1233 | public function render() |
||
1241 | |||
1242 | /** |
||
1243 | * Get or set input data. |
||
1244 | * |
||
1245 | * @param string $key |
||
1246 | * @param null $value |
||
1247 | * |
||
1248 | * @return array|mixed |
||
1249 | */ |
||
1250 | public function input($key, $value = null) |
||
1258 | |||
1259 | /** |
||
1260 | * Register builtin fields. |
||
1261 | * |
||
1262 | * @return void |
||
1263 | */ |
||
1264 | public static function registerBuiltinFields() |
||
1319 | |||
1320 | /** |
||
1321 | * Register custom field. |
||
1322 | * |
||
1323 | * @param string $abstract |
||
1324 | * @param string $class |
||
1325 | * |
||
1326 | * @return void |
||
1327 | */ |
||
1328 | public static function extend($abstract, $class) |
||
1332 | |||
1333 | /** |
||
1334 | * Remove registered field. |
||
1335 | * |
||
1336 | * @param array|string $abstract |
||
1337 | */ |
||
1338 | public static function forget($abstract) |
||
1342 | |||
1343 | /** |
||
1344 | * Find field class. |
||
1345 | * |
||
1346 | * @param string $method |
||
1347 | * |
||
1348 | * @return bool|mixed |
||
1349 | */ |
||
1350 | View Code Duplication | public static function findFieldClass($method) |
|
1360 | |||
1361 | /** |
||
1362 | * Collect assets required by registered field. |
||
1363 | * |
||
1364 | * @return array |
||
1365 | */ |
||
1366 | public static function collectFieldAssets() |
||
1391 | |||
1392 | /** |
||
1393 | * Getter. |
||
1394 | * |
||
1395 | * @param string $name |
||
1396 | * |
||
1397 | * @return array|mixed |
||
1398 | */ |
||
1399 | public function __get($name) |
||
1403 | |||
1404 | /** |
||
1405 | * Setter. |
||
1406 | * |
||
1407 | * @param string $name |
||
1408 | * @param $value |
||
1409 | */ |
||
1410 | public function __set($name, $value) |
||
1414 | |||
1415 | /** |
||
1416 | * Generate a Field object and add to form builder if Field exists. |
||
1417 | * |
||
1418 | * @param string $method |
||
1419 | * @param array $arguments |
||
1420 | * |
||
1421 | * @return Field|void |
||
1422 | */ |
||
1423 | public function __call($method, $arguments) |
||
1435 | } |
||
1436 |
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: