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 |
||
20 | abstract class BaseEngine implements DataTableEngine |
||
21 | { |
||
22 | /** |
||
23 | * Datatables Request object. |
||
24 | * |
||
25 | * @var \Yajra\Datatables\Request |
||
26 | */ |
||
27 | public $request; |
||
28 | |||
29 | /** |
||
30 | * @var \Illuminate\Contracts\Logging\Log |
||
31 | */ |
||
32 | protected $logger; |
||
33 | |||
34 | /** |
||
35 | * Array of result columns/fields. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $columns = []; |
||
40 | |||
41 | /** |
||
42 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $columnDef = [ |
||
47 | 'index' => false, |
||
48 | 'append' => [], |
||
49 | 'edit' => [], |
||
50 | 'filter' => [], |
||
51 | 'order' => [], |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Extra/Added columns. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $extraColumns = []; |
||
60 | |||
61 | /** |
||
62 | * Total records. |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $totalRecords = 0; |
||
67 | |||
68 | /** |
||
69 | * Total filtered records. |
||
70 | * |
||
71 | * @var int |
||
72 | */ |
||
73 | protected $filteredRecords = 0; |
||
74 | |||
75 | /** |
||
76 | * Auto-filter flag. |
||
77 | * |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected $autoFilter = true; |
||
81 | |||
82 | /** |
||
83 | * Callback to override global search. |
||
84 | * |
||
85 | * @var callable |
||
86 | */ |
||
87 | protected $filterCallback; |
||
88 | |||
89 | /** |
||
90 | * Parameters to passed on filterCallback. |
||
91 | * |
||
92 | * @var mixed |
||
93 | */ |
||
94 | protected $filterCallbackParameters; |
||
95 | |||
96 | /** |
||
97 | * DT row templates container. |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $templates = [ |
||
102 | 'DT_RowId' => '', |
||
103 | 'DT_RowClass' => '', |
||
104 | 'DT_RowData' => [], |
||
105 | 'DT_RowAttr' => [], |
||
106 | ]; |
||
107 | |||
108 | /** |
||
109 | * Output transformer. |
||
110 | * |
||
111 | * @var \League\Fractal\TransformerAbstract |
||
112 | */ |
||
113 | protected $transformer = null; |
||
114 | |||
115 | /** |
||
116 | * [internal] Track if any filter was applied for at least one column. |
||
117 | * |
||
118 | * @var boolean |
||
119 | */ |
||
120 | protected $isFilterApplied = false; |
||
121 | |||
122 | /** |
||
123 | * Fractal serializer class. |
||
124 | * |
||
125 | * @var string|null |
||
126 | */ |
||
127 | protected $serializer = null; |
||
128 | |||
129 | /** |
||
130 | * Custom ordering callback. |
||
131 | * |
||
132 | * @var callable |
||
133 | */ |
||
134 | protected $orderCallback; |
||
135 | |||
136 | /** |
||
137 | * Skip paginate as needed. |
||
138 | * |
||
139 | * @var bool |
||
140 | */ |
||
141 | protected $skipPaging = false; |
||
142 | |||
143 | /** |
||
144 | * Array of data to append on json response. |
||
145 | * |
||
146 | * @var array |
||
147 | */ |
||
148 | protected $appends = []; |
||
149 | |||
150 | /** |
||
151 | * Flag for ordering NULLS LAST option. |
||
152 | * |
||
153 | * @var bool |
||
154 | */ |
||
155 | protected $nullsLast = false; |
||
156 | |||
157 | /** |
||
158 | * Add column in collection. |
||
159 | * |
||
160 | * @param string $name |
||
161 | * @param string|callable $content |
||
162 | * @param bool|int $order |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function addColumn($name, $content, $order = false) |
||
173 | |||
174 | /** |
||
175 | * Add DT row index column on response. |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function addIndexColumn() |
||
185 | |||
186 | /** |
||
187 | * Edit column's content. |
||
188 | * |
||
189 | * @param string $name |
||
190 | * @param string|callable $content |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function editColumn($name, $content) |
||
199 | |||
200 | /** |
||
201 | * Remove column from collection. |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function removeColumn() |
||
212 | |||
213 | /** |
||
214 | * Declare columns to escape values. |
||
215 | * |
||
216 | * @param string|array $columns |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function escapeColumns($columns = '*') |
||
225 | |||
226 | /** |
||
227 | * Set columns that should not be escaped. |
||
228 | * |
||
229 | * @param array $columns |
||
230 | * @return $this |
||
231 | */ |
||
232 | public function rawColumns(array $columns) |
||
238 | |||
239 | /** |
||
240 | * Sets DT_RowClass template. |
||
241 | * result: <tr class="output_from_your_template">. |
||
242 | * |
||
243 | * @param string|callable $content |
||
244 | * @return $this |
||
245 | */ |
||
246 | public function setRowClass($content) |
||
252 | |||
253 | /** |
||
254 | * Sets DT_RowId template. |
||
255 | * result: <tr id="output_from_your_template">. |
||
256 | * |
||
257 | * @param string|callable $content |
||
258 | * @return $this |
||
259 | */ |
||
260 | public function setRowId($content) |
||
266 | |||
267 | /** |
||
268 | * Set DT_RowData templates. |
||
269 | * |
||
270 | * @param array $data |
||
271 | * @return $this |
||
272 | */ |
||
273 | public function setRowData(array $data) |
||
279 | |||
280 | /** |
||
281 | * Add DT_RowData template. |
||
282 | * |
||
283 | * @param string $key |
||
284 | * @param string|callable $value |
||
285 | * @return $this |
||
286 | */ |
||
287 | public function addRowData($key, $value) |
||
293 | |||
294 | /** |
||
295 | * Set DT_RowAttr templates. |
||
296 | * result: <tr attr1="attr1" attr2="attr2">. |
||
297 | * |
||
298 | * @param array $data |
||
299 | * @return $this |
||
300 | */ |
||
301 | public function setRowAttr(array $data) |
||
307 | |||
308 | /** |
||
309 | * Add DT_RowAttr template. |
||
310 | * |
||
311 | * @param string $key |
||
312 | * @param string|callable $value |
||
313 | * @return $this |
||
314 | */ |
||
315 | public function addRowAttr($key, $value) |
||
321 | |||
322 | /** |
||
323 | * Add custom filter handler for the give column. |
||
324 | * |
||
325 | * @param string $column |
||
326 | * @param callable $callback |
||
327 | * @return $this |
||
328 | */ |
||
329 | public function filterColumn($column, callable $callback) |
||
335 | |||
336 | /** |
||
337 | * Order each given columns versus the given custom sql. |
||
338 | * |
||
339 | * @param array $columns |
||
340 | * @param string $sql |
||
341 | * @param array $bindings |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
352 | |||
353 | /** |
||
354 | * Override default column ordering. |
||
355 | * |
||
356 | * @param string $column |
||
357 | * @param string $sql |
||
358 | * @param array $bindings |
||
359 | * @return $this |
||
360 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
361 | */ |
||
362 | public function orderColumn($column, $sql, $bindings = []) |
||
368 | |||
369 | /** |
||
370 | * Set data output transformer. |
||
371 | * |
||
372 | * @param \League\Fractal\TransformerAbstract $transformer |
||
373 | * @return $this |
||
374 | */ |
||
375 | public function setTransformer($transformer) |
||
381 | |||
382 | /** |
||
383 | * Set fractal serializer class. |
||
384 | * |
||
385 | * @param string $serializer |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function setSerializer($serializer) |
||
394 | |||
395 | /** |
||
396 | * Append data on json response. |
||
397 | * |
||
398 | * @param mixed $key |
||
399 | * @param mixed $value |
||
400 | * @return $this |
||
401 | */ |
||
402 | public function with($key, $value = '') |
||
414 | |||
415 | /** |
||
416 | * Override default ordering method with a closure callback. |
||
417 | * |
||
418 | * @param callable $closure |
||
419 | * @return $this |
||
420 | */ |
||
421 | public function order(callable $closure) |
||
427 | |||
428 | /** |
||
429 | * Update list of columns that is not allowed for search/sort. |
||
430 | * |
||
431 | * @param array $blacklist |
||
432 | * @return $this |
||
433 | */ |
||
434 | public function blacklist(array $blacklist) |
||
440 | |||
441 | /** |
||
442 | * Update list of columns that is allowed for search/sort. |
||
443 | * |
||
444 | * @param string|array $whitelist |
||
445 | * @return $this |
||
446 | */ |
||
447 | public function whitelist($whitelist = '*') |
||
453 | |||
454 | /** |
||
455 | * Set smart search config at runtime. |
||
456 | * |
||
457 | * @param bool $bool |
||
458 | * @return $this |
||
459 | */ |
||
460 | public function smart($bool = true) |
||
466 | |||
467 | /** |
||
468 | * Set total records manually. |
||
469 | * |
||
470 | * @param int $total |
||
471 | * @return $this |
||
472 | */ |
||
473 | public function setTotalRecords($total) |
||
479 | |||
480 | /** |
||
481 | * Skip pagination as needed. |
||
482 | * |
||
483 | * @return $this |
||
484 | */ |
||
485 | public function skipPaging() |
||
491 | |||
492 | /** |
||
493 | * Set datatables to do ordering with NULLS LAST option. |
||
494 | * |
||
495 | * @return $this |
||
496 | */ |
||
497 | public function orderByNullsLast() |
||
503 | |||
504 | /** |
||
505 | * Push a new column name to blacklist. |
||
506 | * |
||
507 | * @param string $column |
||
508 | * @return $this |
||
509 | */ |
||
510 | public function pushToBlacklist($column) |
||
518 | |||
519 | /** |
||
520 | * Check if column is blacklisted. |
||
521 | * |
||
522 | * @param string $column |
||
523 | * @return bool |
||
524 | */ |
||
525 | protected function isBlacklisted($column) |
||
539 | |||
540 | /** |
||
541 | * Get columns definition. |
||
542 | * |
||
543 | * @return array |
||
544 | */ |
||
545 | protected function getColumnsDefinition() |
||
552 | |||
553 | /** |
||
554 | * Get dataTables config instance. |
||
555 | * |
||
556 | * @return \Yajra\Datatables\Config |
||
557 | */ |
||
558 | protected function config() |
||
562 | |||
563 | /** |
||
564 | * Perform necessary filters. |
||
565 | * |
||
566 | * @return void |
||
567 | */ |
||
568 | protected function filterRecords() |
||
581 | |||
582 | /** |
||
583 | * Apply pagination. |
||
584 | * |
||
585 | * @return void |
||
586 | */ |
||
587 | protected function paginate() |
||
593 | |||
594 | /** |
||
595 | * Transform output. |
||
596 | * |
||
597 | * @param mixed $output |
||
598 | * @return array |
||
599 | */ |
||
600 | protected function transform($output) |
||
632 | |||
633 | /** |
||
634 | * Get or create transformer serializer instance. |
||
635 | * |
||
636 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
637 | */ |
||
638 | protected function createSerializer() |
||
646 | |||
647 | /** |
||
648 | * Get or create transformer instance. |
||
649 | * |
||
650 | * @return \League\Fractal\TransformerAbstract |
||
651 | */ |
||
652 | protected function createTransformer() |
||
660 | |||
661 | /** |
||
662 | * Get processed data. |
||
663 | * |
||
664 | * @param bool|false $object |
||
665 | * @return array |
||
666 | */ |
||
667 | protected function getProcessedData($object = false) |
||
678 | |||
679 | /** |
||
680 | * Render json response. |
||
681 | * |
||
682 | * @param array $data |
||
683 | * @return \Illuminate\Http\JsonResponse |
||
684 | */ |
||
685 | protected function render(array $data) |
||
705 | |||
706 | /** |
||
707 | * Append debug parameters on output. |
||
708 | * |
||
709 | * @param array $output |
||
710 | * @return array |
||
711 | */ |
||
712 | abstract protected function showDebugger(array $output); |
||
713 | |||
714 | /** |
||
715 | * Return an error json response. |
||
716 | * |
||
717 | * @param \Exception $exception |
||
718 | * @return \Illuminate\Http\JsonResponse |
||
719 | * @throws \Yajra\Datatables\Exception |
||
720 | */ |
||
721 | protected function errorResponse(\Exception $exception) |
||
738 | |||
739 | /** |
||
740 | * Get monolog/logger instance. |
||
741 | * |
||
742 | * @return \Illuminate\Contracts\Logging\Log |
||
743 | */ |
||
744 | public function getLogger() |
||
750 | |||
751 | /** |
||
752 | * Set monolog/logger instance. |
||
753 | * |
||
754 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
755 | * @return $this |
||
756 | */ |
||
757 | public function setLogger(Log $logger) |
||
763 | |||
764 | /** |
||
765 | * Setup search keyword. |
||
766 | * |
||
767 | * @param string $value |
||
768 | * @return string |
||
769 | */ |
||
770 | protected function setupKeyword($value) |
||
785 | |||
786 | /** |
||
787 | * Adds % wildcards to the given string. |
||
788 | * |
||
789 | * @param string $str |
||
790 | * @param bool $lowercase |
||
791 | * @return string |
||
792 | */ |
||
793 | protected function wildcardLikeString($str, $lowercase = true) |
||
810 | |||
811 | /** |
||
812 | * Update flags to disable global search. |
||
813 | * |
||
814 | * @param callable $callback |
||
815 | * @param mixed $parameters |
||
816 | * @param bool $autoFilter |
||
817 | */ |
||
818 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
825 | |||
826 | /** |
||
827 | * Get column name to be use for filtering and sorting. |
||
828 | * |
||
829 | * @param integer $index |
||
830 | * @param bool $wantsAlias |
||
831 | * @return string |
||
832 | */ |
||
833 | protected function getColumnName($index, $wantsAlias = false) |
||
848 | |||
849 | /** |
||
850 | * Get column name by order column index. |
||
851 | * |
||
852 | * @param int $index |
||
853 | * @return string |
||
854 | */ |
||
855 | protected function getColumnNameByIndex($index) |
||
861 | |||
862 | /** |
||
863 | * If column name could not be resolved then use primary key. |
||
864 | * |
||
865 | * @return string |
||
866 | */ |
||
867 | protected function getPrimaryKeyName() |
||
871 | |||
872 | /** |
||
873 | * Get column name from string. |
||
874 | * |
||
875 | * @param string $str |
||
876 | * @param bool $wantsAlias |
||
877 | * @return string |
||
878 | */ |
||
879 | protected function extractColumnName($str, $wantsAlias) |
||
897 | } |
||
898 |
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: