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 |
||
| 14 | abstract class DataTable implements DataTableContract, DataTableButtonsContract |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var \Yajra\Datatables\Datatables |
||
| 18 | */ |
||
| 19 | protected $datatables; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var \Illuminate\Contracts\View\Factory |
||
| 23 | */ |
||
| 24 | protected $viewFactory; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Datatables print preview view. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $printPreview; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * List of columns to be exported. |
||
| 35 | * |
||
| 36 | * @var string|array |
||
| 37 | */ |
||
| 38 | protected $exportColumns = '*'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * List of columns to be printed. |
||
| 42 | * |
||
| 43 | * @var string|array |
||
| 44 | */ |
||
| 45 | protected $printColumns = '*'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Query scopes. |
||
| 49 | * |
||
| 50 | * @var \Yajra\Datatables\Contracts\DataTableScopeContract[] |
||
| 51 | */ |
||
| 52 | protected $scopes = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param \Yajra\Datatables\Datatables $datatables |
||
| 56 | * @param \Illuminate\Contracts\View\Factory $viewFactory |
||
| 57 | */ |
||
| 58 | public function __construct(Datatables $datatables, Factory $viewFactory) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Render view. |
||
| 66 | * |
||
| 67 | * @param $view |
||
| 68 | * @param array $data |
||
| 69 | * @param array $mergeData |
||
| 70 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
| 71 | */ |
||
| 72 | public function render($view, $data = [], $mergeData = []) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get Datatables Request instance. |
||
| 98 | * |
||
| 99 | * @return \Yajra\Datatables\Request |
||
| 100 | */ |
||
| 101 | public function request() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Export results to Excel file. |
||
| 108 | * |
||
| 109 | * @return mixed |
||
| 110 | */ |
||
| 111 | public function excel() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Build excel file and prepare for export. |
||
| 118 | * |
||
| 119 | * @return mixed |
||
| 120 | */ |
||
| 121 | protected function buildExcelFile() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get filename for export. |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | protected function filename() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get mapped columns versus final decorated output. |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | protected function getDataForExport() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get decorated data as defined in datatables ajax response. |
||
| 158 | * |
||
| 159 | * @return mixed |
||
| 160 | */ |
||
| 161 | protected function getAjaxResponseData() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get export columns definition. |
||
| 173 | * |
||
| 174 | * @return array|string |
||
| 175 | */ |
||
| 176 | private function exportColumns() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get columns definition from html builder. |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | protected function getColumnsFromBuilder() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Optional method if you want to use html builder. |
||
| 193 | * |
||
| 194 | * @return \Yajra\Datatables\Html\Builder |
||
| 195 | */ |
||
| 196 | public function html() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get Datatables Html Builder instance. |
||
| 203 | * |
||
| 204 | * @return \Yajra\Datatables\Html\Builder |
||
| 205 | */ |
||
| 206 | public function builder() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param array $row |
||
| 213 | * @param array|\Illuminate\Support\Collection $columns |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | protected function buildExportColumn(array $row, $columns) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Build printable and exportable column. |
||
| 223 | * |
||
| 224 | * @param array $row |
||
| 225 | * @param array|\Illuminate\Support\Collection $columns |
||
| 226 | * @param string $type |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | protected function buildColumn(array $row, $columns, $type) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Build column from collection. |
||
| 240 | * |
||
| 241 | * @param array $row |
||
| 242 | * @param \Illuminate\Support\Collection $columns |
||
| 243 | * @param string $type |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | protected function buildColumnByCollection(array $row, Collection $columns, $type) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Export results to CSV file. |
||
| 262 | * |
||
| 263 | * @return mixed |
||
| 264 | */ |
||
| 265 | public function csv() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Export results to PDF file. |
||
| 272 | * |
||
| 273 | * @return mixed |
||
| 274 | */ |
||
| 275 | public function pdf() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Display printable view of datatables. |
||
| 282 | * |
||
| 283 | * @return \Illuminate\Contracts\View\View |
||
| 284 | */ |
||
| 285 | public function printPreview() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get mapped columns versus final decorated output. |
||
| 295 | * |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | protected function getDataForPrint() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get printable columns. |
||
| 311 | * |
||
| 312 | * @return array|string |
||
| 313 | */ |
||
| 314 | protected function printColumns() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Build printable column. |
||
| 321 | * |
||
| 322 | * @param array $row |
||
| 323 | * @param array|\Illuminate\Support\Collection $columns |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | protected function buildPrintColumn(array $row, $columns) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Add basic array query scopes. |
||
| 333 | * |
||
| 334 | * @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function addScope(DataTableScopeContract $scope) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Apply query scopes. |
||
| 346 | * |
||
| 347 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | protected function applyScopes($query) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get default builder parameters. |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | protected function getBuilderParameters() |
||
| 385 | } |
||
| 386 |