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 | * Set auto filter off and run your own filter. |
||
57 | * Overrides global search. |
||
58 | * |
||
59 | * @param callable $callback |
||
60 | * @param bool $globalSearch |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function filter(callable $callback, $globalSearch = false) |
||
69 | |||
70 | /** |
||
71 | * Organizes works. |
||
72 | * |
||
73 | * @param bool $mDataSupport |
||
74 | * @return \Illuminate\Http\JsonResponse |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | public function make($mDataSupport = false) |
||
95 | |||
96 | /** |
||
97 | * Count total items. |
||
98 | * |
||
99 | * @return integer |
||
100 | */ |
||
101 | public function totalCount() |
||
105 | |||
106 | /** |
||
107 | * Counts current query. |
||
108 | * |
||
109 | * @return int |
||
110 | */ |
||
111 | public function count() |
||
120 | |||
121 | /** |
||
122 | * Prepare count query builder. |
||
123 | * |
||
124 | * @return \Illuminate\Database\Query\Builder |
||
125 | */ |
||
126 | protected function prepareCountQuery() |
||
137 | |||
138 | /** |
||
139 | * Check if builder query uses complex sql. |
||
140 | * |
||
141 | * @param \Illuminate\Database\Query\Builder $builder |
||
142 | * @return bool |
||
143 | */ |
||
144 | protected function isComplexQuery($builder) |
||
148 | |||
149 | /** |
||
150 | * Wrap column with DB grammar. |
||
151 | * |
||
152 | * @param string $column |
||
153 | * @return string |
||
154 | */ |
||
155 | protected function wrap($column) |
||
159 | |||
160 | /** |
||
161 | * Perform column search. |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | public function columnSearch() |
||
189 | |||
190 | /** |
||
191 | * Check if column has custom filter handler. |
||
192 | * |
||
193 | * @param string $columnName |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function hasFilterColumn($columnName) |
||
200 | |||
201 | /** |
||
202 | * Get column keyword to use for search. |
||
203 | * |
||
204 | * @param int $i |
||
205 | * @param bool $raw |
||
206 | * @return string |
||
207 | */ |
||
208 | protected function getColumnSearchKeyword($i, $raw = false) |
||
217 | |||
218 | /** |
||
219 | * Apply filterColumn api search. |
||
220 | * |
||
221 | * @param Builder $query |
||
222 | * @param string $columnName |
||
223 | * @param string $keyword |
||
224 | * @param string $boolean |
||
225 | */ |
||
226 | protected function applyFilterColumn(Builder $query, $columnName, $keyword, $boolean = 'and') |
||
233 | |||
234 | /** |
||
235 | * Get the base query builder instance. |
||
236 | * |
||
237 | * @param mixed $instance |
||
238 | * @return \Illuminate\Database\Query\Builder |
||
239 | */ |
||
240 | protected function getBaseQueryBuilder($instance = null) |
||
252 | |||
253 | /** |
||
254 | * Resolve the proper column name be used. |
||
255 | * |
||
256 | * @param string $column |
||
257 | * @return string |
||
258 | */ |
||
259 | protected function resolveRelationColumn($column) |
||
263 | |||
264 | /** |
||
265 | * Compile queries for column search. |
||
266 | * |
||
267 | * @param int $i |
||
268 | * @param string $column |
||
269 | * @param string $keyword |
||
270 | */ |
||
271 | protected function compileColumnSearch($i, $column, $keyword) |
||
280 | |||
281 | /** |
||
282 | * Compile regex query column search. |
||
283 | * |
||
284 | * @param mixed $column |
||
285 | * @param string $keyword |
||
286 | */ |
||
287 | protected function regexColumnSearch($column, $keyword) |
||
307 | |||
308 | /** |
||
309 | * Compile query builder where clause depending on configurations. |
||
310 | * |
||
311 | * @param mixed $query |
||
312 | * @param string $column |
||
313 | * @param string $keyword |
||
314 | * @param string $relation |
||
315 | */ |
||
316 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
328 | |||
329 | /** |
||
330 | * Patch for fix about ambiguous field. |
||
331 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
332 | * |
||
333 | * @param mixed $query |
||
334 | * @param string $column |
||
335 | * @return string |
||
336 | */ |
||
337 | protected function addTablePrefix($query, $column) |
||
348 | |||
349 | /** |
||
350 | * Wrap a column and cast based on database driver. |
||
351 | * |
||
352 | * @param string $column |
||
353 | * @return string |
||
354 | */ |
||
355 | protected function castColumn($column) |
||
366 | |||
367 | /** |
||
368 | * Prepare search keyword based on configurations. |
||
369 | * |
||
370 | * @param string $keyword |
||
371 | * @return string |
||
372 | */ |
||
373 | protected function prepareKeyword($keyword) |
||
389 | |||
390 | /** |
||
391 | * Add custom filter handler for the give column. |
||
392 | * |
||
393 | * @param string $column |
||
394 | * @param callable $callback |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function filterColumn($column, callable $callback) |
||
403 | |||
404 | /** |
||
405 | * Order each given columns versus the given custom sql. |
||
406 | * |
||
407 | * @param array $columns |
||
408 | * @param string $sql |
||
409 | * @param array $bindings |
||
410 | * @return $this |
||
411 | */ |
||
412 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
420 | |||
421 | /** |
||
422 | * Override default column ordering. |
||
423 | * |
||
424 | * @param string $column |
||
425 | * @param string $sql |
||
426 | * @param array $bindings |
||
427 | * @return $this |
||
428 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
429 | */ |
||
430 | public function orderColumn($column, $sql, $bindings = []) |
||
436 | |||
437 | /** |
||
438 | * Set datatables to do ordering with NULLS LAST option. |
||
439 | * |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function orderByNullsLast() |
||
448 | |||
449 | /** |
||
450 | * Perform pagination. |
||
451 | * |
||
452 | * @return void |
||
453 | */ |
||
454 | public function paging() |
||
459 | |||
460 | /** |
||
461 | * Get paginated results. |
||
462 | * |
||
463 | * @return \Illuminate\Support\Collection |
||
464 | */ |
||
465 | public function results() |
||
469 | |||
470 | /** |
||
471 | * Add column in collection. |
||
472 | * |
||
473 | * @param string $name |
||
474 | * @param string|callable $content |
||
475 | * @param bool|int $order |
||
476 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
477 | */ |
||
478 | public function addColumn($name, $content, $order = false) |
||
484 | |||
485 | /** |
||
486 | * Get query builder instance. |
||
487 | * |
||
488 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
489 | */ |
||
490 | public function getQuery() |
||
494 | |||
495 | /** |
||
496 | * Resolve callback parameter instance. |
||
497 | * |
||
498 | * @return \Illuminate\Database\Query\Builder |
||
499 | */ |
||
500 | protected function resolveCallbackParameter() |
||
504 | |||
505 | /** |
||
506 | * Perform default query orderBy clause. |
||
507 | */ |
||
508 | protected function defaultOrdering() |
||
532 | |||
533 | /** |
||
534 | * Check if column has custom sort handler. |
||
535 | * |
||
536 | * @param string $column |
||
537 | * @return bool |
||
538 | */ |
||
539 | protected function hasOrderColumn($column) |
||
543 | |||
544 | /** |
||
545 | * Apply orderColumn custom query. |
||
546 | * |
||
547 | * @param string $column |
||
548 | * @param array $orderable |
||
549 | */ |
||
550 | protected function applyOrderColumn($column, $orderable): void |
||
557 | |||
558 | /** |
||
559 | * Get NULLS LAST SQL. |
||
560 | * |
||
561 | * @param string $column |
||
562 | * @param string $direction |
||
563 | * @return string |
||
564 | */ |
||
565 | protected function getNullsLastSql($column, $direction) |
||
571 | |||
572 | /** |
||
573 | * Perform global search for the given keyword. |
||
574 | * |
||
575 | * @param string $keyword |
||
576 | */ |
||
577 | protected function globalSearch($keyword) |
||
600 | |||
601 | /** |
||
602 | * Append debug parameters on output. |
||
603 | * |
||
604 | * @param array $output |
||
605 | * @return array |
||
606 | */ |
||
607 | protected function showDebugger(array $output) |
||
614 | } |
||
615 |