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 | * [internal] Track if any filter was applied for at least one column. |
||
116 | * |
||
117 | * @var boolean |
||
118 | */ |
||
119 | protected $isFilterApplied = false; |
||
120 | |||
121 | /** |
||
122 | * Fractal serializer class. |
||
123 | * |
||
124 | * @var string|null |
||
125 | */ |
||
126 | protected $serializer = null; |
||
127 | |||
128 | /** |
||
129 | * Custom ordering callback. |
||
130 | * |
||
131 | * @var callable |
||
132 | */ |
||
133 | protected $orderCallback; |
||
134 | |||
135 | /** |
||
136 | * Skip paginate as needed. |
||
137 | * |
||
138 | * @var bool |
||
139 | */ |
||
140 | protected $skipPaging = false; |
||
141 | |||
142 | /** |
||
143 | * Array of data to append on json response. |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | protected $appends = []; |
||
148 | |||
149 | /** |
||
150 | * Flag for ordering NULLS LAST option. |
||
151 | * |
||
152 | * @var bool |
||
153 | */ |
||
154 | protected $nullsLast = false; |
||
155 | |||
156 | /** |
||
157 | * @var \Yajra\Datatables\Config |
||
158 | */ |
||
159 | protected $config; |
||
160 | |||
161 | /** |
||
162 | * Add column in collection. |
||
163 | * |
||
164 | * @param string $name |
||
165 | * @param string|callable $content |
||
166 | * @param bool|int $order |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function addColumn($name, $content, $order = false) |
||
177 | |||
178 | /** |
||
179 | * Add DT row index column on response. |
||
180 | * |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function addIndexColumn() |
||
189 | |||
190 | /** |
||
191 | * Edit column's content. |
||
192 | * |
||
193 | * @param string $name |
||
194 | * @param string|callable $content |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function editColumn($name, $content) |
||
203 | |||
204 | /** |
||
205 | * Remove column from collection. |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function removeColumn() |
||
216 | |||
217 | /** |
||
218 | * Declare columns to escape values. |
||
219 | * |
||
220 | * @param string|array $columns |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function escapeColumns($columns = '*') |
||
229 | |||
230 | /** |
||
231 | * Set columns that should not be escaped. |
||
232 | * |
||
233 | * @param array $columns |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function rawColumns(array $columns) |
||
242 | |||
243 | /** |
||
244 | * Sets DT_RowClass template. |
||
245 | * result: <tr class="output_from_your_template">. |
||
246 | * |
||
247 | * @param string|callable $content |
||
248 | * @return $this |
||
249 | */ |
||
250 | public function setRowClass($content) |
||
256 | |||
257 | /** |
||
258 | * Sets DT_RowId template. |
||
259 | * result: <tr id="output_from_your_template">. |
||
260 | * |
||
261 | * @param string|callable $content |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function setRowId($content) |
||
270 | |||
271 | /** |
||
272 | * Set DT_RowData templates. |
||
273 | * |
||
274 | * @param array $data |
||
275 | * @return $this |
||
276 | */ |
||
277 | public function setRowData(array $data) |
||
283 | |||
284 | /** |
||
285 | * Add DT_RowData template. |
||
286 | * |
||
287 | * @param string $key |
||
288 | * @param string|callable $value |
||
289 | * @return $this |
||
290 | */ |
||
291 | public function addRowData($key, $value) |
||
297 | |||
298 | /** |
||
299 | * Set DT_RowAttr templates. |
||
300 | * result: <tr attr1="attr1" attr2="attr2">. |
||
301 | * |
||
302 | * @param array $data |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function setRowAttr(array $data) |
||
311 | |||
312 | /** |
||
313 | * Add DT_RowAttr template. |
||
314 | * |
||
315 | * @param string $key |
||
316 | * @param string|callable $value |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function addRowAttr($key, $value) |
||
325 | |||
326 | /** |
||
327 | * Add custom filter handler for the give column. |
||
328 | * |
||
329 | * @param string $column |
||
330 | * @param callable $callback |
||
331 | * @return $this |
||
332 | */ |
||
333 | public function filterColumn($column, callable $callback) |
||
339 | |||
340 | /** |
||
341 | * Order each given columns versus the given custom sql. |
||
342 | * |
||
343 | * @param array $columns |
||
344 | * @param string $sql |
||
345 | * @param array $bindings |
||
346 | * @return $this |
||
347 | */ |
||
348 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
356 | |||
357 | /** |
||
358 | * Override default column ordering. |
||
359 | * |
||
360 | * @param string $column |
||
361 | * @param string $sql |
||
362 | * @param array $bindings |
||
363 | * @return $this |
||
364 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
365 | */ |
||
366 | public function orderColumn($column, $sql, $bindings = []) |
||
372 | |||
373 | /** |
||
374 | * Set data output transformer. |
||
375 | * |
||
376 | * @param \League\Fractal\TransformerAbstract $transformer |
||
377 | * @return $this |
||
378 | */ |
||
379 | public function setTransformer($transformer) |
||
385 | |||
386 | /** |
||
387 | * Set fractal serializer class. |
||
388 | * |
||
389 | * @param string $serializer |
||
390 | * @return $this |
||
391 | */ |
||
392 | public function setSerializer($serializer) |
||
398 | |||
399 | /** |
||
400 | * Append data on json response. |
||
401 | * |
||
402 | * @param mixed $key |
||
403 | * @param mixed $value |
||
404 | * @return $this |
||
405 | */ |
||
406 | public function with($key, $value = '') |
||
418 | |||
419 | /** |
||
420 | * Override default ordering method with a closure callback. |
||
421 | * |
||
422 | * @param callable $closure |
||
423 | * @return $this |
||
424 | */ |
||
425 | public function order(callable $closure) |
||
431 | |||
432 | /** |
||
433 | * Update list of columns that is not allowed for search/sort. |
||
434 | * |
||
435 | * @param array $blacklist |
||
436 | * @return $this |
||
437 | */ |
||
438 | public function blacklist(array $blacklist) |
||
444 | |||
445 | /** |
||
446 | * Update list of columns that is allowed for search/sort. |
||
447 | * |
||
448 | * @param string|array $whitelist |
||
449 | * @return $this |
||
450 | */ |
||
451 | public function whitelist($whitelist = '*') |
||
457 | |||
458 | /** |
||
459 | * Set smart search config at runtime. |
||
460 | * |
||
461 | * @param bool $bool |
||
462 | * @return $this |
||
463 | */ |
||
464 | public function smart($bool = true) |
||
470 | |||
471 | /** |
||
472 | * Set total records manually. |
||
473 | * |
||
474 | * @param int $total |
||
475 | * @return $this |
||
476 | */ |
||
477 | public function setTotalRecords($total) |
||
483 | |||
484 | /** |
||
485 | * Skip pagination as needed. |
||
486 | * |
||
487 | * @return $this |
||
488 | */ |
||
489 | public function skipPaging() |
||
495 | |||
496 | /** |
||
497 | * Set datatables to do ordering with NULLS LAST option. |
||
498 | * |
||
499 | * @return $this |
||
500 | */ |
||
501 | public function orderByNullsLast() |
||
507 | |||
508 | /** |
||
509 | * Push a new column name to blacklist. |
||
510 | * |
||
511 | * @param string $column |
||
512 | * @return $this |
||
513 | */ |
||
514 | public function pushToBlacklist($column) |
||
522 | |||
523 | /** |
||
524 | * Check if column is blacklisted. |
||
525 | * |
||
526 | * @param string $column |
||
527 | * @return bool |
||
528 | */ |
||
529 | protected function isBlacklisted($column) |
||
543 | |||
544 | /** |
||
545 | * Get columns definition. |
||
546 | * |
||
547 | * @return array |
||
548 | */ |
||
549 | protected function getColumnsDefinition() |
||
556 | |||
557 | /** |
||
558 | * Perform necessary filters. |
||
559 | * |
||
560 | * @return void |
||
561 | */ |
||
562 | protected function filterRecords() |
||
575 | |||
576 | /** |
||
577 | * Apply pagination. |
||
578 | * |
||
579 | * @return void |
||
580 | */ |
||
581 | protected function paginate() |
||
587 | |||
588 | /** |
||
589 | * Transform output. |
||
590 | * |
||
591 | * @param mixed $output |
||
592 | * @return array |
||
593 | */ |
||
594 | protected function transform($output) |
||
626 | |||
627 | /** |
||
628 | * Get or create transformer serializer instance. |
||
629 | * |
||
630 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
631 | */ |
||
632 | protected function createSerializer() |
||
640 | |||
641 | /** |
||
642 | * Get or create transformer instance. |
||
643 | * |
||
644 | * @return \League\Fractal\TransformerAbstract |
||
645 | */ |
||
646 | protected function createTransformer() |
||
654 | |||
655 | /** |
||
656 | * Get processed data. |
||
657 | * |
||
658 | * @param bool|false $object |
||
659 | * @return array |
||
660 | */ |
||
661 | protected function getProcessedData($object = false) |
||
672 | |||
673 | /** |
||
674 | * Render json response. |
||
675 | * |
||
676 | * @param array $data |
||
677 | * @return \Illuminate\Http\JsonResponse |
||
678 | */ |
||
679 | protected function render(array $data) |
||
699 | |||
700 | /** |
||
701 | * Append debug parameters on output. |
||
702 | * |
||
703 | * @param array $output |
||
704 | * @return array |
||
705 | */ |
||
706 | abstract protected function showDebugger(array $output); |
||
707 | |||
708 | /** |
||
709 | * Return an error json response. |
||
710 | * |
||
711 | * @param \Exception $exception |
||
712 | * @return \Illuminate\Http\JsonResponse |
||
713 | * @throws \Yajra\Datatables\Exception |
||
714 | */ |
||
715 | protected function errorResponse(\Exception $exception) |
||
732 | |||
733 | /** |
||
734 | * Get monolog/logger instance. |
||
735 | * |
||
736 | * @return \Illuminate\Contracts\Logging\Log |
||
737 | */ |
||
738 | public function getLogger() |
||
744 | |||
745 | /** |
||
746 | * Set monolog/logger instance. |
||
747 | * |
||
748 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
749 | * @return $this |
||
750 | */ |
||
751 | public function setLogger(Log $logger) |
||
757 | |||
758 | /** |
||
759 | * Setup search keyword. |
||
760 | * |
||
761 | * @param string $value |
||
762 | * @return string |
||
763 | */ |
||
764 | protected function setupKeyword($value) |
||
779 | |||
780 | /** |
||
781 | * Adds % wildcards to the given string. |
||
782 | * |
||
783 | * @param string $str |
||
784 | * @param bool $lowercase |
||
785 | * @return string |
||
786 | */ |
||
787 | protected function wildcardLikeString($str, $lowercase = true) |
||
804 | |||
805 | /** |
||
806 | * Update flags to disable global search. |
||
807 | * |
||
808 | * @param callable $callback |
||
809 | * @param mixed $parameters |
||
810 | * @param bool $autoFilter |
||
811 | */ |
||
812 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
819 | |||
820 | /** |
||
821 | * Get column name to be use for filtering and sorting. |
||
822 | * |
||
823 | * @param integer $index |
||
824 | * @param bool $wantsAlias |
||
825 | * @return string |
||
826 | */ |
||
827 | protected function getColumnName($index, $wantsAlias = false) |
||
842 | |||
843 | /** |
||
844 | * Get column name by order column index. |
||
845 | * |
||
846 | * @param int $index |
||
847 | * @return string |
||
848 | */ |
||
849 | protected function getColumnNameByIndex($index) |
||
855 | |||
856 | /** |
||
857 | * If column name could not be resolved then use primary key. |
||
858 | * |
||
859 | * @return string |
||
860 | */ |
||
861 | protected function getPrimaryKeyName() |
||
865 | |||
866 | /** |
||
867 | * Get column name from string. |
||
868 | * |
||
869 | * @param string $str |
||
870 | * @param bool $wantsAlias |
||
871 | * @return string |
||
872 | */ |
||
873 | protected function extractColumnName($str, $wantsAlias) |
||
891 | } |
||
892 |
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: