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 |
||
23 | abstract class BaseEngine implements DataTableEngine |
||
24 | { |
||
25 | use Macroable; |
||
26 | |||
27 | /** |
||
28 | * Datatables Request object. |
||
29 | * |
||
30 | * @var \Yajra\Datatables\Request |
||
31 | */ |
||
32 | public $request; |
||
33 | |||
34 | /** |
||
35 | * @var \Illuminate\Contracts\Logging\Log |
||
36 | */ |
||
37 | protected $logger; |
||
38 | |||
39 | /** |
||
40 | * Array of result columns/fields. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $columns = []; |
||
45 | |||
46 | /** |
||
47 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $columnDef = [ |
||
52 | 'index' => false, |
||
53 | 'append' => [], |
||
54 | 'edit' => [], |
||
55 | 'filter' => [], |
||
56 | 'order' => [], |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * Extra/Added columns. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $extraColumns = []; |
||
65 | |||
66 | /** |
||
67 | * Total records. |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $totalRecords = 0; |
||
72 | |||
73 | /** |
||
74 | * Total filtered records. |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | protected $filteredRecords = 0; |
||
79 | |||
80 | /** |
||
81 | * Auto-filter flag. |
||
82 | * |
||
83 | * @var bool |
||
84 | */ |
||
85 | protected $autoFilter = true; |
||
86 | |||
87 | /** |
||
88 | * Callback to override global search. |
||
89 | * |
||
90 | * @var callable |
||
91 | */ |
||
92 | protected $filterCallback; |
||
93 | |||
94 | /** |
||
95 | * DT row templates container. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $templates = [ |
||
100 | 'DT_RowId' => '', |
||
101 | 'DT_RowClass' => '', |
||
102 | 'DT_RowData' => [], |
||
103 | 'DT_RowAttr' => [], |
||
104 | ]; |
||
105 | |||
106 | /** |
||
107 | * [internal] Track if any filter was applied for at least one column. |
||
108 | * |
||
109 | * @var boolean |
||
110 | */ |
||
111 | protected $isFilterApplied = false; |
||
112 | |||
113 | /** |
||
114 | * Custom ordering callback. |
||
115 | * |
||
116 | * @var callable |
||
117 | */ |
||
118 | protected $orderCallback; |
||
119 | |||
120 | /** |
||
121 | * Skip paginate as needed. |
||
122 | * |
||
123 | * @var bool |
||
124 | */ |
||
125 | protected $skipPaging = false; |
||
126 | |||
127 | /** |
||
128 | * Array of data to append on json response. |
||
129 | * |
||
130 | * @var array |
||
131 | */ |
||
132 | protected $appends = []; |
||
133 | |||
134 | /** |
||
135 | * @var \Yajra\Datatables\Config |
||
136 | */ |
||
137 | protected $config; |
||
138 | |||
139 | /** |
||
140 | * Add column in collection. |
||
141 | * |
||
142 | * @param string $name |
||
143 | * @param string|callable $content |
||
144 | * @param bool|int $order |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function addColumn($name, $content, $order = false) |
||
155 | |||
156 | /** |
||
157 | * Add DT row index column on response. |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function addIndexColumn() |
||
167 | |||
168 | /** |
||
169 | * Edit column's content. |
||
170 | * |
||
171 | * @param string $name |
||
172 | * @param string|callable $content |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function editColumn($name, $content) |
||
181 | |||
182 | /** |
||
183 | * Remove column from collection. |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function removeColumn() |
||
194 | |||
195 | /** |
||
196 | * Declare columns to escape values. |
||
197 | * |
||
198 | * @param string|array $columns |
||
199 | * @return $this |
||
200 | */ |
||
201 | public function escapeColumns($columns = '*') |
||
207 | |||
208 | /** |
||
209 | * Set columns that should not be escaped. |
||
210 | * |
||
211 | * @param array $columns |
||
212 | * @return $this |
||
213 | */ |
||
214 | public function rawColumns(array $columns) |
||
220 | |||
221 | /** |
||
222 | * Sets DT_RowClass template. |
||
223 | * result: <tr class="output_from_your_template">. |
||
224 | * |
||
225 | * @param string|callable $content |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function setRowClass($content) |
||
234 | |||
235 | /** |
||
236 | * Sets DT_RowId template. |
||
237 | * result: <tr id="output_from_your_template">. |
||
238 | * |
||
239 | * @param string|callable $content |
||
240 | * @return $this |
||
241 | */ |
||
242 | public function setRowId($content) |
||
248 | |||
249 | /** |
||
250 | * Set DT_RowData templates. |
||
251 | * |
||
252 | * @param array $data |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function setRowData(array $data) |
||
261 | |||
262 | /** |
||
263 | * Add DT_RowData template. |
||
264 | * |
||
265 | * @param string $key |
||
266 | * @param string|callable $value |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function addRowData($key, $value) |
||
275 | |||
276 | /** |
||
277 | * Set DT_RowAttr templates. |
||
278 | * result: <tr attr1="attr1" attr2="attr2">. |
||
279 | * |
||
280 | * @param array $data |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function setRowAttr(array $data) |
||
289 | |||
290 | /** |
||
291 | * Add DT_RowAttr template. |
||
292 | * |
||
293 | * @param string $key |
||
294 | * @param string|callable $value |
||
295 | * @return $this |
||
296 | */ |
||
297 | public function addRowAttr($key, $value) |
||
303 | |||
304 | /** |
||
305 | * Append data on json response. |
||
306 | * |
||
307 | * @param mixed $key |
||
308 | * @param mixed $value |
||
309 | * @return $this |
||
310 | */ |
||
311 | public function with($key, $value = '') |
||
323 | |||
324 | /** |
||
325 | * Override default ordering method with a closure callback. |
||
326 | * |
||
327 | * @param callable $closure |
||
328 | * @return $this |
||
329 | */ |
||
330 | public function order(callable $closure) |
||
336 | |||
337 | /** |
||
338 | * Update list of columns that is not allowed for search/sort. |
||
339 | * |
||
340 | * @param array $blacklist |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function blacklist(array $blacklist) |
||
349 | |||
350 | /** |
||
351 | * Update list of columns that is allowed for search/sort. |
||
352 | * |
||
353 | * @param string|array $whitelist |
||
354 | * @return $this |
||
355 | */ |
||
356 | public function whitelist($whitelist = '*') |
||
362 | |||
363 | /** |
||
364 | * Set smart search config at runtime. |
||
365 | * |
||
366 | * @param bool $bool |
||
367 | * @return $this |
||
368 | */ |
||
369 | public function smart($bool = true) |
||
375 | |||
376 | /** |
||
377 | * Set total records manually. |
||
378 | * |
||
379 | * @param int $total |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function setTotalRecords($total) |
||
388 | |||
389 | /** |
||
390 | * Skip pagination as needed. |
||
391 | * |
||
392 | * @return $this |
||
393 | */ |
||
394 | public function skipPaging() |
||
400 | |||
401 | /** |
||
402 | * Push a new column name to blacklist. |
||
403 | * |
||
404 | * @param string $column |
||
405 | * @return $this |
||
406 | */ |
||
407 | public function pushToBlacklist($column) |
||
415 | |||
416 | /** |
||
417 | * Check if column is blacklisted. |
||
418 | * |
||
419 | * @param string $column |
||
420 | * @return bool |
||
421 | */ |
||
422 | protected function isBlacklisted($column) |
||
436 | |||
437 | /** |
||
438 | * Get columns definition. |
||
439 | * |
||
440 | * @return array |
||
441 | */ |
||
442 | protected function getColumnsDefinition() |
||
449 | |||
450 | /** |
||
451 | * Perform sorting of columns. |
||
452 | */ |
||
453 | public function ordering() |
||
461 | |||
462 | /** |
||
463 | * Resolve callback parameter instance. |
||
464 | * |
||
465 | * @return mixed |
||
466 | */ |
||
467 | abstract protected function resolveCallbackParameter(); |
||
468 | |||
469 | /** |
||
470 | * Perform default query orderBy clause. |
||
471 | */ |
||
472 | abstract protected function defaultOrdering(); |
||
473 | |||
474 | /** |
||
475 | * Set auto filter off and run your own filter. |
||
476 | * Overrides global search. |
||
477 | * |
||
478 | * @param callable $callback |
||
479 | * @param bool $globalSearch |
||
480 | * @return $this |
||
481 | */ |
||
482 | public function filter(callable $callback, $globalSearch = false) |
||
490 | |||
491 | /** |
||
492 | * Perform necessary filters. |
||
493 | * |
||
494 | * @return void |
||
495 | */ |
||
496 | protected function filterRecords() |
||
509 | |||
510 | /** |
||
511 | * Perform global search. |
||
512 | * |
||
513 | * @return void |
||
514 | */ |
||
515 | public function filtering() |
||
527 | |||
528 | /** |
||
529 | * Perform multi-term search by splitting keyword into |
||
530 | * individual words and searches for each of them. |
||
531 | * |
||
532 | * @param string $keyword |
||
533 | */ |
||
534 | protected function smartGlobalSearch($keyword) |
||
542 | |||
543 | /** |
||
544 | * Perform global search for the given keyword. |
||
545 | * |
||
546 | * @param string $keyword |
||
547 | */ |
||
548 | abstract protected function globalSearch($keyword); |
||
549 | |||
550 | /** |
||
551 | * Apply pagination. |
||
552 | * |
||
553 | * @return void |
||
554 | */ |
||
555 | protected function paginate() |
||
561 | |||
562 | /** |
||
563 | * Transform output. |
||
564 | * |
||
565 | * @param mixed $output |
||
566 | * @return array |
||
567 | */ |
||
568 | protected function transform($output) |
||
576 | |||
577 | /** |
||
578 | * Get processed data. |
||
579 | * |
||
580 | * @param bool|false $object |
||
581 | * @return array |
||
582 | */ |
||
583 | protected function getProcessedData($object = false) |
||
594 | |||
595 | /** |
||
596 | * Render json response. |
||
597 | * |
||
598 | * @param array $data |
||
599 | * @return \Illuminate\Http\JsonResponse |
||
600 | */ |
||
601 | protected function render(array $data) |
||
621 | |||
622 | /** |
||
623 | * Append debug parameters on output. |
||
624 | * |
||
625 | * @param array $output |
||
626 | * @return array |
||
627 | */ |
||
628 | abstract protected function showDebugger(array $output); |
||
629 | |||
630 | /** |
||
631 | * Return an error json response. |
||
632 | * |
||
633 | * @param \Exception $exception |
||
634 | * @return \Illuminate\Http\JsonResponse |
||
635 | * @throws \Yajra\Datatables\Exception |
||
636 | */ |
||
637 | protected function errorResponse(\Exception $exception) |
||
654 | |||
655 | /** |
||
656 | * Get monolog/logger instance. |
||
657 | * |
||
658 | * @return \Illuminate\Contracts\Logging\Log |
||
659 | */ |
||
660 | public function getLogger() |
||
666 | |||
667 | /** |
||
668 | * Set monolog/logger instance. |
||
669 | * |
||
670 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
671 | * @return $this |
||
672 | */ |
||
673 | public function setLogger(Log $logger) |
||
679 | |||
680 | /** |
||
681 | * Setup search keyword. |
||
682 | * |
||
683 | * @param string $value |
||
684 | * @return string |
||
685 | */ |
||
686 | protected function setupKeyword($value) |
||
701 | |||
702 | /** |
||
703 | * Get column name to be use for filtering and sorting. |
||
704 | * |
||
705 | * @param integer $index |
||
706 | * @param bool $wantsAlias |
||
707 | * @return string |
||
708 | */ |
||
709 | protected function getColumnName($index, $wantsAlias = false) |
||
724 | |||
725 | /** |
||
726 | * Get column name by order column index. |
||
727 | * |
||
728 | * @param int $index |
||
729 | * @return string |
||
730 | */ |
||
731 | protected function getColumnNameByIndex($index) |
||
737 | |||
738 | /** |
||
739 | * If column name could not be resolved then use primary key. |
||
740 | * |
||
741 | * @return string |
||
742 | */ |
||
743 | protected function getPrimaryKeyName() |
||
747 | } |
||
748 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: