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 CrudController 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 CrudController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class CrudController extends Controller |
||
11 | { |
||
12 | protected $route; |
||
13 | protected $entity; |
||
14 | protected $entityInstance = null; |
||
15 | protected $fields = []; |
||
16 | protected $columns = []; |
||
17 | protected $buttons = [ |
||
18 | 'create', |
||
19 | 'edit', |
||
20 | 'delete', |
||
21 | ]; |
||
22 | protected $paginate = null; |
||
23 | protected $searchable = []; |
||
24 | protected $filters = []; |
||
25 | protected $queryFilters = []; |
||
26 | protected $filterRequire = []; |
||
27 | protected $textsGeneral = [ |
||
28 | 'list_title' => 'Contents', |
||
29 | 'create_title' => '', |
||
30 | 'edit_title' => '', |
||
31 | ]; |
||
32 | protected $texts = []; |
||
33 | protected $htmlFilters = []; |
||
34 | protected $action = null; |
||
35 | protected $formColsClasses = [ |
||
36 | 'col-md-10 col-md-offset-1', |
||
37 | 'col-md-2', |
||
38 | 'col-md-10', |
||
39 | ]; |
||
40 | protected $links = []; |
||
41 | |||
42 | public function index(Request $request) |
||
76 | |||
77 | public function create() |
||
89 | |||
90 | public function store(Request $request) |
||
91 | { |
||
92 | $validate = $this->prepareValidation(); |
||
93 | View Code Duplication | if ($validate['rules']) { |
|
|
|||
94 | $this->validate($request, $validate['rules'], $validate['messages'], $validate['customAttributes']); |
||
95 | } |
||
96 | |||
97 | DB::beginTransaction(); |
||
98 | |||
99 | try { |
||
100 | $row = $this->entity->create(array_merge($request->all(), $this->queryFilters)); |
||
101 | $this->updateForeignRelations($row, $request); |
||
102 | } catch (QueryException $e) { |
||
103 | return redirect() |
||
104 | ->back() |
||
105 | ->with('error', 'Ha ocurrido un error, intente nuevamente'); |
||
106 | } |
||
107 | |||
108 | DB::commit(); |
||
109 | |||
110 | return redirect() |
||
111 | ->route($this->route.'.index') |
||
112 | ->with('success', isset($this->textsGeneral['save_action']) |
||
113 | ? $this->textsGeneral['save_action'] |
||
114 | : trans('eliurkis::crud.save_action')); |
||
115 | } |
||
116 | |||
117 | public function edit($id) |
||
134 | |||
135 | public function update(Request $request, $id) |
||
136 | { |
||
137 | $validate = $this->prepareValidation(); |
||
138 | View Code Duplication | if ($validate['rules']) { |
|
139 | $this->validate($request, $validate['rules'], $validate['messages'], $validate['customAttributes']); |
||
140 | } |
||
141 | |||
142 | DB::beginTransaction(); |
||
143 | |||
144 | try { |
||
145 | $row = $this->entity->findOrFail($id); |
||
146 | $row->update( |
||
147 | array_merge( |
||
148 | $request->all(), |
||
149 | $this->queryFilters |
||
150 | ) |
||
151 | ); |
||
152 | $this->updateForeignRelations($row, $request); |
||
153 | } catch (QueryException $e) { |
||
154 | return redirect() |
||
155 | ->back() |
||
156 | ->with('error', 'Ha ocurrido un error, intente nuevamente'); |
||
157 | } |
||
158 | |||
159 | DB::commit(); |
||
160 | |||
161 | return redirect() |
||
162 | ->route($this->route.'.index', $this->getParamsFilters($row)) |
||
163 | ->with('success', isset($this->textsGeneral['save_action']) |
||
164 | ? $this->textsGeneral['save_action'] |
||
165 | : trans('eliurkis::crud.save_action')); |
||
166 | } |
||
167 | |||
168 | public function destroy($id) |
||
178 | |||
179 | /* Private Actions */ |
||
180 | |||
181 | protected function filters($entity, $request) |
||
197 | |||
198 | protected function htmlFilters() |
||
199 | { |
||
224 | |||
225 | protected function paginate($entity, $request) |
||
241 | |||
242 | protected function search($entity, $request) |
||
262 | |||
263 | protected function getForeignRelationsFields() |
||
274 | |||
275 | protected function getBelongToFields() |
||
286 | |||
287 | protected function updateForeignRelations($row, $request) |
||
296 | |||
297 | protected function getParamsFilters($row) |
||
311 | |||
312 | protected function prepareLinks() |
||
324 | |||
325 | protected function prepareValidation() |
||
342 | |||
343 | protected function prepareRelationalFields($name) |
||
368 | |||
369 | protected function prepareFields() |
||
379 | |||
380 | protected function prepareField($name, $properties = []) |
||
429 | } |
||
430 |
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.