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 | * Html builder extension callback. |
||
71 | * |
||
72 | * @var callable |
||
73 | */ |
||
74 | protected $htmlCallback; |
||
75 | |||
76 | /** |
||
77 | * Export filename. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $filename = ''; |
||
82 | |||
83 | /** |
||
84 | * Custom attributes set on the class. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $attributes = []; |
||
89 | |||
90 | /** |
||
91 | * Callback before sending the response. |
||
92 | * |
||
93 | * @var callable |
||
94 | */ |
||
95 | protected $beforeCallback; |
||
96 | |||
97 | /** |
||
98 | * Callback after processing the response. |
||
99 | * |
||
100 | * @var callable |
||
101 | */ |
||
102 | protected $responseCallback; |
||
103 | |||
104 | /** |
||
105 | * DataTable constructor. |
||
106 | * |
||
107 | * @param \Yajra\Datatables\Datatables $datatables |
||
108 | * @param \Illuminate\Contracts\View\Factory $viewFactory |
||
109 | */ |
||
110 | public function __construct(Datatables $datatables, Factory $viewFactory) |
||
115 | |||
116 | /** |
||
117 | * Process dataTables needed render output. |
||
118 | * |
||
119 | * @param string $view |
||
120 | * @param array $data |
||
121 | * @param array $mergeData |
||
122 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
123 | */ |
||
124 | public function render($view, $data = [], $mergeData = []) |
||
140 | |||
141 | /** |
||
142 | * Get Datatables Request instance. |
||
143 | * |
||
144 | * @return \Yajra\Datatables\Request |
||
145 | */ |
||
146 | public function request() |
||
150 | |||
151 | /** |
||
152 | * Display ajax response. |
||
153 | * |
||
154 | * @return \Illuminate\Http\JsonResponse |
||
155 | */ |
||
156 | public function ajax() |
||
173 | |||
174 | /** |
||
175 | * Display printable view of datatables. |
||
176 | * |
||
177 | * @return \Illuminate\Contracts\View\View |
||
178 | */ |
||
179 | public function printPreview() |
||
185 | |||
186 | /** |
||
187 | * Get mapped columns versus final decorated output. |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | protected function getDataForPrint() |
||
197 | |||
198 | /** |
||
199 | * Get printable columns. |
||
200 | * |
||
201 | * @return array|string |
||
202 | */ |
||
203 | protected function printColumns() |
||
207 | |||
208 | /** |
||
209 | * Get columns definition from html builder. |
||
210 | * |
||
211 | * @return \Illuminate\Support\Collection |
||
212 | */ |
||
213 | protected function getColumnsFromBuilder() |
||
217 | |||
218 | /** |
||
219 | * Optional method if you want to use html builder. |
||
220 | * |
||
221 | * @return \Yajra\Datatables\Html\Builder |
||
222 | */ |
||
223 | public function html() |
||
227 | |||
228 | /** |
||
229 | * Get Datatables Html Builder instance. |
||
230 | * |
||
231 | * @return \Yajra\Datatables\Html\Builder |
||
232 | */ |
||
233 | public function builder() |
||
237 | |||
238 | /** |
||
239 | * Map ajax response to columns definition. |
||
240 | * |
||
241 | * @param mixed $columns |
||
242 | * @param string $type |
||
243 | * @return array |
||
244 | */ |
||
245 | protected function mapResponseToColumns($columns, $type) |
||
255 | |||
256 | /** |
||
257 | * Get decorated data as defined in datatables ajax response. |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | protected function getAjaxResponseData() |
||
270 | |||
271 | /** |
||
272 | * @return \Yajra\Datatables\Html\Builder |
||
273 | */ |
||
274 | protected function getHtmlBuilder() |
||
283 | |||
284 | /** |
||
285 | * Add html builder callback hook. |
||
286 | * |
||
287 | * @param callable $callback |
||
288 | * @return $this |
||
289 | */ |
||
290 | public function withHtml(callable $callback) |
||
296 | |||
297 | /** |
||
298 | * Add callback before sending the response. |
||
299 | * |
||
300 | * @param callable $callback |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function before(callable $callback) |
||
309 | |||
310 | /** |
||
311 | * Add callback after the response was processed. |
||
312 | * |
||
313 | * @param callable $callback |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function response(callable $callback) |
||
322 | |||
323 | /** |
||
324 | * Export results to Excel file. |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | public function excel() |
||
332 | |||
333 | /** |
||
334 | * Build excel file and prepare for export. |
||
335 | * |
||
336 | * @return \Maatwebsite\Excel\Writers\LaravelExcelWriter |
||
337 | */ |
||
338 | protected function buildExcelFile() |
||
349 | |||
350 | /** |
||
351 | * Get export filename. |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | public function getFilename() |
||
359 | |||
360 | /** |
||
361 | * Set export filename. |
||
362 | * |
||
363 | * @param string $filename |
||
364 | * @return DataTable |
||
365 | */ |
||
366 | public function setFilename($filename) |
||
372 | |||
373 | /** |
||
374 | * Get filename for export. |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | protected function filename() |
||
382 | |||
383 | /** |
||
384 | * Get mapped columns versus final decorated output. |
||
385 | * |
||
386 | * @return array |
||
387 | */ |
||
388 | protected function getDataForExport() |
||
394 | |||
395 | /** |
||
396 | * Get export columns definition. |
||
397 | * |
||
398 | * @return array|string |
||
399 | */ |
||
400 | private function exportColumns() |
||
404 | |||
405 | /** |
||
406 | * Export results to CSV file. |
||
407 | * |
||
408 | * @return void |
||
409 | */ |
||
410 | public function csv() |
||
414 | |||
415 | /** |
||
416 | * Export results to PDF file. |
||
417 | * |
||
418 | * @return mixed |
||
419 | */ |
||
420 | public function pdf() |
||
428 | |||
429 | /** |
||
430 | * PDF version of the table using print preview blade template. |
||
431 | * |
||
432 | * @return mixed |
||
433 | */ |
||
434 | public function snappyPdf() |
||
454 | |||
455 | /** |
||
456 | * Add basic array query scopes. |
||
457 | * |
||
458 | * @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope |
||
459 | * @return $this |
||
460 | */ |
||
461 | public function addScope(DataTableScopeContract $scope) |
||
467 | |||
468 | /** |
||
469 | * Set a custom class attribute. |
||
470 | * |
||
471 | * @param mixed $key |
||
472 | * @param mixed|null $value |
||
473 | * @return $this |
||
474 | */ |
||
475 | public function with($key, $value = null) |
||
485 | |||
486 | /** |
||
487 | * Dynamically retrieve the value of an attribute. |
||
488 | * |
||
489 | * @param string $key |
||
490 | * @return mixed|null |
||
491 | */ |
||
492 | public function __get($key) |
||
500 | |||
501 | /** |
||
502 | * Apply query scopes. |
||
503 | * |
||
504 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
505 | * @return mixed |
||
506 | */ |
||
507 | protected function applyScopes($query) |
||
515 | |||
516 | /** |
||
517 | * Get default builder parameters. |
||
518 | * |
||
519 | * @return array |
||
520 | */ |
||
521 | protected function getBuilderParameters() |
||
534 | } |
||
535 |
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.