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 DataTablesEditor 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 DataTablesEditor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class DataTablesEditor |
||
15 | { |
||
16 | use ValidatesRequests; |
||
17 | |||
18 | /** |
||
19 | * Allowed dataTables editor actions. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $actions = ['create', 'edit', 'remove', 'upload']; |
||
24 | |||
25 | /** |
||
26 | * @var \Illuminate\Database\Eloquent\Model |
||
27 | */ |
||
28 | protected $model = null; |
||
29 | |||
30 | /** |
||
31 | * Indicates if all mass assignment is enabled on model. |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $unguarded = false; |
||
36 | |||
37 | /** |
||
38 | * Upload directory relative to storage path. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $uploadDir = 'editor'; |
||
43 | |||
44 | /** |
||
45 | * Filesystem disk config to use for upload. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $disk = 'public'; |
||
50 | |||
51 | /** |
||
52 | * Process dataTables editor action request. |
||
53 | * |
||
54 | * @param Request $request |
||
55 | * @return JsonResponse|mixed |
||
56 | * @throws DataTablesEditorException |
||
57 | */ |
||
58 | public function process(Request $request) |
||
68 | |||
69 | /** |
||
70 | * Process create action request. |
||
71 | * |
||
72 | * @param Request $request |
||
73 | * @return JsonResponse |
||
74 | */ |
||
75 | View Code Duplication | public function create(Request $request) |
|
124 | |||
125 | /** |
||
126 | * Resolve model to used. |
||
127 | * |
||
128 | * @return Model|\Illuminate\Database\Eloquent\SoftDeletes |
||
129 | */ |
||
130 | protected function resolveModel() |
||
140 | |||
141 | /** |
||
142 | * Get create action validation rules. |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | abstract public function createRules(); |
||
147 | |||
148 | /** |
||
149 | * Get create validation messages. |
||
150 | * |
||
151 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
152 | * @return array |
||
153 | */ |
||
154 | protected function createMessages() |
||
158 | |||
159 | /** |
||
160 | * Get custom attributes for validator errors. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function attributes() |
||
168 | |||
169 | /** |
||
170 | * @param Validator $validator |
||
171 | * @return array |
||
172 | */ |
||
173 | protected function formatErrors(Validator $validator) |
||
186 | |||
187 | /** |
||
188 | * Display success data in dataTables editor format. |
||
189 | * |
||
190 | * @param array $data |
||
191 | * @param array $errors |
||
192 | * @return JsonResponse |
||
193 | */ |
||
194 | protected function toJson(array $data, array $errors = []) |
||
203 | |||
204 | /** |
||
205 | * Process edit action request. |
||
206 | * |
||
207 | * @param Request $request |
||
208 | * @return JsonResponse |
||
209 | */ |
||
210 | View Code Duplication | public function edit(Request $request) |
|
259 | |||
260 | /** |
||
261 | * Get elqouent builder of the model. |
||
262 | * |
||
263 | * @return \Illuminate\Database\Eloquent\Builder |
||
264 | */ |
||
265 | protected function getBuilder() |
||
275 | |||
276 | /** |
||
277 | * Get edit action validation rules. |
||
278 | * |
||
279 | * @param Model $model |
||
280 | * @return array |
||
281 | */ |
||
282 | abstract public function editRules(Model $model); |
||
283 | |||
284 | /** |
||
285 | * Get edit validation messages. |
||
286 | * |
||
287 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
288 | * @return array |
||
289 | */ |
||
290 | protected function editMessages() |
||
294 | |||
295 | /** |
||
296 | * Process remove action request. |
||
297 | * |
||
298 | * @param Request $request |
||
299 | * @return JsonResponse |
||
300 | */ |
||
301 | public function remove(Request $request) |
||
355 | |||
356 | /** |
||
357 | * Get remove action validation rules. |
||
358 | * |
||
359 | * @param Model $model |
||
360 | * @return array |
||
361 | */ |
||
362 | abstract public function removeRules(Model $model); |
||
363 | |||
364 | /** |
||
365 | * Get remove validation messages. |
||
366 | * |
||
367 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
368 | * @return array |
||
369 | */ |
||
370 | protected function removeMessages() |
||
374 | |||
375 | /** |
||
376 | * Get remove query exception message. |
||
377 | * |
||
378 | * @param QueryException $exception |
||
379 | * @param Model $model |
||
380 | * @return string |
||
381 | */ |
||
382 | protected function removeExceptionMessage(QueryException $exception, Model $model) |
||
386 | |||
387 | /** |
||
388 | * Get dataTables model. |
||
389 | * |
||
390 | * @return Model |
||
391 | */ |
||
392 | public function getModel() |
||
396 | |||
397 | /** |
||
398 | * Set the dataTables model on runtime. |
||
399 | * |
||
400 | * @param Model|string $model |
||
401 | * @return DataTablesEditor |
||
402 | */ |
||
403 | public function setModel($model) |
||
409 | |||
410 | /** |
||
411 | * Set model unguard state. |
||
412 | * |
||
413 | * @param bool $state |
||
414 | * @return $this |
||
415 | */ |
||
416 | public function unguard($state = true) |
||
422 | |||
423 | /** |
||
424 | * Handle uploading of file. |
||
425 | * |
||
426 | * @param \Illuminate\Http\Request $request |
||
427 | * @return \Illuminate\Http\JsonResponse |
||
428 | */ |
||
429 | public function upload(Request $request) |
||
471 | |||
472 | /** |
||
473 | * Upload validation rules. |
||
474 | * |
||
475 | * @return array |
||
476 | */ |
||
477 | public function uploadRules() |
||
481 | |||
482 | /** |
||
483 | * Get validation messages. |
||
484 | * |
||
485 | * @return array |
||
486 | */ |
||
487 | protected function messages() |
||
491 | |||
492 | /** |
||
493 | * Display dataTables editor validation errors. |
||
494 | * |
||
495 | * @param Validator $validator |
||
496 | * @return JsonResponse |
||
497 | */ |
||
498 | protected function displayValidationErrors(Validator $validator) |
||
507 | } |
||
508 |
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.