Complex classes like BaseEngine 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 BaseEngine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class BaseEngine implements DataTableEngine |
||
21 | { |
||
22 | /** |
||
23 | * Datatables Request object. |
||
24 | * |
||
25 | * @var \Yajra\Datatables\Request |
||
26 | */ |
||
27 | public $request; |
||
28 | |||
29 | /** |
||
30 | * @var \Illuminate\Contracts\Logging\Log |
||
31 | */ |
||
32 | protected $logger; |
||
33 | |||
34 | /** |
||
35 | * Array of result columns/fields. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $columns = []; |
||
40 | |||
41 | /** |
||
42 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $columnDef = [ |
||
47 | 'index' => false, |
||
48 | 'append' => [], |
||
49 | 'edit' => [], |
||
50 | 'filter' => [], |
||
51 | 'order' => [], |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Extra/Added columns. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $extraColumns = []; |
||
60 | |||
61 | /** |
||
62 | * Total records. |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $totalRecords = 0; |
||
67 | |||
68 | /** |
||
69 | * Total filtered records. |
||
70 | * |
||
71 | * @var int |
||
72 | */ |
||
73 | protected $filteredRecords = 0; |
||
74 | |||
75 | /** |
||
76 | * Auto-filter flag. |
||
77 | * |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected $autoFilter = true; |
||
81 | |||
82 | /** |
||
83 | * Callback to override global search. |
||
84 | * |
||
85 | * @var callable |
||
86 | */ |
||
87 | protected $filterCallback; |
||
88 | |||
89 | /** |
||
90 | * Parameters to passed on filterCallback. |
||
91 | * |
||
92 | * @var mixed |
||
93 | */ |
||
94 | protected $filterCallbackParameters; |
||
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 | * Output transformer. |
||
110 | * |
||
111 | * @var mixed |
||
112 | */ |
||
113 | protected $transformer = null; |
||
114 | |||
115 | /** |
||
116 | * [internal] Track if any filter was applied for at least one column. |
||
117 | * |
||
118 | * @var boolean |
||
119 | */ |
||
120 | protected $isFilterApplied = false; |
||
121 | |||
122 | /** |
||
123 | * Fractal serializer class. |
||
124 | * |
||
125 | * @var mixed |
||
126 | */ |
||
127 | protected $serializer = null; |
||
128 | |||
129 | /** |
||
130 | * Custom ordering callback. |
||
131 | * |
||
132 | * @var callable |
||
133 | */ |
||
134 | protected $orderCallback; |
||
135 | |||
136 | /** |
||
137 | * Skip paginate as needed. |
||
138 | * |
||
139 | * @var bool |
||
140 | */ |
||
141 | protected $skipPaging = false; |
||
142 | |||
143 | /** |
||
144 | * Array of data to append on json response. |
||
145 | * |
||
146 | * @var array |
||
147 | */ |
||
148 | protected $appends = []; |
||
149 | |||
150 | /** |
||
151 | * Flag for ordering NULLS LAST option. |
||
152 | * |
||
153 | * @var bool |
||
154 | */ |
||
155 | protected $nullsLast = false; |
||
156 | |||
157 | /** |
||
158 | * @var \Yajra\Datatables\Config |
||
159 | */ |
||
160 | protected $config; |
||
161 | |||
162 | /** |
||
163 | * Add column in collection. |
||
164 | * |
||
165 | * @param string $name |
||
166 | * @param string|callable $content |
||
167 | * @param bool|int $order |
||
168 | * @return $this |
||
169 | */ |
||
170 | public function addColumn($name, $content, $order = false) |
||
178 | |||
179 | /** |
||
180 | * Add DT row index column on response. |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function addIndexColumn() |
||
190 | |||
191 | /** |
||
192 | * Edit column's content. |
||
193 | * |
||
194 | * @param string $name |
||
195 | * @param string|callable $content |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function editColumn($name, $content) |
||
204 | |||
205 | /** |
||
206 | * Remove column from collection. |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function removeColumn() |
||
217 | |||
218 | /** |
||
219 | * Declare columns to escape values. |
||
220 | * |
||
221 | * @param string|array $columns |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function escapeColumns($columns = '*') |
||
230 | |||
231 | /** |
||
232 | * Set columns that should not be escaped. |
||
233 | * |
||
234 | * @param array $columns |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function rawColumns(array $columns) |
||
243 | |||
244 | /** |
||
245 | * Sets DT_RowClass template. |
||
246 | * result: <tr class="output_from_your_template">. |
||
247 | * |
||
248 | * @param string|callable $content |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function setRowClass($content) |
||
257 | |||
258 | /** |
||
259 | * Sets DT_RowId template. |
||
260 | * result: <tr id="output_from_your_template">. |
||
261 | * |
||
262 | * @param string|callable $content |
||
263 | * @return $this |
||
264 | */ |
||
265 | public function setRowId($content) |
||
271 | |||
272 | /** |
||
273 | * Set DT_RowData templates. |
||
274 | * |
||
275 | * @param array $data |
||
276 | * @return $this |
||
277 | */ |
||
278 | public function setRowData(array $data) |
||
284 | |||
285 | /** |
||
286 | * Add DT_RowData template. |
||
287 | * |
||
288 | * @param string $key |
||
289 | * @param string|callable $value |
||
290 | * @return $this |
||
291 | */ |
||
292 | public function addRowData($key, $value) |
||
298 | |||
299 | /** |
||
300 | * Set DT_RowAttr templates. |
||
301 | * result: <tr attr1="attr1" attr2="attr2">. |
||
302 | * |
||
303 | * @param array $data |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function setRowAttr(array $data) |
||
312 | |||
313 | /** |
||
314 | * Add DT_RowAttr template. |
||
315 | * |
||
316 | * @param string $key |
||
317 | * @param string|callable $value |
||
318 | * @return $this |
||
319 | */ |
||
320 | public function addRowAttr($key, $value) |
||
326 | |||
327 | /** |
||
328 | * Add custom filter handler for the give column. |
||
329 | * |
||
330 | * @param string $column |
||
331 | * @param callable $callback |
||
332 | * @return $this |
||
333 | */ |
||
334 | public function filterColumn($column, callable $callback) |
||
340 | |||
341 | /** |
||
342 | * Order each given columns versus the given custom sql. |
||
343 | * |
||
344 | * @param array $columns |
||
345 | * @param string $sql |
||
346 | * @param array $bindings |
||
347 | * @return $this |
||
348 | */ |
||
349 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
357 | |||
358 | /** |
||
359 | * Override default column ordering. |
||
360 | * |
||
361 | * @param string $column |
||
362 | * @param string $sql |
||
363 | * @param array $bindings |
||
364 | * @return $this |
||
365 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
366 | */ |
||
367 | public function orderColumn($column, $sql, $bindings = []) |
||
373 | |||
374 | /** |
||
375 | * Set data output transformer. |
||
376 | * |
||
377 | * @param mixed $transformer |
||
378 | * @return $this |
||
379 | */ |
||
380 | public function setTransformer($transformer) |
||
386 | |||
387 | /** |
||
388 | * Set fractal serializer class. |
||
389 | * |
||
390 | * @param string $serializer |
||
391 | * @return $this |
||
392 | */ |
||
393 | public function setSerializer($serializer) |
||
399 | |||
400 | /** |
||
401 | * Append data on json response. |
||
402 | * |
||
403 | * @param mixed $key |
||
404 | * @param mixed $value |
||
405 | * @return $this |
||
406 | */ |
||
407 | public function with($key, $value = '') |
||
419 | |||
420 | /** |
||
421 | * Override default ordering method with a closure callback. |
||
422 | * |
||
423 | * @param callable $closure |
||
424 | * @return $this |
||
425 | */ |
||
426 | public function order(callable $closure) |
||
432 | |||
433 | /** |
||
434 | * Update list of columns that is not allowed for search/sort. |
||
435 | * |
||
436 | * @param array $blacklist |
||
437 | * @return $this |
||
438 | */ |
||
439 | public function blacklist(array $blacklist) |
||
445 | |||
446 | /** |
||
447 | * Update list of columns that is allowed for search/sort. |
||
448 | * |
||
449 | * @param string|array $whitelist |
||
450 | * @return $this |
||
451 | */ |
||
452 | public function whitelist($whitelist = '*') |
||
458 | |||
459 | /** |
||
460 | * Set smart search config at runtime. |
||
461 | * |
||
462 | * @param bool $bool |
||
463 | * @return $this |
||
464 | */ |
||
465 | public function smart($bool = true) |
||
471 | |||
472 | /** |
||
473 | * Set total records manually. |
||
474 | * |
||
475 | * @param int $total |
||
476 | * @return $this |
||
477 | */ |
||
478 | public function setTotalRecords($total) |
||
484 | |||
485 | /** |
||
486 | * Skip pagination as needed. |
||
487 | * |
||
488 | * @return $this |
||
489 | */ |
||
490 | public function skipPaging() |
||
496 | |||
497 | /** |
||
498 | * Set datatables to do ordering with NULLS LAST option. |
||
499 | * |
||
500 | * @return $this |
||
501 | */ |
||
502 | public function orderByNullsLast() |
||
508 | |||
509 | /** |
||
510 | * Push a new column name to blacklist. |
||
511 | * |
||
512 | * @param string $column |
||
513 | * @return $this |
||
514 | */ |
||
515 | public function pushToBlacklist($column) |
||
523 | |||
524 | /** |
||
525 | * Check if column is blacklisted. |
||
526 | * |
||
527 | * @param string $column |
||
528 | * @return bool |
||
529 | */ |
||
530 | protected function isBlacklisted($column) |
||
544 | |||
545 | /** |
||
546 | * Get columns definition. |
||
547 | * |
||
548 | * @return array |
||
549 | */ |
||
550 | protected function getColumnsDefinition() |
||
557 | |||
558 | /** |
||
559 | * Perform sorting of columns. |
||
560 | */ |
||
561 | public function ordering() |
||
569 | |||
570 | /** |
||
571 | * Resolve callback parameter instance. |
||
572 | * |
||
573 | * @return mixed |
||
574 | */ |
||
575 | abstract protected function resolveCallbackParameter(); |
||
576 | |||
577 | /** |
||
578 | * Perform default query orderBy clause. |
||
579 | */ |
||
580 | abstract protected function defaultOrdering(); |
||
581 | |||
582 | /** |
||
583 | * Perform necessary filters. |
||
584 | * |
||
585 | * @return void |
||
586 | */ |
||
587 | protected function filterRecords() |
||
600 | |||
601 | /** |
||
602 | * Perform global search. |
||
603 | * |
||
604 | * @return void |
||
605 | */ |
||
606 | public function filtering() |
||
618 | |||
619 | /** |
||
620 | * Perform multi-term search by splitting keyword into |
||
621 | * individual words and searches for each of them. |
||
622 | * |
||
623 | * @param string $keyword |
||
624 | */ |
||
625 | protected function smartGlobalSearch($keyword) |
||
633 | |||
634 | /** |
||
635 | * Perform global search for the given keyword. |
||
636 | * |
||
637 | * @param string $keyword |
||
638 | */ |
||
639 | abstract protected function globalSearch($keyword); |
||
640 | |||
641 | /** |
||
642 | * Apply pagination. |
||
643 | * |
||
644 | * @return void |
||
645 | */ |
||
646 | protected function paginate() |
||
652 | |||
653 | /** |
||
654 | * Transform output. |
||
655 | * |
||
656 | * @param mixed $output |
||
657 | * @return array |
||
658 | */ |
||
659 | protected function transform($output) |
||
667 | |||
668 | /** |
||
669 | * Get processed data. |
||
670 | * |
||
671 | * @param bool|false $object |
||
672 | * @return array |
||
673 | */ |
||
674 | protected function getProcessedData($object = false) |
||
685 | |||
686 | /** |
||
687 | * Render json response. |
||
688 | * |
||
689 | * @param array $data |
||
690 | * @return \Illuminate\Http\JsonResponse |
||
691 | */ |
||
692 | protected function render(array $data) |
||
712 | |||
713 | /** |
||
714 | * Append debug parameters on output. |
||
715 | * |
||
716 | * @param array $output |
||
717 | * @return array |
||
718 | */ |
||
719 | abstract protected function showDebugger(array $output); |
||
720 | |||
721 | /** |
||
722 | * Return an error json response. |
||
723 | * |
||
724 | * @param \Exception $exception |
||
725 | * @return \Illuminate\Http\JsonResponse |
||
726 | * @throws \Yajra\Datatables\Exception |
||
727 | */ |
||
728 | protected function errorResponse(\Exception $exception) |
||
745 | |||
746 | /** |
||
747 | * Get monolog/logger instance. |
||
748 | * |
||
749 | * @return \Illuminate\Contracts\Logging\Log |
||
750 | */ |
||
751 | public function getLogger() |
||
757 | |||
758 | /** |
||
759 | * Set monolog/logger instance. |
||
760 | * |
||
761 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
762 | * @return $this |
||
763 | */ |
||
764 | public function setLogger(Log $logger) |
||
770 | |||
771 | /** |
||
772 | * Setup search keyword. |
||
773 | * |
||
774 | * @param string $value |
||
775 | * @return string |
||
776 | */ |
||
777 | protected function setupKeyword($value) |
||
792 | |||
793 | /** |
||
794 | * Update flags to disable global search. |
||
795 | * |
||
796 | * @param callable $callback |
||
797 | * @param mixed $parameters |
||
798 | * @param bool $autoFilter |
||
799 | */ |
||
800 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
807 | |||
808 | /** |
||
809 | * Get column name to be use for filtering and sorting. |
||
810 | * |
||
811 | * @param integer $index |
||
812 | * @param bool $wantsAlias |
||
813 | * @return string |
||
814 | */ |
||
815 | protected function getColumnName($index, $wantsAlias = false) |
||
830 | |||
831 | /** |
||
832 | * Get column name by order column index. |
||
833 | * |
||
834 | * @param int $index |
||
835 | * @return string |
||
836 | */ |
||
837 | protected function getColumnNameByIndex($index) |
||
843 | |||
844 | /** |
||
845 | * If column name could not be resolved then use primary key. |
||
846 | * |
||
847 | * @return string |
||
848 | */ |
||
849 | protected function getPrimaryKeyName() |
||
853 | } |
||
854 |