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 |
||
| 20 | abstract class DataTable implements DataTableContract, DataTableButtonsContract |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var \Yajra\Datatables\Datatables |
||
| 24 | */ |
||
| 25 | protected $datatables; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \Illuminate\Contracts\View\Factory |
||
| 29 | */ |
||
| 30 | protected $viewFactory; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Datatables print preview view. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $printPreview = 'datatables::print'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * List of columns to be exported. |
||
| 41 | * |
||
| 42 | * @var string|array |
||
| 43 | */ |
||
| 44 | protected $exportColumns = '*'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * List of columns to be printed. |
||
| 48 | * |
||
| 49 | * @var string|array |
||
| 50 | */ |
||
| 51 | protected $printColumns = '*'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Query scopes. |
||
| 55 | * |
||
| 56 | * @var \Yajra\Datatables\Contracts\DataTableScopeContract[] |
||
| 57 | */ |
||
| 58 | protected $scopes = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Html builder. |
||
| 62 | * |
||
| 63 | * @var \Yajra\Datatables\Html\Builder |
||
| 64 | */ |
||
| 65 | protected $htmlBuilder; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Export filename. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $filename = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Custom attributes set on the class. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $attributes = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * DataTable constructor. |
||
| 83 | * |
||
| 84 | * @param \Yajra\Datatables\Datatables $datatables |
||
| 85 | * @param \Illuminate\Contracts\View\Factory $viewFactory |
||
| 86 | */ |
||
| 87 | public function __construct(Datatables $datatables, Factory $viewFactory) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Process dataTables needed render output. |
||
| 95 | * |
||
| 96 | * @param string $view |
||
| 97 | * @param array $data |
||
| 98 | * @param array $mergeData |
||
| 99 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
| 100 | */ |
||
| 101 | public function render($view, $data = [], $mergeData = []) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get Datatables Request instance. |
||
| 120 | * |
||
| 121 | * @return \Yajra\Datatables\Request |
||
| 122 | */ |
||
| 123 | public function request() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Display printable view of datatables. |
||
| 130 | * |
||
| 131 | * @return \Illuminate\Contracts\View\View |
||
| 132 | */ |
||
| 133 | public function printPreview() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get mapped columns versus final decorated output. |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | protected function getDataForPrint() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get printable columns. |
||
| 154 | * |
||
| 155 | * @return array|string |
||
| 156 | */ |
||
| 157 | protected function printColumns() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get columns definition from html builder. |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | protected function getColumnsFromBuilder() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Display ajax response. |
||
| 174 | * |
||
| 175 | * @return \Illuminate\Http\JsonResponse |
||
| 176 | */ |
||
| 177 | public function ajax() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Optional method if you want to use html builder. |
||
| 184 | * |
||
| 185 | * @return \Yajra\Datatables\Html\Builder |
||
| 186 | */ |
||
| 187 | public function html() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get Datatables Html Builder instance. |
||
| 194 | * |
||
| 195 | * @return \Yajra\Datatables\Html\Builder |
||
| 196 | */ |
||
| 197 | public function builder() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Map ajax response to columns definition. |
||
| 204 | * |
||
| 205 | * @param mixed $columns |
||
| 206 | * @param string $type |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | protected function mapResponseToColumns($columns, $type) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get decorated data as defined in datatables ajax response. |
||
| 222 | * |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | protected function getAjaxResponseData() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Export results to Excel file. |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public function excel() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Build excel file and prepare for export. |
||
| 247 | * |
||
| 248 | * @return \Maatwebsite\Excel\Writers\LaravelExcelWriter |
||
| 249 | */ |
||
| 250 | protected function buildExcelFile() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get export filename. |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function getFilename() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set export filename. |
||
| 274 | * |
||
| 275 | * @param string $filename |
||
| 276 | * @return DataTable |
||
| 277 | */ |
||
| 278 | public function setFilename($filename) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get filename for export. |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | protected function filename() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get mapped columns versus final decorated output. |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | protected function getDataForExport() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get export columns definition. |
||
| 309 | * |
||
| 310 | * @return array|string |
||
| 311 | */ |
||
| 312 | private function exportColumns() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Export results to CSV file. |
||
| 319 | * |
||
| 320 | * @return void |
||
| 321 | */ |
||
| 322 | public function csv() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Export results to PDF file. |
||
| 329 | * |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | public function pdf() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * PDF version of the table using print preview blade template. |
||
| 343 | * |
||
| 344 | * @return mixed |
||
| 345 | */ |
||
| 346 | public function snappyPdf() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Add basic array query scopes. |
||
| 369 | * |
||
| 370 | * @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope |
||
| 371 | * @return $this |
||
| 372 | */ |
||
| 373 | public function addScope(DataTableScopeContract $scope) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Set a custom class attribute. |
||
| 382 | * |
||
| 383 | * @param mixed $key |
||
| 384 | * @param mixed|null $value |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | public function with($key, $value = null) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Dynamically retrieve the value of an attribute. |
||
| 400 | * |
||
| 401 | * @param string $key |
||
| 402 | * @return mixed|null |
||
| 403 | */ |
||
| 404 | public function __get($key) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Apply query scopes. |
||
| 415 | * |
||
| 416 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
| 417 | * @return mixed |
||
| 418 | */ |
||
| 419 | protected function applyScopes($query) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Get default builder parameters. |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | protected function getBuilderParameters() |
||
| 446 | } |
||
| 447 |
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.