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 = [ |
||
24 | 'create', |
||
25 | 'edit', |
||
26 | 'remove', |
||
27 | 'upload', |
||
28 | 'forceDelete', |
||
29 | 'restore', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * @var \Illuminate\Database\Eloquent\Model |
||
34 | */ |
||
35 | protected $model = null; |
||
36 | |||
37 | /** |
||
38 | * Indicates if all mass assignment is enabled on model. |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $unguarded = false; |
||
43 | |||
44 | /** |
||
45 | * Upload directory relative to storage path. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $uploadDir = 'editor'; |
||
50 | |||
51 | /** |
||
52 | * Flag to force delete a model. |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $forceDeleting = false; |
||
57 | |||
58 | /** |
||
59 | * Flag to restore a model from deleted state. |
||
60 | * |
||
61 | * @var bool |
||
62 | */ |
||
63 | protected $restoring = false; |
||
64 | |||
65 | /** |
||
66 | * Filesystem disk config to use for upload. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $disk = 'public'; |
||
71 | |||
72 | /** |
||
73 | * Process dataTables editor action request. |
||
74 | * |
||
75 | * @param Request $request |
||
76 | * @return JsonResponse|mixed |
||
77 | * @throws DataTablesEditorException |
||
78 | */ |
||
79 | public function process(Request $request) |
||
89 | |||
90 | /** |
||
91 | * Process create action request. |
||
92 | * |
||
93 | * @param Request $request |
||
94 | * @return JsonResponse |
||
95 | */ |
||
96 | public function create(Request $request) |
||
150 | |||
151 | /** |
||
152 | * Resolve model to used. |
||
153 | * |
||
154 | * @return Model|\Illuminate\Database\Eloquent\SoftDeletes |
||
155 | */ |
||
156 | protected function resolveModel() |
||
166 | |||
167 | /** |
||
168 | * Get create action validation rules. |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | abstract public function createRules(); |
||
173 | |||
174 | /** |
||
175 | * Get validation messages. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | protected function messages() |
||
183 | |||
184 | /** |
||
185 | * Get create validation messages. |
||
186 | * |
||
187 | * @return array |
||
188 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
189 | */ |
||
190 | protected function createMessages() |
||
194 | |||
195 | /** |
||
196 | * Get custom attributes for validator errors. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | public function attributes() |
||
204 | |||
205 | /** |
||
206 | * @param Validator $validator |
||
207 | * @return array |
||
208 | */ |
||
209 | protected function formatErrors(Validator $validator) |
||
222 | |||
223 | /** |
||
224 | * Display success data in dataTables editor format. |
||
225 | * |
||
226 | * @param array $data |
||
227 | * @param array $errors |
||
228 | * @return JsonResponse |
||
229 | */ |
||
230 | protected function toJson(array $data, array $errors = []) |
||
239 | |||
240 | /** |
||
241 | * Process restore action request. |
||
242 | * |
||
243 | * @param \Illuminate\Http\Request $request |
||
244 | * @return \Illuminate\Http\JsonResponse |
||
245 | */ |
||
246 | public function restore(Request $request) |
||
252 | |||
253 | /** |
||
254 | * Process edit action request. |
||
255 | * |
||
256 | * @param Request $request |
||
257 | * @return JsonResponse |
||
258 | */ |
||
259 | public function edit(Request $request) |
||
312 | |||
313 | /** |
||
314 | * Get elqouent builder of the model. |
||
315 | * |
||
316 | * @return \Illuminate\Database\Eloquent\Builder |
||
317 | */ |
||
318 | protected function getBuilder() |
||
328 | |||
329 | /** |
||
330 | * Get edit action validation rules. |
||
331 | * |
||
332 | * @param Model $model |
||
333 | * @return array |
||
334 | */ |
||
335 | abstract public function editRules(Model $model); |
||
336 | |||
337 | /** |
||
338 | * Get edit validation messages. |
||
339 | * |
||
340 | * @return array |
||
341 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
342 | */ |
||
343 | protected function editMessages() |
||
347 | |||
348 | /** |
||
349 | * Process force delete action request. |
||
350 | * |
||
351 | * @param \Illuminate\Http\Request $request |
||
352 | * @return \Illuminate\Http\JsonResponse |
||
353 | */ |
||
354 | public function forceDelete(Request $request) |
||
360 | |||
361 | /** |
||
362 | * Process remove action request. |
||
363 | * |
||
364 | * @param Request $request |
||
365 | * @return JsonResponse |
||
366 | */ |
||
367 | public function remove(Request $request) |
||
425 | |||
426 | /** |
||
427 | * Get remove action validation rules. |
||
428 | * |
||
429 | * @param Model $model |
||
430 | * @return array |
||
431 | */ |
||
432 | abstract public function removeRules(Model $model); |
||
433 | |||
434 | /** |
||
435 | * Get remove validation messages. |
||
436 | * |
||
437 | * @return array |
||
438 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
439 | */ |
||
440 | protected function removeMessages() |
||
444 | |||
445 | /** |
||
446 | * Get remove query exception message. |
||
447 | * |
||
448 | * @param QueryException $exception |
||
449 | * @param Model $model |
||
450 | * @return string |
||
451 | */ |
||
452 | protected function removeExceptionMessage(QueryException $exception, Model $model) |
||
456 | |||
457 | /** |
||
458 | * Get dataTables model. |
||
459 | * |
||
460 | * @return Model |
||
461 | */ |
||
462 | public function getModel() |
||
466 | |||
467 | /** |
||
468 | * Set the dataTables model on runtime. |
||
469 | * |
||
470 | * @param Model|string $model |
||
471 | * @return DataTablesEditor |
||
472 | */ |
||
473 | public function setModel($model) |
||
479 | |||
480 | /** |
||
481 | * Set model unguard state. |
||
482 | * |
||
483 | * @param bool $state |
||
484 | * @return $this |
||
485 | */ |
||
486 | public function unguard($state = true) |
||
492 | |||
493 | /** |
||
494 | * Handle uploading of file. |
||
495 | * |
||
496 | * @param \Illuminate\Http\Request $request |
||
497 | * @return \Illuminate\Http\JsonResponse |
||
498 | */ |
||
499 | public function upload(Request $request) |
||
545 | |||
546 | /** |
||
547 | * Upload validation rules. |
||
548 | * |
||
549 | * @return array |
||
550 | */ |
||
551 | public function uploadRules() |
||
555 | |||
556 | /** |
||
557 | * Display dataTables editor validation errors. |
||
558 | * |
||
559 | * @param Validator $validator |
||
560 | * @return JsonResponse |
||
561 | */ |
||
562 | protected function displayValidationErrors(Validator $validator) |
||
571 | } |
||
572 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.