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 | * 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 | private $appends = []; |
||
189 | |||
190 | /** |
||
191 | * Setup search keyword. |
||
192 | * |
||
193 | * @param string $value |
||
194 | * @return string |
||
195 | */ |
||
196 | public function setupKeyword($value) |
||
211 | |||
212 | /** |
||
213 | * Check if DataTables uses smart search. |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | protected function isSmartSearch() |
||
221 | |||
222 | /** |
||
223 | * Get config use wild card status. |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function isWildcard() |
||
231 | |||
232 | /** |
||
233 | * Adds % wildcards to the given string. |
||
234 | * |
||
235 | * @param string $str |
||
236 | * @param bool $lowercase |
||
237 | * @return string |
||
238 | */ |
||
239 | public function wildcardLikeString($str, $lowercase = true) |
||
254 | |||
255 | /** |
||
256 | * Will prefix column if needed. |
||
257 | * |
||
258 | * @param string $column |
||
259 | * @return string |
||
260 | */ |
||
261 | public function prefixColumn($column) |
||
275 | |||
276 | /** |
||
277 | * Will look through the query and all it's joins to determine the table names. |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | public function tableNames() |
||
298 | |||
299 | /** |
||
300 | * Get Query Builder object. |
||
301 | * |
||
302 | * @param mixed $instance |
||
303 | * @return mixed |
||
304 | */ |
||
305 | public function getQueryBuilder($instance = null) |
||
317 | |||
318 | /** |
||
319 | * Check query type is a builder. |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function isQueryBuilder() |
||
327 | |||
328 | /** |
||
329 | * Add column in collection. |
||
330 | * |
||
331 | * @param string $name |
||
332 | * @param string|callable $content |
||
333 | * @param bool|int $order |
||
334 | * @return $this |
||
335 | */ |
||
336 | public function addColumn($name, $content, $order = false) |
||
344 | |||
345 | /** |
||
346 | * Add DT row index column on response. |
||
347 | * |
||
348 | * @return $this |
||
349 | */ |
||
350 | public function addIndexColumn() |
||
356 | |||
357 | /** |
||
358 | * Edit column's content. |
||
359 | * |
||
360 | * @param string $name |
||
361 | * @param string|callable $content |
||
362 | * @return $this |
||
363 | */ |
||
364 | public function editColumn($name, $content) |
||
370 | |||
371 | /** |
||
372 | * Remove column from collection. |
||
373 | * |
||
374 | * @return $this |
||
375 | */ |
||
376 | public function removeColumn() |
||
383 | |||
384 | /** |
||
385 | * Declare columns to escape values. |
||
386 | * |
||
387 | * @param string|array $columns |
||
388 | * @return $this |
||
389 | */ |
||
390 | public function escapeColumns($columns = '*') |
||
396 | |||
397 | /** |
||
398 | * Allows previous API calls where the methods were snake_case. |
||
399 | * Will convert a camelCase API call to a snake_case call. |
||
400 | * Allow query builder method to be used by the engine. |
||
401 | * |
||
402 | * @param string $name |
||
403 | * @param array $arguments |
||
404 | * @return mixed |
||
405 | */ |
||
406 | public function __call($name, $arguments) |
||
419 | |||
420 | /** |
||
421 | * Sets DT_RowClass template. |
||
422 | * result: <tr class="output_from_your_template">. |
||
423 | * |
||
424 | * @param string|callable $content |
||
425 | * @return $this |
||
426 | */ |
||
427 | public function setRowClass($content) |
||
433 | |||
434 | /** |
||
435 | * Sets DT_RowId template. |
||
436 | * result: <tr id="output_from_your_template">. |
||
437 | * |
||
438 | * @param string|callable $content |
||
439 | * @return $this |
||
440 | */ |
||
441 | public function setRowId($content) |
||
447 | |||
448 | /** |
||
449 | * Set DT_RowData templates. |
||
450 | * |
||
451 | * @param array $data |
||
452 | * @return $this |
||
453 | */ |
||
454 | public function setRowData(array $data) |
||
460 | |||
461 | /** |
||
462 | * Add DT_RowData template. |
||
463 | * |
||
464 | * @param string $key |
||
465 | * @param string|callable $value |
||
466 | * @return $this |
||
467 | */ |
||
468 | public function addRowData($key, $value) |
||
474 | |||
475 | /** |
||
476 | * Set DT_RowAttr templates. |
||
477 | * result: <tr attr1="attr1" attr2="attr2">. |
||
478 | * |
||
479 | * @param array $data |
||
480 | * @return $this |
||
481 | */ |
||
482 | public function setRowAttr(array $data) |
||
488 | |||
489 | /** |
||
490 | * Add DT_RowAttr template. |
||
491 | * |
||
492 | * @param string $key |
||
493 | * @param string|callable $value |
||
494 | * @return $this |
||
495 | */ |
||
496 | public function addRowAttr($key, $value) |
||
502 | |||
503 | /** |
||
504 | * Override default column filter search. |
||
505 | * |
||
506 | * @param string $column |
||
507 | * @param string|Closure $method |
||
508 | * @return $this |
||
509 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
510 | * @internal string $1 Special variable that returns the requested search keyword. |
||
511 | */ |
||
512 | public function filterColumn($column, $method) |
||
519 | |||
520 | /** |
||
521 | * Order each given columns versus the given custom sql. |
||
522 | * |
||
523 | * @param array $columns |
||
524 | * @param string $sql |
||
525 | * @param array $bindings |
||
526 | * @return $this |
||
527 | */ |
||
528 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
536 | |||
537 | /** |
||
538 | * Override default column ordering. |
||
539 | * |
||
540 | * @param string $column |
||
541 | * @param string $sql |
||
542 | * @param array $bindings |
||
543 | * @return $this |
||
544 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
545 | */ |
||
546 | public function orderColumn($column, $sql, $bindings = []) |
||
552 | |||
553 | /** |
||
554 | * Set data output transformer. |
||
555 | * |
||
556 | * @param \League\Fractal\TransformerAbstract $transformer |
||
557 | * @return $this |
||
558 | */ |
||
559 | public function setTransformer($transformer) |
||
565 | |||
566 | /** |
||
567 | * Set fractal serializer class. |
||
568 | * |
||
569 | * @param string $serializer |
||
570 | * @return $this |
||
571 | */ |
||
572 | public function setSerializer($serializer) |
||
578 | |||
579 | /** |
||
580 | * Organizes works. |
||
581 | * |
||
582 | * @param bool $mDataSupport |
||
583 | * @param bool $orderFirst |
||
584 | * @return \Illuminate\Http\JsonResponse |
||
585 | */ |
||
586 | public function make($mDataSupport = false, $orderFirst = false) |
||
599 | |||
600 | /** |
||
601 | * Sort records. |
||
602 | * |
||
603 | * @param boolean $skip |
||
604 | * @return void |
||
605 | */ |
||
606 | public function orderRecords($skip) |
||
612 | |||
613 | /** |
||
614 | * Perform necessary filters. |
||
615 | * |
||
616 | * @return void |
||
617 | */ |
||
618 | public function filterRecords() |
||
631 | |||
632 | /** |
||
633 | * Apply pagination. |
||
634 | * |
||
635 | * @return void |
||
636 | */ |
||
637 | public function paginate() |
||
643 | |||
644 | /** |
||
645 | * Render json response. |
||
646 | * |
||
647 | * @param bool $object |
||
648 | * @return \Illuminate\Http\JsonResponse |
||
649 | */ |
||
650 | public function render($object = false) |
||
693 | |||
694 | /** |
||
695 | * Get or create transformer instance. |
||
696 | * |
||
697 | * @return \League\Fractal\TransformerAbstract |
||
698 | */ |
||
699 | protected function createTransformer() |
||
707 | |||
708 | /** |
||
709 | * Get processed data |
||
710 | * |
||
711 | * @param bool|false $object |
||
712 | * @return array |
||
713 | */ |
||
714 | private function getProcessedData($object = false) |
||
725 | |||
726 | /** |
||
727 | * Check if app is in debug mode. |
||
728 | * |
||
729 | * @return bool |
||
730 | */ |
||
731 | public function isDebugging() |
||
735 | |||
736 | /** |
||
737 | * Append debug parameters on output. |
||
738 | * |
||
739 | * @param array $output |
||
740 | * @return array |
||
741 | */ |
||
742 | public function showDebugger(array $output) |
||
749 | |||
750 | /** |
||
751 | * Update flags to disable global search |
||
752 | * |
||
753 | * @param \Closure $callback |
||
754 | * @param mixed $parameters |
||
755 | * @param bool $autoFilter |
||
756 | */ |
||
757 | public function overrideGlobalSearch(\Closure $callback, $parameters, $autoFilter = false) |
||
764 | |||
765 | /** |
||
766 | * Get config is case insensitive status. |
||
767 | * |
||
768 | * @return bool |
||
769 | */ |
||
770 | public function isCaseInsensitive() |
||
774 | |||
775 | /** |
||
776 | * Append data on json response. |
||
777 | * |
||
778 | * @param mixed $key |
||
779 | * @param mixed $value |
||
780 | * @return $this |
||
781 | */ |
||
782 | public function with($key, $value = '') |
||
794 | |||
795 | /** |
||
796 | * Override default ordering method with a closure callback. |
||
797 | * |
||
798 | * @param \Closure $closure |
||
799 | * @return $this |
||
800 | */ |
||
801 | public function order(\Closure $closure) |
||
807 | |||
808 | /** |
||
809 | * Update list of columns that is not allowed for search/sort. |
||
810 | * |
||
811 | * @param array $blacklist |
||
812 | * @return $this |
||
813 | */ |
||
814 | public function blacklist(array $blacklist) |
||
820 | |||
821 | /** |
||
822 | * Update list of columns that is not allowed for search/sort. |
||
823 | * |
||
824 | * @param string|array $whitelist |
||
825 | * @return $this |
||
826 | */ |
||
827 | public function whitelist($whitelist = '*') |
||
833 | |||
834 | /** |
||
835 | * Set smart search config at runtime. |
||
836 | * |
||
837 | * @param bool $bool |
||
838 | * @return $this |
||
839 | */ |
||
840 | public function smart($bool = true) |
||
846 | |||
847 | /** |
||
848 | * Set total records manually. |
||
849 | * |
||
850 | * @param int $total |
||
851 | * @return $this |
||
852 | */ |
||
853 | public function setTotalRecords($total) |
||
859 | |||
860 | /** |
||
861 | * Skip pagination as needed. |
||
862 | */ |
||
863 | public function skipPaging() |
||
869 | |||
870 | /** |
||
871 | * Check if column is blacklisted. |
||
872 | * |
||
873 | * @param string $column |
||
874 | * @return bool |
||
875 | */ |
||
876 | protected function isBlacklisted($column) |
||
888 | |||
889 | /** |
||
890 | * Get column name to be use for filtering and sorting. |
||
891 | * |
||
892 | * @param integer $index |
||
893 | * @param bool $wantsAlias |
||
894 | * @return string |
||
895 | */ |
||
896 | protected function getColumnName($index, $wantsAlias = false) |
||
911 | |||
912 | /** |
||
913 | * Get column name by order column index. |
||
914 | * |
||
915 | * @param int $index |
||
916 | * @return mixed |
||
917 | */ |
||
918 | protected function getColumnNameByIndex($index) |
||
924 | |||
925 | /** |
||
926 | * If column name could not be resolved then use primary key. |
||
927 | * |
||
928 | * @return string |
||
929 | */ |
||
930 | protected function getPrimaryKeyName() |
||
938 | |||
939 | /** |
||
940 | * Check if the engine used was eloquent. |
||
941 | * |
||
942 | * @return bool |
||
943 | */ |
||
944 | protected function isEloquent() |
||
948 | |||
949 | /** |
||
950 | * Get column name from string. |
||
951 | * |
||
952 | * @param string $str |
||
953 | * @param bool $wantsAlias |
||
954 | * @return string |
||
955 | */ |
||
956 | protected function extractColumnName($str, $wantsAlias) |
||
974 | |||
975 | /** |
||
976 | * Check if the current sql language is based on oracle syntax. |
||
977 | * |
||
978 | * @return bool |
||
979 | */ |
||
980 | protected function isOracleSql() |
||
984 | } |
||
985 |
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: