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 |
||
| 16 | abstract class DataTable implements DataTableService, DataTableButtons |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var \Yajra\DataTables\Datatables |
||
| 20 | */ |
||
| 21 | protected $datatables; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \Illuminate\Contracts\View\Factory |
||
| 25 | */ |
||
| 26 | protected $viewFactory; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Datatables print preview view. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $printPreview = 'datatables::print'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * List of columns to be exported. |
||
| 37 | * |
||
| 38 | * @var string|array |
||
| 39 | */ |
||
| 40 | protected $exportColumns = '*'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * List of columns to be printed. |
||
| 44 | * |
||
| 45 | * @var string|array |
||
| 46 | */ |
||
| 47 | protected $printColumns = '*'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Query scopes. |
||
| 51 | * |
||
| 52 | * @var \Yajra\DataTables\Contracts\DataTableScopeContract[] |
||
| 53 | */ |
||
| 54 | protected $scopes = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Html builder. |
||
| 58 | * |
||
| 59 | * @var \Yajra\DataTables\Html\Builder |
||
| 60 | */ |
||
| 61 | protected $htmlBuilder; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Html builder extension callback. |
||
| 65 | * |
||
| 66 | * @var callable |
||
| 67 | */ |
||
| 68 | protected $htmlCallback; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Export filename. |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $filename = ''; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Custom attributes set on the class. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $attributes = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Callback before sending the response. |
||
| 86 | * |
||
| 87 | * @var callable |
||
| 88 | */ |
||
| 89 | protected $beforeCallback; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Callback after processing the response. |
||
| 93 | * |
||
| 94 | * @var callable |
||
| 95 | */ |
||
| 96 | protected $responseCallback; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Available button actions. When calling an action, the value will be used |
||
| 100 | * as the function name (so it should be available) |
||
| 101 | * If you want to add or disable an action, overload and modify this property |
||
| 102 | * |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $actions = ['print', 'csv', 'excel', 'pdf']; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * DataTable constructor. |
||
| 109 | * |
||
| 110 | * @param \Yajra\DataTables\Datatables $datatables |
||
| 111 | * @param \Illuminate\Contracts\View\Factory $viewFactory |
||
| 112 | */ |
||
| 113 | public function __construct(Datatables $datatables, Factory $viewFactory) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Process dataTables needed render output. |
||
| 121 | * |
||
| 122 | * @param string $view |
||
| 123 | * @param array $data |
||
| 124 | * @param array $mergeData |
||
| 125 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
| 126 | */ |
||
| 127 | public function render($view, $data = [], $mergeData = []) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get Datatables Request instance. |
||
| 146 | * |
||
| 147 | * @return \Yajra\DataTables\Request |
||
| 148 | */ |
||
| 149 | public function request() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Display ajax response. |
||
| 156 | * |
||
| 157 | * @return \Illuminate\Http\JsonResponse |
||
| 158 | */ |
||
| 159 | public function ajax() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Display printable view of datatables. |
||
| 179 | * |
||
| 180 | * @return \Illuminate\Contracts\View\View |
||
| 181 | */ |
||
| 182 | public function printPreview() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get mapped columns versus final decorated output. |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | protected function getDataForPrint() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get printable columns. |
||
| 203 | * |
||
| 204 | * @return array|string |
||
| 205 | */ |
||
| 206 | protected function printColumns() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get columns definition from html builder. |
||
| 213 | * |
||
| 214 | * @return \Illuminate\Support\Collection |
||
| 215 | */ |
||
| 216 | protected function getColumnsFromBuilder() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Optional method if you want to use html builder. |
||
| 223 | * |
||
| 224 | * @return \Yajra\DataTables\Html\Builder |
||
| 225 | */ |
||
| 226 | public function html() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get Datatables Html Builder instance. |
||
| 233 | * |
||
| 234 | * @return \Yajra\DataTables\Html\Builder |
||
| 235 | */ |
||
| 236 | public function builder() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Map ajax response to columns definition. |
||
| 243 | * |
||
| 244 | * @param mixed $columns |
||
| 245 | * @param string $type |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | protected function mapResponseToColumns($columns, $type) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get decorated data as defined in datatables ajax response. |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | protected function getAjaxResponseData() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return \Yajra\DataTables\Html\Builder |
||
| 276 | */ |
||
| 277 | protected function getHtmlBuilder() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Add html builder callback hook. |
||
| 289 | * |
||
| 290 | * @param callable $callback |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function withHtml(callable $callback) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Add callback before sending the response. |
||
| 302 | * |
||
| 303 | * @param callable $callback |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | public function before(callable $callback) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Add callback after the response was processed. |
||
| 315 | * |
||
| 316 | * @param callable $callback |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | public function response(callable $callback) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Export results to Excel file. |
||
| 328 | * |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | public function excel() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Build excel file and prepare for export. |
||
| 338 | * |
||
| 339 | * @return \Maatwebsite\Excel\Writers\LaravelExcelWriter |
||
| 340 | */ |
||
| 341 | protected function buildExcelFile() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get export filename. |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function getFilename() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Set export filename. |
||
| 365 | * |
||
| 366 | * @param string $filename |
||
| 367 | * @return DataTable |
||
| 368 | */ |
||
| 369 | public function setFilename($filename) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get filename for export. |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | protected function filename() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get mapped columns versus final decorated output. |
||
| 388 | * |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | protected function getDataForExport() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get export columns definition. |
||
| 400 | * |
||
| 401 | * @return array|string |
||
| 402 | */ |
||
| 403 | private function exportColumns() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Export results to CSV file. |
||
| 410 | * |
||
| 411 | * @return void |
||
| 412 | */ |
||
| 413 | public function csv() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Export results to PDF file. |
||
| 420 | * |
||
| 421 | * @return mixed |
||
| 422 | */ |
||
| 423 | public function pdf() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * PDF version of the table using print preview blade template. |
||
| 434 | * |
||
| 435 | * @return mixed |
||
| 436 | */ |
||
| 437 | public function snappyPdf() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Add basic array query scopes. |
||
| 453 | * |
||
| 454 | * @param \Yajra\DataTables\Contracts\DataTableScopeContract $scope |
||
| 455 | * @return $this |
||
| 456 | */ |
||
| 457 | public function addScope(DataTableScopeContract $scope) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Set a custom class attribute. |
||
| 466 | * |
||
| 467 | * @param mixed $key |
||
| 468 | * @param mixed|null $value |
||
| 469 | * @return $this |
||
| 470 | */ |
||
| 471 | public function with($key, $value = null) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Dynamically retrieve the value of an attribute. |
||
| 484 | * |
||
| 485 | * @param string $key |
||
| 486 | * @return mixed|null |
||
| 487 | */ |
||
| 488 | public function __get($key) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Apply query scopes. |
||
| 499 | * |
||
| 500 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
| 501 | * @return mixed |
||
| 502 | */ |
||
| 503 | protected function applyScopes($query) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get default builder parameters. |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | protected function getBuilderParameters() |
||
| 521 | } |
||
| 522 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.