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 | * Allowed dataTables editor actions. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $actions = [ |
||
| 27 | 'create', |
||
| 28 | 'edit', |
||
| 29 | 'remove', |
||
| 30 | 'upload', |
||
| 31 | 'forceDelete', |
||
| 32 | 'restore', |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \Illuminate\Database\Eloquent\Model |
||
| 37 | */ |
||
| 38 | protected $model = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Indicates if all mass assignment is enabled on model. |
||
| 42 | * |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $unguarded = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Upload directory relative to storage path. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $uploadDir = 'editor'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Flag to force delete a model. |
||
| 56 | * |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | protected $forceDeleting = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Flag to restore a model from deleted state. |
||
| 63 | * |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | protected $restoring = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Filesystem disk config to use for upload. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $disk = 'public'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Current request data that is being processed. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $currentData = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Process dataTables editor action request. |
||
| 84 | * |
||
| 85 | * @param Request $request |
||
| 86 | * @return JsonResponse|mixed |
||
| 87 | * @throws DataTablesEditorException |
||
| 88 | */ |
||
| 89 | public function process(Request $request) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | protected function getUseFriendlyErrorMessage() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Display success data in dataTables editor format. |
||
| 120 | * |
||
| 121 | * @param array $data |
||
| 122 | * @param array $errors |
||
| 123 | * @param string $error |
||
| 124 | * @return JsonResponse |
||
| 125 | */ |
||
| 126 | protected function toJson(array $data, array $errors = [], $error = '') |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Process create action request. |
||
| 143 | * |
||
| 144 | * @param Request $request |
||
| 145 | * @return JsonResponse |
||
| 146 | * @throws \Exception |
||
| 147 | */ |
||
| 148 | public function create(Request $request) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Resolve model to used. |
||
| 207 | * |
||
| 208 | * @return Model|\Illuminate\Database\Eloquent\SoftDeletes |
||
| 209 | */ |
||
| 210 | protected function resolveModel() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get create action validation rules. |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | abstract public function createRules(); |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get validation messages. |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | protected function messages() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get create validation messages. |
||
| 240 | * |
||
| 241 | * @return array |
||
| 242 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
| 243 | */ |
||
| 244 | protected function createMessages() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get custom attributes for validator errors. |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function attributes() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param Validator $validator |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | protected function formatErrors(Validator $validator) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Process restore action request. |
||
| 279 | * |
||
| 280 | * @param \Illuminate\Http\Request $request |
||
| 281 | * @return \Illuminate\Http\JsonResponse |
||
| 282 | */ |
||
| 283 | public function restore(Request $request) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Process edit action request. |
||
| 292 | * |
||
| 293 | * @param Request $request |
||
| 294 | * @return JsonResponse |
||
| 295 | */ |
||
| 296 | public function edit(Request $request) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get elqouent builder of the model. |
||
| 354 | * |
||
| 355 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 356 | */ |
||
| 357 | protected function getBuilder() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get edit action validation rules. |
||
| 370 | * |
||
| 371 | * @param Model $model |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | abstract public function editRules(Model $model); |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get edit validation messages. |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
| 381 | */ |
||
| 382 | protected function editMessages() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Process force delete action request. |
||
| 389 | * |
||
| 390 | * @param \Illuminate\Http\Request $request |
||
| 391 | * @return \Illuminate\Http\JsonResponse |
||
| 392 | * @throws \Exception |
||
| 393 | */ |
||
| 394 | public function forceDelete(Request $request) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Process remove action request. |
||
| 403 | * |
||
| 404 | * @param Request $request |
||
| 405 | * @return JsonResponse |
||
| 406 | * @throws \Exception |
||
| 407 | */ |
||
| 408 | public function remove(Request $request) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get remove action validation rules. |
||
| 471 | * |
||
| 472 | * @param Model $model |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | abstract public function removeRules(Model $model); |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get remove validation messages. |
||
| 479 | * |
||
| 480 | * @return array |
||
| 481 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
| 482 | */ |
||
| 483 | protected function removeMessages() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Get remove query exception message. |
||
| 490 | * |
||
| 491 | * @param QueryException $exception |
||
| 492 | * @param Model $model |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | protected function removeExceptionMessage(QueryException $exception, Model $model) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Get dataTables model. |
||
| 502 | * |
||
| 503 | * @return Model |
||
| 504 | */ |
||
| 505 | public function getModel() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Set the dataTables model on runtime. |
||
| 512 | * |
||
| 513 | * @param Model|string $model |
||
| 514 | * @return DataTablesEditor |
||
| 515 | */ |
||
| 516 | public function setModel($model) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Set model unguard state. |
||
| 525 | * |
||
| 526 | * @param bool $state |
||
| 527 | * @return $this |
||
| 528 | */ |
||
| 529 | public function unguard($state = true) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Handle uploading of file. |
||
| 538 | * |
||
| 539 | * @param \Illuminate\Http\Request $request |
||
| 540 | * @return \Illuminate\Http\JsonResponse |
||
| 541 | */ |
||
| 542 | public function upload(Request $request) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Upload validation rules. |
||
| 594 | * |
||
| 595 | * @return array |
||
| 596 | */ |
||
| 597 | public function uploadRules() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @param string $field |
||
| 604 | * @param UploadedFile $uploadedFile |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | protected function getUploadedFilename($field, UploadedFile $uploadedFile) |
||
| 611 | } |
||
| 612 |
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.