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 |
||
15 | abstract class DataTable implements DataTableService, DataTableButtons |
||
16 | { |
||
17 | /** |
||
18 | * Datatables print preview view. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $printPreview = 'datatables::print'; |
||
23 | |||
24 | /** |
||
25 | * List of columns to be exported. |
||
26 | * |
||
27 | * @var string|array |
||
28 | */ |
||
29 | protected $exportColumns = '*'; |
||
30 | |||
31 | /** |
||
32 | * List of columns to be printed. |
||
33 | * |
||
34 | * @var string|array |
||
35 | */ |
||
36 | protected $printColumns = '*'; |
||
37 | |||
38 | /** |
||
39 | * Query scopes. |
||
40 | * |
||
41 | * @var \Yajra\DataTables\Contracts\DataTableScope[] |
||
42 | */ |
||
43 | protected $scopes = []; |
||
44 | |||
45 | /** |
||
46 | * Html builder. |
||
47 | * |
||
48 | * @var \Yajra\DataTables\Html\Builder |
||
49 | */ |
||
50 | protected $htmlBuilder; |
||
51 | |||
52 | /** |
||
53 | * Html builder extension callback. |
||
54 | * |
||
55 | * @var callable |
||
56 | */ |
||
57 | protected $htmlCallback; |
||
58 | |||
59 | /** |
||
60 | * Export filename. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $filename = ''; |
||
65 | |||
66 | /** |
||
67 | * Custom attributes set on the class. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $attributes = []; |
||
72 | |||
73 | /** |
||
74 | * Callback before sending the response. |
||
75 | * |
||
76 | * @var callable |
||
77 | */ |
||
78 | protected $beforeCallback; |
||
79 | |||
80 | /** |
||
81 | * Callback after processing the response. |
||
82 | * |
||
83 | * @var callable |
||
84 | */ |
||
85 | protected $responseCallback; |
||
86 | |||
87 | /** |
||
88 | * Available button actions. When calling an action, the value will be used |
||
89 | * as the function name (so it should be available) |
||
90 | * If you want to add or disable an action, overload and modify this property |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $actions = ['print', 'csv', 'excel', 'pdf']; |
||
95 | |||
96 | /** |
||
97 | * @var \Yajra\DataTables\Utilities\Request |
||
98 | */ |
||
99 | protected $request; |
||
100 | |||
101 | /** |
||
102 | * Process dataTables needed render output. |
||
103 | * |
||
104 | * @param string $view |
||
105 | * @param array $data |
||
106 | * @param array $mergeData |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function render($view, $data = [], $mergeData = []) |
||
125 | |||
126 | /** |
||
127 | * Get Datatables Request instance. |
||
128 | * |
||
129 | * @return \Yajra\DataTables\Utilities\Request |
||
130 | */ |
||
131 | public function request() |
||
135 | |||
136 | /** |
||
137 | * Display ajax response. |
||
138 | * |
||
139 | * @return \Illuminate\Http\JsonResponse |
||
140 | */ |
||
141 | public function ajax() |
||
157 | |||
158 | /** |
||
159 | * Build dataTable class. |
||
160 | * |
||
161 | * @return \Yajra\DataTables\DataTableAbstract |
||
162 | */ |
||
163 | abstract protected function dataTable(); |
||
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 \Illuminate\Support\Collection |
||
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 | * @return \Yajra\DataTables\Html\Builder |
||
264 | */ |
||
265 | protected function getHtmlBuilder() |
||
274 | |||
275 | /** |
||
276 | * Add html builder callback hook. |
||
277 | * |
||
278 | * @param callable $callback |
||
279 | * @return $this |
||
280 | */ |
||
281 | public function withHtml(callable $callback) |
||
287 | |||
288 | /** |
||
289 | * Add callback before sending the response. |
||
290 | * |
||
291 | * @param callable $callback |
||
292 | * @return $this |
||
293 | */ |
||
294 | public function before(callable $callback) |
||
300 | |||
301 | /** |
||
302 | * Add callback after the response was processed. |
||
303 | * |
||
304 | * @param callable $callback |
||
305 | * @return $this |
||
306 | */ |
||
307 | public function response(callable $callback) |
||
313 | |||
314 | /** |
||
315 | * Export results to Excel file. |
||
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | public function excel() |
||
323 | |||
324 | /** |
||
325 | * Build excel file and prepare for export. |
||
326 | * |
||
327 | * @return \Maatwebsite\Excel\Writers\LaravelExcelWriter |
||
328 | */ |
||
329 | protected function buildExcelFile() |
||
340 | |||
341 | /** |
||
342 | * Get export filename. |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | public function getFilename() |
||
350 | |||
351 | /** |
||
352 | * Set export filename. |
||
353 | * |
||
354 | * @param string $filename |
||
355 | * @return DataTable |
||
356 | */ |
||
357 | public function setFilename($filename) |
||
363 | |||
364 | /** |
||
365 | * Get filename for export. |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | protected function filename() |
||
373 | |||
374 | /** |
||
375 | * Get mapped columns versus final decorated output. |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | protected function getDataForExport() |
||
385 | |||
386 | /** |
||
387 | * Get export columns definition. |
||
388 | * |
||
389 | * @return array|string |
||
390 | */ |
||
391 | private function exportColumns() |
||
395 | |||
396 | /** |
||
397 | * Export results to CSV file. |
||
398 | * |
||
399 | * @return void |
||
400 | */ |
||
401 | public function csv() |
||
405 | |||
406 | /** |
||
407 | * Export results to PDF file. |
||
408 | * |
||
409 | * @return mixed |
||
410 | */ |
||
411 | public function pdf() |
||
419 | |||
420 | /** |
||
421 | * PDF version of the table using print preview blade template. |
||
422 | * |
||
423 | * @return mixed |
||
424 | */ |
||
425 | public function snappyPdf() |
||
438 | |||
439 | /** |
||
440 | * Add basic array query scopes. |
||
441 | * |
||
442 | * @param \Yajra\DataTables\Contracts\DataTableScope $scope |
||
443 | * @return $this |
||
444 | */ |
||
445 | public function addScope(DataTableScope $scope) |
||
451 | |||
452 | /** |
||
453 | * Set a custom class attribute. |
||
454 | * |
||
455 | * @param mixed $key |
||
456 | * @param mixed|null $value |
||
457 | * @return $this |
||
458 | */ |
||
459 | public function with($key, $value = null) |
||
469 | |||
470 | /** |
||
471 | * Dynamically retrieve the value of an attribute. |
||
472 | * |
||
473 | * @param string $key |
||
474 | * @return mixed|null |
||
475 | */ |
||
476 | public function __get($key) |
||
484 | |||
485 | /** |
||
486 | * Apply query scopes. |
||
487 | * |
||
488 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
489 | * @return mixed |
||
490 | */ |
||
491 | protected function applyScopes($query) |
||
499 | |||
500 | /** |
||
501 | * Get default builder parameters. |
||
502 | * |
||
503 | * @return array |
||
504 | */ |
||
505 | protected function getBuilderParameters() |
||
509 | } |
||
510 |