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 |
||
58 | class Form |
||
59 | { |
||
60 | /** |
||
61 | * Eloquent model of the form. |
||
62 | * |
||
63 | * @var |
||
64 | */ |
||
65 | protected $model; |
||
66 | |||
67 | /** |
||
68 | * @var \Illuminate\Validation\Validator |
||
69 | */ |
||
70 | protected $validator; |
||
71 | |||
72 | /** |
||
73 | * @var Builder |
||
74 | */ |
||
75 | protected $builder; |
||
76 | |||
77 | /** |
||
78 | * Saving callback. |
||
79 | * |
||
80 | * @var Closure |
||
81 | */ |
||
82 | protected $saving; |
||
83 | |||
84 | /** |
||
85 | * Saved callback. |
||
86 | * |
||
87 | * @var Closure |
||
88 | */ |
||
89 | protected $saved; |
||
90 | |||
91 | /** |
||
92 | * Data for save to current model from input. |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $updates = []; |
||
97 | |||
98 | /** |
||
99 | * Data for save to model's relations from input. |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $relations = []; |
||
104 | |||
105 | /** |
||
106 | * Input data. |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $inputs = []; |
||
111 | |||
112 | /** |
||
113 | * @var callable |
||
114 | */ |
||
115 | protected $callable; |
||
116 | |||
117 | /** |
||
118 | * Allow delete item in form page. |
||
119 | * |
||
120 | * @var bool |
||
121 | */ |
||
122 | protected $allowDeletion = true; |
||
123 | |||
124 | /** |
||
125 | * Available fields. |
||
126 | * |
||
127 | * @var array |
||
128 | */ |
||
129 | public static $availableFields = []; |
||
130 | |||
131 | /** |
||
132 | * Collected field assets. |
||
133 | * |
||
134 | * @var array |
||
135 | */ |
||
136 | protected static $collectedAssets = []; |
||
137 | |||
138 | /** |
||
139 | * @param \$model |
||
140 | * @param \Closure $callback |
||
141 | */ |
||
142 | public function __construct($model, Closure $callback) |
||
152 | |||
153 | /** |
||
154 | * Set up the form. |
||
155 | */ |
||
156 | protected function setUp() |
||
160 | |||
161 | /** |
||
162 | * @param Field $field |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function pushField(Field $field) |
||
174 | |||
175 | /** |
||
176 | * @return Model |
||
177 | */ |
||
178 | public function model() |
||
182 | |||
183 | /** |
||
184 | * @return Builder |
||
185 | */ |
||
186 | public function builder() |
||
190 | |||
191 | /** |
||
192 | * Disable deletion in form page. |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function disableDeletion() |
||
204 | |||
205 | /** |
||
206 | * Generate a edit form. |
||
207 | * |
||
208 | * @param $id |
||
209 | * |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function edit($id) |
||
221 | |||
222 | /** |
||
223 | * @param $id |
||
224 | * |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function view($id) |
||
236 | |||
237 | /** |
||
238 | * Destroy data entity and remove files. |
||
239 | * |
||
240 | * @param $id |
||
241 | * |
||
242 | * @return mixed |
||
243 | */ |
||
244 | public function destroy($id) |
||
258 | |||
259 | /** |
||
260 | * Remove files or images in record. |
||
261 | * |
||
262 | * @param $id |
||
263 | */ |
||
264 | protected function deleteFilesAndImages($id) |
||
277 | |||
278 | /** |
||
279 | * Store a new record. |
||
280 | * |
||
281 | * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
282 | */ |
||
283 | public function store() |
||
312 | |||
313 | /** |
||
314 | * Prepare input data for insert or update. |
||
315 | * |
||
316 | * @param array $data |
||
317 | * @param callable $callback |
||
318 | */ |
||
319 | protected function prepare($data = [], Closure $callback = null) |
||
335 | |||
336 | /** |
||
337 | * Get inputs for relations. |
||
338 | * |
||
339 | * @param array $inputs |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | protected function getRelationInputs($inputs = []) |
||
359 | |||
360 | /** |
||
361 | * Callback after saving a Model. |
||
362 | * |
||
363 | * @param Closure|null $callback |
||
364 | * |
||
365 | * @return void |
||
366 | */ |
||
367 | protected function complete(Closure $callback = null) |
||
373 | |||
374 | /** |
||
375 | * Save relations data. |
||
376 | * |
||
377 | * @param array $relations |
||
378 | * |
||
379 | * @return void |
||
380 | */ |
||
381 | protected function saveRelation($relations) |
||
408 | |||
409 | /** |
||
410 | * Handle update. |
||
411 | * |
||
412 | * @param int $id |
||
413 | * |
||
414 | * @return $this|\Illuminate\Http\RedirectResponse |
||
415 | */ |
||
416 | public function update($id) |
||
456 | |||
457 | /** |
||
458 | * Handle editable update. |
||
459 | * |
||
460 | * @param array $input |
||
461 | * |
||
462 | * @return array |
||
463 | */ |
||
464 | protected function handleEditable(array $input = []) |
||
476 | |||
477 | /** |
||
478 | * Handle orderable update. |
||
479 | * |
||
480 | * @param int $id |
||
481 | * @param array $input |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | protected function handleOrderable($id, array $input = []) |
||
499 | |||
500 | /** |
||
501 | * Update relation data. |
||
502 | * |
||
503 | * @param array $relations |
||
504 | * |
||
505 | * @return void |
||
506 | */ |
||
507 | protected function updateRelation($relations) |
||
546 | |||
547 | /** |
||
548 | * Prepare input data for update. |
||
549 | * |
||
550 | * @param $updates |
||
551 | * |
||
552 | * @return array |
||
553 | */ |
||
554 | protected function prepareUpdate($updates) |
||
584 | |||
585 | /** |
||
586 | * Prepare input data for insert. |
||
587 | * |
||
588 | * @param $inserts |
||
589 | * |
||
590 | * @return array |
||
591 | */ |
||
592 | protected function prepareInsert($inserts) |
||
619 | |||
620 | /** |
||
621 | * Set saving callback. |
||
622 | * |
||
623 | * @param callable $callback |
||
624 | * |
||
625 | * @return void |
||
626 | */ |
||
627 | public function saving(Closure $callback) |
||
631 | |||
632 | /** |
||
633 | * Set saved callback. |
||
634 | * |
||
635 | * @param callable $callback |
||
636 | * |
||
637 | * @return void |
||
638 | */ |
||
639 | public function saved(Closure $callback) |
||
643 | |||
644 | /** |
||
645 | * @param array $data |
||
646 | * @param string|array $columns |
||
647 | * |
||
648 | * @return array|mixed |
||
649 | */ |
||
650 | protected function getDataByColumn($data, $columns) |
||
668 | |||
669 | /** |
||
670 | * Find field object by column. |
||
671 | * |
||
672 | * @param $column |
||
673 | * |
||
674 | * @return mixed |
||
675 | */ |
||
676 | protected function getFieldByColumn($column) |
||
688 | |||
689 | /** |
||
690 | * Set original data for each field. |
||
691 | * |
||
692 | * @return void |
||
693 | */ |
||
694 | protected function setFieldOriginalValue() |
||
702 | |||
703 | /** |
||
704 | * Set all fields value in form. |
||
705 | * |
||
706 | * @param $id |
||
707 | * |
||
708 | * @return void |
||
709 | */ |
||
710 | protected function setFieldValue($id) |
||
722 | |||
723 | /** |
||
724 | * Validation fails. |
||
725 | * |
||
726 | * @param array $input |
||
727 | * |
||
728 | * @return bool |
||
729 | */ |
||
730 | protected function validationFails($input) |
||
745 | |||
746 | /** |
||
747 | * Get all relations of model from callable. |
||
748 | * |
||
749 | * @return array |
||
750 | */ |
||
751 | public function getRelations() |
||
775 | |||
776 | /** |
||
777 | * Get current resource route url. |
||
778 | * |
||
779 | * @param int $slice |
||
780 | * |
||
781 | * @return string |
||
782 | */ |
||
783 | public function resource($slice = -2) |
||
795 | |||
796 | /** |
||
797 | * Render the form contents. |
||
798 | * |
||
799 | * @return string |
||
800 | */ |
||
801 | public function render() |
||
809 | |||
810 | /** |
||
811 | * Get or set input data. |
||
812 | * |
||
813 | * @param string $key |
||
814 | * @param null $value |
||
815 | * |
||
816 | * @return array|mixed |
||
817 | */ |
||
818 | public function input($key, $value = null) |
||
826 | |||
827 | /** |
||
828 | * Register builtin fields. |
||
829 | * |
||
830 | * @return void |
||
831 | */ |
||
832 | public static function registerBuiltinFields() |
||
881 | |||
882 | /** |
||
883 | * Register custom field. |
||
884 | * |
||
885 | * @param string $abstract |
||
886 | * @param string $class |
||
887 | * |
||
888 | * @return void |
||
889 | */ |
||
890 | public static function extend($abstract, $class) |
||
894 | |||
895 | /** |
||
896 | * Remove registered field. |
||
897 | * |
||
898 | * @param array|string $abstract |
||
899 | */ |
||
900 | public static function forget($abstract) |
||
904 | |||
905 | /** |
||
906 | * Find field class. |
||
907 | * |
||
908 | * @param string $method |
||
909 | * |
||
910 | * @return bool|mixed |
||
911 | */ |
||
912 | View Code Duplication | public static function findFieldClass($method) |
|
922 | |||
923 | /** |
||
924 | * Collect assets required by registered field. |
||
925 | * |
||
926 | * @return array |
||
927 | */ |
||
928 | public static function collectFieldAssets() |
||
953 | |||
954 | /** |
||
955 | * Getter. |
||
956 | * |
||
957 | * @param string $name |
||
958 | * |
||
959 | * @return array|mixed |
||
960 | */ |
||
961 | public function __get($name) |
||
965 | |||
966 | /** |
||
967 | * Setter. |
||
968 | * |
||
969 | * @param string $name |
||
970 | * @param $value |
||
971 | */ |
||
972 | public function __set($name, $value) |
||
976 | |||
977 | /** |
||
978 | * Generate a Field object and add to form builder if Field exists. |
||
979 | * |
||
980 | * @param string $method |
||
981 | * @param array $arguments |
||
982 | * |
||
983 | * @return Field|void |
||
984 | */ |
||
985 | View Code Duplication | public function __call($method, $arguments) |
|
997 | |||
998 | /** |
||
999 | * Render the contents of the form when casting to string. |
||
1000 | * |
||
1001 | * @return string |
||
1002 | */ |
||
1003 | public function __toString() |
||
1007 | } |
||
1008 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.