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 \Psr\Log\LoggerInterface |
||
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 | 'only' => null, |
||
58 | 'hidden' => [], |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * Extra/Added columns. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $extraColumns = []; |
||
67 | |||
68 | /** |
||
69 | * Total records. |
||
70 | * |
||
71 | * @var int |
||
72 | */ |
||
73 | protected $totalRecords = 0; |
||
74 | |||
75 | /** |
||
76 | * Total filtered records. |
||
77 | * |
||
78 | * @var int |
||
79 | */ |
||
80 | protected $filteredRecords = 0; |
||
81 | |||
82 | /** |
||
83 | * Auto-filter flag. |
||
84 | * |
||
85 | * @var bool |
||
86 | */ |
||
87 | protected $autoFilter = true; |
||
88 | |||
89 | /** |
||
90 | * Callback to override global search. |
||
91 | * |
||
92 | * @var callable |
||
93 | */ |
||
94 | protected $filterCallback; |
||
95 | |||
96 | /** |
||
97 | * DT row templates container. |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $templates = [ |
||
102 | 'DT_RowId' => '', |
||
103 | 'DT_RowClass' => '', |
||
104 | 'DT_RowData' => [], |
||
105 | 'DT_RowAttr' => [], |
||
106 | ]; |
||
107 | |||
108 | /** |
||
109 | * [internal] Track if any filter was applied for at least one column. |
||
110 | * |
||
111 | * @var bool |
||
112 | */ |
||
113 | protected $isFilterApplied = false; |
||
114 | |||
115 | /** |
||
116 | * Custom ordering callback. |
||
117 | * |
||
118 | * @var callable |
||
119 | */ |
||
120 | protected $orderCallback; |
||
121 | |||
122 | /** |
||
123 | * Skip paginate as needed. |
||
124 | * |
||
125 | * @var bool |
||
126 | */ |
||
127 | protected $skipPaging = false; |
||
128 | |||
129 | /** |
||
130 | * Array of data to append on json response. |
||
131 | * |
||
132 | * @var array |
||
133 | */ |
||
134 | protected $appends = []; |
||
135 | |||
136 | /** |
||
137 | * @var \Yajra\DataTables\Utilities\Config |
||
138 | */ |
||
139 | protected $config; |
||
140 | |||
141 | /** |
||
142 | * @var mixed |
||
143 | */ |
||
144 | protected $serializer; |
||
145 | |||
146 | /** |
||
147 | * Can the DataTable engine be created with these parameters. |
||
148 | * |
||
149 | * @param mixed $source |
||
150 | * @return bool |
||
151 | */ |
||
152 | public static function canCreate($source) |
||
156 | |||
157 | /** |
||
158 | * Factory method, create and return an instance for the DataTable engine. |
||
159 | * |
||
160 | * @param mixed $source |
||
161 | * @return DataTableAbstract |
||
162 | */ |
||
163 | public static function create($source) |
||
167 | |||
168 | /** |
||
169 | * Add column in collection. |
||
170 | * |
||
171 | * @param string $name |
||
172 | * @param string|callable $content |
||
173 | * @param bool|int $order |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function addColumn($name, $content, $order = false) |
||
184 | |||
185 | /** |
||
186 | * Add DT row index column on response. |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function addIndexColumn() |
||
196 | |||
197 | /** |
||
198 | * Edit column's content. |
||
199 | * |
||
200 | * @param string $name |
||
201 | * @param string|callable $content |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function editColumn($name, $content) |
||
210 | |||
211 | /** |
||
212 | * Remove column from collection. |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function removeColumn() |
||
223 | |||
224 | /** |
||
225 | * Get only selected columns in response. |
||
226 | * |
||
227 | * @param array $columns |
||
228 | * @return $this |
||
229 | */ |
||
230 | public function only(array $columns = []) |
||
236 | |||
237 | /** |
||
238 | * Declare columns to escape values. |
||
239 | * |
||
240 | * @param string|array $columns |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function escapeColumns($columns = '*') |
||
249 | |||
250 | /** |
||
251 | * Add a makeHidden() to the row object. |
||
252 | * |
||
253 | * @param array $attributes |
||
254 | * @return $this |
||
255 | */ |
||
256 | public function makeHidden(array $attributes = []) |
||
262 | |||
263 | /** |
||
264 | * Set columns that should not be escaped. |
||
265 | * Optionally merge the defaults from config. |
||
266 | * |
||
267 | * @param array $columns |
||
268 | * @param bool $merge |
||
269 | * @return $this |
||
270 | */ |
||
271 | public function rawColumns(array $columns, $merge = false) |
||
283 | |||
284 | /** |
||
285 | * Sets DT_RowClass template. |
||
286 | * result: <tr class="output_from_your_template">. |
||
287 | * |
||
288 | * @param string|callable $content |
||
289 | * @return $this |
||
290 | */ |
||
291 | public function setRowClass($content) |
||
297 | |||
298 | /** |
||
299 | * Sets DT_RowId template. |
||
300 | * result: <tr id="output_from_your_template">. |
||
301 | * |
||
302 | * @param string|callable $content |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function setRowId($content) |
||
311 | |||
312 | /** |
||
313 | * Set DT_RowData templates. |
||
314 | * |
||
315 | * @param array $data |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function setRowData(array $data) |
||
324 | |||
325 | /** |
||
326 | * Add DT_RowData template. |
||
327 | * |
||
328 | * @param string $key |
||
329 | * @param string|callable $value |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function addRowData($key, $value) |
||
338 | |||
339 | /** |
||
340 | * Set DT_RowAttr templates. |
||
341 | * result: <tr attr1="attr1" attr2="attr2">. |
||
342 | * |
||
343 | * @param array $data |
||
344 | * @return $this |
||
345 | */ |
||
346 | public function setRowAttr(array $data) |
||
352 | |||
353 | /** |
||
354 | * Add DT_RowAttr template. |
||
355 | * |
||
356 | * @param string $key |
||
357 | * @param string|callable $value |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function addRowAttr($key, $value) |
||
366 | |||
367 | /** |
||
368 | * Append data on json response. |
||
369 | * |
||
370 | * @param mixed $key |
||
371 | * @param mixed $value |
||
372 | * @return $this |
||
373 | */ |
||
374 | public function with($key, $value = '') |
||
386 | |||
387 | /** |
||
388 | * Add with query callback value on response. |
||
389 | * |
||
390 | * @param string $key |
||
391 | * @param callable $value |
||
392 | * @return $this |
||
393 | */ |
||
394 | public function withQuery($key, callable $value) |
||
400 | |||
401 | /** |
||
402 | * Override default ordering method with a closure callback. |
||
403 | * |
||
404 | * @param callable $closure |
||
405 | * @return $this |
||
406 | */ |
||
407 | public function order(callable $closure) |
||
413 | |||
414 | /** |
||
415 | * Update list of columns that is not allowed for search/sort. |
||
416 | * |
||
417 | * @param array $blacklist |
||
418 | * @return $this |
||
419 | */ |
||
420 | public function blacklist(array $blacklist) |
||
426 | |||
427 | /** |
||
428 | * Update list of columns that is allowed for search/sort. |
||
429 | * |
||
430 | * @param string|array $whitelist |
||
431 | * @return $this |
||
432 | */ |
||
433 | public function whitelist($whitelist = '*') |
||
439 | |||
440 | /** |
||
441 | * Set smart search config at runtime. |
||
442 | * |
||
443 | * @param bool $bool |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function smart($bool = true) |
||
452 | |||
453 | /** |
||
454 | * Set total records manually. |
||
455 | * |
||
456 | * @param int $total |
||
457 | * @return $this |
||
458 | */ |
||
459 | public function setTotalRecords($total) |
||
465 | |||
466 | /** |
||
467 | * Set filtered records manually. |
||
468 | * |
||
469 | * @param int $total |
||
470 | * @return $this |
||
471 | */ |
||
472 | public function setFilteredRecords($total) |
||
478 | |||
479 | /** |
||
480 | * Skip pagination as needed. |
||
481 | * |
||
482 | * @return $this |
||
483 | */ |
||
484 | public function skipPaging() |
||
490 | |||
491 | /** |
||
492 | * Push a new column name to blacklist. |
||
493 | * |
||
494 | * @param string $column |
||
495 | * @return $this |
||
496 | */ |
||
497 | public function pushToBlacklist($column) |
||
505 | |||
506 | /** |
||
507 | * Check if column is blacklisted. |
||
508 | * |
||
509 | * @param string $column |
||
510 | * @return bool |
||
511 | */ |
||
512 | protected function isBlacklisted($column) |
||
526 | |||
527 | /** |
||
528 | * Get columns definition. |
||
529 | * |
||
530 | * @return array |
||
531 | */ |
||
532 | protected function getColumnsDefinition() |
||
539 | |||
540 | /** |
||
541 | * Perform sorting of columns. |
||
542 | */ |
||
543 | public function ordering() |
||
551 | |||
552 | /** |
||
553 | * Resolve callback parameter instance. |
||
554 | * |
||
555 | * @return mixed |
||
556 | */ |
||
557 | abstract protected function resolveCallbackParameter(); |
||
558 | |||
559 | /** |
||
560 | * Perform default query orderBy clause. |
||
561 | */ |
||
562 | abstract protected function defaultOrdering(); |
||
563 | |||
564 | /** |
||
565 | * Set auto filter off and run your own filter. |
||
566 | * Overrides global search. |
||
567 | * |
||
568 | * @param callable $callback |
||
569 | * @param bool $globalSearch |
||
570 | * @return $this |
||
571 | */ |
||
572 | public function filter(callable $callback, $globalSearch = false) |
||
580 | |||
581 | /** |
||
582 | * Convert instance to array. |
||
583 | * |
||
584 | * @return array |
||
585 | */ |
||
586 | public function toArray() |
||
590 | |||
591 | /** |
||
592 | * Convert the object to its JSON representation. |
||
593 | * |
||
594 | * @param int $options |
||
595 | * @return \Illuminate\Http\JsonResponse |
||
596 | */ |
||
597 | public function toJson($options = 0) |
||
605 | |||
606 | /** |
||
607 | * Count filtered items. |
||
608 | * |
||
609 | * @return int |
||
610 | */ |
||
611 | protected function filteredCount() |
||
615 | |||
616 | /** |
||
617 | * Perform necessary filters. |
||
618 | * |
||
619 | * @return void |
||
620 | */ |
||
621 | protected function filterRecords() |
||
634 | |||
635 | /** |
||
636 | * Perform global search. |
||
637 | * |
||
638 | * @return void |
||
639 | */ |
||
640 | public function filtering() |
||
652 | |||
653 | /** |
||
654 | * Perform multi-term search by splitting keyword into |
||
655 | * individual words and searches for each of them. |
||
656 | * |
||
657 | * @param string $keyword |
||
658 | */ |
||
659 | protected function smartGlobalSearch($keyword) |
||
669 | |||
670 | /** |
||
671 | * Perform global search for the given keyword. |
||
672 | * |
||
673 | * @param string $keyword |
||
674 | */ |
||
675 | abstract protected function globalSearch($keyword); |
||
676 | |||
677 | /** |
||
678 | * Apply pagination. |
||
679 | * |
||
680 | * @return void |
||
681 | */ |
||
682 | protected function paginate() |
||
688 | |||
689 | /** |
||
690 | * Transform output. |
||
691 | * |
||
692 | * @param mixed $results |
||
693 | * @param mixed $processed |
||
694 | * @return array |
||
695 | */ |
||
696 | protected function transform($results, $processed) |
||
708 | |||
709 | /** |
||
710 | * Get processed data. |
||
711 | * |
||
712 | * @param mixed $results |
||
713 | * @param bool $object |
||
714 | * @return array |
||
715 | */ |
||
716 | protected function processResults($results, $object = false) |
||
727 | |||
728 | /** |
||
729 | * Render json response. |
||
730 | * |
||
731 | * @param array $data |
||
732 | * @return \Illuminate\Http\JsonResponse |
||
733 | */ |
||
734 | protected function render(array $data) |
||
754 | |||
755 | /** |
||
756 | * Attach custom with meta on response. |
||
757 | * |
||
758 | * @param array $data |
||
759 | * @return array |
||
760 | */ |
||
761 | protected function attachAppends(array $data) |
||
765 | |||
766 | /** |
||
767 | * Append debug parameters on output. |
||
768 | * |
||
769 | * @param array $output |
||
770 | * @return array |
||
771 | */ |
||
772 | protected function showDebugger(array $output) |
||
778 | |||
779 | /** |
||
780 | * Return an error json response. |
||
781 | * |
||
782 | * @param \Exception $exception |
||
783 | * @return \Illuminate\Http\JsonResponse |
||
784 | * @throws \Yajra\DataTables\Exceptions\Exception |
||
785 | */ |
||
786 | protected function errorResponse(\Exception $exception) |
||
805 | |||
806 | /** |
||
807 | * Get monolog/logger instance. |
||
808 | * |
||
809 | * @return \Psr\Log\LoggerInterface |
||
810 | */ |
||
811 | public function getLogger() |
||
817 | |||
818 | /** |
||
819 | * Set monolog/logger instance. |
||
820 | * |
||
821 | * @param \Psr\Log\LoggerInterface $logger |
||
822 | * @return $this |
||
823 | */ |
||
824 | public function setLogger(LoggerInterface $logger) |
||
830 | |||
831 | /** |
||
832 | * Setup search keyword. |
||
833 | * |
||
834 | * @param string $value |
||
835 | * @return string |
||
836 | */ |
||
837 | protected function setupKeyword($value) |
||
852 | |||
853 | /** |
||
854 | * Get column name to be use for filtering and sorting. |
||
855 | * |
||
856 | * @param int $index |
||
857 | * @param bool $wantsAlias |
||
858 | * @return string |
||
859 | */ |
||
860 | protected function getColumnName($index, $wantsAlias = false) |
||
875 | |||
876 | /** |
||
877 | * Get column name by order column index. |
||
878 | * |
||
879 | * @param int $index |
||
880 | * @return string |
||
881 | */ |
||
882 | protected function getColumnNameByIndex($index) |
||
889 | |||
890 | /** |
||
891 | * If column name could not be resolved then use primary key. |
||
892 | * |
||
893 | * @return string |
||
894 | */ |
||
895 | protected function getPrimaryKeyName() |
||
899 | } |
||
900 |