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 | * Select trashed records in count function for models with soft deletes trait. |
||
110 | * By default we do not select soft deleted records |
||
111 | * |
||
112 | * @var bool |
||
113 | */ |
||
114 | protected $withTrashed = false; |
||
115 | |||
116 | /** |
||
117 | * Callback to override global search. |
||
118 | * |
||
119 | * @var \Closure |
||
120 | */ |
||
121 | protected $filterCallback; |
||
122 | |||
123 | /** |
||
124 | * Parameters to passed on filterCallback. |
||
125 | * |
||
126 | * @var mixed |
||
127 | */ |
||
128 | protected $filterCallbackParameters; |
||
129 | |||
130 | /** |
||
131 | * DT row templates container. |
||
132 | * |
||
133 | * @var array |
||
134 | */ |
||
135 | protected $templates = [ |
||
136 | 'DT_RowId' => '', |
||
137 | 'DT_RowClass' => '', |
||
138 | 'DT_RowData' => [], |
||
139 | 'DT_RowAttr' => [], |
||
140 | ]; |
||
141 | |||
142 | /** |
||
143 | * Output transformer. |
||
144 | * |
||
145 | * @var \League\Fractal\TransformerAbstract |
||
146 | */ |
||
147 | protected $transformer = null; |
||
148 | |||
149 | /** |
||
150 | * Database prefix |
||
151 | * |
||
152 | * @var string |
||
153 | */ |
||
154 | protected $prefix; |
||
155 | |||
156 | /** |
||
157 | * Database driver used. |
||
158 | * |
||
159 | * @var string |
||
160 | */ |
||
161 | protected $database; |
||
162 | |||
163 | /** |
||
164 | * [internal] Track if any filter was applied for at least one column |
||
165 | * |
||
166 | * @var boolean |
||
167 | */ |
||
168 | protected $isFilterApplied = false; |
||
169 | |||
170 | /** |
||
171 | * Fractal serializer class. |
||
172 | * |
||
173 | * @var string|null |
||
174 | */ |
||
175 | protected $serializer = null; |
||
176 | |||
177 | /** |
||
178 | * Custom ordering callback. |
||
179 | * |
||
180 | * @var \Closure |
||
181 | */ |
||
182 | protected $orderCallback; |
||
183 | |||
184 | /** |
||
185 | * Skip paginate as needed. |
||
186 | * |
||
187 | * @var bool |
||
188 | */ |
||
189 | protected $skipPaging = false; |
||
190 | |||
191 | /** |
||
192 | * Array of data to append on json response. |
||
193 | * |
||
194 | * @var array |
||
195 | */ |
||
196 | private $appends = []; |
||
197 | |||
198 | /** |
||
199 | * Setup search keyword. |
||
200 | * |
||
201 | * @param string $value |
||
202 | * @return string |
||
203 | */ |
||
204 | public function setupKeyword($value) |
||
219 | |||
220 | /** |
||
221 | * Check if DataTables uses smart search. |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | protected function isSmartSearch() |
||
229 | |||
230 | /** |
||
231 | * Get config use wild card status. |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function isWildcard() |
||
239 | |||
240 | /** |
||
241 | * Adds % wildcards to the given string. |
||
242 | * |
||
243 | * @param string $str |
||
244 | * @param bool $lowercase |
||
245 | * @return string |
||
246 | */ |
||
247 | public function wildcardLikeString($str, $lowercase = true) |
||
262 | |||
263 | /** |
||
264 | * Will prefix column if needed. |
||
265 | * |
||
266 | * @param string $column |
||
267 | * @return string |
||
268 | */ |
||
269 | public function prefixColumn($column) |
||
283 | |||
284 | /** |
||
285 | * Will look through the query and all it's joins to determine the table names. |
||
286 | * |
||
287 | * @return array |
||
288 | */ |
||
289 | public function tableNames() |
||
306 | |||
307 | /** |
||
308 | * Get Query Builder object. |
||
309 | * |
||
310 | * @param mixed $instance |
||
311 | * @return mixed |
||
312 | */ |
||
313 | public function getQueryBuilder($instance = null) |
||
325 | |||
326 | /** |
||
327 | * Check query type is a builder. |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | public function isQueryBuilder() |
||
335 | |||
336 | /** |
||
337 | * Add column in collection. |
||
338 | * |
||
339 | * @param string $name |
||
340 | * @param string|callable $content |
||
341 | * @param bool|int $order |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function addColumn($name, $content, $order = false) |
||
352 | |||
353 | /** |
||
354 | * Add DT row index column on response. |
||
355 | * |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function addIndexColumn() |
||
364 | |||
365 | /** |
||
366 | * Edit column's content. |
||
367 | * |
||
368 | * @param string $name |
||
369 | * @param string|callable $content |
||
370 | * @return $this |
||
371 | */ |
||
372 | public function editColumn($name, $content) |
||
378 | |||
379 | /** |
||
380 | * Remove column from collection. |
||
381 | * |
||
382 | * @return $this |
||
383 | */ |
||
384 | public function removeColumn() |
||
391 | |||
392 | /** |
||
393 | * Declare columns to escape values. |
||
394 | * |
||
395 | * @param string|array $columns |
||
396 | * @return $this |
||
397 | */ |
||
398 | public function escapeColumns($columns = '*') |
||
404 | |||
405 | /** |
||
406 | * Change withTrashed flag value. |
||
407 | * |
||
408 | * @param bool $withTrashed |
||
409 | * @return $this |
||
410 | */ |
||
411 | public function withTrashed($withTrashed = true) |
||
417 | |||
418 | /** |
||
419 | * Allows previous API calls where the methods were snake_case. |
||
420 | * Will convert a camelCase API call to a snake_case call. |
||
421 | * Allow query builder method to be used by the engine. |
||
422 | * |
||
423 | * @param string $name |
||
424 | * @param array $arguments |
||
425 | * @return mixed |
||
426 | */ |
||
427 | public function __call($name, $arguments) |
||
440 | |||
441 | /** |
||
442 | * Sets DT_RowClass template. |
||
443 | * result: <tr class="output_from_your_template">. |
||
444 | * |
||
445 | * @param string|callable $content |
||
446 | * @return $this |
||
447 | */ |
||
448 | public function setRowClass($content) |
||
454 | |||
455 | /** |
||
456 | * Sets DT_RowId template. |
||
457 | * result: <tr id="output_from_your_template">. |
||
458 | * |
||
459 | * @param string|callable $content |
||
460 | * @return $this |
||
461 | */ |
||
462 | public function setRowId($content) |
||
468 | |||
469 | /** |
||
470 | * Set DT_RowData templates. |
||
471 | * |
||
472 | * @param array $data |
||
473 | * @return $this |
||
474 | */ |
||
475 | public function setRowData(array $data) |
||
481 | |||
482 | /** |
||
483 | * Add DT_RowData template. |
||
484 | * |
||
485 | * @param string $key |
||
486 | * @param string|callable $value |
||
487 | * @return $this |
||
488 | */ |
||
489 | public function addRowData($key, $value) |
||
495 | |||
496 | /** |
||
497 | * Set DT_RowAttr templates. |
||
498 | * result: <tr attr1="attr1" attr2="attr2">. |
||
499 | * |
||
500 | * @param array $data |
||
501 | * @return $this |
||
502 | */ |
||
503 | public function setRowAttr(array $data) |
||
509 | |||
510 | /** |
||
511 | * Add DT_RowAttr template. |
||
512 | * |
||
513 | * @param string $key |
||
514 | * @param string|callable $value |
||
515 | * @return $this |
||
516 | */ |
||
517 | public function addRowAttr($key, $value) |
||
523 | |||
524 | /** |
||
525 | * Override default column filter search. |
||
526 | * |
||
527 | * @param string $column |
||
528 | * @param string|Closure $method |
||
529 | * @return $this |
||
530 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
531 | * @internal string $1 Special variable that returns the requested search keyword. |
||
532 | */ |
||
533 | public function filterColumn($column, $method) |
||
540 | |||
541 | /** |
||
542 | * Order each given columns versus the given custom sql. |
||
543 | * |
||
544 | * @param array $columns |
||
545 | * @param string $sql |
||
546 | * @param array $bindings |
||
547 | * @return $this |
||
548 | */ |
||
549 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
557 | |||
558 | /** |
||
559 | * Override default column ordering. |
||
560 | * |
||
561 | * @param string $column |
||
562 | * @param string $sql |
||
563 | * @param array $bindings |
||
564 | * @return $this |
||
565 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
566 | */ |
||
567 | public function orderColumn($column, $sql, $bindings = []) |
||
573 | |||
574 | /** |
||
575 | * Set data output transformer. |
||
576 | * |
||
577 | * @param \League\Fractal\TransformerAbstract $transformer |
||
578 | * @return $this |
||
579 | */ |
||
580 | public function setTransformer($transformer) |
||
586 | |||
587 | /** |
||
588 | * Set fractal serializer class. |
||
589 | * |
||
590 | * @param string $serializer |
||
591 | * @return $this |
||
592 | */ |
||
593 | public function setSerializer($serializer) |
||
599 | |||
600 | /** |
||
601 | * Organizes works. |
||
602 | * |
||
603 | * @param bool $mDataSupport |
||
604 | * @param bool $orderFirst |
||
605 | * @return \Illuminate\Http\JsonResponse |
||
606 | */ |
||
607 | public function make($mDataSupport = false, $orderFirst = false) |
||
620 | |||
621 | /** |
||
622 | * Sort records. |
||
623 | * |
||
624 | * @param boolean $skip |
||
625 | * @return void |
||
626 | */ |
||
627 | public function orderRecords($skip) |
||
633 | |||
634 | /** |
||
635 | * Perform necessary filters. |
||
636 | * |
||
637 | * @return void |
||
638 | */ |
||
639 | public function filterRecords() |
||
652 | |||
653 | /** |
||
654 | * Apply pagination. |
||
655 | * |
||
656 | * @return void |
||
657 | */ |
||
658 | public function paginate() |
||
664 | |||
665 | /** |
||
666 | * Render json response. |
||
667 | * |
||
668 | * @param bool $object |
||
669 | * @return \Illuminate\Http\JsonResponse |
||
670 | */ |
||
671 | public function render($object = false) |
||
714 | |||
715 | /** |
||
716 | * Get or create transformer instance. |
||
717 | * |
||
718 | * @return \League\Fractal\TransformerAbstract |
||
719 | */ |
||
720 | protected function createTransformer() |
||
728 | |||
729 | /** |
||
730 | * Get processed data |
||
731 | * |
||
732 | * @param bool|false $object |
||
733 | * @return array |
||
734 | */ |
||
735 | private function getProcessedData($object = false) |
||
746 | |||
747 | /** |
||
748 | * Check if app is in debug mode. |
||
749 | * |
||
750 | * @return bool |
||
751 | */ |
||
752 | public function isDebugging() |
||
756 | |||
757 | /** |
||
758 | * Append debug parameters on output. |
||
759 | * |
||
760 | * @param array $output |
||
761 | * @return array |
||
762 | */ |
||
763 | public function showDebugger(array $output) |
||
770 | |||
771 | /** |
||
772 | * Update flags to disable global search |
||
773 | * |
||
774 | * @param \Closure $callback |
||
775 | * @param mixed $parameters |
||
776 | * @param bool $autoFilter |
||
777 | */ |
||
778 | public function overrideGlobalSearch(\Closure $callback, $parameters, $autoFilter = false) |
||
785 | |||
786 | /** |
||
787 | * Get config is case insensitive status. |
||
788 | * |
||
789 | * @return bool |
||
790 | */ |
||
791 | public function isCaseInsensitive() |
||
795 | |||
796 | /** |
||
797 | * Append data on json response. |
||
798 | * |
||
799 | * @param mixed $key |
||
800 | * @param mixed $value |
||
801 | * @return $this |
||
802 | */ |
||
803 | public function with($key, $value = '') |
||
815 | |||
816 | /** |
||
817 | * Override default ordering method with a closure callback. |
||
818 | * |
||
819 | * @param \Closure $closure |
||
820 | * @return $this |
||
821 | */ |
||
822 | public function order(\Closure $closure) |
||
828 | |||
829 | /** |
||
830 | * Update list of columns that is not allowed for search/sort. |
||
831 | * |
||
832 | * @param array $blacklist |
||
833 | * @return $this |
||
834 | */ |
||
835 | public function blacklist(array $blacklist) |
||
841 | |||
842 | /** |
||
843 | * Update list of columns that is not allowed for search/sort. |
||
844 | * |
||
845 | * @param string|array $whitelist |
||
846 | * @return $this |
||
847 | */ |
||
848 | public function whitelist($whitelist = '*') |
||
854 | |||
855 | /** |
||
856 | * Set smart search config at runtime. |
||
857 | * |
||
858 | * @param bool $bool |
||
859 | * @return $this |
||
860 | */ |
||
861 | public function smart($bool = true) |
||
867 | |||
868 | /** |
||
869 | * Set total records manually. |
||
870 | * |
||
871 | * @param int $total |
||
872 | * @return $this |
||
873 | */ |
||
874 | public function setTotalRecords($total) |
||
880 | |||
881 | /** |
||
882 | * Skip pagination as needed. |
||
883 | */ |
||
884 | public function skipPaging() |
||
890 | |||
891 | /** |
||
892 | * Check if column is blacklisted. |
||
893 | * |
||
894 | * @param string $column |
||
895 | * @return bool |
||
896 | */ |
||
897 | protected function isBlacklisted($column) |
||
909 | |||
910 | /** |
||
911 | * Get column name to be use for filtering and sorting. |
||
912 | * |
||
913 | * @param integer $index |
||
914 | * @param bool $wantsAlias |
||
915 | * @return string |
||
916 | */ |
||
917 | protected function getColumnName($index, $wantsAlias = false) |
||
932 | |||
933 | /** |
||
934 | * Get column name by order column index. |
||
935 | * |
||
936 | * @param int $index |
||
937 | * @return mixed |
||
938 | */ |
||
939 | protected function getColumnNameByIndex($index) |
||
945 | |||
946 | /** |
||
947 | * If column name could not be resolved then use primary key. |
||
948 | * |
||
949 | * @return string |
||
950 | */ |
||
951 | protected function getPrimaryKeyName() |
||
959 | |||
960 | /** |
||
961 | * Check if the engine used was eloquent. |
||
962 | * |
||
963 | * @return bool |
||
964 | */ |
||
965 | protected function isEloquent() |
||
969 | |||
970 | /** |
||
971 | * Get column name from string. |
||
972 | * |
||
973 | * @param string $str |
||
974 | * @param bool $wantsAlias |
||
975 | * @return string |
||
976 | */ |
||
977 | protected function extractColumnName($str, $wantsAlias) |
||
995 | |||
996 | /** |
||
997 | * Check if the current sql language is based on oracle syntax. |
||
998 | * |
||
999 | * @return bool |
||
1000 | */ |
||
1001 | protected function isOracleSql() |
||
1005 | } |
||
1006 |
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: