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 | * @param bool $globalSearch |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function filter(Closure $callback, $globalSearch = false) |
||
65 | |||
66 | /** |
||
67 | * Organizes works |
||
68 | * |
||
69 | * @param bool $mDataSupport |
||
70 | * @param bool $orderFirst |
||
71 | * @return \Illuminate\Http\JsonResponse |
||
72 | */ |
||
73 | public function make($mDataSupport = false, $orderFirst = false) |
||
77 | |||
78 | /** |
||
79 | * Count total items. |
||
80 | * |
||
81 | * @return integer |
||
82 | */ |
||
83 | public function totalCount() |
||
87 | |||
88 | /** |
||
89 | * Counts current query. |
||
90 | * |
||
91 | * @return int |
||
92 | */ |
||
93 | public function count() |
||
106 | |||
107 | /** |
||
108 | * Wrap column with DB grammar. |
||
109 | * |
||
110 | * @param string $column |
||
111 | * @return string |
||
112 | */ |
||
113 | protected function wrap($column) |
||
117 | |||
118 | /** |
||
119 | * Perform global search. |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | public function filtering() |
||
184 | |||
185 | /** |
||
186 | * Perform filter column on selected field. |
||
187 | * |
||
188 | * @param mixed $query |
||
189 | * @param string|Closure $method |
||
190 | * @param mixed $parameters |
||
191 | * @param string $column |
||
192 | * @param string $keyword |
||
193 | */ |
||
194 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
214 | |||
215 | /** |
||
216 | * Build Query Builder Parameters. |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | protected function parameterize() |
||
229 | |||
230 | /** |
||
231 | * Get eager loads keys if eloquent. |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | protected function getEagerLoads() |
||
243 | |||
244 | /** |
||
245 | * Add relation query on global search. |
||
246 | * |
||
247 | * @param mixed $query |
||
248 | * @param string $relation |
||
249 | * @param string $column |
||
250 | * @param string $keyword |
||
251 | */ |
||
252 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
263 | |||
264 | /** |
||
265 | * Compile query builder where clause depending on configurations. |
||
266 | * |
||
267 | * @param mixed $query |
||
268 | * @param string $column |
||
269 | * @param string $keyword |
||
270 | * @param string $relation |
||
271 | */ |
||
272 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
284 | |||
285 | /** |
||
286 | * Wrap a column and cast in pgsql. |
||
287 | * |
||
288 | * @param string $column |
||
289 | * @return string |
||
290 | */ |
||
291 | public function castColumn($column) |
||
302 | |||
303 | /** |
||
304 | * Prepare search keyword based on configurations. |
||
305 | * |
||
306 | * @param string $keyword |
||
307 | * @return string |
||
308 | */ |
||
309 | protected function prepareKeyword($keyword) |
||
325 | |||
326 | /** |
||
327 | * Perform column search. |
||
328 | * |
||
329 | * @return void |
||
330 | */ |
||
331 | public function columnSearch() |
||
379 | |||
380 | /** |
||
381 | * Get proper keyword to use for search. |
||
382 | * |
||
383 | * @param int $i |
||
384 | * @param bool $raw |
||
385 | * @return string |
||
386 | */ |
||
387 | private function getSearchKeyword($i, $raw = false) |
||
396 | |||
397 | /** |
||
398 | * Join eager loaded relation and get the related column name. |
||
399 | * |
||
400 | * @param string $relation |
||
401 | * @param string $relationColumn |
||
402 | * @return string |
||
403 | */ |
||
404 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
448 | |||
449 | /** |
||
450 | * Compile queries for column search. |
||
451 | * |
||
452 | * @param int $i |
||
453 | * @param mixed $column |
||
454 | * @param string $keyword |
||
455 | */ |
||
456 | protected function compileColumnSearch($i, $column, $keyword) |
||
465 | |||
466 | /** |
||
467 | * Compile regex query column search. |
||
468 | * |
||
469 | * @param mixed $column |
||
470 | * @param string $keyword |
||
471 | */ |
||
472 | protected function regexColumnSearch($column, $keyword) |
||
482 | |||
483 | /** |
||
484 | * Perform sorting of columns. |
||
485 | * |
||
486 | * @return void |
||
487 | */ |
||
488 | public function ordering() |
||
529 | |||
530 | /** |
||
531 | * Perform pagination |
||
532 | * |
||
533 | * @return void |
||
534 | */ |
||
535 | public function paging() |
||
540 | |||
541 | /** |
||
542 | * Get results |
||
543 | * |
||
544 | * @return array|static[] |
||
545 | */ |
||
546 | public function results() |
||
550 | } |
||
551 |