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 |
||
20 | class QueryBuilderEngine extends BaseEngine |
||
21 | { |
||
22 | /** |
||
23 | * Builder object. |
||
24 | * |
||
25 | * @var \Illuminate\Database\Query\Builder |
||
26 | */ |
||
27 | protected $query; |
||
28 | |||
29 | /** |
||
30 | * Database connection used. |
||
31 | * |
||
32 | * @var \Illuminate\Database\Connection |
||
33 | */ |
||
34 | protected $connection; |
||
35 | |||
36 | /** |
||
37 | * @param \Illuminate\Database\Query\Builder $builder |
||
38 | */ |
||
39 | public function __construct(Builder $builder) |
||
50 | |||
51 | /** |
||
52 | * Set auto filter off and run your own filter. |
||
53 | * Overrides global search. |
||
54 | * |
||
55 | * @param callable $callback |
||
56 | * @param bool $globalSearch |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function filter(callable $callback, $globalSearch = false) |
||
65 | |||
66 | /** |
||
67 | * Perform global search for the given keyword. |
||
68 | * |
||
69 | * @param string $keyword |
||
70 | */ |
||
71 | protected function globalSearch($keyword) |
||
91 | |||
92 | /** |
||
93 | * Get the base query builder instance. |
||
94 | * |
||
95 | * @param mixed $instance |
||
96 | * @return \Illuminate\Database\Query\Builder |
||
97 | */ |
||
98 | protected function getBaseQueryBuilder($instance = null) |
||
110 | |||
111 | /** |
||
112 | * Check if column has custom filter handler. |
||
113 | * |
||
114 | * @param string $columnName |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function hasCustomFilter($columnName) |
||
121 | |||
122 | /** |
||
123 | * Apply filterColumn api search. |
||
124 | * |
||
125 | * @param mixed $query |
||
126 | * @param string $columnName |
||
127 | * @param string $keyword |
||
128 | * @param string $boolean |
||
129 | */ |
||
130 | protected function applyFilterColumn($query, $columnName, $keyword, $boolean = 'and') |
||
137 | |||
138 | /** |
||
139 | * Compile query builder where clause depending on configurations. |
||
140 | * |
||
141 | * @param mixed $query |
||
142 | * @param string $column |
||
143 | * @param string $keyword |
||
144 | * @param string $relation |
||
145 | */ |
||
146 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
158 | |||
159 | /** |
||
160 | * Patch for fix about ambiguous field. |
||
161 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
162 | * |
||
163 | * @param mixed $query |
||
164 | * @param string $column |
||
165 | * @return string |
||
166 | */ |
||
167 | protected function addTablePrefix($query, $column) |
||
178 | |||
179 | /** |
||
180 | * Wrap column with DB grammar. |
||
181 | * |
||
182 | * @param string $column |
||
183 | * @return string |
||
184 | */ |
||
185 | protected function wrap($column) |
||
189 | |||
190 | /** |
||
191 | * Wrap a column and cast based on database driver. |
||
192 | * |
||
193 | * @param string $column |
||
194 | * @return string |
||
195 | */ |
||
196 | protected function castColumn($column) |
||
207 | |||
208 | /** |
||
209 | * Prepare search keyword based on configurations. |
||
210 | * |
||
211 | * @param string $keyword |
||
212 | * @return string |
||
213 | */ |
||
214 | protected function prepareKeyword($keyword) |
||
230 | |||
231 | /** |
||
232 | * Organizes works. |
||
233 | * |
||
234 | * @param bool $mDataSupport |
||
235 | * @return \Illuminate\Http\JsonResponse |
||
236 | * @throws \Exception |
||
237 | */ |
||
238 | public function make($mDataSupport = false) |
||
256 | |||
257 | /** |
||
258 | * Count total items. |
||
259 | * |
||
260 | * @return integer |
||
261 | */ |
||
262 | public function totalCount() |
||
266 | |||
267 | /** |
||
268 | * Counts current query. |
||
269 | * |
||
270 | * @return int |
||
271 | */ |
||
272 | public function count() |
||
281 | |||
282 | /** |
||
283 | * Prepare count query builder. |
||
284 | * |
||
285 | * @return \Illuminate\Database\Query\Builder |
||
286 | */ |
||
287 | protected function prepareCountQuery() |
||
298 | |||
299 | /** |
||
300 | * Check if builder query uses complex sql. |
||
301 | * |
||
302 | * @param \Illuminate\Database\Query\Builder $builder |
||
303 | * @return bool |
||
304 | */ |
||
305 | protected function isComplexQuery($builder) |
||
309 | |||
310 | /** |
||
311 | * Perform sorting of columns. |
||
312 | * |
||
313 | * @return void |
||
314 | */ |
||
315 | public function ordering() |
||
373 | |||
374 | /** |
||
375 | * Check if column has custom sort handler. |
||
376 | * |
||
377 | * @param string $column |
||
378 | * @return bool |
||
379 | */ |
||
380 | protected function hasCustomOrder($column) |
||
384 | |||
385 | /** |
||
386 | * Apply orderColumn custom query. |
||
387 | * |
||
388 | * @param string $column |
||
389 | * @param array $orderable |
||
390 | */ |
||
391 | protected function applyOrderColumn($column, $orderable): void |
||
398 | |||
399 | /** |
||
400 | * Get eager loads keys if eloquent. |
||
401 | * |
||
402 | * @return array |
||
403 | */ |
||
404 | protected function getEagerLoads() |
||
412 | |||
413 | /** |
||
414 | * Join eager loaded relation and get the related column name. |
||
415 | * |
||
416 | * @param string $relation |
||
417 | * @param string $relationColumn |
||
418 | * @return string |
||
419 | */ |
||
420 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
467 | |||
468 | /** |
||
469 | * Perform join query. |
||
470 | * |
||
471 | * @param string $table |
||
472 | * @param string $foreign |
||
473 | * @param string $other |
||
474 | */ |
||
475 | protected function performJoin($table, $foreign, $other) |
||
486 | |||
487 | /** |
||
488 | * Get NULLS LAST SQL. |
||
489 | * |
||
490 | * @param string $column |
||
491 | * @param string $direction |
||
492 | * @return string |
||
493 | */ |
||
494 | protected function getNullsLastSql($column, $direction) |
||
500 | |||
501 | /** |
||
502 | * Perform column search. |
||
503 | * |
||
504 | * @return void |
||
505 | */ |
||
506 | public function columnSearch() |
||
538 | |||
539 | /** |
||
540 | * Get column keyword to use for search. |
||
541 | * |
||
542 | * @param int $i |
||
543 | * @param bool $raw |
||
544 | * @return string |
||
545 | */ |
||
546 | protected function getColumnSearchKeyword($i, $raw = false) |
||
555 | |||
556 | /** |
||
557 | * Compile queries for column search. |
||
558 | * |
||
559 | * @param int $i |
||
560 | * @param string $column |
||
561 | * @param string $keyword |
||
562 | */ |
||
563 | protected function compileColumnSearch($i, $column, $keyword) |
||
572 | |||
573 | /** |
||
574 | * Compile regex query column search. |
||
575 | * |
||
576 | * @param mixed $column |
||
577 | * @param string $keyword |
||
578 | */ |
||
579 | protected function regexColumnSearch($column, $keyword) |
||
599 | |||
600 | /** |
||
601 | * Perform pagination. |
||
602 | * |
||
603 | * @return void |
||
604 | */ |
||
605 | public function paging() |
||
610 | |||
611 | /** |
||
612 | * Get paginated results. |
||
613 | * |
||
614 | * @return \Illuminate\Support\Collection |
||
615 | */ |
||
616 | public function results() |
||
620 | |||
621 | /** |
||
622 | * Add column in collection. |
||
623 | * |
||
624 | * @param string $name |
||
625 | * @param string|callable $content |
||
626 | * @param bool|int $order |
||
627 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
628 | */ |
||
629 | public function addColumn($name, $content, $order = false) |
||
635 | |||
636 | /** |
||
637 | * Get query builder instance. |
||
638 | * |
||
639 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
640 | */ |
||
641 | public function getQuery() |
||
645 | |||
646 | /** |
||
647 | * Append debug parameters on output. |
||
648 | * |
||
649 | * @param array $output |
||
650 | * @return array |
||
651 | */ |
||
652 | protected function showDebugger(array $output) |
||
659 | } |
||
660 |