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