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 |
||
19 | class QueryBuilderEngine extends BaseEngine |
||
20 | { |
||
21 | /** |
||
22 | * @param \Illuminate\Database\Query\Builder $builder |
||
23 | * @param \Yajra\Datatables\Request $request |
||
24 | */ |
||
25 | public function __construct(Builder $builder, Request $request) |
||
30 | |||
31 | /** |
||
32 | * Initialize attributes. |
||
33 | * |
||
34 | * @param \Yajra\Datatables\Request $request |
||
35 | * @param \Illuminate\Database\Query\Builder $builder |
||
36 | * @param string $type |
||
37 | */ |
||
38 | protected function init($request, $builder, $type = 'builder') |
||
50 | |||
51 | /** |
||
52 | * Set auto filter off and run your own filter. |
||
53 | * Overrides global search |
||
54 | * |
||
55 | * @param \Closure $callback |
||
56 | * @return $this |
||
57 | */ |
||
58 | public function filter(Closure $callback) |
||
64 | |||
65 | /** |
||
66 | * Organizes works |
||
67 | * |
||
68 | * @param bool $mDataSupport |
||
69 | * @param bool $orderFirst |
||
70 | * @return \Illuminate\Http\JsonResponse |
||
71 | */ |
||
72 | public function make($mDataSupport = false, $orderFirst = false) |
||
76 | |||
77 | /** |
||
78 | * Count total items. |
||
79 | * |
||
80 | * @return integer |
||
81 | */ |
||
82 | public function totalCount() |
||
86 | |||
87 | /** |
||
88 | * Counts current query. |
||
89 | * |
||
90 | * @return int |
||
91 | */ |
||
92 | public function count() |
||
105 | |||
106 | /** |
||
107 | * Wrap column with DB grammar. |
||
108 | * |
||
109 | * @param string $column |
||
110 | * @return string |
||
111 | */ |
||
112 | protected function wrap($column) { |
||
115 | |||
116 | /** |
||
117 | * Perform global search. |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | public function filtering() |
||
182 | |||
183 | /** |
||
184 | * Perform filter column on selected field. |
||
185 | * |
||
186 | * @param mixed $query |
||
187 | * @param string|Closure $method |
||
188 | * @param mixed $parameters |
||
189 | * @param string $column |
||
190 | * @param string $keyword |
||
191 | */ |
||
192 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
212 | |||
213 | /** |
||
214 | * Build Query Builder Parameters. |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | protected function parameterize() |
||
227 | |||
228 | /** |
||
229 | * Get eager loads keys if eloquent. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | protected function getEagerLoads() |
||
241 | |||
242 | /** |
||
243 | * Add relation query on global search. |
||
244 | * |
||
245 | * @param mixed $query |
||
246 | * @param string $relation |
||
247 | * @param string $column |
||
248 | * @param string $keyword |
||
249 | */ |
||
250 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
261 | |||
262 | /** |
||
263 | * Compile query builder where clause depending on configurations. |
||
264 | * |
||
265 | * @param mixed $query |
||
266 | * @param string $column |
||
267 | * @param string $keyword |
||
268 | * @param string $relation |
||
269 | */ |
||
270 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
291 | |||
292 | /** |
||
293 | * Wrap a column and cast in pgsql. |
||
294 | * |
||
295 | * @param string $column |
||
296 | * @return string |
||
297 | */ |
||
298 | public function castColumn($column) |
||
309 | |||
310 | /** |
||
311 | * Perform column search. |
||
312 | * |
||
313 | * @return void |
||
314 | */ |
||
315 | public function columnSearch() |
||
363 | |||
364 | /** |
||
365 | * Get proper keyword to use for search. |
||
366 | * |
||
367 | * @param int $i |
||
368 | * @param bool $raw |
||
369 | * @return string |
||
370 | */ |
||
371 | private function getSearchKeyword($i, $raw = false) |
||
380 | |||
381 | /** |
||
382 | * Compile queries for column search. |
||
383 | * |
||
384 | * @param int $i |
||
385 | * @param mixed $column |
||
386 | * @param string $keyword |
||
387 | */ |
||
388 | protected function compileColumnSearch($i, $column, $keyword) |
||
397 | |||
398 | /** |
||
399 | * Compile regex query column search. |
||
400 | * |
||
401 | * @param mixed $column |
||
402 | * @param string $keyword |
||
403 | */ |
||
404 | protected function regexColumnSearch($column, $keyword) |
||
414 | |||
415 | /** |
||
416 | * Perform sorting of columns. |
||
417 | * |
||
418 | * @return void |
||
419 | */ |
||
420 | public function ordering() |
||
461 | |||
462 | /** |
||
463 | * Join eager loaded relation and get the related column name. |
||
464 | * |
||
465 | * @param string $relation |
||
466 | * @param string $relationColumn |
||
467 | * @return string |
||
468 | */ |
||
469 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
513 | |||
514 | /** |
||
515 | * Perform pagination |
||
516 | * |
||
517 | * @return void |
||
518 | */ |
||
519 | public function paging() |
||
524 | |||
525 | /** |
||
526 | * Get results |
||
527 | * |
||
528 | * @return array|static[] |
||
529 | */ |
||
530 | public function results() |
||
534 | } |
||
535 |