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 |
||
| 12 | abstract class DataTable implements DataTableButtons |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * DataTables print preview view. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $printPreview = 'datatables::print'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Name of the dataTable variable. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $dataTableVariable = 'dataTable'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * List of columns to be excluded from export. |
||
| 30 | * |
||
| 31 | * @var string|array |
||
| 32 | */ |
||
| 33 | protected $excludeFromExport = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * List of columns to be excluded from printing. |
||
| 37 | * |
||
| 38 | * @var string|array |
||
| 39 | */ |
||
| 40 | protected $excludeFromPrint = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * List of columns to be exported. |
||
| 44 | * |
||
| 45 | * @var string|array |
||
| 46 | */ |
||
| 47 | protected $exportColumns = '*'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * List of columns to be printed. |
||
| 51 | * |
||
| 52 | * @var string|array |
||
| 53 | */ |
||
| 54 | protected $printColumns = '*'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Query scopes. |
||
| 58 | * |
||
| 59 | * @var \Yajra\DataTables\Contracts\DataTableScope[] |
||
| 60 | */ |
||
| 61 | protected $scopes = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Html builder. |
||
| 65 | * |
||
| 66 | * @var \Yajra\DataTables\Html\Builder |
||
| 67 | */ |
||
| 68 | protected $htmlBuilder; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Html builder extension callback. |
||
| 72 | * |
||
| 73 | * @var callable |
||
| 74 | */ |
||
| 75 | protected $htmlCallback; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Export filename. |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $filename = ''; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Custom attributes set on the class. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $attributes = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Callback before sending the response. |
||
| 93 | * |
||
| 94 | * @var callable |
||
| 95 | */ |
||
| 96 | protected $beforeCallback; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Callback after processing the response. |
||
| 100 | * |
||
| 101 | * @var callable |
||
| 102 | */ |
||
| 103 | protected $responseCallback; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Available button actions. When calling an action, the value will be used |
||
| 107 | * as the function name (so it should be available) |
||
| 108 | * If you want to add or disable an action, overload and modify this property. |
||
| 109 | * |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | protected $actions = ['print', 'csv', 'excel', 'pdf']; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var \Yajra\DataTables\Utilities\Request |
||
| 116 | */ |
||
| 117 | protected $request; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Export class handler. |
||
| 121 | * |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | protected $exportClass = DataTablesExportHandler::class; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * CSV export type writer. |
||
| 128 | * |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | protected $csvWriter = 'Csv'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Excel export type writer. |
||
| 135 | * |
||
| 136 | * @var string |
||
| 137 | */ |
||
| 138 | protected $excelWriter = 'Xlsx'; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * PDF export type writer. |
||
| 142 | * |
||
| 143 | * @var string |
||
| 144 | */ |
||
| 145 | protected $pdfWriter = 'Dompdf'; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Process dataTables needed render output. |
||
| 149 | * |
||
| 150 | * @param string $view |
||
| 151 | * @param array $data |
||
| 152 | * @param array $mergeData |
||
| 153 | * @return mixed |
||
| 154 | */ |
||
| 155 | public function render($view, $data = [], $mergeData = []) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get DataTables Request instance. |
||
| 174 | * |
||
| 175 | * @return \Yajra\DataTables\Utilities\Request |
||
| 176 | */ |
||
| 177 | public function request() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Display ajax response. |
||
| 184 | * |
||
| 185 | * @return \Illuminate\Http\JsonResponse |
||
| 186 | */ |
||
| 187 | public function ajax() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Display printable view of datatables. |
||
| 213 | * |
||
| 214 | * @return \Illuminate\Contracts\View\View |
||
| 215 | */ |
||
| 216 | public function printPreview() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get mapped columns versus final decorated output. |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | protected function getDataForPrint() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get printable columns. |
||
| 237 | * |
||
| 238 | * @return array|string |
||
| 239 | */ |
||
| 240 | protected function printColumns() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get filtered print columns definition from html builder. |
||
| 247 | * |
||
| 248 | * @return \Illuminate\Support\Collection |
||
| 249 | */ |
||
| 250 | protected function getPrintColumnsFromBuilder() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get filtered export columns definition from html builder. |
||
| 257 | * |
||
| 258 | * @return \Illuminate\Support\Collection |
||
| 259 | */ |
||
| 260 | protected function getExportColumnsFromBuilder() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get columns definition from html builder. |
||
| 267 | * |
||
| 268 | * @return \Illuminate\Support\Collection |
||
| 269 | */ |
||
| 270 | protected function getColumnsFromBuilder() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Optional method if you want to use html builder. |
||
| 277 | * |
||
| 278 | * @return \Yajra\DataTables\Html\Builder |
||
| 279 | */ |
||
| 280 | public function html() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get DataTables Html Builder instance. |
||
| 287 | * |
||
| 288 | * @return \Yajra\DataTables\Html\Builder |
||
| 289 | */ |
||
| 290 | public function builder() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Map ajax response to columns definition. |
||
| 297 | * |
||
| 298 | * @param mixed $columns |
||
| 299 | * @param string $type |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | protected function mapResponseToColumns($columns, $type) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get decorated data as defined in datatables ajax response. |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | protected function getAjaxResponseData() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return \Yajra\DataTables\Html\Builder |
||
| 328 | */ |
||
| 329 | protected function getHtmlBuilder() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Add html builder callback hook. |
||
| 341 | * |
||
| 342 | * @param callable $callback |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function withHtml(callable $callback) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Add callback before sending the response. |
||
| 354 | * |
||
| 355 | * @param callable $callback |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function before(callable $callback) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Add callback after the response was processed. |
||
| 367 | * |
||
| 368 | * @param callable $callback |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function response(callable $callback) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Export results to Excel file. |
||
| 380 | * |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | public function excel() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Build excel file and prepare for export. |
||
| 392 | * |
||
| 393 | * @return \Maatwebsite\Excel\Concerns\Exportable |
||
|
|
|||
| 394 | */ |
||
| 395 | protected function buildExcelFile() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get export filename. |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function getFilename() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Set export filename. |
||
| 414 | * |
||
| 415 | * @param string $filename |
||
| 416 | * @return DataTable |
||
| 417 | */ |
||
| 418 | public function setFilename($filename) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get filename for export. |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | protected function filename() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get mapped columns versus final decorated output. |
||
| 437 | * |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | protected function getDataForExport() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get export columns definition. |
||
| 449 | * |
||
| 450 | * @return array|string |
||
| 451 | */ |
||
| 452 | private function exportColumns() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Convert array to collection of Column class. |
||
| 459 | * |
||
| 460 | * @param array $columns |
||
| 461 | * @return Collection |
||
| 462 | */ |
||
| 463 | private function toColumnsCollection(array $columns) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Export results to CSV file. |
||
| 483 | * |
||
| 484 | * @return mixed |
||
| 485 | */ |
||
| 486 | public function csv() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Export results to PDF file. |
||
| 495 | * |
||
| 496 | * @return mixed |
||
| 497 | */ |
||
| 498 | public function pdf() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * PDF version of the table using print preview blade template. |
||
| 509 | * |
||
| 510 | * @return mixed |
||
| 511 | */ |
||
| 512 | public function snappyPdf() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Add basic array query scopes. |
||
| 526 | * |
||
| 527 | * @param \Yajra\DataTables\Contracts\DataTableScope $scope |
||
| 528 | * @return $this |
||
| 529 | */ |
||
| 530 | public function addScope(DataTableScope $scope) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Push multiples scopes to array query scopes. |
||
| 539 | * |
||
| 540 | * @param array $scopes |
||
| 541 | * @return $this |
||
| 542 | */ |
||
| 543 | public function addScopes(array $scopes) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Set a custom class attribute. |
||
| 552 | * |
||
| 553 | * @param mixed $key |
||
| 554 | * @param mixed|null $value |
||
| 555 | * @return $this |
||
| 556 | */ |
||
| 557 | public function with($key, $value = null) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Dynamically retrieve the value of an attribute. |
||
| 570 | * |
||
| 571 | * @param string $key |
||
| 572 | * @return mixed|null |
||
| 573 | */ |
||
| 574 | public function __get($key) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Apply query scopes. |
||
| 583 | * |
||
| 584 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
| 585 | * @return mixed |
||
| 586 | */ |
||
| 587 | protected function applyScopes($query) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Determine if the DataTable has scopes. |
||
| 598 | * |
||
| 599 | * @param array $scopes |
||
| 600 | * @param bool $validateAll |
||
| 601 | * @return bool |
||
| 602 | */ |
||
| 603 | protected function hasScopes(array $scopes, $validateAll = false) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Get default builder parameters. |
||
| 614 | * |
||
| 615 | * @return array |
||
| 616 | */ |
||
| 617 | protected function getBuilderParameters() |
||
| 621 | } |
||
| 622 |
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.