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 | * Database connection used. |
||
30 | * |
||
31 | * @var \Illuminate\Database\Connection |
||
32 | */ |
||
33 | protected $connection; |
||
34 | |||
35 | /** |
||
36 | * Builder object. |
||
37 | * |
||
38 | * @var \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
39 | */ |
||
40 | protected $query; |
||
41 | |||
42 | /** |
||
43 | * Query builder object. |
||
44 | * |
||
45 | * @var \Illuminate\Database\Query\Builder |
||
46 | */ |
||
47 | protected $builder; |
||
48 | |||
49 | /** |
||
50 | * Array of result columns/fields. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $columns = []; |
||
55 | |||
56 | /** |
||
57 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $columnDef = [ |
||
62 | 'index' => false, |
||
63 | 'append' => [], |
||
64 | 'edit' => [], |
||
65 | 'excess' => ['rn', 'row_num'], |
||
66 | 'filter' => [], |
||
67 | 'order' => [], |
||
68 | 'escape' => [], |
||
69 | 'blacklist' => ['password', 'remember_token'], |
||
70 | 'whitelist' => '*', |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * Query type. |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $query_type; |
||
79 | |||
80 | /** |
||
81 | * Extra/Added columns. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $extraColumns = []; |
||
86 | |||
87 | /** |
||
88 | * Total records. |
||
89 | * |
||
90 | * @var int |
||
91 | */ |
||
92 | protected $totalRecords = 0; |
||
93 | |||
94 | /** |
||
95 | * Total filtered records. |
||
96 | * |
||
97 | * @var int |
||
98 | */ |
||
99 | protected $filteredRecords = 0; |
||
100 | |||
101 | /** |
||
102 | * Auto-filter flag. |
||
103 | * |
||
104 | * @var bool |
||
105 | */ |
||
106 | protected $autoFilter = true; |
||
107 | |||
108 | /** |
||
109 | * Callback to override global search. |
||
110 | * |
||
111 | * @var callable |
||
112 | */ |
||
113 | protected $filterCallback; |
||
114 | |||
115 | /** |
||
116 | * Parameters to passed on filterCallback. |
||
117 | * |
||
118 | * @var mixed |
||
119 | */ |
||
120 | protected $filterCallbackParameters; |
||
121 | |||
122 | /** |
||
123 | * DT row templates container. |
||
124 | * |
||
125 | * @var array |
||
126 | */ |
||
127 | protected $templates = [ |
||
128 | 'DT_RowId' => '', |
||
129 | 'DT_RowClass' => '', |
||
130 | 'DT_RowData' => [], |
||
131 | 'DT_RowAttr' => [], |
||
132 | ]; |
||
133 | |||
134 | /** |
||
135 | * Output transformer. |
||
136 | * |
||
137 | * @var \League\Fractal\TransformerAbstract |
||
138 | */ |
||
139 | protected $transformer = null; |
||
140 | |||
141 | /** |
||
142 | * Database prefix |
||
143 | * |
||
144 | * @var string |
||
145 | */ |
||
146 | protected $prefix; |
||
147 | |||
148 | /** |
||
149 | * Database driver used. |
||
150 | * |
||
151 | * @var string |
||
152 | */ |
||
153 | protected $database; |
||
154 | |||
155 | /** |
||
156 | * [internal] Track if any filter was applied for at least one column |
||
157 | * |
||
158 | * @var boolean |
||
159 | */ |
||
160 | protected $isFilterApplied = false; |
||
161 | |||
162 | /** |
||
163 | * Fractal serializer class. |
||
164 | * |
||
165 | * @var string|null |
||
166 | */ |
||
167 | protected $serializer = null; |
||
168 | |||
169 | /** |
||
170 | * Custom ordering callback. |
||
171 | * |
||
172 | * @var callable |
||
173 | */ |
||
174 | protected $orderCallback; |
||
175 | |||
176 | /** |
||
177 | * Skip paginate as needed. |
||
178 | * |
||
179 | * @var bool |
||
180 | */ |
||
181 | protected $skipPaging = false; |
||
182 | |||
183 | /** |
||
184 | * Array of data to append on json response. |
||
185 | * |
||
186 | * @var array |
||
187 | */ |
||
188 | protected $appends = []; |
||
189 | |||
190 | /** |
||
191 | * Flag for ordering NULLS LAST option. |
||
192 | * |
||
193 | * @var bool |
||
194 | */ |
||
195 | protected $nullsLast = false; |
||
196 | |||
197 | /** |
||
198 | * Add column in collection. |
||
199 | * |
||
200 | * @param string $name |
||
201 | * @param string|callable $content |
||
202 | * @param bool|int $order |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function addColumn($name, $content, $order = false) |
||
213 | |||
214 | /** |
||
215 | * Add DT row index column on response. |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function addIndexColumn() |
||
225 | |||
226 | /** |
||
227 | * Edit column's content. |
||
228 | * |
||
229 | * @param string $name |
||
230 | * @param string|callable $content |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function editColumn($name, $content) |
||
239 | |||
240 | /** |
||
241 | * Remove column from collection. |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function removeColumn() |
||
252 | |||
253 | /** |
||
254 | * Declare columns to escape values. |
||
255 | * |
||
256 | * @param string|array $columns |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function escapeColumns($columns = '*') |
||
265 | |||
266 | /** |
||
267 | * Allows previous API calls where the methods were snake_case. |
||
268 | * Will convert a camelCase API call to a snake_case call. |
||
269 | * Allow query builder method to be used by the engine. |
||
270 | * |
||
271 | * @param string $name |
||
272 | * @param array $arguments |
||
273 | * @return mixed |
||
274 | */ |
||
275 | public function __call($name, $arguments) |
||
288 | |||
289 | /** |
||
290 | * Get Query Builder object. |
||
291 | * |
||
292 | * @param mixed $instance |
||
293 | * @return mixed |
||
294 | */ |
||
295 | public function getQueryBuilder($instance = null) |
||
307 | |||
308 | /** |
||
309 | * Check query type is a builder. |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function isQueryBuilder() |
||
317 | |||
318 | /** |
||
319 | * Sets DT_RowClass template. |
||
320 | * result: <tr class="output_from_your_template">. |
||
321 | * |
||
322 | * @param string|callable $content |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function setRowClass($content) |
||
331 | |||
332 | /** |
||
333 | * Sets DT_RowId template. |
||
334 | * result: <tr id="output_from_your_template">. |
||
335 | * |
||
336 | * @param string|callable $content |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function setRowId($content) |
||
345 | |||
346 | /** |
||
347 | * Set DT_RowData templates. |
||
348 | * |
||
349 | * @param array $data |
||
350 | * @return $this |
||
351 | */ |
||
352 | public function setRowData(array $data) |
||
358 | |||
359 | /** |
||
360 | * Add DT_RowData template. |
||
361 | * |
||
362 | * @param string $key |
||
363 | * @param string|callable $value |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function addRowData($key, $value) |
||
372 | |||
373 | /** |
||
374 | * Set DT_RowAttr templates. |
||
375 | * result: <tr attr1="attr1" attr2="attr2">. |
||
376 | * |
||
377 | * @param array $data |
||
378 | * @return $this |
||
379 | */ |
||
380 | public function setRowAttr(array $data) |
||
386 | |||
387 | /** |
||
388 | * Add DT_RowAttr template. |
||
389 | * |
||
390 | * @param string $key |
||
391 | * @param string|callable $value |
||
392 | * @return $this |
||
393 | */ |
||
394 | public function addRowAttr($key, $value) |
||
400 | |||
401 | /** |
||
402 | * Override default column filter search. |
||
403 | * |
||
404 | * @param string $column |
||
405 | * @param string|callable $method |
||
406 | * @return $this |
||
407 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
408 | * @internal string $1 Special variable that returns the requested search keyword. |
||
409 | */ |
||
410 | public function filterColumn($column, $method) |
||
417 | |||
418 | /** |
||
419 | * Order each given columns versus the given custom sql. |
||
420 | * |
||
421 | * @param array $columns |
||
422 | * @param string $sql |
||
423 | * @param array $bindings |
||
424 | * @return $this |
||
425 | */ |
||
426 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
434 | |||
435 | /** |
||
436 | * Override default column ordering. |
||
437 | * |
||
438 | * @param string $column |
||
439 | * @param string $sql |
||
440 | * @param array $bindings |
||
441 | * @return $this |
||
442 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
443 | */ |
||
444 | public function orderColumn($column, $sql, $bindings = []) |
||
450 | |||
451 | /** |
||
452 | * Set data output transformer. |
||
453 | * |
||
454 | * @param \League\Fractal\TransformerAbstract $transformer |
||
455 | * @return $this |
||
456 | */ |
||
457 | public function setTransformer($transformer) |
||
463 | |||
464 | /** |
||
465 | * Set fractal serializer class. |
||
466 | * |
||
467 | * @param string $serializer |
||
468 | * @return $this |
||
469 | */ |
||
470 | public function setSerializer($serializer) |
||
476 | |||
477 | /** |
||
478 | * Organizes works. |
||
479 | * |
||
480 | * @param bool $mDataSupport |
||
481 | * @param bool $orderFirst |
||
482 | * @return \Illuminate\Http\JsonResponse |
||
483 | */ |
||
484 | public function make($mDataSupport = false, $orderFirst = false) |
||
497 | |||
498 | /** |
||
499 | * Sort records. |
||
500 | * |
||
501 | * @param boolean $skip |
||
502 | * @return void |
||
503 | */ |
||
504 | protected function orderRecords($skip) |
||
510 | |||
511 | /** |
||
512 | * Perform necessary filters. |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | protected function filterRecords() |
||
529 | |||
530 | /** |
||
531 | * Apply pagination. |
||
532 | * |
||
533 | * @return void |
||
534 | */ |
||
535 | protected function paginate() |
||
541 | |||
542 | /** |
||
543 | * Render json response. |
||
544 | * |
||
545 | * @param bool $object |
||
546 | * @return \Illuminate\Http\JsonResponse |
||
547 | */ |
||
548 | protected function render($object = false) |
||
591 | |||
592 | /** |
||
593 | * Get or create transformer serializer instance. |
||
594 | * |
||
595 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
596 | */ |
||
597 | protected function createSerializer() |
||
605 | |||
606 | /** |
||
607 | * Get or create transformer instance. |
||
608 | * |
||
609 | * @return \League\Fractal\TransformerAbstract |
||
610 | */ |
||
611 | protected function createTransformer() |
||
619 | |||
620 | /** |
||
621 | * Get processed data |
||
622 | * |
||
623 | * @param bool|false $object |
||
624 | * @return array |
||
625 | */ |
||
626 | protected function getProcessedData($object = false) |
||
637 | |||
638 | /** |
||
639 | * Check if app is in debug mode. |
||
640 | * |
||
641 | * @return bool |
||
642 | */ |
||
643 | public function isDebugging() |
||
647 | |||
648 | /** |
||
649 | * Append debug parameters on output. |
||
650 | * |
||
651 | * @param array $output |
||
652 | * @return array |
||
653 | */ |
||
654 | protected function showDebugger(array $output) |
||
661 | |||
662 | /** |
||
663 | * Get config is case insensitive status. |
||
664 | * |
||
665 | * @return bool |
||
666 | */ |
||
667 | public function isCaseInsensitive() |
||
671 | |||
672 | /** |
||
673 | * Append data on json response. |
||
674 | * |
||
675 | * @param mixed $key |
||
676 | * @param mixed $value |
||
677 | * @return $this |
||
678 | */ |
||
679 | public function with($key, $value = '') |
||
691 | |||
692 | /** |
||
693 | * Override default ordering method with a closure callback. |
||
694 | * |
||
695 | * @param callable $closure |
||
696 | * @return $this |
||
697 | */ |
||
698 | public function order(callable $closure) |
||
704 | |||
705 | /** |
||
706 | * Update list of columns that is not allowed for search/sort. |
||
707 | * |
||
708 | * @param array $blacklist |
||
709 | * @return $this |
||
710 | */ |
||
711 | public function blacklist(array $blacklist) |
||
717 | |||
718 | /** |
||
719 | * Update list of columns that is not allowed for search/sort. |
||
720 | * |
||
721 | * @param string|array $whitelist |
||
722 | * @return $this |
||
723 | */ |
||
724 | public function whitelist($whitelist = '*') |
||
730 | |||
731 | /** |
||
732 | * Set smart search config at runtime. |
||
733 | * |
||
734 | * @param bool $bool |
||
735 | * @return $this |
||
736 | */ |
||
737 | public function smart($bool = true) |
||
743 | |||
744 | /** |
||
745 | * Set total records manually. |
||
746 | * |
||
747 | * @param int $total |
||
748 | * @return $this |
||
749 | */ |
||
750 | public function setTotalRecords($total) |
||
756 | |||
757 | /** |
||
758 | * Skip pagination as needed. |
||
759 | * |
||
760 | * @return $this |
||
761 | */ |
||
762 | public function skipPaging() |
||
768 | |||
769 | /** |
||
770 | * Check if the current sql language is based on oracle syntax. |
||
771 | * |
||
772 | * @return bool |
||
773 | */ |
||
774 | public function isOracleSql() |
||
778 | |||
779 | /** |
||
780 | * Set datatables to do ordering with NULLS LAST option. |
||
781 | * |
||
782 | * @return $this |
||
783 | */ |
||
784 | public function orderByNullsLast() |
||
790 | |||
791 | /** |
||
792 | * Setup search keyword. |
||
793 | * |
||
794 | * @param string $value |
||
795 | * @return string |
||
796 | */ |
||
797 | protected function setupKeyword($value) |
||
812 | |||
813 | /** |
||
814 | * Check if DataTables uses smart search. |
||
815 | * |
||
816 | * @return bool |
||
817 | */ |
||
818 | public function isSmartSearch() |
||
822 | |||
823 | /** |
||
824 | * Get config use wild card status. |
||
825 | * |
||
826 | * @return bool |
||
827 | */ |
||
828 | public function isWildcard() |
||
832 | |||
833 | /** |
||
834 | * Adds % wildcards to the given string. |
||
835 | * |
||
836 | * @param string $str |
||
837 | * @param bool $lowercase |
||
838 | * @return string |
||
839 | */ |
||
840 | protected function wildcardLikeString($str, $lowercase = true) |
||
857 | |||
858 | /** |
||
859 | * Update flags to disable global search |
||
860 | * |
||
861 | * @param callable $callback |
||
862 | * @param mixed $parameters |
||
863 | * @param bool $autoFilter |
||
864 | */ |
||
865 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
872 | |||
873 | /** |
||
874 | * Check if column is blacklisted. |
||
875 | * |
||
876 | * @param string $column |
||
877 | * @return bool |
||
878 | */ |
||
879 | protected function isBlacklisted($column) |
||
891 | |||
892 | /** |
||
893 | * Get column name to be use for filtering and sorting. |
||
894 | * |
||
895 | * @param integer $index |
||
896 | * @param bool $wantsAlias |
||
897 | * @return string |
||
898 | */ |
||
899 | protected function getColumnName($index, $wantsAlias = false) |
||
914 | |||
915 | /** |
||
916 | * Get column name by order column index. |
||
917 | * |
||
918 | * @param int $index |
||
919 | * @return mixed |
||
920 | */ |
||
921 | protected function getColumnNameByIndex($index) |
||
927 | |||
928 | /** |
||
929 | * If column name could not be resolved then use primary key. |
||
930 | * |
||
931 | * @return string |
||
932 | */ |
||
933 | protected function getPrimaryKeyName() |
||
941 | |||
942 | /** |
||
943 | * Check if the engine used was eloquent. |
||
944 | * |
||
945 | * @return bool |
||
946 | */ |
||
947 | public function isEloquent() |
||
951 | |||
952 | /** |
||
953 | * Get column name from string. |
||
954 | * |
||
955 | * @param string $str |
||
956 | * @param bool $wantsAlias |
||
957 | * @return string |
||
958 | */ |
||
959 | protected function extractColumnName($str, $wantsAlias) |
||
977 | } |
||
978 |
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: