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 DataTableEngine |
||
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 | 'filter' => [], |
||
50 | 'order' => [], |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Extra/Added columns. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $extraColumns = []; |
||
59 | |||
60 | /** |
||
61 | * Total records. |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | protected $totalRecords = 0; |
||
66 | |||
67 | /** |
||
68 | * Total filtered records. |
||
69 | * |
||
70 | * @var int |
||
71 | */ |
||
72 | protected $filteredRecords = 0; |
||
73 | |||
74 | /** |
||
75 | * Auto-filter flag. |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $autoFilter = true; |
||
80 | |||
81 | /** |
||
82 | * Callback to override global search. |
||
83 | * |
||
84 | * @var callable |
||
85 | */ |
||
86 | protected $filterCallback; |
||
87 | |||
88 | /** |
||
89 | * Parameters to passed on filterCallback. |
||
90 | * |
||
91 | * @var mixed |
||
92 | */ |
||
93 | protected $filterCallbackParameters; |
||
94 | |||
95 | /** |
||
96 | * DT row templates container. |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $templates = [ |
||
101 | 'DT_RowId' => '', |
||
102 | 'DT_RowClass' => '', |
||
103 | 'DT_RowData' => [], |
||
104 | 'DT_RowAttr' => [], |
||
105 | ]; |
||
106 | |||
107 | /** |
||
108 | * Output transformer. |
||
109 | * |
||
110 | * @var \League\Fractal\TransformerAbstract |
||
111 | */ |
||
112 | protected $transformer = null; |
||
113 | |||
114 | /** |
||
115 | * Database prefix. |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | protected $prefix; |
||
120 | |||
121 | /** |
||
122 | * Database driver used. |
||
123 | * |
||
124 | * @var string |
||
125 | */ |
||
126 | protected $database; |
||
127 | |||
128 | /** |
||
129 | * [internal] Track if any filter was applied for at least one column. |
||
130 | * |
||
131 | * @var boolean |
||
132 | */ |
||
133 | protected $isFilterApplied = false; |
||
134 | |||
135 | /** |
||
136 | * Fractal serializer class. |
||
137 | * |
||
138 | * @var string|null |
||
139 | */ |
||
140 | protected $serializer = null; |
||
141 | |||
142 | /** |
||
143 | * Custom ordering callback. |
||
144 | * |
||
145 | * @var callable |
||
146 | */ |
||
147 | protected $orderCallback; |
||
148 | |||
149 | /** |
||
150 | * Skip paginate as needed. |
||
151 | * |
||
152 | * @var bool |
||
153 | */ |
||
154 | protected $skipPaging = false; |
||
155 | |||
156 | /** |
||
157 | * Array of data to append on json response. |
||
158 | * |
||
159 | * @var array |
||
160 | */ |
||
161 | protected $appends = []; |
||
162 | |||
163 | /** |
||
164 | * Flag for ordering NULLS LAST option. |
||
165 | * |
||
166 | * @var bool |
||
167 | */ |
||
168 | protected $nullsLast = false; |
||
169 | |||
170 | /** |
||
171 | * Add column in collection. |
||
172 | * |
||
173 | * @param string $name |
||
174 | * @param string|callable $content |
||
175 | * @param bool|int $order |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function addColumn($name, $content, $order = false) |
||
186 | |||
187 | /** |
||
188 | * Add DT row index column on response. |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function addIndexColumn() |
||
198 | |||
199 | /** |
||
200 | * Edit column's content. |
||
201 | * |
||
202 | * @param string $name |
||
203 | * @param string|callable $content |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function editColumn($name, $content) |
||
212 | |||
213 | /** |
||
214 | * Remove column from collection. |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function removeColumn() |
||
225 | |||
226 | /** |
||
227 | * Declare columns to escape values. |
||
228 | * |
||
229 | * @param string|array $columns |
||
230 | * @return $this |
||
231 | */ |
||
232 | public function escapeColumns($columns = '*') |
||
238 | |||
239 | /** |
||
240 | * Set columns that should not be escaped. |
||
241 | * |
||
242 | * @param array $columns |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function rawColumns(array $columns) |
||
251 | |||
252 | /** |
||
253 | * Sets DT_RowClass template. |
||
254 | * result: <tr class="output_from_your_template">. |
||
255 | * |
||
256 | * @param string|callable $content |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function setRowClass($content) |
||
265 | |||
266 | /** |
||
267 | * Sets DT_RowId template. |
||
268 | * result: <tr id="output_from_your_template">. |
||
269 | * |
||
270 | * @param string|callable $content |
||
271 | * @return $this |
||
272 | */ |
||
273 | public function setRowId($content) |
||
279 | |||
280 | /** |
||
281 | * Set DT_RowData templates. |
||
282 | * |
||
283 | * @param array $data |
||
284 | * @return $this |
||
285 | */ |
||
286 | public function setRowData(array $data) |
||
292 | |||
293 | /** |
||
294 | * Add DT_RowData template. |
||
295 | * |
||
296 | * @param string $key |
||
297 | * @param string|callable $value |
||
298 | * @return $this |
||
299 | */ |
||
300 | public function addRowData($key, $value) |
||
306 | |||
307 | /** |
||
308 | * Set DT_RowAttr templates. |
||
309 | * result: <tr attr1="attr1" attr2="attr2">. |
||
310 | * |
||
311 | * @param array $data |
||
312 | * @return $this |
||
313 | */ |
||
314 | public function setRowAttr(array $data) |
||
320 | |||
321 | /** |
||
322 | * Add DT_RowAttr template. |
||
323 | * |
||
324 | * @param string $key |
||
325 | * @param string|callable $value |
||
326 | * @return $this |
||
327 | */ |
||
328 | public function addRowAttr($key, $value) |
||
334 | |||
335 | /** |
||
336 | * Add custom filter handler for the give column. |
||
337 | * |
||
338 | * @param string $column |
||
339 | * @param callable $callback |
||
340 | * @return $this |
||
341 | */ |
||
342 | public function filterColumn($column, callable $callback) |
||
348 | |||
349 | /** |
||
350 | * Order each given columns versus the given custom sql. |
||
351 | * |
||
352 | * @param array $columns |
||
353 | * @param string $sql |
||
354 | * @param array $bindings |
||
355 | * @return $this |
||
356 | */ |
||
357 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
365 | |||
366 | /** |
||
367 | * Override default column ordering. |
||
368 | * |
||
369 | * @param string $column |
||
370 | * @param string $sql |
||
371 | * @param array $bindings |
||
372 | * @return $this |
||
373 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
374 | */ |
||
375 | public function orderColumn($column, $sql, $bindings = []) |
||
381 | |||
382 | /** |
||
383 | * Set data output transformer. |
||
384 | * |
||
385 | * @param \League\Fractal\TransformerAbstract $transformer |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function setTransformer($transformer) |
||
394 | |||
395 | /** |
||
396 | * Set fractal serializer class. |
||
397 | * |
||
398 | * @param string $serializer |
||
399 | * @return $this |
||
400 | */ |
||
401 | public function setSerializer($serializer) |
||
407 | |||
408 | /** |
||
409 | * Check if config uses case insensitive search. |
||
410 | * |
||
411 | * @return bool |
||
412 | */ |
||
413 | public function isCaseInsensitive() |
||
417 | |||
418 | /** |
||
419 | * Append data on json response. |
||
420 | * |
||
421 | * @param mixed $key |
||
422 | * @param mixed $value |
||
423 | * @return $this |
||
424 | */ |
||
425 | public function with($key, $value = '') |
||
437 | |||
438 | /** |
||
439 | * Override default ordering method with a closure callback. |
||
440 | * |
||
441 | * @param callable $closure |
||
442 | * @return $this |
||
443 | */ |
||
444 | public function order(callable $closure) |
||
450 | |||
451 | /** |
||
452 | * Update list of columns that is not allowed for search/sort. |
||
453 | * |
||
454 | * @param array $blacklist |
||
455 | * @return $this |
||
456 | */ |
||
457 | public function blacklist(array $blacklist) |
||
463 | |||
464 | /** |
||
465 | * Update list of columns that is allowed for search/sort. |
||
466 | * |
||
467 | * @param string|array $whitelist |
||
468 | * @return $this |
||
469 | */ |
||
470 | public function whitelist($whitelist = '*') |
||
476 | |||
477 | /** |
||
478 | * Set smart search config at runtime. |
||
479 | * |
||
480 | * @param bool $bool |
||
481 | * @return $this |
||
482 | */ |
||
483 | public function smart($bool = true) |
||
489 | |||
490 | /** |
||
491 | * Set total records manually. |
||
492 | * |
||
493 | * @param int $total |
||
494 | * @return $this |
||
495 | */ |
||
496 | public function setTotalRecords($total) |
||
502 | |||
503 | /** |
||
504 | * Skip pagination as needed. |
||
505 | * |
||
506 | * @return $this |
||
507 | */ |
||
508 | public function skipPaging() |
||
514 | |||
515 | /** |
||
516 | * Check if the current sql language is based on oracle syntax. |
||
517 | * |
||
518 | * @return bool |
||
519 | */ |
||
520 | public function isOracleSql() |
||
524 | |||
525 | /** |
||
526 | * Set datatables to do ordering with NULLS LAST option. |
||
527 | * |
||
528 | * @return $this |
||
529 | */ |
||
530 | public function orderByNullsLast() |
||
536 | |||
537 | /** |
||
538 | * Push a new column name to blacklist. |
||
539 | * |
||
540 | * @param string $column |
||
541 | * @return $this |
||
542 | */ |
||
543 | public function pushToBlacklist($column) |
||
551 | |||
552 | /** |
||
553 | * Check if column is blacklisted. |
||
554 | * |
||
555 | * @param string $column |
||
556 | * @return bool |
||
557 | */ |
||
558 | protected function isBlacklisted($column) |
||
572 | |||
573 | /** |
||
574 | * Perform necessary filters. |
||
575 | * |
||
576 | * @return void |
||
577 | */ |
||
578 | protected function filterRecords() |
||
591 | |||
592 | /** |
||
593 | * Apply pagination. |
||
594 | * |
||
595 | * @return void |
||
596 | */ |
||
597 | protected function paginate() |
||
603 | |||
604 | /** |
||
605 | * Transform output. |
||
606 | * |
||
607 | * @param mixed $output |
||
608 | * @return array |
||
609 | */ |
||
610 | protected function transform($output) |
||
642 | |||
643 | /** |
||
644 | * Get or create transformer serializer instance. |
||
645 | * |
||
646 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
647 | */ |
||
648 | protected function createSerializer() |
||
656 | |||
657 | /** |
||
658 | * Get or create transformer instance. |
||
659 | * |
||
660 | * @return \League\Fractal\TransformerAbstract |
||
661 | */ |
||
662 | protected function createTransformer() |
||
670 | |||
671 | /** |
||
672 | * Get processed data. |
||
673 | * |
||
674 | * @param bool|false $object |
||
675 | * @return array |
||
676 | */ |
||
677 | protected function getProcessedData($object = false) |
||
688 | |||
689 | /** |
||
690 | * Get columns definition. |
||
691 | * |
||
692 | * @return array |
||
693 | */ |
||
694 | protected function getColumnsDefinition() |
||
701 | |||
702 | /** |
||
703 | * Render json response. |
||
704 | * |
||
705 | * @param array $data |
||
706 | * @return \Illuminate\Http\JsonResponse |
||
707 | */ |
||
708 | protected function render(array $data) |
||
728 | |||
729 | /** |
||
730 | * Check if app is in debug mode. |
||
731 | * |
||
732 | * @return bool |
||
733 | */ |
||
734 | public function isDebugging() |
||
738 | |||
739 | /** |
||
740 | * Append debug parameters on output. |
||
741 | * |
||
742 | * @param array $output |
||
743 | * @return array |
||
744 | */ |
||
745 | abstract protected function showDebugger(array $output); |
||
746 | |||
747 | /** |
||
748 | * Return an error json response. |
||
749 | * |
||
750 | * @param \Exception $exception |
||
751 | * @return \Illuminate\Http\JsonResponse |
||
752 | * @throws \Yajra\Datatables\Exception |
||
753 | */ |
||
754 | protected function errorResponse(\Exception $exception) |
||
771 | |||
772 | /** |
||
773 | * Get monolog/logger instance. |
||
774 | * |
||
775 | * @return \Illuminate\Contracts\Logging\Log |
||
776 | */ |
||
777 | public function getLogger() |
||
783 | |||
784 | /** |
||
785 | * Set monolog/logger instance. |
||
786 | * |
||
787 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
788 | * @return $this |
||
789 | */ |
||
790 | public function setLogger(Log $logger) |
||
796 | |||
797 | /** |
||
798 | * Setup search keyword. |
||
799 | * |
||
800 | * @param string $value |
||
801 | * @return string |
||
802 | */ |
||
803 | protected function setupKeyword($value) |
||
818 | |||
819 | /** |
||
820 | * Check if config uses smart search. |
||
821 | * |
||
822 | * @return bool |
||
823 | */ |
||
824 | public function isSmartSearch() |
||
828 | |||
829 | /** |
||
830 | * Check if config uses wild card search. |
||
831 | * |
||
832 | * @return bool |
||
833 | */ |
||
834 | public function isWildcard() |
||
838 | |||
839 | /** |
||
840 | * Adds % wildcards to the given string. |
||
841 | * |
||
842 | * @param string $str |
||
843 | * @param bool $lowercase |
||
844 | * @return string |
||
845 | */ |
||
846 | protected function wildcardLikeString($str, $lowercase = true) |
||
863 | |||
864 | /** |
||
865 | * Update flags to disable global search. |
||
866 | * |
||
867 | * @param callable $callback |
||
868 | * @param mixed $parameters |
||
869 | * @param bool $autoFilter |
||
870 | */ |
||
871 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
878 | |||
879 | /** |
||
880 | * Get column name to be use for filtering and sorting. |
||
881 | * |
||
882 | * @param integer $index |
||
883 | * @param bool $wantsAlias |
||
884 | * @return string |
||
885 | */ |
||
886 | protected function getColumnName($index, $wantsAlias = false) |
||
901 | |||
902 | /** |
||
903 | * Get column name by order column index. |
||
904 | * |
||
905 | * @param int $index |
||
906 | * @return string |
||
907 | */ |
||
908 | protected function getColumnNameByIndex($index) |
||
914 | |||
915 | /** |
||
916 | * If column name could not be resolved then use primary key. |
||
917 | * |
||
918 | * @return string |
||
919 | */ |
||
920 | protected function getPrimaryKeyName() |
||
924 | |||
925 | /** |
||
926 | * Get column name from string. |
||
927 | * |
||
928 | * @param string $str |
||
929 | * @param bool $wantsAlias |
||
930 | * @return string |
||
931 | */ |
||
932 | protected function extractColumnName($str, $wantsAlias) |
||
950 | } |
||
951 |
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: