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 |
||
19 | abstract class BaseEngine implements DataTableEngineContract |
||
20 | { |
||
21 | /** |
||
22 | * Datatables Request object. |
||
23 | * |
||
24 | * @var \Yajra\Datatables\Request |
||
25 | */ |
||
26 | public $request; |
||
27 | |||
28 | /** |
||
29 | * @var \Illuminate\Contracts\Logging\Log |
||
30 | */ |
||
31 | protected $logger; |
||
32 | |||
33 | /** |
||
34 | * Array of result columns/fields. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $columns = []; |
||
39 | |||
40 | /** |
||
41 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $columnDef = [ |
||
46 | 'index' => false, |
||
47 | 'append' => [], |
||
48 | 'edit' => [], |
||
49 | 'excess' => ['rn', 'row_num'], |
||
50 | 'filter' => [], |
||
51 | 'order' => [], |
||
52 | 'escape' => '*', |
||
53 | 'raw' => ['action'], |
||
54 | 'blacklist' => ['password', 'remember_token'], |
||
55 | 'whitelist' => '*', |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * Extra/Added columns. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $extraColumns = []; |
||
64 | |||
65 | /** |
||
66 | * Total records. |
||
67 | * |
||
68 | * @var int |
||
69 | */ |
||
70 | protected $totalRecords = 0; |
||
71 | |||
72 | /** |
||
73 | * Total filtered records. |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | protected $filteredRecords = 0; |
||
78 | |||
79 | /** |
||
80 | * Auto-filter flag. |
||
81 | * |
||
82 | * @var bool |
||
83 | */ |
||
84 | protected $autoFilter = true; |
||
85 | |||
86 | /** |
||
87 | * Callback to override global search. |
||
88 | * |
||
89 | * @var callable |
||
90 | */ |
||
91 | protected $filterCallback; |
||
92 | |||
93 | /** |
||
94 | * Parameters to passed on filterCallback. |
||
95 | * |
||
96 | * @var mixed |
||
97 | */ |
||
98 | protected $filterCallbackParameters; |
||
99 | |||
100 | /** |
||
101 | * DT row templates container. |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $templates = [ |
||
106 | 'DT_RowId' => '', |
||
107 | 'DT_RowClass' => '', |
||
108 | 'DT_RowData' => [], |
||
109 | 'DT_RowAttr' => [], |
||
110 | ]; |
||
111 | |||
112 | /** |
||
113 | * Output transformer. |
||
114 | * |
||
115 | * @var \League\Fractal\TransformerAbstract |
||
116 | */ |
||
117 | protected $transformer = null; |
||
118 | |||
119 | /** |
||
120 | * Database prefix. |
||
121 | * |
||
122 | * @var string |
||
123 | */ |
||
124 | protected $prefix; |
||
125 | |||
126 | /** |
||
127 | * Database driver used. |
||
128 | * |
||
129 | * @var string |
||
130 | */ |
||
131 | protected $database; |
||
132 | |||
133 | /** |
||
134 | * [internal] Track if any filter was applied for at least one column. |
||
135 | * |
||
136 | * @var boolean |
||
137 | */ |
||
138 | protected $isFilterApplied = false; |
||
139 | |||
140 | /** |
||
141 | * Fractal serializer class. |
||
142 | * |
||
143 | * @var string|null |
||
144 | */ |
||
145 | protected $serializer = null; |
||
146 | |||
147 | /** |
||
148 | * Custom ordering callback. |
||
149 | * |
||
150 | * @var callable |
||
151 | */ |
||
152 | protected $orderCallback; |
||
153 | |||
154 | /** |
||
155 | * Skip paginate as needed. |
||
156 | * |
||
157 | * @var bool |
||
158 | */ |
||
159 | protected $skipPaging = false; |
||
160 | |||
161 | /** |
||
162 | * Array of data to append on json response. |
||
163 | * |
||
164 | * @var array |
||
165 | */ |
||
166 | protected $appends = []; |
||
167 | |||
168 | /** |
||
169 | * Flag for ordering NULLS LAST option. |
||
170 | * |
||
171 | * @var bool |
||
172 | */ |
||
173 | protected $nullsLast = false; |
||
174 | |||
175 | /** |
||
176 | * Add column in collection. |
||
177 | * |
||
178 | * @param string $name |
||
179 | * @param string|callable $content |
||
180 | * @param bool|int $order |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function addColumn($name, $content, $order = false) |
||
191 | |||
192 | /** |
||
193 | * Add DT row index column on response. |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function addIndexColumn() |
||
203 | |||
204 | /** |
||
205 | * Edit column's content. |
||
206 | * |
||
207 | * @param string $name |
||
208 | * @param string|callable $content |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function editColumn($name, $content) |
||
217 | |||
218 | /** |
||
219 | * Remove column from collection. |
||
220 | * |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function removeColumn() |
||
230 | |||
231 | /** |
||
232 | * Declare columns to escape values. |
||
233 | * |
||
234 | * @param string|array $columns |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function escapeColumns($columns = '*') |
||
243 | |||
244 | /** |
||
245 | * Set columns that should not be escaped. |
||
246 | * |
||
247 | * @param array $columns |
||
248 | * @return $this |
||
249 | */ |
||
250 | public function rawColumns(array $columns) |
||
256 | |||
257 | /** |
||
258 | * Sets DT_RowClass template. |
||
259 | * result: <tr class="output_from_your_template">. |
||
260 | * |
||
261 | * @param string|callable $content |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function setRowClass($content) |
||
270 | |||
271 | /** |
||
272 | * Sets DT_RowId template. |
||
273 | * result: <tr id="output_from_your_template">. |
||
274 | * |
||
275 | * @param string|callable $content |
||
276 | * @return $this |
||
277 | */ |
||
278 | public function setRowId($content) |
||
284 | |||
285 | /** |
||
286 | * Set DT_RowData templates. |
||
287 | * |
||
288 | * @param array $data |
||
289 | * @return $this |
||
290 | */ |
||
291 | public function setRowData(array $data) |
||
297 | |||
298 | /** |
||
299 | * Add DT_RowData template. |
||
300 | * |
||
301 | * @param string $key |
||
302 | * @param string|callable $value |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function addRowData($key, $value) |
||
311 | |||
312 | /** |
||
313 | * Set DT_RowAttr templates. |
||
314 | * result: <tr attr1="attr1" attr2="attr2">. |
||
315 | * |
||
316 | * @param array $data |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function setRowAttr(array $data) |
||
325 | |||
326 | /** |
||
327 | * Add DT_RowAttr template. |
||
328 | * |
||
329 | * @param string $key |
||
330 | * @param string|callable $value |
||
331 | * @return $this |
||
332 | */ |
||
333 | public function addRowAttr($key, $value) |
||
339 | |||
340 | /** |
||
341 | * Add custom filter handler for the give column. |
||
342 | * |
||
343 | * @param string $column |
||
344 | * @param callable $callback |
||
345 | * @return $this |
||
346 | */ |
||
347 | public function filterColumn($column, callable $callback) |
||
353 | |||
354 | /** |
||
355 | * Order each given columns versus the given custom sql. |
||
356 | * |
||
357 | * @param array $columns |
||
358 | * @param string $sql |
||
359 | * @param array $bindings |
||
360 | * @return $this |
||
361 | */ |
||
362 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
370 | |||
371 | /** |
||
372 | * Override default column ordering. |
||
373 | * |
||
374 | * @param string $column |
||
375 | * @param string $sql |
||
376 | * @param array $bindings |
||
377 | * @return $this |
||
378 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
379 | */ |
||
380 | public function orderColumn($column, $sql, $bindings = []) |
||
386 | |||
387 | /** |
||
388 | * Set data output transformer. |
||
389 | * |
||
390 | * @param \League\Fractal\TransformerAbstract $transformer |
||
391 | * @return $this |
||
392 | */ |
||
393 | public function setTransformer($transformer) |
||
399 | |||
400 | /** |
||
401 | * Set fractal serializer class. |
||
402 | * |
||
403 | * @param string $serializer |
||
404 | * @return $this |
||
405 | */ |
||
406 | public function setSerializer($serializer) |
||
412 | |||
413 | /** |
||
414 | * Organizes works. |
||
415 | * |
||
416 | * @param bool $mDataSupport |
||
417 | * @return \Illuminate\Http\JsonResponse |
||
418 | * @throws \Exception |
||
419 | */ |
||
420 | public function make($mDataSupport = false) |
||
438 | |||
439 | /** |
||
440 | * Perform necessary filters. |
||
441 | * |
||
442 | * @return void |
||
443 | */ |
||
444 | protected function filterRecords() |
||
457 | |||
458 | /** |
||
459 | * Apply pagination. |
||
460 | * |
||
461 | * @return void |
||
462 | */ |
||
463 | protected function paginate() |
||
469 | |||
470 | /** |
||
471 | * Transform output. |
||
472 | * |
||
473 | * @param mixed $output |
||
474 | * @return array |
||
475 | */ |
||
476 | protected function transform($output) |
||
508 | |||
509 | /** |
||
510 | * Get or create transformer serializer instance. |
||
511 | * |
||
512 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
513 | */ |
||
514 | protected function createSerializer() |
||
522 | |||
523 | /** |
||
524 | * Get or create transformer instance. |
||
525 | * |
||
526 | * @return \League\Fractal\TransformerAbstract |
||
527 | */ |
||
528 | protected function createTransformer() |
||
536 | |||
537 | /** |
||
538 | * Get processed data. |
||
539 | * |
||
540 | * @param bool|false $object |
||
541 | * @return array |
||
542 | */ |
||
543 | protected function getProcessedData($object = false) |
||
554 | |||
555 | /** |
||
556 | * Get columns definition. |
||
557 | * |
||
558 | * @return array |
||
559 | */ |
||
560 | protected function getColumnsDefinition() |
||
567 | |||
568 | /** |
||
569 | * Render json response. |
||
570 | * |
||
571 | * @param array $data |
||
572 | * @return \Illuminate\Http\JsonResponse |
||
573 | */ |
||
574 | protected function render(array $data) |
||
594 | |||
595 | /** |
||
596 | * Check if app is in debug mode. |
||
597 | * |
||
598 | * @return bool |
||
599 | */ |
||
600 | public function isDebugging() |
||
604 | |||
605 | /** |
||
606 | * Append debug parameters on output. |
||
607 | * |
||
608 | * @param array $output |
||
609 | * @return array |
||
610 | */ |
||
611 | abstract protected function showDebugger(array $output); |
||
612 | |||
613 | /** |
||
614 | * Return an error json response. |
||
615 | * |
||
616 | * @param \Exception $exception |
||
617 | * @return \Illuminate\Http\JsonResponse |
||
618 | * @throws \Yajra\Datatables\Exception |
||
619 | */ |
||
620 | protected function errorResponse(\Exception $exception) |
||
637 | |||
638 | /** |
||
639 | * Get monolog/logger instance. |
||
640 | * |
||
641 | * @return \Illuminate\Contracts\Logging\Log |
||
642 | */ |
||
643 | public function getLogger() |
||
649 | |||
650 | /** |
||
651 | * Set monolog/logger instance. |
||
652 | * |
||
653 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
654 | * @return $this |
||
655 | */ |
||
656 | public function setLogger(Log $logger) |
||
662 | |||
663 | /** |
||
664 | * Check if config uses case insensitive search. |
||
665 | * |
||
666 | * @return bool |
||
667 | */ |
||
668 | public function isCaseInsensitive() |
||
672 | |||
673 | /** |
||
674 | * Append data on json response. |
||
675 | * |
||
676 | * @param mixed $key |
||
677 | * @param mixed $value |
||
678 | * @return $this |
||
679 | */ |
||
680 | public function with($key, $value = '') |
||
692 | |||
693 | /** |
||
694 | * Override default ordering method with a closure callback. |
||
695 | * |
||
696 | * @param callable $closure |
||
697 | * @return $this |
||
698 | */ |
||
699 | public function order(callable $closure) |
||
705 | |||
706 | /** |
||
707 | * Update list of columns that is not allowed for search/sort. |
||
708 | * |
||
709 | * @param array $blacklist |
||
710 | * @return $this |
||
711 | */ |
||
712 | public function blacklist(array $blacklist) |
||
718 | |||
719 | /** |
||
720 | * Update list of columns that is allowed for search/sort. |
||
721 | * |
||
722 | * @param string|array $whitelist |
||
723 | * @return $this |
||
724 | */ |
||
725 | public function whitelist($whitelist = '*') |
||
731 | |||
732 | /** |
||
733 | * Set smart search config at runtime. |
||
734 | * |
||
735 | * @param bool $bool |
||
736 | * @return $this |
||
737 | */ |
||
738 | public function smart($bool = true) |
||
744 | |||
745 | /** |
||
746 | * Set total records manually. |
||
747 | * |
||
748 | * @param int $total |
||
749 | * @return $this |
||
750 | */ |
||
751 | public function setTotalRecords($total) |
||
757 | |||
758 | /** |
||
759 | * Skip pagination as needed. |
||
760 | * |
||
761 | * @return $this |
||
762 | */ |
||
763 | public function skipPaging() |
||
769 | |||
770 | /** |
||
771 | * Check if the current sql language is based on oracle syntax. |
||
772 | * |
||
773 | * @return bool |
||
774 | */ |
||
775 | public function isOracleSql() |
||
779 | |||
780 | /** |
||
781 | * Set datatables to do ordering with NULLS LAST option. |
||
782 | * |
||
783 | * @return $this |
||
784 | */ |
||
785 | public function orderByNullsLast() |
||
791 | |||
792 | /** |
||
793 | * Push a new column name to blacklist. |
||
794 | * |
||
795 | * @param string $column |
||
796 | * @return $this |
||
797 | */ |
||
798 | public function pushToBlacklist($column) |
||
806 | |||
807 | /** |
||
808 | * Check if column is blacklisted. |
||
809 | * |
||
810 | * @param string $column |
||
811 | * @return bool |
||
812 | */ |
||
813 | protected function isBlacklisted($column) |
||
825 | |||
826 | /** |
||
827 | * Setup search keyword. |
||
828 | * |
||
829 | * @param string $value |
||
830 | * @return string |
||
831 | */ |
||
832 | protected function setupKeyword($value) |
||
847 | |||
848 | /** |
||
849 | * Check if config uses smart search. |
||
850 | * |
||
851 | * @return bool |
||
852 | */ |
||
853 | public function isSmartSearch() |
||
857 | |||
858 | /** |
||
859 | * Check if config uses wild card search. |
||
860 | * |
||
861 | * @return bool |
||
862 | */ |
||
863 | public function isWildcard() |
||
867 | |||
868 | /** |
||
869 | * Adds % wildcards to the given string. |
||
870 | * |
||
871 | * @param string $str |
||
872 | * @param bool $lowercase |
||
873 | * @return string |
||
874 | */ |
||
875 | protected function wildcardLikeString($str, $lowercase = true) |
||
892 | |||
893 | /** |
||
894 | * Update flags to disable global search. |
||
895 | * |
||
896 | * @param callable $callback |
||
897 | * @param mixed $parameters |
||
898 | * @param bool $autoFilter |
||
899 | */ |
||
900 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
907 | |||
908 | /** |
||
909 | * Get column name to be use for filtering and sorting. |
||
910 | * |
||
911 | * @param integer $index |
||
912 | * @param bool $wantsAlias |
||
913 | * @return string |
||
914 | */ |
||
915 | protected function getColumnName($index, $wantsAlias = false) |
||
930 | |||
931 | /** |
||
932 | * Get column name by order column index. |
||
933 | * |
||
934 | * @param int $index |
||
935 | * @return string |
||
936 | */ |
||
937 | protected function getColumnNameByIndex($index) |
||
943 | |||
944 | /** |
||
945 | * If column name could not be resolved then use primary key. |
||
946 | * |
||
947 | * @return string |
||
948 | */ |
||
949 | protected function getPrimaryKeyName() |
||
953 | |||
954 | /** |
||
955 | * Get column name from string. |
||
956 | * |
||
957 | * @param string $str |
||
958 | * @param bool $wantsAlias |
||
959 | * @return string |
||
960 | */ |
||
961 | protected function extractColumnName($str, $wantsAlias) |
||
979 | } |
||
980 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: