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 | * @param \Illuminate\Database\Query\Builder $builder |
||
50 | */ |
||
51 | public function __construct(Builder $builder) |
||
62 | |||
63 | /** |
||
64 | * Organizes works. |
||
65 | * |
||
66 | * @param bool $mDataSupport |
||
67 | * @return \Illuminate\Http\JsonResponse |
||
68 | * @throws \Exception |
||
69 | */ |
||
70 | public function make($mDataSupport = true) |
||
84 | |||
85 | /** |
||
86 | * Prepare query by executing count, filter, order and paginate. |
||
87 | */ |
||
88 | protected function prepareQuery() |
||
102 | |||
103 | /** |
||
104 | * Count total items. |
||
105 | * |
||
106 | * @return integer |
||
107 | */ |
||
108 | public function totalCount() |
||
112 | |||
113 | /** |
||
114 | * Counts current query. |
||
115 | * |
||
116 | * @return int |
||
117 | */ |
||
118 | public function count() |
||
127 | |||
128 | /** |
||
129 | * Prepare count query builder. |
||
130 | * |
||
131 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
132 | */ |
||
133 | protected function prepareCountQuery() |
||
144 | |||
145 | /** |
||
146 | * Check if builder query uses complex sql. |
||
147 | * |
||
148 | * @param \Illuminate\Database\Query\Builder $builder |
||
149 | * @return bool |
||
150 | */ |
||
151 | protected function isComplexQuery($builder) |
||
155 | |||
156 | /** |
||
157 | * Wrap column with DB grammar. |
||
158 | * |
||
159 | * @param string $column |
||
160 | * @return string |
||
161 | */ |
||
162 | protected function wrap($column) |
||
166 | |||
167 | /** |
||
168 | * Get paginated results. |
||
169 | * |
||
170 | * @return \Illuminate\Support\Collection |
||
171 | */ |
||
172 | public function results() |
||
176 | |||
177 | /** |
||
178 | * Get filtered, ordered and paginated query. |
||
179 | * |
||
180 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
181 | */ |
||
182 | public function getFilteredQuery() |
||
188 | |||
189 | /** |
||
190 | * Get query builder instance. |
||
191 | * |
||
192 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
193 | */ |
||
194 | public function getQuery() |
||
198 | |||
199 | /** |
||
200 | * Perform column search. |
||
201 | * |
||
202 | * @return void |
||
203 | */ |
||
204 | public function columnSearch() |
||
228 | |||
229 | /** |
||
230 | * Check if column has custom filter handler. |
||
231 | * |
||
232 | * @param string $columnName |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function hasFilterColumn($columnName) |
||
239 | |||
240 | /** |
||
241 | * Get column keyword to use for search. |
||
242 | * |
||
243 | * @param int $i |
||
244 | * @param bool $raw |
||
245 | * @return string |
||
246 | */ |
||
247 | protected function getColumnSearchKeyword($i, $raw = false) |
||
256 | |||
257 | /** |
||
258 | * Apply filterColumn api search. |
||
259 | * |
||
260 | * @param mixed $query |
||
261 | * @param string $columnName |
||
262 | * @param string $keyword |
||
263 | * @param string $boolean |
||
264 | */ |
||
265 | protected function applyFilterColumn($query, $columnName, $keyword, $boolean = 'and') |
||
280 | |||
281 | /** |
||
282 | * Get the base query builder instance. |
||
283 | * |
||
284 | * @param mixed $instance |
||
285 | * @return \Illuminate\Database\Query\Builder |
||
286 | */ |
||
287 | protected function getBaseQueryBuilder($instance = null) |
||
299 | |||
300 | /** |
||
301 | * Resolve the proper column name be used. |
||
302 | * |
||
303 | * @param string $column |
||
304 | * @return string |
||
305 | */ |
||
306 | protected function resolveRelationColumn($column) |
||
310 | |||
311 | /** |
||
312 | * Compile queries for column search. |
||
313 | * |
||
314 | * @param int $i |
||
315 | * @param string $column |
||
316 | * @param string $keyword |
||
317 | */ |
||
318 | protected function compileColumnSearch($i, $column, $keyword) |
||
327 | |||
328 | /** |
||
329 | * Compile regex query column search. |
||
330 | * |
||
331 | * @param mixed $column |
||
332 | * @param string $keyword |
||
333 | */ |
||
334 | protected function regexColumnSearch($column, $keyword) |
||
355 | |||
356 | /** |
||
357 | * Wrap a column and cast based on database driver. |
||
358 | * |
||
359 | * @param string $column |
||
360 | * @return string |
||
361 | */ |
||
362 | protected function castColumn($column) |
||
373 | |||
374 | /** |
||
375 | * Compile query builder where clause depending on configurations. |
||
376 | * |
||
377 | * @param mixed $query |
||
378 | * @param string $column |
||
379 | * @param string $keyword |
||
380 | * @param string $boolean |
||
381 | */ |
||
382 | protected function compileQuerySearch($query, $column, $keyword, $boolean = 'or') |
||
394 | |||
395 | /** |
||
396 | * Patch for fix about ambiguous field. |
||
397 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
398 | * |
||
399 | * @param mixed $query |
||
400 | * @param string $column |
||
401 | * @return string |
||
402 | */ |
||
403 | protected function addTablePrefix($query, $column) |
||
414 | |||
415 | /** |
||
416 | * Prepare search keyword based on configurations. |
||
417 | * |
||
418 | * @param string $keyword |
||
419 | * @return string |
||
420 | */ |
||
421 | protected function prepareKeyword($keyword) |
||
437 | |||
438 | /** |
||
439 | * Add custom filter handler for the give column. |
||
440 | * |
||
441 | * @param string $column |
||
442 | * @param callable $callback |
||
443 | * @return $this |
||
444 | */ |
||
445 | public function filterColumn($column, callable $callback) |
||
451 | |||
452 | /** |
||
453 | * Order each given columns versus the given custom sql. |
||
454 | * |
||
455 | * @param array $columns |
||
456 | * @param string $sql |
||
457 | * @param array $bindings |
||
458 | * @return $this |
||
459 | */ |
||
460 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
468 | |||
469 | /** |
||
470 | * Override default column ordering. |
||
471 | * |
||
472 | * @param string $column |
||
473 | * @param string $sql |
||
474 | * @param array $bindings |
||
475 | * @return $this |
||
476 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
477 | */ |
||
478 | public function orderColumn($column, $sql, $bindings = []) |
||
484 | |||
485 | /** |
||
486 | * Set datatables to do ordering with NULLS LAST option. |
||
487 | * |
||
488 | * @return $this |
||
489 | */ |
||
490 | public function orderByNullsLast() |
||
496 | |||
497 | /** |
||
498 | * Paginate dataTable using limit without offset |
||
499 | * with additional where clause via callback. |
||
500 | * |
||
501 | * @param callable $callback |
||
502 | * @return $this |
||
503 | */ |
||
504 | public function limit(callable $callback) |
||
509 | |||
510 | /** |
||
511 | * Perform pagination. |
||
512 | * |
||
513 | * @return void |
||
514 | */ |
||
515 | public function paging() |
||
525 | |||
526 | /** |
||
527 | * Add column in collection. |
||
528 | * |
||
529 | * @param string $name |
||
530 | * @param string|callable $content |
||
531 | * @param bool|int $order |
||
532 | * @return \Yajra\DataTables\DataTableAbstract|\Yajra\DataTables\Builders\QueryDataTable |
||
533 | */ |
||
534 | public function addColumn($name, $content, $order = false) |
||
540 | |||
541 | /** |
||
542 | * Resolve callback parameter instance. |
||
543 | * |
||
544 | * @return \Illuminate\Database\Query\Builder |
||
545 | */ |
||
546 | protected function resolveCallbackParameter() |
||
550 | |||
551 | /** |
||
552 | * Perform default query orderBy clause. |
||
553 | */ |
||
554 | protected function defaultOrdering() |
||
578 | |||
579 | /** |
||
580 | * Check if column has custom sort handler. |
||
581 | * |
||
582 | * @param string $column |
||
583 | * @return bool |
||
584 | */ |
||
585 | protected function hasOrderColumn($column) |
||
589 | |||
590 | /** |
||
591 | * Apply orderColumn custom query. |
||
592 | * |
||
593 | * @param string $column |
||
594 | * @param array $orderable |
||
595 | */ |
||
596 | protected function applyOrderColumn($column, $orderable): void |
||
603 | |||
604 | /** |
||
605 | * Get NULLS LAST SQL. |
||
606 | * |
||
607 | * @param string $column |
||
608 | * @param string $direction |
||
609 | * @return string |
||
610 | */ |
||
611 | protected function getNullsLastSql($column, $direction) |
||
617 | |||
618 | /** |
||
619 | * Perform global search for the given keyword. |
||
620 | * |
||
621 | * @param string $keyword |
||
622 | */ |
||
623 | protected function globalSearch($keyword) |
||
644 | |||
645 | /** |
||
646 | * Append debug parameters on output. |
||
647 | * |
||
648 | * @param array $output |
||
649 | * @return array |
||
650 | */ |
||
651 | protected function showDebugger(array $output) |
||
658 | } |
||
659 |