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 |
||
| 11 | abstract class DataTable implements DataTableButtons |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * DataTables print preview view. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $printPreview = 'datatables::print'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Name of the dataTable variable. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $dataTableVariable = 'dataTable'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * List of columns to be excluded from export. |
||
| 29 | * |
||
| 30 | * @var string|array |
||
| 31 | */ |
||
| 32 | protected $excludeFromExport = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * List of columns to be excluded from printing. |
||
| 36 | * |
||
| 37 | * @var string|array |
||
| 38 | */ |
||
| 39 | protected $excludeFromPrint = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * List of columns to be exported. |
||
| 43 | * |
||
| 44 | * @var string|array |
||
| 45 | */ |
||
| 46 | protected $exportColumns = '*'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * List of columns to be printed. |
||
| 50 | * |
||
| 51 | * @var string|array |
||
| 52 | */ |
||
| 53 | protected $printColumns = '*'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Query scopes. |
||
| 57 | * |
||
| 58 | * @var \Yajra\DataTables\Contracts\DataTableScope[] |
||
| 59 | */ |
||
| 60 | protected $scopes = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Html builder. |
||
| 64 | * |
||
| 65 | * @var \Yajra\DataTables\Html\Builder |
||
| 66 | */ |
||
| 67 | protected $htmlBuilder; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Html builder extension callback. |
||
| 71 | * |
||
| 72 | * @var callable |
||
| 73 | */ |
||
| 74 | protected $htmlCallback; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Export filename. |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $filename = ''; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Custom attributes set on the class. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $attributes = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Callback before sending the response. |
||
| 92 | * |
||
| 93 | * @var callable |
||
| 94 | */ |
||
| 95 | protected $beforeCallback; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Callback after processing the response. |
||
| 99 | * |
||
| 100 | * @var callable |
||
| 101 | */ |
||
| 102 | protected $responseCallback; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Available button actions. When calling an action, the value will be used |
||
| 106 | * as the function name (so it should be available) |
||
| 107 | * If you want to add or disable an action, overload and modify this property. |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $actions = ['print', 'csv', 'excel', 'pdf']; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var \Yajra\DataTables\Utilities\Request |
||
| 115 | */ |
||
| 116 | protected $request; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Export class handler. |
||
| 120 | * |
||
| 121 | * @var string |
||
| 122 | */ |
||
| 123 | protected $exportClass = DataTablesExportHandler::class; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * CSV export type writer. |
||
| 127 | * |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | protected $csvWriter = 'Csv'; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Excel export type writer. |
||
| 134 | * |
||
| 135 | * @var string |
||
| 136 | */ |
||
| 137 | protected $excelWriter = 'Xlsx'; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * PDF export type writer. |
||
| 141 | * |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $pdfWriter = 'Dompdf'; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Process dataTables needed render output. |
||
| 148 | * |
||
| 149 | * @param string $view |
||
| 150 | * @param array $data |
||
| 151 | * @param array $mergeData |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | public function render($view, $data = [], $mergeData = []) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get DataTables Request instance. |
||
| 173 | * |
||
| 174 | * @return \Yajra\DataTables\Utilities\Request |
||
| 175 | */ |
||
| 176 | public function request() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Display ajax response. |
||
| 183 | * |
||
| 184 | * @return \Illuminate\Http\JsonResponse |
||
| 185 | */ |
||
| 186 | public function ajax() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Display printable view of datatables. |
||
| 212 | * |
||
| 213 | * @return \Illuminate\Contracts\View\View |
||
| 214 | */ |
||
| 215 | public function printPreview() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get mapped columns versus final decorated output. |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | protected function getDataForPrint() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get printable columns. |
||
| 236 | * |
||
| 237 | * @return array|string |
||
| 238 | */ |
||
| 239 | protected function printColumns() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get filtered print columns definition from html builder. |
||
| 246 | * |
||
| 247 | * @return \Illuminate\Support\Collection |
||
| 248 | */ |
||
| 249 | protected function getPrintColumnsFromBuilder() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get filtered export columns definition from html builder. |
||
| 256 | * |
||
| 257 | * @return \Illuminate\Support\Collection |
||
| 258 | */ |
||
| 259 | protected function getExportColumnsFromBuilder() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get columns definition from html builder. |
||
| 266 | * |
||
| 267 | * @return \Illuminate\Support\Collection |
||
| 268 | */ |
||
| 269 | protected function getColumnsFromBuilder() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Optional method if you want to use html builder. |
||
| 276 | * |
||
| 277 | * @return \Yajra\DataTables\Html\Builder |
||
| 278 | */ |
||
| 279 | public function html() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get DataTables Html Builder instance. |
||
| 286 | * |
||
| 287 | * @return \Yajra\DataTables\Html\Builder |
||
| 288 | */ |
||
| 289 | public function builder() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Map ajax response to columns definition. |
||
| 296 | * |
||
| 297 | * @param mixed $columns |
||
| 298 | * @param string $type |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | protected function mapResponseToColumns($columns, $type) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get decorated data as defined in datatables ajax response. |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | protected function getAjaxResponseData() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return \Yajra\DataTables\Html\Builder |
||
| 329 | */ |
||
| 330 | protected function getHtmlBuilder() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Add html builder callback hook. |
||
| 342 | * |
||
| 343 | * @param callable $callback |
||
| 344 | * @return $this |
||
| 345 | */ |
||
| 346 | public function withHtml(callable $callback) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Add callback before sending the response. |
||
| 355 | * |
||
| 356 | * @param callable $callback |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | public function before(callable $callback) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Add callback after the response was processed. |
||
| 368 | * |
||
| 369 | * @param callable $callback |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function response(callable $callback) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Export results to Excel file. |
||
| 381 | * |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | public function excel() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Build excel file and prepare for export. |
||
| 393 | * |
||
| 394 | * @return \Maatwebsite\Excel\Concerns\Exportable |
||
|
|
|||
| 395 | */ |
||
| 396 | protected function buildExcelFile() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get export filename. |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function getFilename() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Set export filename. |
||
| 415 | * |
||
| 416 | * @param string $filename |
||
| 417 | * @return DataTable |
||
| 418 | */ |
||
| 419 | public function setFilename($filename) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get filename for export. |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | protected function filename() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get mapped columns versus final decorated output. |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | protected function getDataForExport() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get export columns definition. |
||
| 450 | * |
||
| 451 | * @return array|string |
||
| 452 | */ |
||
| 453 | private function exportColumns() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Export results to CSV file. |
||
| 460 | * |
||
| 461 | * @return mixed |
||
| 462 | */ |
||
| 463 | public function csv() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Export results to PDF file. |
||
| 472 | * |
||
| 473 | * @return mixed |
||
| 474 | */ |
||
| 475 | public function pdf() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * PDF version of the table using print preview blade template. |
||
| 486 | * |
||
| 487 | * @return mixed |
||
| 488 | */ |
||
| 489 | public function snappyPdf() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Add basic array query scopes. |
||
| 503 | * |
||
| 504 | * @param \Yajra\DataTables\Contracts\DataTableScope $scope |
||
| 505 | * @return $this |
||
| 506 | */ |
||
| 507 | public function addScope(DataTableScope $scope) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Push multiples scopes to array query scopes. |
||
| 516 | * |
||
| 517 | * @param array $scopes |
||
| 518 | * @return $this |
||
| 519 | */ |
||
| 520 | public function addScopes(array $scopes) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Set a custom class attribute. |
||
| 529 | * |
||
| 530 | * @param mixed $key |
||
| 531 | * @param mixed|null $value |
||
| 532 | * @return $this |
||
| 533 | */ |
||
| 534 | public function with($key, $value = null) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Dynamically retrieve the value of an attribute. |
||
| 547 | * |
||
| 548 | * @param string $key |
||
| 549 | * @return mixed|null |
||
| 550 | */ |
||
| 551 | public function __get($key) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Apply query scopes. |
||
| 560 | * |
||
| 561 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
| 562 | * @return mixed |
||
| 563 | */ |
||
| 564 | protected function applyScopes($query) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Determine if the DataTable has scopes. |
||
| 575 | * |
||
| 576 | * @param array $scopes |
||
| 577 | * @param bool $validateAll |
||
| 578 | * @return bool |
||
| 579 | */ |
||
| 580 | protected function hasScopes(array $scopes, $validateAll = false) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Get default builder parameters. |
||
| 592 | * |
||
| 593 | * @return array |
||
| 594 | */ |
||
| 595 | protected function getBuilderParameters() |
||
| 599 | } |
||
| 600 |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.