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 |
||
21 | class QueryBuilderEngine extends BaseEngine |
||
22 | { |
||
23 | /** |
||
24 | * Builder object. |
||
25 | * |
||
26 | * @var \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
27 | */ |
||
28 | protected $query; |
||
29 | |||
30 | /** |
||
31 | * Query builder object. |
||
32 | * |
||
33 | * @var \Illuminate\Database\Query\Builder |
||
34 | */ |
||
35 | protected $builder; |
||
36 | |||
37 | /** |
||
38 | * Database connection used. |
||
39 | * |
||
40 | * @var \Illuminate\Database\Connection |
||
41 | */ |
||
42 | protected $connection; |
||
43 | |||
44 | /** |
||
45 | * @param \Illuminate\Database\Query\Builder $builder |
||
46 | * @param \Yajra\Datatables\Request $request |
||
47 | */ |
||
48 | public function __construct(Builder $builder, Request $request) |
||
58 | |||
59 | /** |
||
60 | * Set auto filter off and run your own filter. |
||
61 | * Overrides global search. |
||
62 | * |
||
63 | * @param callable $callback |
||
64 | * @param bool $globalSearch |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function filter(callable $callback, $globalSearch = false) |
||
73 | |||
74 | /** |
||
75 | * Perform global search. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | View Code Duplication | public function filtering() |
|
|
|||
80 | { |
||
81 | $keyword = $this->request->keyword(); |
||
82 | |||
83 | if ($this->config()->isSmartSearch()) { |
||
84 | $this->smartGlobalSearch($keyword); |
||
85 | |||
86 | return; |
||
87 | } |
||
88 | |||
89 | $this->globalSearch($keyword); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Perform multi-term search by splitting keyword into |
||
94 | * individual words and searches for each of them. |
||
95 | * |
||
96 | * @param string $keyword |
||
97 | */ |
||
98 | private function smartGlobalSearch($keyword) |
||
106 | |||
107 | /** |
||
108 | * Perform global search for the given keyword. |
||
109 | * |
||
110 | * @param string $keyword |
||
111 | */ |
||
112 | protected function globalSearch($keyword) |
||
133 | |||
134 | /** |
||
135 | * Get the base query builder instance. |
||
136 | * |
||
137 | * @param mixed $instance |
||
138 | * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
139 | */ |
||
140 | protected function getBaseQueryBuilder($instance = null) |
||
152 | |||
153 | /** |
||
154 | * Check if column has custom filter handler. |
||
155 | * |
||
156 | * @param string $columnName |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function hasCustomFilter($columnName) |
||
163 | |||
164 | /** |
||
165 | * Apply filterColumn api search. |
||
166 | * |
||
167 | * @param mixed $query |
||
168 | * @param string $columnName |
||
169 | * @param string $keyword |
||
170 | * @param string $boolean |
||
171 | */ |
||
172 | protected function applyFilterColumn($query, $columnName, $keyword, $boolean = 'and') |
||
179 | |||
180 | /** |
||
181 | * Compile query builder where clause depending on configurations. |
||
182 | * |
||
183 | * @param mixed $query |
||
184 | * @param string $column |
||
185 | * @param string $keyword |
||
186 | * @param string $relation |
||
187 | */ |
||
188 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
200 | |||
201 | /** |
||
202 | * Patch for fix about ambiguous field. |
||
203 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
204 | * |
||
205 | * @param mixed $query |
||
206 | * @param string $column |
||
207 | * @return string |
||
208 | */ |
||
209 | protected function addTablePrefix($query, $column) |
||
220 | |||
221 | /** |
||
222 | * Wrap column with DB grammar. |
||
223 | * |
||
224 | * @param string $column |
||
225 | * @return string |
||
226 | */ |
||
227 | protected function wrap($column) |
||
231 | |||
232 | /** |
||
233 | * Wrap a column and cast based on database driver. |
||
234 | * |
||
235 | * @param string $column |
||
236 | * @return string |
||
237 | */ |
||
238 | protected function castColumn($column) |
||
249 | |||
250 | /** |
||
251 | * Prepare search keyword based on configurations. |
||
252 | * |
||
253 | * @param string $keyword |
||
254 | * @return string |
||
255 | */ |
||
256 | protected function prepareKeyword($keyword) |
||
272 | |||
273 | /** |
||
274 | * Organizes works. |
||
275 | * |
||
276 | * @param bool $mDataSupport |
||
277 | * @return \Illuminate\Http\JsonResponse |
||
278 | * @throws \Exception |
||
279 | */ |
||
280 | public function make($mDataSupport = false) |
||
298 | |||
299 | /** |
||
300 | * Count total items. |
||
301 | * |
||
302 | * @return integer |
||
303 | */ |
||
304 | public function totalCount() |
||
308 | |||
309 | /** |
||
310 | * Counts current query. |
||
311 | * |
||
312 | * @return int |
||
313 | */ |
||
314 | public function count() |
||
323 | |||
324 | /** |
||
325 | * Prepare count query builder. |
||
326 | * |
||
327 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
328 | */ |
||
329 | protected function prepareCountQuery() |
||
340 | |||
341 | /** |
||
342 | * Check if builder query uses complex sql. |
||
343 | * |
||
344 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
345 | * @return bool |
||
346 | */ |
||
347 | protected function isComplexQuery($builder) |
||
351 | |||
352 | /** |
||
353 | * Perform sorting of columns. |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | public function ordering() |
||
418 | |||
419 | /** |
||
420 | * Check if column has custom sort handler. |
||
421 | * |
||
422 | * @param string $column |
||
423 | * @return bool |
||
424 | */ |
||
425 | protected function hasCustomOrder($column) |
||
429 | |||
430 | /** |
||
431 | * Get eager loads keys if eloquent. |
||
432 | * |
||
433 | * @return array |
||
434 | */ |
||
435 | protected function getEagerLoads() |
||
443 | |||
444 | /** |
||
445 | * Join eager loaded relation and get the related column name. |
||
446 | * |
||
447 | * @param string $relation |
||
448 | * @param string $relationColumn |
||
449 | * @return string |
||
450 | */ |
||
451 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
498 | |||
499 | /** |
||
500 | * Perform join query. |
||
501 | * |
||
502 | * @param string $table |
||
503 | * @param string $foreign |
||
504 | * @param string $other |
||
505 | */ |
||
506 | protected function performJoin($table, $foreign, $other) |
||
517 | |||
518 | /** |
||
519 | * Get NULLS LAST SQL. |
||
520 | * |
||
521 | * @param string $column |
||
522 | * @param string $direction |
||
523 | * @return string |
||
524 | */ |
||
525 | protected function getNullsLastSql($column, $direction) |
||
531 | |||
532 | /** |
||
533 | * Perform column search. |
||
534 | * |
||
535 | * @return void |
||
536 | */ |
||
537 | public function columnSearch() |
||
569 | |||
570 | /** |
||
571 | * Get column keyword to use for search. |
||
572 | * |
||
573 | * @param int $i |
||
574 | * @param bool $raw |
||
575 | * @return string |
||
576 | */ |
||
577 | protected function getColumnSearchKeyword($i, $raw = false) |
||
586 | |||
587 | /** |
||
588 | * Compile queries for column search. |
||
589 | * |
||
590 | * @param int $i |
||
591 | * @param string $column |
||
592 | * @param string $keyword |
||
593 | */ |
||
594 | protected function compileColumnSearch($i, $column, $keyword) |
||
603 | |||
604 | /** |
||
605 | * Compile regex query column search. |
||
606 | * |
||
607 | * @param mixed $column |
||
608 | * @param string $keyword |
||
609 | */ |
||
610 | protected function regexColumnSearch($column, $keyword) |
||
628 | |||
629 | /** |
||
630 | * Perform pagination. |
||
631 | * |
||
632 | * @return void |
||
633 | */ |
||
634 | public function paging() |
||
639 | |||
640 | /** |
||
641 | * Get paginated results. |
||
642 | * |
||
643 | * @return \Illuminate\Support\Collection |
||
644 | */ |
||
645 | public function results() |
||
649 | |||
650 | /** |
||
651 | * Add column in collection. |
||
652 | * |
||
653 | * @param string $name |
||
654 | * @param string|callable $content |
||
655 | * @param bool|int $order |
||
656 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
657 | */ |
||
658 | public function addColumn($name, $content, $order = false) |
||
664 | |||
665 | /** |
||
666 | * Get query builder instance. |
||
667 | * |
||
668 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
669 | */ |
||
670 | public function getQuery() |
||
674 | |||
675 | /** |
||
676 | * Append debug parameters on output. |
||
677 | * |
||
678 | * @param array $output |
||
679 | * @return array |
||
680 | */ |
||
681 | protected function showDebugger(array $output) |
||
688 | } |
||
689 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.