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) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Resolve model to used. |
||
| 103 | * |
||
| 104 | * @return Model |
||
| 105 | */ |
||
| 106 | protected function resolveModel() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get create action validation rules. |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | abstract public function createRules(); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get create validation messages. |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | protected function createMessages() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param Validator $validator |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | protected function formatErrors(Validator $validator) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Display success data in dataTables editor format. |
||
| 152 | * |
||
| 153 | * @param array $data |
||
| 154 | * @param array $errors |
||
| 155 | * @return JsonResponse |
||
| 156 | */ |
||
| 157 | protected function toJson(array $data, array $errors = []) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Process edit action request. |
||
| 169 | * |
||
| 170 | * @param Request $request |
||
| 171 | * @return JsonResponse |
||
| 172 | */ |
||
| 173 | public function edit(Request $request) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get edit action validation rules. |
||
| 225 | * |
||
| 226 | * @param Model $model |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | abstract public function editRules(Model $model); |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get edit validation messages. |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | protected function editMessages() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Process remove action request. |
||
| 243 | * |
||
| 244 | * @param Request $request |
||
| 245 | * @return JsonResponse |
||
| 246 | */ |
||
| 247 | public function remove(Request $request) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get remove action validation rules. |
||
| 301 | * |
||
| 302 | * @param Model $model |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | abstract public function removeRules(Model $model); |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get remove validation messages. |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | protected function removeMessages() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get remove query exception message. |
||
| 319 | * |
||
| 320 | * @param QueryException $exception |
||
| 321 | * @param Model $model |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | protected function removeExceptionMessage(QueryException $exception, Model $model) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get dataTables model. |
||
| 331 | * |
||
| 332 | * @return Model |
||
| 333 | */ |
||
| 334 | public function getModel() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Set the dataTables model on runtime. |
||
| 341 | * |
||
| 342 | * @param Model|string $model |
||
| 343 | * @return DataTablesEditor |
||
| 344 | */ |
||
| 345 | public function setModel($model) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Display dataTables editor validation errors. |
||
| 354 | * |
||
| 355 | * @param Validator $validator |
||
| 356 | * @return JsonResponse |
||
| 357 | */ |
||
| 358 | protected function displayValidationErrors(Validator $validator) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get custom attributes for validator errors. |
||
| 370 | * |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public function attributes() |
||
| 377 | } |
||
| 378 |
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.