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 |
||
18 | class QueryBuilderEngine extends BaseEngine |
||
19 | { |
||
20 | /** |
||
21 | * @param \Illuminate\Database\Query\Builder $builder |
||
22 | * @param \Yajra\Datatables\Request $request |
||
23 | */ |
||
24 | public function __construct(Builder $builder, Request $request) |
||
29 | |||
30 | /** |
||
31 | * Initialize attributes. |
||
32 | * |
||
33 | * @param \Yajra\Datatables\Request $request |
||
34 | * @param \Illuminate\Database\Query\Builder $builder |
||
35 | * @param string $type |
||
36 | */ |
||
37 | protected function init($request, $builder, $type = 'builder') |
||
49 | |||
50 | /** |
||
51 | * Set auto filter off and run your own filter. |
||
52 | * Overrides global search |
||
53 | * |
||
54 | * @param \Closure $callback |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function filter(Closure $callback) |
||
63 | |||
64 | /** |
||
65 | * Organizes works |
||
66 | * |
||
67 | * @param bool $mDataSupport |
||
68 | * @param bool $orderFirst |
||
69 | * @return \Illuminate\Http\JsonResponse |
||
70 | */ |
||
71 | public function make($mDataSupport = false, $orderFirst = false) |
||
75 | |||
76 | /** |
||
77 | * Count total items. |
||
78 | * |
||
79 | * @return integer |
||
80 | */ |
||
81 | public function totalCount() |
||
85 | |||
86 | /** |
||
87 | * Counts current query. |
||
88 | * |
||
89 | * @return int |
||
90 | */ |
||
91 | public function count() |
||
104 | |||
105 | /** |
||
106 | * Perform global search. |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | public function filtering() |
||
171 | |||
172 | /** |
||
173 | * Perform filter column on selected field. |
||
174 | * |
||
175 | * @param mixed $query |
||
176 | * @param string|Closure $method |
||
177 | * @param mixed $parameters |
||
178 | * @param string $column |
||
179 | * @param string $keyword |
||
180 | */ |
||
181 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
201 | |||
202 | /** |
||
203 | * Build Query Builder Parameters. |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | protected function parameterize() |
||
216 | |||
217 | /** |
||
218 | * Get eager loads keys if eloquent. |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | protected function getEagerLoads() |
||
230 | |||
231 | /** |
||
232 | * Add relation query on global search. |
||
233 | * |
||
234 | * @param mixed $query |
||
235 | * @param string $relation |
||
236 | * @param string $column |
||
237 | * @param string $keyword |
||
238 | */ |
||
239 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
250 | |||
251 | /** |
||
252 | * Add a query on global search. |
||
253 | * |
||
254 | * @param mixed $query |
||
255 | * @param string $column |
||
256 | * @param string $keyword |
||
257 | */ |
||
258 | protected function compileGlobalSearch($query, $column, $keyword) |
||
273 | |||
274 | /** |
||
275 | * Wrap a column and cast in pgsql. |
||
276 | * |
||
277 | * @param string $column |
||
278 | * @return string |
||
279 | */ |
||
280 | public function castColumn($column) |
||
289 | |||
290 | /** |
||
291 | * Perform column search. |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | public function columnSearch() |
||
350 | |||
351 | /** |
||
352 | * Get proper keyword to use for search. |
||
353 | * |
||
354 | * @param int $i |
||
355 | * @param bool $raw |
||
356 | * @return string |
||
357 | */ |
||
358 | private function getSearchKeyword($i, $raw = false) |
||
367 | |||
368 | /** |
||
369 | * Compile queries for column search. |
||
370 | * |
||
371 | * @param int $i |
||
372 | * @param mixed $column |
||
373 | * @param string $keyword |
||
374 | * @param bool $caseSensitive |
||
375 | */ |
||
376 | protected function compileColumnSearch($i, $column, $keyword, $caseSensitive = true) |
||
388 | |||
389 | /** |
||
390 | * Compile regex query column search. |
||
391 | * |
||
392 | * @param mixed $column |
||
393 | * @param string $keyword |
||
394 | * @param bool $caseSensitive |
||
395 | */ |
||
396 | protected function regexColumnSearch($column, $keyword, $caseSensitive = true) |
||
406 | |||
407 | /** |
||
408 | * Perform sorting of columns. |
||
409 | * |
||
410 | * @return void |
||
411 | */ |
||
412 | public function ordering() |
||
453 | |||
454 | /** |
||
455 | * Join eager loaded relation and get the related column name. |
||
456 | * |
||
457 | * @param string $relation |
||
458 | * @param string $relationColumn |
||
459 | * @return string |
||
460 | */ |
||
461 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
499 | |||
500 | /** |
||
501 | * Perform pagination |
||
502 | * |
||
503 | * @return void |
||
504 | */ |
||
505 | public function paging() |
||
510 | |||
511 | /** |
||
512 | * Get results |
||
513 | * |
||
514 | * @return array|static[] |
||
515 | */ |
||
516 | public function results() |
||
520 | } |
||
521 |