Complex classes like DataTable 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 DataTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class DataTable implements DataTableButtons |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Datatables print preview view. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $printPreview = 'datatables::print'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * List of columns to be exported. |
||
| 25 | * |
||
| 26 | * @var string|array |
||
| 27 | */ |
||
| 28 | protected $exportColumns = '*'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * List of columns to be printed. |
||
| 32 | * |
||
| 33 | * @var string|array |
||
| 34 | */ |
||
| 35 | protected $printColumns = '*'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Query scopes. |
||
| 39 | * |
||
| 40 | * @var \Yajra\DataTables\Contracts\DataTableScope[] |
||
| 41 | */ |
||
| 42 | protected $scopes = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Html builder. |
||
| 46 | * |
||
| 47 | * @var \Yajra\DataTables\Html\Builder |
||
| 48 | */ |
||
| 49 | protected $htmlBuilder; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Html builder extension callback. |
||
| 53 | * |
||
| 54 | * @var callable |
||
| 55 | */ |
||
| 56 | protected $htmlCallback; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Export filename. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $filename = ''; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Custom attributes set on the class. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $attributes = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Callback before sending the response. |
||
| 74 | * |
||
| 75 | * @var callable |
||
| 76 | */ |
||
| 77 | protected $beforeCallback; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Callback after processing the response. |
||
| 81 | * |
||
| 82 | * @var callable |
||
| 83 | */ |
||
| 84 | protected $responseCallback; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Available button actions. When calling an action, the value will be used |
||
| 88 | * as the function name (so it should be available) |
||
| 89 | * If you want to add or disable an action, overload and modify this property |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $actions = ['print', 'csv', 'excel', 'pdf']; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var \Yajra\DataTables\Utilities\Request |
||
| 97 | */ |
||
| 98 | protected $request; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Process dataTables needed render output. |
||
| 102 | * |
||
| 103 | * @param string $view |
||
| 104 | * @param array $data |
||
| 105 | * @param array $mergeData |
||
| 106 | * @return mixed |
||
| 107 | */ |
||
| 108 | public function render($view, $data = [], $mergeData = []) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get Datatables Request instance. |
||
| 127 | * |
||
| 128 | * @return \Yajra\DataTables\Utilities\Request |
||
| 129 | */ |
||
| 130 | public function request() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Display ajax response. |
||
| 137 | * |
||
| 138 | * @return \Illuminate\Http\JsonResponse |
||
| 139 | */ |
||
| 140 | public function ajax() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Display printable view of datatables. |
||
| 166 | * |
||
| 167 | * @return \Illuminate\Contracts\View\View |
||
| 168 | */ |
||
| 169 | public function printPreview() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get mapped columns versus final decorated output. |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | protected function getDataForPrint() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get printable columns. |
||
| 190 | * |
||
| 191 | * @return array|string |
||
| 192 | */ |
||
| 193 | protected function printColumns() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get columns definition from html builder. |
||
| 200 | * |
||
| 201 | * @return \Illuminate\Support\Collection |
||
| 202 | */ |
||
| 203 | protected function getColumnsFromBuilder() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Optional method if you want to use html builder. |
||
| 210 | * |
||
| 211 | * @return \Yajra\DataTables\Html\Builder |
||
| 212 | */ |
||
| 213 | public function html() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get Datatables Html Builder instance. |
||
| 220 | * |
||
| 221 | * @return \Yajra\DataTables\Html\Builder |
||
| 222 | */ |
||
| 223 | public function builder() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Map ajax response to columns definition. |
||
| 230 | * |
||
| 231 | * @param mixed $columns |
||
| 232 | * @param string $type |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | protected function mapResponseToColumns($columns, $type) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get decorated data as defined in datatables ajax response. |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | protected function getAjaxResponseData() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return \Yajra\DataTables\Html\Builder |
||
| 263 | */ |
||
| 264 | protected function getHtmlBuilder() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Add html builder callback hook. |
||
| 276 | * |
||
| 277 | * @param callable $callback |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function withHtml(callable $callback) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Add callback before sending the response. |
||
| 289 | * |
||
| 290 | * @param callable $callback |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function before(callable $callback) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Add callback after the response was processed. |
||
| 302 | * |
||
| 303 | * @param callable $callback |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | public function response(callable $callback) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Export results to Excel file. |
||
| 315 | * |
||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | public function excel() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Build excel file and prepare for export. |
||
| 325 | * |
||
| 326 | * @return \Maatwebsite\Excel\Writers\LaravelExcelWriter |
||
| 327 | */ |
||
| 328 | protected function buildExcelFile() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get export filename. |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function getFilename() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Set export filename. |
||
| 352 | * |
||
| 353 | * @param string $filename |
||
| 354 | * @return DataTable |
||
| 355 | */ |
||
| 356 | public function setFilename($filename) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get filename for export. |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | protected function filename() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get mapped columns versus final decorated output. |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | protected function getDataForExport() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get export columns definition. |
||
| 387 | * |
||
| 388 | * @return array|string |
||
| 389 | */ |
||
| 390 | private function exportColumns() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Export results to CSV file. |
||
| 397 | * |
||
| 398 | * @return void |
||
| 399 | */ |
||
| 400 | public function csv() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Export results to PDF file. |
||
| 407 | * |
||
| 408 | * @return mixed |
||
| 409 | */ |
||
| 410 | public function pdf() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * PDF version of the table using print preview blade template. |
||
| 421 | * |
||
| 422 | * @return mixed |
||
| 423 | */ |
||
| 424 | public function snappyPdf() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Add basic array query scopes. |
||
| 440 | * |
||
| 441 | * @param \Yajra\DataTables\Contracts\DataTableScope $scope |
||
| 442 | * @return $this |
||
| 443 | */ |
||
| 444 | public function addScope(DataTableScope $scope) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Set a custom class attribute. |
||
| 453 | * |
||
| 454 | * @param mixed $key |
||
| 455 | * @param mixed|null $value |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | public function with($key, $value = null) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Dynamically retrieve the value of an attribute. |
||
| 471 | * |
||
| 472 | * @param string $key |
||
| 473 | * @return mixed|null |
||
| 474 | */ |
||
| 475 | public function __get($key) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Apply query scopes. |
||
| 486 | * |
||
| 487 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
| 488 | * @return mixed |
||
| 489 | */ |
||
| 490 | protected function applyScopes($query) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get default builder parameters. |
||
| 501 | * |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | protected function getBuilderParameters() |
||
| 508 | } |
||
| 509 |