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 |
||
17 | abstract class DataTablesEditor |
||
18 | { |
||
19 | use ValidatesRequests; |
||
20 | |||
21 | /** |
||
22 | * Action performed by the editor. |
||
23 | * |
||
24 | * @var string|null |
||
25 | */ |
||
26 | protected $action = null; |
||
27 | |||
28 | /** |
||
29 | * Allowed dataTables editor actions. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $actions = [ |
||
34 | 'create', |
||
35 | 'edit', |
||
36 | 'remove', |
||
37 | 'upload', |
||
38 | 'forceDelete', |
||
39 | 'restore', |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * List of custom editor actions. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $customActions = []; |
||
48 | |||
49 | /** |
||
50 | * @var \Illuminate\Database\Eloquent\Model |
||
51 | */ |
||
52 | protected $model = null; |
||
53 | |||
54 | /** |
||
55 | * Indicates if all mass assignment is enabled on model. |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $unguarded = false; |
||
60 | |||
61 | /** |
||
62 | * Upload directory relative to storage path. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $uploadDir = 'editor'; |
||
67 | |||
68 | /** |
||
69 | * Flag to force delete a model. |
||
70 | * |
||
71 | * @var bool |
||
72 | */ |
||
73 | protected $forceDeleting = false; |
||
74 | |||
75 | /** |
||
76 | * Flag to restore a model from deleted state. |
||
77 | * |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected $restoring = false; |
||
81 | |||
82 | /** |
||
83 | * Filesystem disk config to use for upload. |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $disk = 'public'; |
||
88 | |||
89 | /** |
||
90 | * Current request data that is being processed. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $currentData = []; |
||
95 | |||
96 | /** |
||
97 | * Process dataTables editor action request. |
||
98 | * |
||
99 | * @param Request $request |
||
100 | * @return JsonResponse|mixed |
||
101 | * @throws DataTablesEditorException |
||
102 | */ |
||
103 | public function process(Request $request) |
||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function getUseFriendlyErrorMessage() |
||
131 | |||
132 | /** |
||
133 | * Display success data in dataTables editor format. |
||
134 | * |
||
135 | * @param array $data |
||
136 | * @param array $errors |
||
137 | * @param string $error |
||
138 | * @return JsonResponse |
||
139 | */ |
||
140 | protected function toJson(array $data, array $errors = [], $error = '') |
||
161 | |||
162 | /** |
||
163 | * Process create action request. |
||
164 | * |
||
165 | * @param Request $request |
||
166 | * @return JsonResponse |
||
167 | * @throws \Exception |
||
168 | */ |
||
169 | public function create(Request $request) |
||
225 | |||
226 | /** |
||
227 | * Resolve model to used. |
||
228 | * |
||
229 | * @return Model|\Illuminate\Database\Eloquent\SoftDeletes |
||
230 | */ |
||
231 | protected function resolveModel() |
||
241 | |||
242 | /** |
||
243 | * Get create action validation rules. |
||
244 | * |
||
245 | * @return array |
||
246 | */ |
||
247 | public function createRules() { |
||
250 | |||
251 | /** |
||
252 | * Get validation messages. |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | protected function messages() |
||
260 | |||
261 | /** |
||
262 | * Get create validation messages. |
||
263 | * |
||
264 | * @return array |
||
265 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
266 | */ |
||
267 | protected function createMessages() |
||
271 | |||
272 | /** |
||
273 | * Get custom attributes for validator errors. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | public function attributes() |
||
281 | |||
282 | /** |
||
283 | * @param Validator $validator |
||
284 | * @return array |
||
285 | */ |
||
286 | protected function formatErrors(Validator $validator) |
||
299 | |||
300 | /** |
||
301 | * Process restore action request. |
||
302 | * |
||
303 | * @param \Illuminate\Http\Request $request |
||
304 | * @return \Illuminate\Http\JsonResponse |
||
305 | */ |
||
306 | public function restore(Request $request) |
||
312 | |||
313 | /** |
||
314 | * Process edit action request. |
||
315 | * |
||
316 | * @param Request $request |
||
317 | * @return JsonResponse |
||
318 | */ |
||
319 | public function edit(Request $request) |
||
374 | |||
375 | /** |
||
376 | * Get elqouent builder of the model. |
||
377 | * |
||
378 | * @return \Illuminate\Database\Eloquent\Builder |
||
379 | */ |
||
380 | protected function getBuilder() |
||
390 | |||
391 | /** |
||
392 | * Get edit action validation rules. |
||
393 | * |
||
394 | * @param Model $model |
||
395 | * @return array |
||
396 | */ |
||
397 | public function editRules(Model $model) { |
||
400 | |||
401 | /** |
||
402 | * Get edit validation messages. |
||
403 | * |
||
404 | * @return array |
||
405 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
406 | */ |
||
407 | protected function editMessages() |
||
411 | |||
412 | /** |
||
413 | * Process force delete action request. |
||
414 | * |
||
415 | * @param \Illuminate\Http\Request $request |
||
416 | * @return \Illuminate\Http\JsonResponse |
||
417 | * @throws \Exception |
||
418 | */ |
||
419 | public function forceDelete(Request $request) |
||
425 | |||
426 | /** |
||
427 | * Process remove action request. |
||
428 | * |
||
429 | * @param Request $request |
||
430 | * @return JsonResponse |
||
431 | * @throws \Exception |
||
432 | */ |
||
433 | public function remove(Request $request) |
||
493 | |||
494 | /** |
||
495 | * Get remove action validation rules. |
||
496 | * |
||
497 | * @param Model $model |
||
498 | * @return array |
||
499 | */ |
||
500 | public function removeRules(Model $model) { |
||
503 | |||
504 | /** |
||
505 | * Get remove validation messages. |
||
506 | * |
||
507 | * @return array |
||
508 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
509 | */ |
||
510 | protected function removeMessages() |
||
514 | |||
515 | /** |
||
516 | * Get remove query exception message. |
||
517 | * |
||
518 | * @param QueryException $exception |
||
519 | * @param Model $model |
||
520 | * @return string |
||
521 | */ |
||
522 | protected function removeExceptionMessage(QueryException $exception, Model $model) |
||
526 | |||
527 | /** |
||
528 | * Get dataTables model. |
||
529 | * |
||
530 | * @return Model |
||
531 | */ |
||
532 | public function getModel() |
||
536 | |||
537 | /** |
||
538 | * Set the dataTables model on runtime. |
||
539 | * |
||
540 | * @param Model|string $model |
||
541 | * @return DataTablesEditor |
||
542 | */ |
||
543 | public function setModel($model) |
||
549 | |||
550 | /** |
||
551 | * Set model unguard state. |
||
552 | * |
||
553 | * @param bool $state |
||
554 | * @return $this |
||
555 | */ |
||
556 | public function unguard($state = true) |
||
562 | |||
563 | /** |
||
564 | * Handle uploading of file. |
||
565 | * |
||
566 | * @param \Illuminate\Http\Request $request |
||
567 | * @return \Illuminate\Http\JsonResponse |
||
568 | */ |
||
569 | public function upload(Request $request) |
||
620 | |||
621 | /** |
||
622 | * Upload validation rules. |
||
623 | * |
||
624 | * @return array |
||
625 | */ |
||
626 | public function uploadRules() |
||
630 | |||
631 | /** |
||
632 | * @param string $field |
||
633 | * @param UploadedFile $uploadedFile |
||
634 | * @return string |
||
635 | */ |
||
636 | protected function getUploadedFilename($field, UploadedFile $uploadedFile) |
||
640 | } |
||
641 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.