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 implements DataTableEngineContract |
||
21 | { |
||
22 | /** |
||
23 | * @param \Illuminate\Database\Query\Builder $builder |
||
24 | * @param \Yajra\Datatables\Request $request |
||
25 | */ |
||
26 | public function __construct(Builder $builder, Request $request) |
||
27 | { |
||
28 | $this->query = $builder; |
||
29 | $this->init($request, $builder); |
||
30 | } |
||
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') |
||
|
|||
40 | { |
||
41 | $this->request = $request; |
||
42 | $this->query_type = $type; |
||
43 | $this->columns = $builder->columns; |
||
44 | $this->connection = $builder->getConnection(); |
||
45 | $this->prefix = $this->connection->getTablePrefix(); |
||
46 | $this->database = $this->connection->getDriverName(); |
||
47 | if ($this->isDebugging()) { |
||
48 | $this->connection->enableQueryLog(); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function filter(Closure $callback) |
||
56 | { |
||
57 | $this->overrideGlobalSearch($callback, $this->query); |
||
58 | |||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | public function make($mDataSupport = false, $orderFirst = false) |
||
69 | |||
70 | /** |
||
71 | * @inheritdoc |
||
72 | */ |
||
73 | public function totalCount() |
||
77 | |||
78 | /** |
||
79 | * Counts current query. |
||
80 | * |
||
81 | * @return int |
||
82 | */ |
||
83 | public function count() |
||
96 | |||
97 | /** |
||
98 | * Perform global search. |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function filtering() |
||
147 | |||
148 | /** |
||
149 | * Get eager loads keys if eloquent. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | private function getEagerLoads() |
||
161 | |||
162 | /** |
||
163 | * Perform filter column on selected field. |
||
164 | * |
||
165 | * @param mixed $query |
||
166 | * @param string $method |
||
167 | * @param mixed $parameters |
||
168 | * @param string $column |
||
169 | * @param string $keyword |
||
170 | */ |
||
171 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
191 | |||
192 | /** |
||
193 | * Build Query Builder Parameters. |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | protected function parameterize() |
||
206 | |||
207 | /** |
||
208 | * Add relation query on global search. |
||
209 | * |
||
210 | * @param mixed $query |
||
211 | * @param string $relation |
||
212 | * @param string $column |
||
213 | * @param string $keyword |
||
214 | */ |
||
215 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
225 | |||
226 | /** |
||
227 | * Add a query on global search. |
||
228 | * |
||
229 | * @param mixed $query |
||
230 | * @param string $column |
||
231 | * @param string $keyword |
||
232 | */ |
||
233 | protected function compileGlobalSearch($query, $column, $keyword) |
||
244 | |||
245 | /** |
||
246 | * Wrap a column and cast in pgsql. |
||
247 | * |
||
248 | * @param string $column |
||
249 | * @return string |
||
250 | */ |
||
251 | public function castColumn($column) |
||
260 | |||
261 | /** |
||
262 | * Perform column search. |
||
263 | * |
||
264 | * @return void |
||
265 | */ |
||
266 | public function columnSearch() |
||
292 | |||
293 | /** |
||
294 | * Get proper keyword to use for search. |
||
295 | * |
||
296 | * @param int $i |
||
297 | * @return string |
||
298 | */ |
||
299 | private function getSearchKeyword($i) |
||
307 | |||
308 | /** |
||
309 | * Perform case insensitive column search. |
||
310 | * |
||
311 | * @param int $i |
||
312 | * @param string $column |
||
313 | * @param string $keyword |
||
314 | */ |
||
315 | protected function caseInsensitiveColumnSearch($i, $column, $keyword) |
||
327 | |||
328 | /** |
||
329 | * Perform case sensitive column search. |
||
330 | * |
||
331 | * @param int $i |
||
332 | * @param string $column |
||
333 | * @param string $keyword |
||
334 | */ |
||
335 | protected function caseSensitiveColumnSearch($i, $column, $keyword) |
||
347 | |||
348 | /** |
||
349 | * @inheritdoc |
||
350 | */ |
||
351 | public function ordering() |
||
381 | |||
382 | /** |
||
383 | * Get order by column name. |
||
384 | * |
||
385 | * @param array $orderable |
||
386 | * @return string |
||
387 | */ |
||
388 | private function setupOrderColumn(array $orderable) |
||
400 | |||
401 | /** |
||
402 | * @inheritdoc |
||
403 | */ |
||
404 | public function paging() |
||
409 | |||
410 | /** |
||
411 | * Get results |
||
412 | * |
||
413 | * @return array|static[] |
||
414 | */ |
||
415 | public function results() |
||
419 | } |
||
420 |