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 |
||
| 21 | abstract class DataTable implements DataTableContract, DataTableButtonsContract |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var \Yajra\Datatables\Datatables |
||
| 25 | */ |
||
| 26 | protected $datatables; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var \Illuminate\Contracts\View\Factory |
||
| 30 | */ |
||
| 31 | protected $viewFactory; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Datatables print preview view. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $printPreview = 'datatables::print'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * List of columns to be exported. |
||
| 42 | * |
||
| 43 | * @var string|array |
||
| 44 | */ |
||
| 45 | protected $exportColumns = '*'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * List of columns to be printed. |
||
| 49 | * |
||
| 50 | * @var string|array |
||
| 51 | */ |
||
| 52 | protected $printColumns = '*'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Query scopes. |
||
| 56 | * |
||
| 57 | * @var \Yajra\Datatables\Contracts\DataTableScopeContract[] |
||
| 58 | */ |
||
| 59 | protected $scopes = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Html builder. |
||
| 63 | * |
||
| 64 | * @var \Yajra\Datatables\Html\Builder |
||
| 65 | */ |
||
| 66 | protected $htmlBuilder; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Export filename. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $filename = ''; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Custom attributes set on the class. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $attributes = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Callback before sending the response. |
||
| 84 | * |
||
| 85 | * @var callable |
||
| 86 | */ |
||
| 87 | protected $beforeCallback; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Callback after processing the response. |
||
| 91 | * |
||
| 92 | * @var callable |
||
| 93 | */ |
||
| 94 | protected $afterCallback; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * DataTable constructor. |
||
| 98 | * |
||
| 99 | * @param \Yajra\Datatables\Datatables $datatables |
||
| 100 | * @param \Illuminate\Contracts\View\Factory $viewFactory |
||
| 101 | */ |
||
| 102 | public function __construct(Datatables $datatables, Factory $viewFactory) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Process dataTables needed render output. |
||
| 110 | * |
||
| 111 | * @param string $view |
||
| 112 | * @param array $data |
||
| 113 | * @param array $mergeData |
||
| 114 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
| 115 | */ |
||
| 116 | public function render($view, $data = [], $mergeData = []) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get Datatables Request instance. |
||
| 135 | * |
||
| 136 | * @return \Yajra\Datatables\Request |
||
| 137 | */ |
||
| 138 | public function request() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Display ajax response. |
||
| 145 | * |
||
| 146 | * @return \Illuminate\Http\JsonResponse |
||
| 147 | */ |
||
| 148 | public function ajax() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Display printable view of datatables. |
||
| 167 | * |
||
| 168 | * @return \Illuminate\Contracts\View\View |
||
| 169 | */ |
||
| 170 | public function printPreview() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get mapped columns versus final decorated output. |
||
| 179 | * |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | protected function getDataForPrint() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get printable columns. |
||
| 191 | * |
||
| 192 | * @return array|string |
||
| 193 | */ |
||
| 194 | protected function printColumns() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get columns definition from html builder. |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | protected function getColumnsFromBuilder() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Optional method if you want to use html builder. |
||
| 211 | * |
||
| 212 | * @return \Yajra\Datatables\Html\Builder |
||
| 213 | */ |
||
| 214 | public function html() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get Datatables Html Builder instance. |
||
| 221 | * |
||
| 222 | * @return \Yajra\Datatables\Html\Builder |
||
| 223 | */ |
||
| 224 | public function builder() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Map ajax response to columns definition. |
||
| 231 | * |
||
| 232 | * @param mixed $columns |
||
| 233 | * @param string $type |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | protected function mapResponseToColumns($columns, $type) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get decorated data as defined in datatables ajax response. |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | protected function getAjaxResponseData() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Add callback before sending the response. |
||
| 264 | * |
||
| 265 | * @param callable $callback |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function before(callable $callback) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Add callback after the response was processed. |
||
| 277 | * |
||
| 278 | * @param callable $callback |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | public function after(callable $callback) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Export results to Excel file. |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | public function excel() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Build excel file and prepare for export. |
||
| 300 | * |
||
| 301 | * @return \Maatwebsite\Excel\Writers\LaravelExcelWriter |
||
| 302 | */ |
||
| 303 | protected function buildExcelFile() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get export filename. |
||
| 317 | * |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public function getFilename() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set export filename. |
||
| 327 | * |
||
| 328 | * @param string $filename |
||
| 329 | * @return DataTable |
||
| 330 | */ |
||
| 331 | public function setFilename($filename) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get filename for export. |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | protected function filename() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get mapped columns versus final decorated output. |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | protected function getDataForExport() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get export columns definition. |
||
| 362 | * |
||
| 363 | * @return array|string |
||
| 364 | */ |
||
| 365 | private function exportColumns() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Export results to CSV file. |
||
| 372 | * |
||
| 373 | * @return void |
||
| 374 | */ |
||
| 375 | public function csv() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Export results to PDF file. |
||
| 382 | * |
||
| 383 | * @return mixed |
||
| 384 | */ |
||
| 385 | public function pdf() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * PDF version of the table using print preview blade template. |
||
| 396 | * |
||
| 397 | * @return mixed |
||
| 398 | */ |
||
| 399 | public function snappyPdf() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Add basic array query scopes. |
||
| 422 | * |
||
| 423 | * @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope |
||
| 424 | * @return $this |
||
| 425 | */ |
||
| 426 | public function addScope(DataTableScopeContract $scope) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Set a custom class attribute. |
||
| 435 | * |
||
| 436 | * @param mixed $key |
||
| 437 | * @param mixed|null $value |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function with($key, $value = null) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Dynamically retrieve the value of an attribute. |
||
| 453 | * |
||
| 454 | * @param string $key |
||
| 455 | * @return mixed|null |
||
| 456 | */ |
||
| 457 | public function __get($key) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Apply query scopes. |
||
| 468 | * |
||
| 469 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
| 470 | * @return mixed |
||
| 471 | */ |
||
| 472 | protected function applyScopes($query) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get default builder parameters. |
||
| 483 | * |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | protected function getBuilderParameters() |
||
| 499 | } |
||
| 500 |
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.