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 int |
||
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() |
||
227 | |||
228 | /** |
||
229 | * Check if column has custom filter handler. |
||
230 | * |
||
231 | * @param string $columnName |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function hasFilterColumn($columnName) |
||
238 | |||
239 | /** |
||
240 | * Get column keyword to use for search. |
||
241 | * |
||
242 | * @param int $i |
||
243 | * @param bool $raw |
||
244 | * @return string |
||
245 | */ |
||
246 | protected function getColumnSearchKeyword($i, $raw = false) |
||
255 | |||
256 | /** |
||
257 | * Apply filterColumn api search. |
||
258 | * |
||
259 | * @param mixed $query |
||
260 | * @param string $columnName |
||
261 | * @param string $keyword |
||
262 | * @param string $boolean |
||
263 | */ |
||
264 | protected function applyFilterColumn($query, $columnName, $keyword, $boolean = 'and') |
||
279 | |||
280 | /** |
||
281 | * Get the base query builder instance. |
||
282 | * |
||
283 | * @param mixed $instance |
||
284 | * @return \Illuminate\Database\Query\Builder |
||
285 | */ |
||
286 | protected function getBaseQueryBuilder($instance = null) |
||
298 | |||
299 | /** |
||
300 | * Resolve the proper column name be used. |
||
301 | * |
||
302 | * @param string $column |
||
303 | * @return string |
||
304 | */ |
||
305 | protected function resolveRelationColumn($column) |
||
309 | |||
310 | /** |
||
311 | * Compile queries for column search. |
||
312 | * |
||
313 | * @param int $i |
||
314 | * @param string $column |
||
315 | * @param string $keyword |
||
316 | */ |
||
317 | protected function compileColumnSearch($i, $column, $keyword) |
||
326 | |||
327 | /** |
||
328 | * Compile regex query column search. |
||
329 | * |
||
330 | * @param mixed $column |
||
331 | * @param string $keyword |
||
332 | */ |
||
333 | protected function regexColumnSearch($column, $keyword) |
||
356 | |||
357 | /** |
||
358 | * Wrap a column and cast based on database driver. |
||
359 | * |
||
360 | * @param string $column |
||
361 | * @return string |
||
362 | */ |
||
363 | protected function castColumn($column) |
||
374 | |||
375 | /** |
||
376 | * Compile query builder where clause depending on configurations. |
||
377 | * |
||
378 | * @param mixed $query |
||
379 | * @param string $column |
||
380 | * @param string $keyword |
||
381 | * @param string $boolean |
||
382 | */ |
||
383 | protected function compileQuerySearch($query, $column, $keyword, $boolean = 'or') |
||
395 | |||
396 | /** |
||
397 | * Patch for fix about ambiguous field. |
||
398 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
399 | * |
||
400 | * @param mixed $query |
||
401 | * @param string $column |
||
402 | * @return string |
||
403 | */ |
||
404 | protected function addTablePrefix($query, $column) |
||
415 | |||
416 | /** |
||
417 | * Prepare search keyword based on configurations. |
||
418 | * |
||
419 | * @param string $keyword |
||
420 | * @return string |
||
421 | */ |
||
422 | protected function prepareKeyword($keyword) |
||
438 | |||
439 | /** |
||
440 | * Add custom filter handler for the give column. |
||
441 | * |
||
442 | * @param string $column |
||
443 | * @param callable $callback |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function filterColumn($column, callable $callback) |
||
452 | |||
453 | /** |
||
454 | * Order each given columns versus the given custom sql. |
||
455 | * |
||
456 | * @param array $columns |
||
457 | * @param string $sql |
||
458 | * @param array $bindings |
||
459 | * @return $this |
||
460 | */ |
||
461 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
469 | |||
470 | /** |
||
471 | * Override default column ordering. |
||
472 | * |
||
473 | * @param string $column |
||
474 | * @param string $sql |
||
475 | * @param array $bindings |
||
476 | * @return $this |
||
477 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
478 | */ |
||
479 | public function orderColumn($column, $sql, $bindings = []) |
||
485 | |||
486 | /** |
||
487 | * Set datatables to do ordering with NULLS LAST option. |
||
488 | * |
||
489 | * @return $this |
||
490 | */ |
||
491 | public function orderByNullsLast() |
||
497 | |||
498 | /** |
||
499 | * Paginate dataTable using limit without offset |
||
500 | * with additional where clause via callback. |
||
501 | * |
||
502 | * @param callable $callback |
||
503 | * @return $this |
||
504 | */ |
||
505 | public function limit(callable $callback) |
||
511 | |||
512 | /** |
||
513 | * Perform pagination. |
||
514 | * |
||
515 | * @return void |
||
516 | */ |
||
517 | public function paging() |
||
527 | |||
528 | /** |
||
529 | * Add column in collection. |
||
530 | * |
||
531 | * @param string $name |
||
532 | * @param string|callable $content |
||
533 | * @param bool|int $order |
||
534 | * @return $this |
||
535 | */ |
||
536 | public function addColumn($name, $content, $order = false) |
||
542 | |||
543 | /** |
||
544 | * Resolve callback parameter instance. |
||
545 | * |
||
546 | * @return \Illuminate\Database\Query\Builder |
||
547 | */ |
||
548 | protected function resolveCallbackParameter() |
||
552 | |||
553 | /** |
||
554 | * Perform default query orderBy clause. |
||
555 | */ |
||
556 | protected function defaultOrdering() |
||
580 | |||
581 | /** |
||
582 | * Check if column has custom sort handler. |
||
583 | * |
||
584 | * @param string $column |
||
585 | * @return bool |
||
586 | */ |
||
587 | protected function hasOrderColumn($column) |
||
591 | |||
592 | /** |
||
593 | * Apply orderColumn custom query. |
||
594 | * |
||
595 | * @param string $column |
||
596 | * @param array $orderable |
||
597 | */ |
||
598 | protected function applyOrderColumn($column, $orderable) |
||
605 | |||
606 | /** |
||
607 | * Get NULLS LAST SQL. |
||
608 | * |
||
609 | * @param string $column |
||
610 | * @param string $direction |
||
611 | * @return string |
||
612 | */ |
||
613 | protected function getNullsLastSql($column, $direction) |
||
619 | |||
620 | /** |
||
621 | * Perform global search for the given keyword. |
||
622 | * |
||
623 | * @param string $keyword |
||
624 | */ |
||
625 | protected function globalSearch($keyword) |
||
646 | |||
647 | /** |
||
648 | * Append debug parameters on output. |
||
649 | * |
||
650 | * @param array $output |
||
651 | * @return array |
||
652 | */ |
||
653 | protected function showDebugger(array $output) |
||
660 | } |
||
661 |