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 |
||
23 | abstract class BaseEngine implements DataTableEngineContract |
||
24 | { |
||
25 | /** |
||
26 | * Datatables Request object. |
||
27 | * |
||
28 | * @var \Yajra\Datatables\Request |
||
29 | */ |
||
30 | public $request; |
||
31 | |||
32 | /** |
||
33 | * Database connection used. |
||
34 | * |
||
35 | * @var \Illuminate\Database\Connection |
||
36 | */ |
||
37 | protected $connection; |
||
38 | |||
39 | /** |
||
40 | * Builder object. |
||
41 | * |
||
42 | * @var mixed |
||
43 | */ |
||
44 | protected $query; |
||
45 | |||
46 | /** |
||
47 | * Query builder object. |
||
48 | * |
||
49 | * @var \Illuminate\Database\Query\Builder |
||
50 | */ |
||
51 | protected $builder; |
||
52 | |||
53 | /** |
||
54 | * Array of result columns/fields. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $columns = []; |
||
59 | |||
60 | /** |
||
61 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $columnDef = [ |
||
66 | 'append' => [], |
||
67 | 'edit' => [], |
||
68 | 'excess' => ['rn', 'row_num'], |
||
69 | 'filter' => [], |
||
70 | 'order' => [], |
||
71 | 'escape' => [], |
||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * Query type. |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $query_type; |
||
80 | |||
81 | /** |
||
82 | * Extra/Added columns. |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $extraColumns = []; |
||
87 | |||
88 | /** |
||
89 | * Total records. |
||
90 | * |
||
91 | * @var int |
||
92 | */ |
||
93 | protected $totalRecords = 0; |
||
94 | |||
95 | /** |
||
96 | * Total filtered records. |
||
97 | * |
||
98 | * @var int |
||
99 | */ |
||
100 | protected $filteredRecords = 0; |
||
101 | |||
102 | /** |
||
103 | * Auto-filter flag. |
||
104 | * |
||
105 | * @var bool |
||
106 | */ |
||
107 | protected $autoFilter = true; |
||
108 | |||
109 | /** |
||
110 | * Callback to override global search. |
||
111 | * |
||
112 | * @var \Closure |
||
113 | */ |
||
114 | protected $filterCallback; |
||
115 | |||
116 | /** |
||
117 | * Parameters to passed on filterCallback. |
||
118 | * |
||
119 | * @var mixed |
||
120 | */ |
||
121 | protected $filterCallbackParameters; |
||
122 | |||
123 | /** |
||
124 | * DT row templates container. |
||
125 | * |
||
126 | * @var array |
||
127 | */ |
||
128 | protected $templates = [ |
||
129 | 'DT_RowId' => '', |
||
130 | 'DT_RowClass' => '', |
||
131 | 'DT_RowData' => [], |
||
132 | 'DT_RowAttr' => [], |
||
133 | ]; |
||
134 | |||
135 | /** |
||
136 | * Output transformer. |
||
137 | * |
||
138 | * @var \League\Fractal\TransformerAbstract |
||
139 | */ |
||
140 | protected $transformer = null; |
||
141 | |||
142 | /** |
||
143 | * Database prefix |
||
144 | * |
||
145 | * @var string |
||
146 | */ |
||
147 | protected $prefix; |
||
148 | |||
149 | /** |
||
150 | * Database driver used. |
||
151 | * |
||
152 | * @var string |
||
153 | */ |
||
154 | protected $database; |
||
155 | |||
156 | /** |
||
157 | * [internal] Track if any filter was applied for at least one column |
||
158 | * |
||
159 | * @var boolean |
||
160 | */ |
||
161 | protected $isFilterApplied = false; |
||
162 | |||
163 | /** |
||
164 | * Fractal serializer class. |
||
165 | * |
||
166 | * @var string |
||
167 | */ |
||
168 | protected $serializer; |
||
169 | |||
170 | /** |
||
171 | * Custom ordering callback. |
||
172 | * |
||
173 | * @var \Closure |
||
174 | */ |
||
175 | protected $orderCallback; |
||
176 | |||
177 | /** |
||
178 | * Array of data to append on json response. |
||
179 | * |
||
180 | * @var array |
||
181 | */ |
||
182 | private $appends = []; |
||
183 | |||
184 | /** |
||
185 | * Setup search keyword. |
||
186 | * |
||
187 | * @param string $value |
||
188 | * @return string |
||
189 | */ |
||
190 | public function setupKeyword($value) |
||
201 | |||
202 | /** |
||
203 | * Get config use wild card status. |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function isWildcard() |
||
211 | |||
212 | /** |
||
213 | * Adds % wildcards to the given string. |
||
214 | * |
||
215 | * @param string $str |
||
216 | * @param bool $lowercase |
||
217 | * @return string |
||
218 | */ |
||
219 | public function wildcardLikeString($str, $lowercase = true) |
||
234 | |||
235 | /** |
||
236 | * Setup column name to be use for filtering. |
||
237 | * |
||
238 | * @param integer $i |
||
239 | * @param bool $wantsAlias |
||
240 | * @return string |
||
241 | */ |
||
242 | public function setupColumnName($i, $wantsAlias = false) |
||
251 | |||
252 | /** |
||
253 | * Get column name by order column index. |
||
254 | * |
||
255 | * @param int $column |
||
256 | * @return mixed |
||
257 | */ |
||
258 | protected function getColumnName($column) |
||
264 | |||
265 | /** |
||
266 | * Get column name from string. |
||
267 | * |
||
268 | * @param string $str |
||
269 | * @param bool $wantsAlias |
||
270 | * @return string |
||
271 | */ |
||
272 | public function extractColumnName($str, $wantsAlias) |
||
290 | |||
291 | /** |
||
292 | * Will prefix column if needed. |
||
293 | * |
||
294 | * @param string $column |
||
295 | * @return string |
||
296 | */ |
||
297 | public function prefixColumn($column) |
||
311 | |||
312 | /** |
||
313 | * Will look through the query and all it's joins to determine the table names. |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | public function tableNames() |
||
334 | |||
335 | /** |
||
336 | * Get Query Builder object. |
||
337 | * |
||
338 | * @param mixed $instance |
||
339 | * @return mixed |
||
340 | */ |
||
341 | public function getQueryBuilder($instance = null) |
||
353 | |||
354 | /** |
||
355 | * Check query type is a builder. |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | public function isQueryBuilder() |
||
363 | |||
364 | /** |
||
365 | * Add column in collection. |
||
366 | * |
||
367 | * @param string $name |
||
368 | * @param string $content |
||
369 | * @param bool|int $order |
||
370 | * @return $this |
||
371 | */ |
||
372 | public function addColumn($name, $content, $order = false) |
||
380 | |||
381 | /** |
||
382 | * Edit column's content. |
||
383 | * |
||
384 | * @param string $name |
||
385 | * @param string $content |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function editColumn($name, $content) |
||
394 | |||
395 | /** |
||
396 | * Remove column from collection. |
||
397 | * |
||
398 | * @return $this |
||
399 | */ |
||
400 | public function removeColumn() |
||
407 | |||
408 | /** |
||
409 | * Declare columns to escape values. |
||
410 | * |
||
411 | * @param string|array $columns |
||
412 | * @return $this |
||
413 | */ |
||
414 | public function escapeColumns($columns = '*') |
||
420 | |||
421 | /** |
||
422 | * Allows previous API calls where the methods were snake_case. |
||
423 | * Will convert a camelCase API call to a snake_case call. |
||
424 | * |
||
425 | * @param $name |
||
426 | * @param $arguments |
||
427 | * @return $this|mixed |
||
428 | */ |
||
429 | public function __call($name, $arguments) |
||
442 | |||
443 | /** |
||
444 | * Sets DT_RowClass template |
||
445 | * result: <tr class="output_from_your_template">. |
||
446 | * |
||
447 | * @param string|callable $content |
||
448 | * @return $this |
||
449 | */ |
||
450 | public function setRowClass($content) |
||
456 | |||
457 | /** |
||
458 | * Sets DT_RowId template |
||
459 | * result: <tr id="output_from_your_template">. |
||
460 | * |
||
461 | * @param string|callable $content |
||
462 | * @return $this |
||
463 | */ |
||
464 | public function setRowId($content) |
||
470 | |||
471 | /** |
||
472 | * Set DT_RowData templates. |
||
473 | * |
||
474 | * @param array $data |
||
475 | * @return $this |
||
476 | */ |
||
477 | public function setRowData(array $data) |
||
483 | |||
484 | /** |
||
485 | * Add DT_RowData template. |
||
486 | * |
||
487 | * @param string $key |
||
488 | * @param string|callable $value |
||
489 | * @return $this |
||
490 | */ |
||
491 | public function addRowData($key, $value) |
||
497 | |||
498 | /** |
||
499 | * Set DT_RowAttr templates |
||
500 | * result: <tr attr1="attr1" attr2="attr2">. |
||
501 | * |
||
502 | * @param array $data |
||
503 | * @return $this |
||
504 | */ |
||
505 | public function setRowAttr(array $data) |
||
511 | |||
512 | /** |
||
513 | * Add DT_RowAttr template. |
||
514 | * |
||
515 | * @param string $key |
||
516 | * @param string|callable $value |
||
517 | * @return $this |
||
518 | */ |
||
519 | public function addRowAttr($key, $value) |
||
525 | |||
526 | /** |
||
527 | * Override default column filter search. |
||
528 | * |
||
529 | * @param string $column |
||
530 | * @param string $method |
||
531 | * @return $this |
||
532 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
533 | * @internal string $1 Special variable that returns the requested search keyword. |
||
534 | */ |
||
535 | public function filterColumn($column, $method) |
||
542 | |||
543 | /** |
||
544 | * Override default column ordering. |
||
545 | * |
||
546 | * @param string $column |
||
547 | * @param string $sql |
||
548 | * @param array $bindings |
||
549 | * @return $this |
||
550 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
551 | */ |
||
552 | public function orderColumn($column, $sql, $bindings = []) |
||
558 | |||
559 | /** |
||
560 | * Set data output transformer. |
||
561 | * |
||
562 | * @param \League\Fractal\TransformerAbstract $transformer |
||
563 | * @return $this |
||
564 | */ |
||
565 | public function setTransformer($transformer) |
||
571 | |||
572 | /** |
||
573 | * Set fractal serializer class. |
||
574 | * |
||
575 | * @param string $serializer |
||
576 | * @return $this |
||
577 | */ |
||
578 | public function setSerializer($serializer) |
||
584 | |||
585 | /** |
||
586 | * Organizes works. |
||
587 | * |
||
588 | * @param bool $mDataSupport |
||
589 | * @param bool $orderFirst |
||
590 | * @return \Illuminate\Http\JsonResponse |
||
591 | */ |
||
592 | public function make($mDataSupport = false, $orderFirst = false) |
||
605 | |||
606 | /** |
||
607 | * Count total items. |
||
608 | * |
||
609 | * @return integer |
||
610 | */ |
||
611 | abstract public function totalCount(); |
||
612 | |||
613 | /** |
||
614 | * Sort records. |
||
615 | * |
||
616 | * @param boolean $skip |
||
617 | * @return void |
||
618 | */ |
||
619 | public function orderRecords($skip) |
||
625 | |||
626 | /** |
||
627 | * Perform sorting of columns. |
||
628 | * |
||
629 | * @return void |
||
630 | */ |
||
631 | abstract public function ordering(); |
||
632 | |||
633 | /** |
||
634 | * Perform necessary filters. |
||
635 | * |
||
636 | * @return void |
||
637 | */ |
||
638 | public function filterRecords() |
||
651 | |||
652 | /** |
||
653 | * Perform global search. |
||
654 | * |
||
655 | * @return void |
||
656 | */ |
||
657 | abstract public function filtering(); |
||
658 | |||
659 | /** |
||
660 | * Perform column search. |
||
661 | * |
||
662 | * @return void |
||
663 | */ |
||
664 | abstract public function columnSearch(); |
||
665 | |||
666 | /** |
||
667 | * Count results. |
||
668 | * |
||
669 | * @return integer |
||
670 | */ |
||
671 | abstract public function count(); |
||
672 | |||
673 | /** |
||
674 | * Apply pagination. |
||
675 | * |
||
676 | * @return void |
||
677 | */ |
||
678 | public function paginate() |
||
684 | |||
685 | /** |
||
686 | * Perform pagination |
||
687 | * |
||
688 | * @return void |
||
689 | */ |
||
690 | abstract public function paging(); |
||
691 | |||
692 | /** |
||
693 | * Render json response. |
||
694 | * |
||
695 | * @param bool $object |
||
696 | * @return \Illuminate\Http\JsonResponse |
||
697 | */ |
||
698 | public function render($object = false) |
||
743 | |||
744 | /** |
||
745 | * Get results |
||
746 | * |
||
747 | * @return array |
||
748 | */ |
||
749 | abstract public function results(); |
||
750 | |||
751 | /** |
||
752 | * Get processed data |
||
753 | * |
||
754 | * @param bool|false $object |
||
755 | * @return array |
||
756 | */ |
||
757 | private function getProcessedData($object = false) |
||
767 | |||
768 | /** |
||
769 | * Check if app is in debug mode. |
||
770 | * |
||
771 | * @return bool |
||
772 | */ |
||
773 | public function isDebugging() |
||
777 | |||
778 | /** |
||
779 | * Append debug parameters on output. |
||
780 | * |
||
781 | * @param array $output |
||
782 | * @return array |
||
783 | */ |
||
784 | public function showDebugger(array $output) |
||
791 | |||
792 | /** |
||
793 | * Set auto filter off and run your own filter. |
||
794 | * Overrides global search |
||
795 | * |
||
796 | * @param \Closure $callback |
||
797 | * @return $this |
||
798 | */ |
||
799 | abstract public function filter(\Closure $callback); |
||
800 | |||
801 | /** |
||
802 | * Update flags to disable global search |
||
803 | * |
||
804 | * @param \Closure $callback |
||
805 | * @param mixed $parameters |
||
806 | * @return void |
||
807 | */ |
||
808 | public function overrideGlobalSearch(\Closure $callback, $parameters) |
||
815 | |||
816 | /** |
||
817 | * Get config is case insensitive status. |
||
818 | * |
||
819 | * @return bool |
||
820 | */ |
||
821 | public function isCaseInsensitive() |
||
825 | |||
826 | /** |
||
827 | * Append data on json response. |
||
828 | * |
||
829 | * @param mixed $key |
||
830 | * @param mixed $value |
||
831 | * @return $this |
||
832 | */ |
||
833 | public function with($key, $value = '') |
||
845 | |||
846 | /** |
||
847 | * Override default ordering method with a closure callback. |
||
848 | * |
||
849 | * @param \Closure $closure |
||
850 | * @return $this |
||
851 | */ |
||
852 | public function order(\Closure $closure) |
||
858 | |||
859 | /** |
||
860 | * Check if the current sql language is based on oracle syntax. |
||
861 | * |
||
862 | * @return bool |
||
863 | */ |
||
864 | public function isOracleSql() |
||
868 | } |
||
869 |