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 |
||
| 22 | abstract class DataTable implements DataTableContract, DataTableButtonsContract |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var \Yajra\Datatables\Datatables |
||
| 26 | */ |
||
| 27 | protected $datatables; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \Illuminate\Contracts\View\Factory |
||
| 31 | */ |
||
| 32 | protected $viewFactory; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Datatables print preview view. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $printPreview = 'datatables::print'; |
||
| 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\DataTableScopeContract[] |
||
| 59 | */ |
||
| 60 | protected $scopes = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Html builder. |
||
| 64 | * |
||
| 65 | * @var \Yajra\Datatables\Html\Builder |
||
| 66 | */ |
||
| 67 | protected $htmlBuilder; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Export filename. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $filename = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Custom attributes set on the class. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $attributes = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Callback before sending the response. |
||
| 85 | * |
||
| 86 | * @var callable |
||
| 87 | */ |
||
| 88 | protected $beforeCallback; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Callback after processing the response. |
||
| 92 | * |
||
| 93 | * @var callable |
||
| 94 | */ |
||
| 95 | protected $responseCallback; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * DataTable constructor. |
||
| 99 | * |
||
| 100 | * @param \Yajra\Datatables\Datatables $datatables |
||
| 101 | * @param \Illuminate\Contracts\View\Factory $viewFactory |
||
| 102 | */ |
||
| 103 | public function __construct(Datatables $datatables, Factory $viewFactory) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Process dataTables needed render output. |
||
| 111 | * |
||
| 112 | * @param string $view |
||
| 113 | * @param array $data |
||
| 114 | * @param array $mergeData |
||
| 115 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
| 116 | */ |
||
| 117 | public function render($view, $data = [], $mergeData = []) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get Datatables Request instance. |
||
| 136 | * |
||
| 137 | * @return \Yajra\Datatables\Request |
||
| 138 | */ |
||
| 139 | public function request() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Display ajax response. |
||
| 146 | * |
||
| 147 | * @return \Illuminate\Http\JsonResponse |
||
| 148 | */ |
||
| 149 | public function ajax() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Display printable view of datatables. |
||
| 169 | * |
||
| 170 | * @return \Illuminate\Contracts\View\View |
||
| 171 | */ |
||
| 172 | public function printPreview() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get mapped columns versus final decorated output. |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | protected function getDataForPrint() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get printable columns. |
||
| 193 | * |
||
| 194 | * @return array|string |
||
| 195 | */ |
||
| 196 | protected function printColumns() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get columns definition from html builder. |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | protected function getColumnsFromBuilder() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Optional method if you want to use html builder. |
||
| 213 | * |
||
| 214 | * @return \Yajra\Datatables\Html\Builder |
||
| 215 | */ |
||
| 216 | public function html() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get Datatables Html Builder instance. |
||
| 223 | * |
||
| 224 | * @return \Yajra\Datatables\Html\Builder |
||
| 225 | */ |
||
| 226 | public function builder() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Map ajax response to columns definition. |
||
| 233 | * |
||
| 234 | * @param mixed $columns |
||
| 235 | * @param string $type |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | protected function mapResponseToColumns($columns, $type) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get decorated data as defined in datatables ajax response. |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | protected function getAjaxResponseData() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Add callback before sending the response. |
||
| 266 | * |
||
| 267 | * @param callable $callback |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | public function before(callable $callback) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Add callback after the response was processed. |
||
| 279 | * |
||
| 280 | * @param callable $callback |
||
| 281 | * @return $this |
||
| 282 | */ |
||
| 283 | public function response(callable $callback) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Export results to Excel file. |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | public function excel() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Build excel file and prepare for export. |
||
| 302 | * |
||
| 303 | * @return \Maatwebsite\Excel\Writers\LaravelExcelWriter |
||
| 304 | */ |
||
| 305 | protected function buildExcelFile() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get export filename. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function getFilename() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Set export filename. |
||
| 329 | * |
||
| 330 | * @param string $filename |
||
| 331 | * @return DataTable |
||
| 332 | */ |
||
| 333 | public function setFilename($filename) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get filename for export. |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | protected function filename() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get mapped columns versus final decorated output. |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | protected function getDataForExport() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Get export columns definition. |
||
| 364 | * |
||
| 365 | * @return array|string |
||
| 366 | */ |
||
| 367 | private function exportColumns() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Export results to CSV file. |
||
| 374 | * |
||
| 375 | * @return void |
||
| 376 | */ |
||
| 377 | public function csv() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Export results to PDF file. |
||
| 384 | * |
||
| 385 | * @return mixed |
||
| 386 | */ |
||
| 387 | public function pdf() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * PDF version of the table using print preview blade template. |
||
| 398 | * |
||
| 399 | * @return mixed |
||
| 400 | */ |
||
| 401 | public function snappyPdf() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Add basic array query scopes. |
||
| 424 | * |
||
| 425 | * @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope |
||
| 426 | * @return $this |
||
| 427 | */ |
||
| 428 | public function addScope(DataTableScopeContract $scope) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Set a custom class attribute. |
||
| 437 | * |
||
| 438 | * @param mixed $key |
||
| 439 | * @param mixed|null $value |
||
| 440 | * @return $this |
||
| 441 | */ |
||
| 442 | public function with($key, $value = null) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Dynamically retrieve the value of an attribute. |
||
| 455 | * |
||
| 456 | * @param string $key |
||
| 457 | * @return mixed|null |
||
| 458 | */ |
||
| 459 | public function __get($key) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Apply query scopes. |
||
| 470 | * |
||
| 471 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
| 472 | * @return mixed |
||
| 473 | */ |
||
| 474 | protected function applyScopes($query) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Get default builder parameters. |
||
| 485 | * |
||
| 486 | * @return array |
||
| 487 | */ |
||
| 488 | protected function getBuilderParameters() |
||
| 501 | } |
||
| 502 |
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.