Complex classes like QueryDataTable 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 QueryDataTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class QueryDataTable extends DataTableAbstract |
||
12 | { |
||
13 | /** |
||
14 | * Builder object. |
||
15 | * |
||
16 | * @var \Illuminate\Database\Query\Builder |
||
17 | */ |
||
18 | protected $query; |
||
19 | |||
20 | /** |
||
21 | * Database connection used. |
||
22 | * |
||
23 | * @var \Illuminate\Database\Connection |
||
24 | */ |
||
25 | protected $connection; |
||
26 | |||
27 | /** |
||
28 | * Flag for ordering NULLS LAST option. |
||
29 | * |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $nullsLast = false; |
||
33 | |||
34 | /** |
||
35 | * Flag to check if query preparation was already done. |
||
36 | * |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $prepared = false; |
||
40 | |||
41 | /** |
||
42 | * Query callback for custom pagination using limit without offset. |
||
43 | * |
||
44 | * @var callable |
||
45 | */ |
||
46 | protected $limitCallback; |
||
47 | |||
48 | /** |
||
49 | * Flag to skip total records count query. |
||
50 | * |
||
51 | * @var bool |
||
52 | */ |
||
53 | protected $skipTotalRecords = false; |
||
54 | |||
55 | /** |
||
56 | * Flag to keep the select bindings. |
||
57 | * |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $keepSelectBindings = false; |
||
61 | |||
62 | /** |
||
63 | * Can the DataTable engine be created with these parameters. |
||
64 | * |
||
65 | * @param mixed $source |
||
66 | * @return bool |
||
67 | */ |
||
68 | public static function canCreate($source) |
||
72 | |||
73 | /** |
||
74 | * @param \Illuminate\Database\Query\Builder $builder |
||
75 | */ |
||
76 | public function __construct(Builder $builder) |
||
87 | |||
88 | /** |
||
89 | * Organizes works. |
||
90 | * |
||
91 | * @param bool $mDataSupport |
||
92 | * @return \Illuminate\Http\JsonResponse |
||
93 | * @throws \Exception |
||
94 | */ |
||
95 | public function make($mDataSupport = true) |
||
109 | |||
110 | /** |
||
111 | * Perform search using search pane values. |
||
112 | */ |
||
113 | protected function searchPanesSearch() |
||
131 | |||
132 | /** |
||
133 | * Prepare query by executing count, filter, order and paginate. |
||
134 | */ |
||
135 | protected function prepareQuery() |
||
149 | |||
150 | /** |
||
151 | * Skip total records and set the recordsTotal equals to recordsFiltered. |
||
152 | * This will improve the performance by skipping the total count query. |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function skipTotalRecords() |
||
162 | |||
163 | /** |
||
164 | * Keep the select bindings. |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function keepSelectBindings() |
||
174 | |||
175 | /** |
||
176 | * Count total items. |
||
177 | * |
||
178 | * @return int |
||
179 | */ |
||
180 | public function totalCount() |
||
190 | |||
191 | /** |
||
192 | * Count filtered items. |
||
193 | * |
||
194 | * @return int |
||
195 | */ |
||
196 | protected function filteredCount() |
||
205 | |||
206 | /** |
||
207 | * Counts current query. |
||
208 | * |
||
209 | * @return int |
||
210 | */ |
||
211 | public function count() |
||
220 | |||
221 | /** |
||
222 | * Prepare count query builder. |
||
223 | * |
||
224 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
225 | */ |
||
226 | protected function prepareCountQuery() |
||
240 | |||
241 | /** |
||
242 | * Check if builder query uses complex sql. |
||
243 | * |
||
244 | * @param \Illuminate\Database\Query\Builder $builder |
||
245 | * @return bool |
||
246 | */ |
||
247 | protected function isComplexQuery($builder) |
||
251 | |||
252 | /** |
||
253 | * Wrap column with DB grammar. |
||
254 | * |
||
255 | * @param string $column |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function wrap($column) |
||
262 | |||
263 | /** |
||
264 | * Get paginated results. |
||
265 | * |
||
266 | * @return \Illuminate\Support\Collection |
||
267 | */ |
||
268 | public function results() |
||
272 | |||
273 | /** |
||
274 | * Get filtered, ordered and paginated query. |
||
275 | * |
||
276 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
277 | */ |
||
278 | public function getFilteredQuery() |
||
284 | |||
285 | /** |
||
286 | * Get query builder instance. |
||
287 | * |
||
288 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
289 | */ |
||
290 | public function getQuery() |
||
294 | |||
295 | /** |
||
296 | * Perform column search. |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | public function columnSearch() |
||
323 | |||
324 | /** |
||
325 | * Check if column has custom filter handler. |
||
326 | * |
||
327 | * @param string $columnName |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function hasFilterColumn($columnName) |
||
334 | |||
335 | /** |
||
336 | * Get column keyword to use for search. |
||
337 | * |
||
338 | * @param int $i |
||
339 | * @param bool $raw |
||
340 | * @return string |
||
341 | */ |
||
342 | protected function getColumnSearchKeyword($i, $raw = false) |
||
351 | |||
352 | /** |
||
353 | * Apply filterColumn api search. |
||
354 | * |
||
355 | * @param mixed $query |
||
356 | * @param string $columnName |
||
357 | * @param string $keyword |
||
358 | * @param string $boolean |
||
359 | */ |
||
360 | protected function applyFilterColumn($query, $columnName, $keyword, $boolean = 'and') |
||
375 | |||
376 | /** |
||
377 | * Get the base query builder instance. |
||
378 | * |
||
379 | * @param mixed $instance |
||
380 | * @return \Illuminate\Database\Query\Builder |
||
381 | */ |
||
382 | protected function getBaseQueryBuilder($instance = null) |
||
394 | |||
395 | /** |
||
396 | * Resolve the proper column name be used. |
||
397 | * |
||
398 | * @param string $column |
||
399 | * @return string |
||
400 | */ |
||
401 | protected function resolveRelationColumn($column) |
||
405 | |||
406 | /** |
||
407 | * Compile queries for column search. |
||
408 | * |
||
409 | * @param int $i |
||
410 | * @param string $column |
||
411 | * @param string $keyword |
||
412 | */ |
||
413 | protected function compileColumnSearch($i, $column, $keyword) |
||
422 | |||
423 | /** |
||
424 | * Compile regex query column search. |
||
425 | * |
||
426 | * @param mixed $column |
||
427 | * @param string $keyword |
||
428 | */ |
||
429 | protected function regexColumnSearch($column, $keyword) |
||
452 | |||
453 | /** |
||
454 | * Wrap a column and cast based on database driver. |
||
455 | * |
||
456 | * @param string $column |
||
457 | * @return string |
||
458 | */ |
||
459 | protected function castColumn($column) |
||
470 | |||
471 | /** |
||
472 | * Compile query builder where clause depending on configurations. |
||
473 | * |
||
474 | * @param mixed $query |
||
475 | * @param string $column |
||
476 | * @param string $keyword |
||
477 | * @param string $boolean |
||
478 | */ |
||
479 | protected function compileQuerySearch($query, $column, $keyword, $boolean = 'or') |
||
491 | |||
492 | /** |
||
493 | * Patch for fix about ambiguous field. |
||
494 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
495 | * |
||
496 | * @param mixed $query |
||
497 | * @param string $column |
||
498 | * @return string |
||
499 | */ |
||
500 | protected function addTablePrefix($query, $column) |
||
511 | |||
512 | /** |
||
513 | * Prepare search keyword based on configurations. |
||
514 | * |
||
515 | * @param string $keyword |
||
516 | * @return string |
||
517 | */ |
||
518 | protected function prepareKeyword($keyword) |
||
538 | |||
539 | /** |
||
540 | * Add custom filter handler for the give column. |
||
541 | * |
||
542 | * @param string $column |
||
543 | * @param callable $callback |
||
544 | * @return $this |
||
545 | */ |
||
546 | public function filterColumn($column, callable $callback) |
||
552 | |||
553 | /** |
||
554 | * Order each given columns versus the given custom sql. |
||
555 | * |
||
556 | * @param array $columns |
||
557 | * @param string $sql |
||
558 | * @param array $bindings |
||
559 | * @return $this |
||
560 | */ |
||
561 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
569 | |||
570 | /** |
||
571 | * Override default column ordering. |
||
572 | * |
||
573 | * @param string $column |
||
574 | * @param string $sql |
||
575 | * @param array $bindings |
||
576 | * @return $this |
||
577 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
578 | */ |
||
579 | public function orderColumn($column, $sql, $bindings = []) |
||
585 | |||
586 | /** |
||
587 | * Set datatables to do ordering with NULLS LAST option. |
||
588 | * |
||
589 | * @return $this |
||
590 | */ |
||
591 | public function orderByNullsLast() |
||
597 | |||
598 | /** |
||
599 | * Paginate dataTable using limit without offset |
||
600 | * with additional where clause via callback. |
||
601 | * |
||
602 | * @param callable $callback |
||
603 | * @return $this |
||
604 | */ |
||
605 | public function limit(callable $callback) |
||
611 | |||
612 | /** |
||
613 | * Perform pagination. |
||
614 | * |
||
615 | * @return void |
||
616 | */ |
||
617 | public function paging() |
||
627 | |||
628 | /** |
||
629 | * Add column in collection. |
||
630 | * |
||
631 | * @param string $name |
||
632 | * @param string|callable $content |
||
633 | * @param bool|int $order |
||
634 | * @return $this |
||
635 | */ |
||
636 | public function addColumn($name, $content, $order = false) |
||
642 | |||
643 | /** |
||
644 | * Resolve callback parameter instance. |
||
645 | * |
||
646 | * @return \Illuminate\Database\Query\Builder |
||
647 | */ |
||
648 | protected function resolveCallbackParameter() |
||
652 | |||
653 | /** |
||
654 | * Perform default query orderBy clause. |
||
655 | */ |
||
656 | protected function defaultOrdering() |
||
680 | |||
681 | /** |
||
682 | * Check if column has custom sort handler. |
||
683 | * |
||
684 | * @param string $column |
||
685 | * @return bool |
||
686 | */ |
||
687 | protected function hasOrderColumn($column) |
||
691 | |||
692 | /** |
||
693 | * Apply orderColumn custom query. |
||
694 | * |
||
695 | * @param string $column |
||
696 | * @param array $orderable |
||
697 | */ |
||
698 | protected function applyOrderColumn($column, $orderable) |
||
713 | |||
714 | /** |
||
715 | * Get NULLS LAST SQL. |
||
716 | * |
||
717 | * @param string $column |
||
718 | * @param string $direction |
||
719 | * @return string |
||
720 | */ |
||
721 | protected function getNullsLastSql($column, $direction) |
||
731 | |||
732 | /** |
||
733 | * Perform global search for the given keyword. |
||
734 | * |
||
735 | * @param string $keyword |
||
736 | */ |
||
737 | protected function globalSearch($keyword) |
||
758 | |||
759 | /** |
||
760 | * Append debug parameters on output. |
||
761 | * |
||
762 | * @param array $output |
||
763 | * @return array |
||
764 | */ |
||
765 | protected function showDebugger(array $output) |
||
777 | |||
778 | /** |
||
779 | * Attach custom with meta on response. |
||
780 | * |
||
781 | * @param array $data |
||
782 | * @return array |
||
783 | */ |
||
784 | protected function attachAppends(array $data) |
||
797 | } |
||
798 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.