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 |
||
| 16 | abstract class DataTablesEditor |
||
| 17 | { |
||
| 18 | use ValidatesRequests; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Allowed dataTables editor actions. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $actions = [ |
||
| 26 | 'create', |
||
| 27 | 'edit', |
||
| 28 | 'remove', |
||
| 29 | 'upload', |
||
| 30 | 'forceDelete', |
||
| 31 | 'restore', |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \Illuminate\Database\Eloquent\Model |
||
| 36 | */ |
||
| 37 | protected $model = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Indicates if all mass assignment is enabled on model. |
||
| 41 | * |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $unguarded = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Upload directory relative to storage path. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $uploadDir = 'editor'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Flag to force delete a model. |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $forceDeleting = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Flag to restore a model from deleted state. |
||
| 62 | * |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | protected $restoring = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Filesystem disk config to use for upload. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $disk = 'public'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Current request data that is being processed. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $currentData = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Process dataTables editor action request. |
||
| 83 | * |
||
| 84 | * @param Request $request |
||
| 85 | * @return JsonResponse|mixed |
||
| 86 | * @throws DataTablesEditorException |
||
| 87 | */ |
||
| 88 | public function process(Request $request) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | protected function getUseFriendlyErrorMessage() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Display success data in dataTables editor format. |
||
| 117 | * |
||
| 118 | * @param array $data |
||
| 119 | * @param array $errors |
||
| 120 | * @param string $error |
||
| 121 | * @return JsonResponse |
||
| 122 | */ |
||
| 123 | protected function toJson(array $data, array $errors = [], $error = '') |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Process create action request. |
||
| 140 | * |
||
| 141 | * @param Request $request |
||
| 142 | * @return JsonResponse |
||
| 143 | * @throws \Exception |
||
| 144 | */ |
||
| 145 | public function create(Request $request) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Resolve model to used. |
||
| 204 | * |
||
| 205 | * @return Model|\Illuminate\Database\Eloquent\SoftDeletes |
||
| 206 | */ |
||
| 207 | protected function resolveModel() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get create action validation rules. |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | abstract public function createRules(); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get validation messages. |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | protected function messages() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get create validation messages. |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
| 240 | */ |
||
| 241 | protected function createMessages() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get custom attributes for validator errors. |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public function attributes() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param Validator $validator |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | protected function formatErrors(Validator $validator) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Process restore action request. |
||
| 276 | * |
||
| 277 | * @param \Illuminate\Http\Request $request |
||
| 278 | * @return \Illuminate\Http\JsonResponse |
||
| 279 | */ |
||
| 280 | public function restore(Request $request) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Process edit action request. |
||
| 289 | * |
||
| 290 | * @param Request $request |
||
| 291 | * @return JsonResponse |
||
| 292 | */ |
||
| 293 | public function edit(Request $request) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get elqouent builder of the model. |
||
| 351 | * |
||
| 352 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 353 | */ |
||
| 354 | protected function getBuilder() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get edit action validation rules. |
||
| 367 | * |
||
| 368 | * @param Model $model |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | abstract public function editRules(Model $model); |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get edit validation messages. |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
| 378 | */ |
||
| 379 | protected function editMessages() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Process force delete action request. |
||
| 386 | * |
||
| 387 | * @param \Illuminate\Http\Request $request |
||
| 388 | * @return \Illuminate\Http\JsonResponse |
||
| 389 | * @throws \Exception |
||
| 390 | */ |
||
| 391 | public function forceDelete(Request $request) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Process remove action request. |
||
| 400 | * |
||
| 401 | * @param Request $request |
||
| 402 | * @return JsonResponse |
||
| 403 | * @throws \Exception |
||
| 404 | */ |
||
| 405 | public function remove(Request $request) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get remove action validation rules. |
||
| 469 | * |
||
| 470 | * @param Model $model |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | abstract public function removeRules(Model $model); |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Get remove validation messages. |
||
| 477 | * |
||
| 478 | * @return array |
||
| 479 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
| 480 | */ |
||
| 481 | protected function removeMessages() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Get remove query exception message. |
||
| 488 | * |
||
| 489 | * @param QueryException $exception |
||
| 490 | * @param Model $model |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | protected function removeExceptionMessage(QueryException $exception, Model $model) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get dataTables model. |
||
| 500 | * |
||
| 501 | * @return Model |
||
| 502 | */ |
||
| 503 | public function getModel() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Set the dataTables model on runtime. |
||
| 510 | * |
||
| 511 | * @param Model|string $model |
||
| 512 | * @return DataTablesEditor |
||
| 513 | */ |
||
| 514 | public function setModel($model) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Set model unguard state. |
||
| 523 | * |
||
| 524 | * @param bool $state |
||
| 525 | * @return $this |
||
| 526 | */ |
||
| 527 | public function unguard($state = true) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Handle uploading of file. |
||
| 536 | * |
||
| 537 | * @param \Illuminate\Http\Request $request |
||
| 538 | * @return \Illuminate\Http\JsonResponse |
||
| 539 | */ |
||
| 540 | public function upload(Request $request) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Upload validation rules. |
||
| 589 | * |
||
| 590 | * @return array |
||
| 591 | */ |
||
| 592 | public function uploadRules() |
||
| 596 | } |
||
| 597 |
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.