Complex classes like QueryBuilderEngine 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 QueryBuilderEngine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class QueryBuilderEngine extends BaseEngine |
||
18 | { |
||
19 | /** |
||
20 | * Builder object. |
||
21 | * |
||
22 | * @var \Illuminate\Database\Query\Builder |
||
23 | */ |
||
24 | protected $query; |
||
25 | |||
26 | /** |
||
27 | * Database connection used. |
||
28 | * |
||
29 | * @var \Illuminate\Database\Connection |
||
30 | */ |
||
31 | protected $connection; |
||
32 | |||
33 | /** |
||
34 | * Flag for ordering NULLS LAST option. |
||
35 | * |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $nullsLast = false; |
||
39 | |||
40 | /** |
||
41 | * @param \Illuminate\Database\Query\Builder $builder |
||
42 | */ |
||
43 | public function __construct(Builder $builder) |
||
44 | { |
||
45 | $this->query = $builder; |
||
46 | $this->request = resolve('datatables.request'); |
||
47 | $this->config = resolve('datatables.config'); |
||
48 | $this->columns = $builder->columns; |
||
49 | $this->connection = $builder->getConnection(); |
||
50 | if ($this->config->isDebugging()) { |
||
51 | $this->connection->enableQueryLog(); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Organizes works. |
||
57 | * |
||
58 | * @param bool $mDataSupport |
||
59 | * @return \Illuminate\Http\JsonResponse |
||
60 | * @throws \Exception |
||
61 | */ |
||
62 | public function make($mDataSupport = false) |
||
63 | { |
||
64 | try { |
||
65 | $this->totalRecords = $this->totalCount(); |
||
66 | |||
67 | if ($this->totalRecords) { |
||
68 | $this->filterRecords(); |
||
69 | $this->ordering(); |
||
70 | $this->paginate(); |
||
71 | } |
||
72 | |||
73 | $data = $this->transform($this->getProcessedData($mDataSupport)); |
||
74 | |||
75 | return $this->render($data); |
||
76 | } catch (\Exception $exception) { |
||
77 | return $this->errorResponse($exception); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Count total items. |
||
83 | * |
||
84 | * @return integer |
||
85 | */ |
||
86 | public function totalCount() |
||
90 | |||
91 | /** |
||
92 | * Counts current query. |
||
93 | * |
||
94 | * @return int |
||
95 | */ |
||
96 | public function count() |
||
105 | |||
106 | /** |
||
107 | * Prepare count query builder. |
||
108 | * |
||
109 | * @return \Illuminate\Database\Query\Builder |
||
110 | */ |
||
111 | protected function prepareCountQuery() |
||
122 | |||
123 | /** |
||
124 | * Check if builder query uses complex sql. |
||
125 | * |
||
126 | * @param \Illuminate\Database\Query\Builder $builder |
||
127 | * @return bool |
||
128 | */ |
||
129 | protected function isComplexQuery($builder) |
||
133 | |||
134 | /** |
||
135 | * Wrap column with DB grammar. |
||
136 | * |
||
137 | * @param string $column |
||
138 | * @return string |
||
139 | */ |
||
140 | protected function wrap($column) |
||
144 | |||
145 | /** |
||
146 | * Perform column search. |
||
147 | * |
||
148 | * @return void |
||
149 | */ |
||
150 | public function columnSearch() |
||
174 | |||
175 | /** |
||
176 | * Check if column has custom filter handler. |
||
177 | * |
||
178 | * @param string $columnName |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function hasFilterColumn($columnName) |
||
185 | |||
186 | /** |
||
187 | * Get column keyword to use for search. |
||
188 | * |
||
189 | * @param int $i |
||
190 | * @param bool $raw |
||
191 | * @return string |
||
192 | */ |
||
193 | protected function getColumnSearchKeyword($i, $raw = false) |
||
202 | |||
203 | /** |
||
204 | * Apply filterColumn api search. |
||
205 | * |
||
206 | * @param Builder $query |
||
207 | * @param string $columnName |
||
208 | * @param string $keyword |
||
209 | * @param string $boolean |
||
210 | */ |
||
211 | protected function applyFilterColumn(Builder $query, $columnName, $keyword, $boolean = 'and') |
||
218 | |||
219 | /** |
||
220 | * Get the base query builder instance. |
||
221 | * |
||
222 | * @param mixed $instance |
||
223 | * @return \Illuminate\Database\Query\Builder |
||
224 | */ |
||
225 | protected function getBaseQueryBuilder($instance = null) |
||
237 | |||
238 | /** |
||
239 | * Resolve the proper column name be used. |
||
240 | * |
||
241 | * @param string $column |
||
242 | * @return string |
||
243 | */ |
||
244 | protected function resolveRelationColumn($column) |
||
248 | |||
249 | /** |
||
250 | * Compile queries for column search. |
||
251 | * |
||
252 | * @param int $i |
||
253 | * @param string $column |
||
254 | * @param string $keyword |
||
255 | */ |
||
256 | protected function compileColumnSearch($i, $column, $keyword) |
||
265 | |||
266 | /** |
||
267 | * Compile regex query column search. |
||
268 | * |
||
269 | * @param mixed $column |
||
270 | * @param string $keyword |
||
271 | */ |
||
272 | protected function regexColumnSearch($column, $keyword) |
||
292 | |||
293 | /** |
||
294 | * Compile query builder where clause depending on configurations. |
||
295 | * |
||
296 | * @param mixed $query |
||
297 | * @param string $column |
||
298 | * @param string $keyword |
||
299 | * @param string $relation |
||
300 | */ |
||
301 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
313 | |||
314 | /** |
||
315 | * Patch for fix about ambiguous field. |
||
316 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
317 | * |
||
318 | * @param mixed $query |
||
319 | * @param string $column |
||
320 | * @return string |
||
321 | */ |
||
322 | protected function addTablePrefix($query, $column) |
||
333 | |||
334 | /** |
||
335 | * Wrap a column and cast based on database driver. |
||
336 | * |
||
337 | * @param string $column |
||
338 | * @return string |
||
339 | */ |
||
340 | protected function castColumn($column) |
||
351 | |||
352 | /** |
||
353 | * Prepare search keyword based on configurations. |
||
354 | * |
||
355 | * @param string $keyword |
||
356 | * @return string |
||
357 | */ |
||
358 | protected function prepareKeyword($keyword) |
||
374 | |||
375 | /** |
||
376 | * Add custom filter handler for the give column. |
||
377 | * |
||
378 | * @param string $column |
||
379 | * @param callable $callback |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function filterColumn($column, callable $callback) |
||
388 | |||
389 | /** |
||
390 | * Order each given columns versus the given custom sql. |
||
391 | * |
||
392 | * @param array $columns |
||
393 | * @param string $sql |
||
394 | * @param array $bindings |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
405 | |||
406 | /** |
||
407 | * Override default column ordering. |
||
408 | * |
||
409 | * @param string $column |
||
410 | * @param string $sql |
||
411 | * @param array $bindings |
||
412 | * @return $this |
||
413 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
414 | */ |
||
415 | public function orderColumn($column, $sql, $bindings = []) |
||
421 | |||
422 | /** |
||
423 | * Set datatables to do ordering with NULLS LAST option. |
||
424 | * |
||
425 | * @return $this |
||
426 | */ |
||
427 | public function orderByNullsLast() |
||
433 | |||
434 | /** |
||
435 | * Perform pagination. |
||
436 | * |
||
437 | * @return void |
||
438 | */ |
||
439 | public function paging() |
||
444 | |||
445 | /** |
||
446 | * Get paginated results. |
||
447 | * |
||
448 | * @return \Illuminate\Support\Collection |
||
449 | */ |
||
450 | public function results() |
||
454 | |||
455 | /** |
||
456 | * Add column in collection. |
||
457 | * |
||
458 | * @param string $name |
||
459 | * @param string|callable $content |
||
460 | * @param bool|int $order |
||
461 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
462 | */ |
||
463 | public function addColumn($name, $content, $order = false) |
||
469 | |||
470 | /** |
||
471 | * Get query builder instance. |
||
472 | * |
||
473 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
474 | */ |
||
475 | public function getQuery() |
||
479 | |||
480 | /** |
||
481 | * Resolve callback parameter instance. |
||
482 | * |
||
483 | * @return \Illuminate\Database\Query\Builder |
||
484 | */ |
||
485 | protected function resolveCallbackParameter() |
||
489 | |||
490 | /** |
||
491 | * Perform default query orderBy clause. |
||
492 | */ |
||
493 | protected function defaultOrdering() |
||
517 | |||
518 | /** |
||
519 | * Check if column has custom sort handler. |
||
520 | * |
||
521 | * @param string $column |
||
522 | * @return bool |
||
523 | */ |
||
524 | protected function hasOrderColumn($column) |
||
528 | |||
529 | /** |
||
530 | * Apply orderColumn custom query. |
||
531 | * |
||
532 | * @param string $column |
||
533 | * @param array $orderable |
||
534 | */ |
||
535 | protected function applyOrderColumn($column, $orderable): void |
||
542 | |||
543 | /** |
||
544 | * Get NULLS LAST SQL. |
||
545 | * |
||
546 | * @param string $column |
||
547 | * @param string $direction |
||
548 | * @return string |
||
549 | */ |
||
550 | protected function getNullsLastSql($column, $direction) |
||
556 | |||
557 | /** |
||
558 | * Perform global search for the given keyword. |
||
559 | * |
||
560 | * @param string $keyword |
||
561 | */ |
||
562 | protected function globalSearch($keyword) |
||
585 | |||
586 | /** |
||
587 | * Append debug parameters on output. |
||
588 | * |
||
589 | * @param array $output |
||
590 | * @return array |
||
591 | */ |
||
592 | protected function showDebugger(array $output) |
||
599 | } |
||
600 |