Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | * @param \Illuminate\Database\Query\Builder $builder |
||
24 | * @param \Yajra\Datatables\Request $request |
||
25 | */ |
||
26 | public function __construct(Builder $builder, Request $request) |
||
31 | |||
32 | /** |
||
33 | * Initialize attributes. |
||
34 | * |
||
35 | * @param \Yajra\Datatables\Request $request |
||
36 | * @param \Illuminate\Database\Query\Builder $builder |
||
37 | * @param string $type |
||
38 | */ |
||
39 | protected function init($request, $builder, $type = 'builder') |
||
51 | |||
52 | /** |
||
53 | * Set auto filter off and run your own filter. |
||
54 | * Overrides global search |
||
55 | * |
||
56 | * @param \Closure $callback |
||
57 | * @param bool $globalSearch |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function filter(Closure $callback, $globalSearch = false) |
||
66 | |||
67 | /** |
||
68 | * Organizes works |
||
69 | * |
||
70 | * @param bool $mDataSupport |
||
71 | * @param bool $orderFirst |
||
72 | * @return \Illuminate\Http\JsonResponse |
||
73 | */ |
||
74 | public function make($mDataSupport = false, $orderFirst = false) |
||
78 | |||
79 | /** |
||
80 | * Count total items. |
||
81 | * |
||
82 | * @return integer |
||
83 | */ |
||
84 | public function totalCount() |
||
88 | |||
89 | /** |
||
90 | * Counts current query. |
||
91 | * |
||
92 | * @return int |
||
93 | */ |
||
94 | public function count() |
||
112 | |||
113 | /** |
||
114 | * Wrap column with DB grammar. |
||
115 | * |
||
116 | * @param string $column |
||
117 | * @return string |
||
118 | */ |
||
119 | protected function wrap($column) |
||
123 | |||
124 | /** |
||
125 | * Check if model use SoftDeletes trait |
||
126 | * |
||
127 | * @return boolean |
||
128 | */ |
||
129 | private function modelUseSoftDeletes() |
||
137 | |||
138 | /** |
||
139 | * Perform global search. |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function filtering() |
||
204 | |||
205 | /** |
||
206 | * Perform filter column on selected field. |
||
207 | * |
||
208 | * @param mixed $query |
||
209 | * @param string|Closure $method |
||
210 | * @param mixed $parameters |
||
211 | * @param string $column |
||
212 | * @param string $keyword |
||
213 | */ |
||
214 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
234 | |||
235 | /** |
||
236 | * Build Query Builder Parameters. |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | protected function parameterize() |
||
249 | |||
250 | /** |
||
251 | * Get eager loads keys if eloquent. |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | protected function getEagerLoads() |
||
263 | |||
264 | /** |
||
265 | * Add relation query on global search. |
||
266 | * |
||
267 | * @param mixed $query |
||
268 | * @param string $relation |
||
269 | * @param string $column |
||
270 | * @param string $keyword |
||
271 | */ |
||
272 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
288 | |||
289 | /** |
||
290 | * Compile query builder where clause depending on configurations. |
||
291 | * |
||
292 | * @param mixed $query |
||
293 | * @param string $column |
||
294 | * @param string $keyword |
||
295 | * @param string $relation |
||
296 | */ |
||
297 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
308 | |||
309 | /** |
||
310 | * Wrap a column and cast in pgsql. |
||
311 | * |
||
312 | * @param string $column |
||
313 | * @return string |
||
314 | */ |
||
315 | public function castColumn($column) |
||
326 | |||
327 | /** |
||
328 | * Prepare search keyword based on configurations. |
||
329 | * |
||
330 | * @param string $keyword |
||
331 | * @return string |
||
332 | */ |
||
333 | protected function prepareKeyword($keyword) |
||
349 | |||
350 | /** |
||
351 | * Perform column search. |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | public function columnSearch() |
||
403 | |||
404 | /** |
||
405 | * Get proper keyword to use for search. |
||
406 | * |
||
407 | * @param int $i |
||
408 | * @param bool $raw |
||
409 | * @return string |
||
410 | */ |
||
411 | private function getSearchKeyword($i, $raw = false) |
||
420 | |||
421 | /** |
||
422 | * Join eager loaded relation and get the related column name. |
||
423 | * |
||
424 | * @param string $relation |
||
425 | * @param string $relationColumn |
||
426 | * @return string |
||
427 | */ |
||
428 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
472 | |||
473 | /** |
||
474 | * Compile queries for column search. |
||
475 | * |
||
476 | * @param int $i |
||
477 | * @param mixed $column |
||
478 | * @param string $keyword |
||
479 | */ |
||
480 | protected function compileColumnSearch($i, $column, $keyword) |
||
489 | |||
490 | /** |
||
491 | * Compile regex query column search. |
||
492 | * |
||
493 | * @param mixed $column |
||
494 | * @param string $keyword |
||
495 | */ |
||
496 | protected function regexColumnSearch($column, $keyword) |
||
506 | |||
507 | /** |
||
508 | * Perform sorting of columns. |
||
509 | * |
||
510 | * @return void |
||
511 | */ |
||
512 | public function ordering() |
||
561 | |||
562 | /** |
||
563 | * Perform pagination |
||
564 | * |
||
565 | * @return void |
||
566 | */ |
||
567 | public function paging() |
||
572 | |||
573 | /** |
||
574 | * Get results |
||
575 | * |
||
576 | * @return array|static[] |
||
577 | */ |
||
578 | public function results() |
||
582 | } |
||
583 |