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 \Closure |
||
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 \Closure |
||
173 | */ |
||
174 | protected $orderCallback; |
||
175 | |||
176 | /** |
||
177 | * Array of data to append on json response. |
||
178 | * |
||
179 | * @var array |
||
180 | */ |
||
181 | private $appends = []; |
||
182 | |||
183 | /** |
||
184 | * Setup search keyword. |
||
185 | * |
||
186 | * @param string $value |
||
187 | * @return string |
||
188 | */ |
||
189 | public function setupKeyword($value) |
||
204 | |||
205 | /** |
||
206 | * Check if DataTables uses smart search. |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | protected function isSmartSearch() |
||
214 | |||
215 | /** |
||
216 | * Get config use wild card status. |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function isWildcard() |
||
224 | |||
225 | /** |
||
226 | * Adds % wildcards to the given string. |
||
227 | * |
||
228 | * @param string $str |
||
229 | * @param bool $lowercase |
||
230 | * @return string |
||
231 | */ |
||
232 | public function wildcardLikeString($str, $lowercase = true) |
||
247 | |||
248 | /** |
||
249 | * Will prefix column if needed. |
||
250 | * |
||
251 | * @param string $column |
||
252 | * @return string |
||
253 | */ |
||
254 | public function prefixColumn($column) |
||
268 | |||
269 | /** |
||
270 | * Will look through the query and all it's joins to determine the table names. |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | public function tableNames() |
||
291 | |||
292 | /** |
||
293 | * Get Query Builder object. |
||
294 | * |
||
295 | * @param mixed $instance |
||
296 | * @return mixed |
||
297 | */ |
||
298 | public function getQueryBuilder($instance = null) |
||
310 | |||
311 | /** |
||
312 | * Check query type is a builder. |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | public function isQueryBuilder() |
||
320 | |||
321 | /** |
||
322 | * Add column in collection. |
||
323 | * |
||
324 | * @param string $name |
||
325 | * @param string|callable $content |
||
326 | * @param bool|int $order |
||
327 | * @return $this |
||
328 | */ |
||
329 | public function addColumn($name, $content, $order = false) |
||
337 | |||
338 | /** |
||
339 | * Add DT row index column on response. |
||
340 | * |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function addIndexColumn() |
||
349 | |||
350 | /** |
||
351 | * Edit column's content. |
||
352 | * |
||
353 | * @param string $name |
||
354 | * @param string|callable $content |
||
355 | * @return $this |
||
356 | */ |
||
357 | public function editColumn($name, $content) |
||
363 | |||
364 | /** |
||
365 | * Remove column from collection. |
||
366 | * |
||
367 | * @return $this |
||
368 | */ |
||
369 | public function removeColumn() |
||
376 | |||
377 | /** |
||
378 | * Declare columns to escape values. |
||
379 | * |
||
380 | * @param string|array $columns |
||
381 | * @return $this |
||
382 | */ |
||
383 | public function escapeColumns($columns = '*') |
||
389 | |||
390 | /** |
||
391 | * Allows previous API calls where the methods were snake_case. |
||
392 | * Will convert a camelCase API call to a snake_case call. |
||
393 | * Allow query builder method to be used by the engine. |
||
394 | * |
||
395 | * @param string $name |
||
396 | * @param array $arguments |
||
397 | * @return mixed |
||
398 | */ |
||
399 | public function __call($name, $arguments) |
||
412 | |||
413 | /** |
||
414 | * Sets DT_RowClass template. |
||
415 | * result: <tr class="output_from_your_template">. |
||
416 | * |
||
417 | * @param string|callable $content |
||
418 | * @return $this |
||
419 | */ |
||
420 | public function setRowClass($content) |
||
426 | |||
427 | /** |
||
428 | * Sets DT_RowId template. |
||
429 | * result: <tr id="output_from_your_template">. |
||
430 | * |
||
431 | * @param string|callable $content |
||
432 | * @return $this |
||
433 | */ |
||
434 | public function setRowId($content) |
||
440 | |||
441 | /** |
||
442 | * Set DT_RowData templates. |
||
443 | * |
||
444 | * @param array $data |
||
445 | * @return $this |
||
446 | */ |
||
447 | public function setRowData(array $data) |
||
453 | |||
454 | /** |
||
455 | * Add DT_RowData template. |
||
456 | * |
||
457 | * @param string $key |
||
458 | * @param string|callable $value |
||
459 | * @return $this |
||
460 | */ |
||
461 | public function addRowData($key, $value) |
||
467 | |||
468 | /** |
||
469 | * Set DT_RowAttr templates. |
||
470 | * result: <tr attr1="attr1" attr2="attr2">. |
||
471 | * |
||
472 | * @param array $data |
||
473 | * @return $this |
||
474 | */ |
||
475 | public function setRowAttr(array $data) |
||
481 | |||
482 | /** |
||
483 | * Add DT_RowAttr template. |
||
484 | * |
||
485 | * @param string $key |
||
486 | * @param string|callable $value |
||
487 | * @return $this |
||
488 | */ |
||
489 | public function addRowAttr($key, $value) |
||
495 | |||
496 | /** |
||
497 | * Override default column filter search. |
||
498 | * |
||
499 | * @param string $column |
||
500 | * @param string|Closure $method |
||
501 | * @return $this |
||
502 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
503 | * @internal string $1 Special variable that returns the requested search keyword. |
||
504 | */ |
||
505 | public function filterColumn($column, $method) |
||
512 | |||
513 | /** |
||
514 | * Order each given columns versus the given custom sql. |
||
515 | * |
||
516 | * @param array $columns |
||
517 | * @param string $sql |
||
518 | * @param array $bindings |
||
519 | * @return $this |
||
520 | */ |
||
521 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
529 | |||
530 | /** |
||
531 | * Override default column ordering. |
||
532 | * |
||
533 | * @param string $column |
||
534 | * @param string $sql |
||
535 | * @param array $bindings |
||
536 | * @return $this |
||
537 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
538 | */ |
||
539 | public function orderColumn($column, $sql, $bindings = []) |
||
545 | |||
546 | /** |
||
547 | * Set data output transformer. |
||
548 | * |
||
549 | * @param \League\Fractal\TransformerAbstract $transformer |
||
550 | * @return $this |
||
551 | */ |
||
552 | public function setTransformer($transformer) |
||
558 | |||
559 | /** |
||
560 | * Set fractal serializer class. |
||
561 | * |
||
562 | * @param string $serializer |
||
563 | * @return $this |
||
564 | */ |
||
565 | public function setSerializer($serializer) |
||
571 | |||
572 | /** |
||
573 | * Organizes works. |
||
574 | * |
||
575 | * @param bool $mDataSupport |
||
576 | * @param bool $orderFirst |
||
577 | * @return \Illuminate\Http\JsonResponse |
||
578 | */ |
||
579 | public function make($mDataSupport = false, $orderFirst = false) |
||
592 | |||
593 | /** |
||
594 | * Sort records. |
||
595 | * |
||
596 | * @param boolean $skip |
||
597 | * @return void |
||
598 | */ |
||
599 | public function orderRecords($skip) |
||
605 | |||
606 | /** |
||
607 | * Perform necessary filters. |
||
608 | * |
||
609 | * @return void |
||
610 | */ |
||
611 | public function filterRecords() |
||
624 | |||
625 | /** |
||
626 | * Apply pagination. |
||
627 | * |
||
628 | * @return void |
||
629 | */ |
||
630 | public function paginate() |
||
636 | |||
637 | /** |
||
638 | * Render json response. |
||
639 | * |
||
640 | * @param bool $object |
||
641 | * @return \Illuminate\Http\JsonResponse |
||
642 | */ |
||
643 | public function render($object = false) |
||
686 | |||
687 | /** |
||
688 | * Get or create transformer instance. |
||
689 | * |
||
690 | * @return \League\Fractal\TransformerAbstract |
||
691 | */ |
||
692 | protected function createTransformer() |
||
700 | |||
701 | /** |
||
702 | * Get processed data |
||
703 | * |
||
704 | * @param bool|false $object |
||
705 | * @return array |
||
706 | */ |
||
707 | private function getProcessedData($object = false) |
||
718 | |||
719 | /** |
||
720 | * Check if app is in debug mode. |
||
721 | * |
||
722 | * @return bool |
||
723 | */ |
||
724 | public function isDebugging() |
||
728 | |||
729 | /** |
||
730 | * Append debug parameters on output. |
||
731 | * |
||
732 | * @param array $output |
||
733 | * @return array |
||
734 | */ |
||
735 | public function showDebugger(array $output) |
||
742 | |||
743 | /** |
||
744 | * Update flags to disable global search |
||
745 | * |
||
746 | * @param \Closure $callback |
||
747 | * @param mixed $parameters |
||
748 | * @param bool $autoFilter |
||
749 | */ |
||
750 | public function overrideGlobalSearch(\Closure $callback, $parameters, $autoFilter = false) |
||
757 | |||
758 | /** |
||
759 | * Get config is case insensitive status. |
||
760 | * |
||
761 | * @return bool |
||
762 | */ |
||
763 | public function isCaseInsensitive() |
||
767 | |||
768 | /** |
||
769 | * Append data on json response. |
||
770 | * |
||
771 | * @param mixed $key |
||
772 | * @param mixed $value |
||
773 | * @return $this |
||
774 | */ |
||
775 | public function with($key, $value = '') |
||
787 | |||
788 | /** |
||
789 | * Override default ordering method with a closure callback. |
||
790 | * |
||
791 | * @param \Closure $closure |
||
792 | * @return $this |
||
793 | */ |
||
794 | public function order(\Closure $closure) |
||
800 | |||
801 | /** |
||
802 | * Update list of columns that is not allowed for search/sort. |
||
803 | * |
||
804 | * @param array $blacklist |
||
805 | * @return $this |
||
806 | */ |
||
807 | public function blacklist(array $blacklist) |
||
813 | |||
814 | /** |
||
815 | * Update list of columns that is not allowed for search/sort. |
||
816 | * |
||
817 | * @param string|array $whitelist |
||
818 | * @return $this |
||
819 | */ |
||
820 | public function whitelist($whitelist = '*') |
||
826 | |||
827 | /** |
||
828 | * Set smart search config at runtime. |
||
829 | * |
||
830 | * @param bool $bool |
||
831 | * @return $this |
||
832 | */ |
||
833 | public function smart($bool = true) |
||
839 | |||
840 | /** |
||
841 | * Check if column is blacklisted. |
||
842 | * |
||
843 | * @param string $column |
||
844 | * @return bool |
||
845 | */ |
||
846 | protected function isBlacklisted($column) |
||
858 | |||
859 | /** |
||
860 | * Get column name to be use for filtering and sorting. |
||
861 | * |
||
862 | * @param integer $index |
||
863 | * @param bool $wantsAlias |
||
864 | * @return string |
||
865 | */ |
||
866 | protected function getColumnName($index, $wantsAlias = false) |
||
881 | |||
882 | /** |
||
883 | * Get column name by order column index. |
||
884 | * |
||
885 | * @param int $index |
||
886 | * @return mixed |
||
887 | */ |
||
888 | protected function getColumnNameByIndex($index) |
||
894 | |||
895 | /** |
||
896 | * If column name could not be resolved then use primary key. |
||
897 | * |
||
898 | * @return string |
||
899 | */ |
||
900 | protected function getPrimaryKeyName() |
||
908 | |||
909 | /** |
||
910 | * Check if the engine used was eloquent. |
||
911 | * |
||
912 | * @return bool |
||
913 | */ |
||
914 | protected function isEloquent() |
||
918 | |||
919 | /** |
||
920 | * Get column name from string. |
||
921 | * |
||
922 | * @param string $str |
||
923 | * @param bool $wantsAlias |
||
924 | * @return string |
||
925 | */ |
||
926 | protected function extractColumnName($str, $wantsAlias) |
||
944 | |||
945 | /** |
||
946 | * Check if the current sql language is based on oracle syntax. |
||
947 | * |
||
948 | * @return bool |
||
949 | */ |
||
950 | protected function isOracleSql() |
||
954 | |||
955 | /** |
||
956 | * Set total records manually. |
||
957 | * |
||
958 | * @param int $total |
||
959 | * @return $this |
||
960 | */ |
||
961 | public function setTotalRecords($total) |
||
967 | } |
||
968 |
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: