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 | * Perform global search. |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | public function filtering() |
||
190 | |||
191 | /** |
||
192 | * Perform filter column on selected field. |
||
193 | * |
||
194 | * @param mixed $query |
||
195 | * @param string|Closure $method |
||
196 | * @param mixed $parameters |
||
197 | * @param string $column |
||
198 | * @param string $keyword |
||
199 | */ |
||
200 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
220 | |||
221 | /** |
||
222 | * Build Query Builder Parameters. |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | protected function parameterize() |
||
235 | |||
236 | /** |
||
237 | * Get eager loads keys if eloquent. |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | protected function getEagerLoads() |
||
249 | |||
250 | /** |
||
251 | * Add relation query on global search. |
||
252 | * |
||
253 | * @param mixed $query |
||
254 | * @param string $relation |
||
255 | * @param string $column |
||
256 | * @param string $keyword |
||
257 | */ |
||
258 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
269 | |||
270 | /** |
||
271 | * Compile query builder where clause depending on configurations. |
||
272 | * |
||
273 | * @param mixed $query |
||
274 | * @param string $column |
||
275 | * @param string $keyword |
||
276 | * @param string $relation |
||
277 | */ |
||
278 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
290 | |||
291 | /** |
||
292 | * Wrap a column and cast in pgsql. |
||
293 | * |
||
294 | * @param string $column |
||
295 | * @return string |
||
296 | */ |
||
297 | public function castColumn($column) |
||
308 | |||
309 | /** |
||
310 | * Prepare search keyword based on configurations. |
||
311 | * |
||
312 | * @param string $keyword |
||
313 | * @return string |
||
314 | */ |
||
315 | protected function prepareKeyword($keyword) |
||
331 | |||
332 | /** |
||
333 | * Perform column search. |
||
334 | * |
||
335 | * @return void |
||
336 | */ |
||
337 | public function columnSearch() |
||
385 | |||
386 | /** |
||
387 | * Get proper keyword to use for search. |
||
388 | * |
||
389 | * @param int $i |
||
390 | * @param bool $raw |
||
391 | * @return string |
||
392 | */ |
||
393 | private function getSearchKeyword($i, $raw = false) |
||
402 | |||
403 | /** |
||
404 | * Check if model use SoftDeletes trait |
||
405 | * |
||
406 | * @return boolean |
||
407 | */ |
||
408 | private function modelUseSoftDeletes() |
||
416 | |||
417 | /** |
||
418 | * Join eager loaded relation and get the related column name. |
||
419 | * |
||
420 | * @param string $relation |
||
421 | * @param string $relationColumn |
||
422 | * @return string |
||
423 | */ |
||
424 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
468 | |||
469 | /** |
||
470 | * Compile queries for column search. |
||
471 | * |
||
472 | * @param int $i |
||
473 | * @param mixed $column |
||
474 | * @param string $keyword |
||
475 | */ |
||
476 | protected function compileColumnSearch($i, $column, $keyword) |
||
485 | |||
486 | /** |
||
487 | * Compile regex query column search. |
||
488 | * |
||
489 | * @param mixed $column |
||
490 | * @param string $keyword |
||
491 | */ |
||
492 | protected function regexColumnSearch($column, $keyword) |
||
502 | |||
503 | /** |
||
504 | * Perform sorting of columns. |
||
505 | * |
||
506 | * @return void |
||
507 | */ |
||
508 | public function ordering() |
||
549 | |||
550 | /** |
||
551 | * Perform pagination |
||
552 | * |
||
553 | * @return void |
||
554 | */ |
||
555 | public function paging() |
||
560 | |||
561 | /** |
||
562 | * Get results |
||
563 | * |
||
564 | * @return array|static[] |
||
565 | */ |
||
566 | public function results() |
||
570 | } |
||
571 |