Complex classes like DataTableAbstract 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 DataTableAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | abstract class DataTableAbstract implements DataTable, Arrayable, Jsonable |
||
24 | { |
||
25 | use Macroable; |
||
26 | |||
27 | /** |
||
28 | * DataTables Request object. |
||
29 | * |
||
30 | * @var \Yajra\DataTables\Utilities\Request |
||
31 | */ |
||
32 | public $request; |
||
33 | |||
34 | /** |
||
35 | * @var \Illuminate\Contracts\Logging\Log |
||
36 | */ |
||
37 | protected $logger; |
||
38 | |||
39 | /** |
||
40 | * Array of result columns/fields. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $columns = []; |
||
45 | |||
46 | /** |
||
47 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $columnDef = [ |
||
52 | 'index' => false, |
||
53 | 'append' => [], |
||
54 | 'edit' => [], |
||
55 | 'filter' => [], |
||
56 | 'order' => [], |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * Extra/Added columns. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $extraColumns = []; |
||
65 | |||
66 | /** |
||
67 | * Total records. |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $totalRecords = 0; |
||
72 | |||
73 | /** |
||
74 | * Total filtered records. |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | protected $filteredRecords = 0; |
||
79 | |||
80 | /** |
||
81 | * Auto-filter flag. |
||
82 | * |
||
83 | * @var bool |
||
84 | */ |
||
85 | protected $autoFilter = true; |
||
86 | |||
87 | /** |
||
88 | * Callback to override global search. |
||
89 | * |
||
90 | * @var callable |
||
91 | */ |
||
92 | protected $filterCallback; |
||
93 | |||
94 | /** |
||
95 | * DT row templates container. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $templates = [ |
||
100 | 'DT_RowId' => '', |
||
101 | 'DT_RowClass' => '', |
||
102 | 'DT_RowData' => [], |
||
103 | 'DT_RowAttr' => [], |
||
104 | ]; |
||
105 | |||
106 | /** |
||
107 | * [internal] Track if any filter was applied for at least one column. |
||
108 | * |
||
109 | * @var bool |
||
110 | */ |
||
111 | protected $isFilterApplied = false; |
||
112 | |||
113 | /** |
||
114 | * Custom ordering callback. |
||
115 | * |
||
116 | * @var callable |
||
117 | */ |
||
118 | protected $orderCallback; |
||
119 | |||
120 | /** |
||
121 | * Skip paginate as needed. |
||
122 | * |
||
123 | * @var bool |
||
124 | */ |
||
125 | protected $skipPaging = false; |
||
126 | |||
127 | /** |
||
128 | * Array of data to append on json response. |
||
129 | * |
||
130 | * @var array |
||
131 | */ |
||
132 | protected $appends = []; |
||
133 | |||
134 | /** |
||
135 | * @var \Yajra\DataTables\Utilities\Config |
||
136 | */ |
||
137 | protected $config; |
||
138 | |||
139 | /** |
||
140 | * Add column in collection. |
||
141 | * |
||
142 | * @param string $name |
||
143 | * @param string|callable $content |
||
144 | * @param bool|int $order |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function addColumn($name, $content, $order = false) |
||
155 | |||
156 | /** |
||
157 | * Add DT row index column on response. |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function addIndexColumn() |
||
167 | |||
168 | /** |
||
169 | * Edit column's content. |
||
170 | * |
||
171 | * @param string $name |
||
172 | * @param string|callable $content |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function editColumn($name, $content) |
||
181 | |||
182 | /** |
||
183 | * Remove column from collection. |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function removeColumn() |
||
194 | |||
195 | /** |
||
196 | * Declare columns to escape values. |
||
197 | * |
||
198 | * @param string|array $columns |
||
199 | * @return $this |
||
200 | */ |
||
201 | public function escapeColumns($columns = '*') |
||
207 | |||
208 | /** |
||
209 | * Set columns that should not be escaped. |
||
210 | * |
||
211 | * @param array $columns |
||
212 | * @return $this |
||
213 | */ |
||
214 | public function rawColumns(array $columns) |
||
220 | |||
221 | /** |
||
222 | * Sets DT_RowClass template. |
||
223 | * result: <tr class="output_from_your_template">. |
||
224 | * |
||
225 | * @param string|callable $content |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function setRowClass($content) |
||
234 | |||
235 | /** |
||
236 | * Sets DT_RowId template. |
||
237 | * result: <tr id="output_from_your_template">. |
||
238 | * |
||
239 | * @param string|callable $content |
||
240 | * @return $this |
||
241 | */ |
||
242 | public function setRowId($content) |
||
248 | |||
249 | /** |
||
250 | * Set DT_RowData templates. |
||
251 | * |
||
252 | * @param array $data |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function setRowData(array $data) |
||
261 | |||
262 | /** |
||
263 | * Add DT_RowData template. |
||
264 | * |
||
265 | * @param string $key |
||
266 | * @param string|callable $value |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function addRowData($key, $value) |
||
275 | |||
276 | /** |
||
277 | * Set DT_RowAttr templates. |
||
278 | * result: <tr attr1="attr1" attr2="attr2">. |
||
279 | * |
||
280 | * @param array $data |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function setRowAttr(array $data) |
||
289 | |||
290 | /** |
||
291 | * Add DT_RowAttr template. |
||
292 | * |
||
293 | * @param string $key |
||
294 | * @param string|callable $value |
||
295 | * @return $this |
||
296 | */ |
||
297 | public function addRowAttr($key, $value) |
||
303 | |||
304 | /** |
||
305 | * Append data on json response. |
||
306 | * |
||
307 | * @param mixed $key |
||
308 | * @param mixed $value |
||
309 | * @return $this |
||
310 | */ |
||
311 | public function with($key, $value = '') |
||
323 | |||
324 | /** |
||
325 | * Override default ordering method with a closure callback. |
||
326 | * |
||
327 | * @param callable $closure |
||
328 | * @return $this |
||
329 | */ |
||
330 | public function order(callable $closure) |
||
336 | |||
337 | /** |
||
338 | * Update list of columns that is not allowed for search/sort. |
||
339 | * |
||
340 | * @param array $blacklist |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function blacklist(array $blacklist) |
||
349 | |||
350 | /** |
||
351 | * Update list of columns that is allowed for search/sort. |
||
352 | * |
||
353 | * @param string|array $whitelist |
||
354 | * @return $this |
||
355 | */ |
||
356 | public function whitelist($whitelist = '*') |
||
362 | |||
363 | /** |
||
364 | * Set smart search config at runtime. |
||
365 | * |
||
366 | * @param bool $bool |
||
367 | * @return $this |
||
368 | */ |
||
369 | public function smart($bool = true) |
||
375 | |||
376 | /** |
||
377 | * Set total records manually. |
||
378 | * |
||
379 | * @param int $total |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function setTotalRecords($total) |
||
388 | |||
389 | /** |
||
390 | * Skip pagination as needed. |
||
391 | * |
||
392 | * @return $this |
||
393 | */ |
||
394 | public function skipPaging() |
||
400 | |||
401 | /** |
||
402 | * Push a new column name to blacklist. |
||
403 | * |
||
404 | * @param string $column |
||
405 | * @return $this |
||
406 | */ |
||
407 | public function pushToBlacklist($column) |
||
415 | |||
416 | /** |
||
417 | * Check if column is blacklisted. |
||
418 | * |
||
419 | * @param string $column |
||
420 | * @return bool |
||
421 | */ |
||
422 | protected function isBlacklisted($column) |
||
436 | |||
437 | /** |
||
438 | * Get columns definition. |
||
439 | * |
||
440 | * @return array |
||
441 | */ |
||
442 | protected function getColumnsDefinition() |
||
449 | |||
450 | /** |
||
451 | * Perform sorting of columns. |
||
452 | */ |
||
453 | public function ordering() |
||
461 | |||
462 | /** |
||
463 | * Resolve callback parameter instance. |
||
464 | * |
||
465 | * @return mixed |
||
466 | */ |
||
467 | abstract protected function resolveCallbackParameter(); |
||
468 | |||
469 | /** |
||
470 | * Perform default query orderBy clause. |
||
471 | */ |
||
472 | abstract protected function defaultOrdering(); |
||
473 | |||
474 | /** |
||
475 | * Set auto filter off and run your own filter. |
||
476 | * Overrides global search. |
||
477 | * |
||
478 | * @param callable $callback |
||
479 | * @param bool $globalSearch |
||
480 | * @return $this |
||
481 | */ |
||
482 | public function filter(callable $callback, $globalSearch = false) |
||
490 | |||
491 | /** |
||
492 | * Convert instance to array. |
||
493 | * |
||
494 | * @return array |
||
495 | */ |
||
496 | public function toArray() |
||
500 | |||
501 | /** |
||
502 | * Convert the object to its JSON representation. |
||
503 | * |
||
504 | * @param int $options |
||
505 | * @return \Illuminate\Http\JsonResponse |
||
506 | */ |
||
507 | public function toJson($options = 0) |
||
515 | |||
516 | /** |
||
517 | * Perform necessary filters. |
||
518 | * |
||
519 | * @return void |
||
520 | */ |
||
521 | protected function filterRecords() |
||
534 | |||
535 | /** |
||
536 | * Perform global search. |
||
537 | * |
||
538 | * @return void |
||
539 | */ |
||
540 | public function filtering() |
||
552 | |||
553 | /** |
||
554 | * Perform multi-term search by splitting keyword into |
||
555 | * individual words and searches for each of them. |
||
556 | * |
||
557 | * @param string $keyword |
||
558 | */ |
||
559 | protected function smartGlobalSearch($keyword) |
||
569 | |||
570 | /** |
||
571 | * Perform global search for the given keyword. |
||
572 | * |
||
573 | * @param string $keyword |
||
574 | */ |
||
575 | abstract protected function globalSearch($keyword); |
||
576 | |||
577 | /** |
||
578 | * Apply pagination. |
||
579 | * |
||
580 | * @return void |
||
581 | */ |
||
582 | protected function paginate() |
||
588 | |||
589 | /** |
||
590 | * Transform output. |
||
591 | * |
||
592 | * @param mixed $results |
||
593 | * @param mixed $processed |
||
594 | * @return array |
||
595 | */ |
||
596 | protected function transform($results, $processed) |
||
608 | |||
609 | /** |
||
610 | * Get processed data. |
||
611 | * |
||
612 | * @param mixed $results |
||
613 | * @param bool $object |
||
614 | * @return array |
||
615 | */ |
||
616 | protected function processResults($results, $object = false) |
||
627 | |||
628 | /** |
||
629 | * Render json response. |
||
630 | * |
||
631 | * @param array $data |
||
632 | * @return \Illuminate\Http\JsonResponse |
||
633 | */ |
||
634 | protected function render(array $data) |
||
654 | |||
655 | /** |
||
656 | * Append debug parameters on output. |
||
657 | * |
||
658 | * @param array $output |
||
659 | * @return array |
||
660 | */ |
||
661 | protected function showDebugger(array $output) |
||
667 | |||
668 | /** |
||
669 | * Return an error json response. |
||
670 | * |
||
671 | * @param \Exception $exception |
||
672 | * @return \Illuminate\Http\JsonResponse |
||
673 | * @throws \Yajra\DataTables\Exceptions\Exception |
||
674 | */ |
||
675 | protected function errorResponse(\Exception $exception) |
||
692 | |||
693 | /** |
||
694 | * Get monolog/logger instance. |
||
695 | * |
||
696 | * @return \Illuminate\Contracts\Logging\Log |
||
697 | */ |
||
698 | public function getLogger() |
||
704 | |||
705 | /** |
||
706 | * Set monolog/logger instance. |
||
707 | * |
||
708 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
709 | * @return $this |
||
710 | */ |
||
711 | public function setLogger(Log $logger) |
||
717 | |||
718 | /** |
||
719 | * Setup search keyword. |
||
720 | * |
||
721 | * @param string $value |
||
722 | * @return string |
||
723 | */ |
||
724 | protected function setupKeyword($value) |
||
739 | |||
740 | /** |
||
741 | * Get column name to be use for filtering and sorting. |
||
742 | * |
||
743 | * @param int $index |
||
744 | * @param bool $wantsAlias |
||
745 | * @return string |
||
746 | */ |
||
747 | protected function getColumnName($index, $wantsAlias = false) |
||
762 | |||
763 | /** |
||
764 | * Get column name by order column index. |
||
765 | * |
||
766 | * @param int $index |
||
767 | * @return string |
||
768 | */ |
||
769 | protected function getColumnNameByIndex($index) |
||
776 | |||
777 | /** |
||
778 | * If column name could not be resolved then use primary key. |
||
779 | * |
||
780 | * @return string |
||
781 | */ |
||
782 | protected function getPrimaryKeyName() |
||
786 | } |
||
787 |