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