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 |
||
| 12 | abstract class DataTablesEditor |
||
| 13 | { |
||
| 14 | use ValidatesRequests; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Allowed dataTables editor actions. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $actions = ['create', 'edit', 'remove']; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \Illuminate\Database\Eloquent\Model |
||
| 25 | */ |
||
| 26 | protected $model = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Process dataTables editor action request. |
||
| 30 | * |
||
| 31 | * @param Request $request |
||
| 32 | * @return JsonResponse|mixed |
||
| 33 | * @throws DataTablesEditorException |
||
| 34 | */ |
||
| 35 | public function process(Request $request) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Process create action request. |
||
| 48 | * |
||
| 49 | * @param Request $request |
||
| 50 | * @return JsonResponse |
||
| 51 | */ |
||
| 52 | public function create(Request $request) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Resolve model to used. |
||
| 106 | * |
||
| 107 | * @return Model|\Illuminate\Database\Eloquent\SoftDeletes |
||
| 108 | */ |
||
| 109 | protected function resolveModel() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get create action validation rules. |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | abstract public function createRules(); |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get create validation messages. |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | protected function createMessages() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get custom attributes for validator errors. |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public function attributes() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param Validator $validator |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | protected function formatErrors(Validator $validator) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Display success data in dataTables editor format. |
||
| 165 | * |
||
| 166 | * @param array $data |
||
| 167 | * @param array $errors |
||
| 168 | * @return JsonResponse |
||
| 169 | */ |
||
| 170 | protected function toJson(array $data, array $errors = []) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Process edit action request. |
||
| 182 | * |
||
| 183 | * @param Request $request |
||
| 184 | * @return JsonResponse |
||
| 185 | */ |
||
| 186 | public function edit(Request $request) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get elqouent builder of the model. |
||
| 241 | * |
||
| 242 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 243 | */ |
||
| 244 | protected function getBuilder() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get edit action validation rules. |
||
| 257 | * |
||
| 258 | * @param Model $model |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | abstract public function editRules(Model $model); |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get edit validation messages. |
||
| 265 | * |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | protected function editMessages() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Process remove action request. |
||
| 275 | * |
||
| 276 | * @param Request $request |
||
| 277 | * @return JsonResponse |
||
| 278 | */ |
||
| 279 | public function remove(Request $request) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get remove action validation rules. |
||
| 337 | * |
||
| 338 | * @param Model $model |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | abstract public function removeRules(Model $model); |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get remove validation messages. |
||
| 345 | * |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | protected function removeMessages() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get remove query exception message. |
||
| 355 | * |
||
| 356 | * @param QueryException $exception |
||
| 357 | * @param Model $model |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | protected function removeExceptionMessage(QueryException $exception, Model $model) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get dataTables model. |
||
| 367 | * |
||
| 368 | * @return Model |
||
| 369 | */ |
||
| 370 | public function getModel() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Set the dataTables model on runtime. |
||
| 377 | * |
||
| 378 | * @param Model|string $model |
||
| 379 | * @return DataTablesEditor |
||
| 380 | */ |
||
| 381 | public function setModel($model) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Display dataTables editor validation errors. |
||
| 390 | * |
||
| 391 | * @param Validator $validator |
||
| 392 | * @return JsonResponse |
||
| 393 | */ |
||
| 394 | protected function displayValidationErrors(Validator $validator) |
||
| 403 | } |
||
| 404 |
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.