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 |
||
16 | class QueryBuilderEngine extends BaseEngine |
||
17 | { |
||
18 | /** |
||
19 | * Builder object. |
||
20 | * |
||
21 | * @var \Illuminate\Database\Query\Builder |
||
22 | */ |
||
23 | protected $query; |
||
24 | |||
25 | /** |
||
26 | * Database connection used. |
||
27 | * |
||
28 | * @var \Illuminate\Database\Connection |
||
29 | */ |
||
30 | protected $connection; |
||
31 | |||
32 | /** |
||
33 | * @param \Illuminate\Database\Query\Builder $builder |
||
34 | */ |
||
35 | public function __construct(Builder $builder) |
||
36 | { |
||
37 | $this->query = $builder; |
||
38 | $this->request = resolve('datatables.request'); |
||
39 | $this->config = resolve('datatables.config'); |
||
40 | $this->columns = $builder->columns; |
||
41 | $this->connection = $builder->getConnection(); |
||
42 | if ($this->config->isDebugging()) { |
||
43 | $this->connection->enableQueryLog(); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Set auto filter off and run your own filter. |
||
49 | * Overrides global search. |
||
50 | * |
||
51 | * @param callable $callback |
||
52 | * @param bool $globalSearch |
||
53 | * @return $this |
||
54 | */ |
||
55 | public function filter(callable $callback, $globalSearch = false) |
||
56 | { |
||
57 | $this->overrideGlobalSearch($callback, $this->query, $globalSearch); |
||
58 | |||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Organizes works. |
||
64 | * |
||
65 | * @param bool $mDataSupport |
||
66 | * @return \Illuminate\Http\JsonResponse |
||
67 | * @throws \Exception |
||
68 | */ |
||
69 | public function make($mDataSupport = false) |
||
70 | { |
||
71 | try { |
||
72 | $this->totalRecords = $this->totalCount(); |
||
73 | |||
74 | if ($this->totalRecords) { |
||
75 | $this->filterRecords(); |
||
76 | $this->ordering(); |
||
77 | $this->paginate(); |
||
78 | } |
||
79 | |||
80 | $data = $this->transform($this->getProcessedData($mDataSupport)); |
||
81 | |||
82 | return $this->render($data); |
||
83 | } catch (\Exception $exception) { |
||
84 | return $this->errorResponse($exception); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Count total items. |
||
90 | * |
||
91 | * @return integer |
||
92 | */ |
||
93 | public function totalCount() |
||
97 | |||
98 | /** |
||
99 | * Counts current query. |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | public function count() |
||
112 | |||
113 | /** |
||
114 | * Prepare count query builder. |
||
115 | * |
||
116 | * @return \Illuminate\Database\Query\Builder |
||
117 | */ |
||
118 | protected function prepareCountQuery() |
||
129 | |||
130 | /** |
||
131 | * Check if builder query uses complex sql. |
||
132 | * |
||
133 | * @param \Illuminate\Database\Query\Builder $builder |
||
134 | * @return bool |
||
135 | */ |
||
136 | protected function isComplexQuery($builder) |
||
140 | |||
141 | /** |
||
142 | * Wrap column with DB grammar. |
||
143 | * |
||
144 | * @param string $column |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function wrap($column) |
||
151 | |||
152 | /** |
||
153 | * Perform sorting of columns. |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public function ordering() |
||
185 | |||
186 | /** |
||
187 | * Get the base query builder instance. |
||
188 | * |
||
189 | * @param mixed $instance |
||
190 | * @return \Illuminate\Database\Query\Builder |
||
191 | */ |
||
192 | protected function getBaseQueryBuilder($instance = null) |
||
204 | |||
205 | /** |
||
206 | * Check if column has custom sort handler. |
||
207 | * |
||
208 | * @param string $column |
||
209 | * @return bool |
||
210 | */ |
||
211 | protected function hasCustomOrder($column) |
||
215 | |||
216 | /** |
||
217 | * Apply orderColumn custom query. |
||
218 | * |
||
219 | * @param string $column |
||
220 | * @param array $orderable |
||
221 | */ |
||
222 | protected function applyOrderColumn($column, $orderable): void |
||
229 | |||
230 | /** |
||
231 | * Resolve the proper column name be used. |
||
232 | * |
||
233 | * @param string $column |
||
234 | * @return string |
||
235 | */ |
||
236 | protected function resolveOrderByColumn($column) |
||
240 | |||
241 | /** |
||
242 | * Get NULLS LAST SQL. |
||
243 | * |
||
244 | * @param string $column |
||
245 | * @param string $direction |
||
246 | * @return string |
||
247 | */ |
||
248 | protected function getNullsLastSql($column, $direction) |
||
254 | |||
255 | /** |
||
256 | * Perform column search. |
||
257 | * |
||
258 | * @return void |
||
259 | */ |
||
260 | public function columnSearch() |
||
292 | |||
293 | /** |
||
294 | * Check if column has custom filter handler. |
||
295 | * |
||
296 | * @param string $columnName |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function hasCustomFilter($columnName) |
||
303 | |||
304 | /** |
||
305 | * Get column keyword to use for search. |
||
306 | * |
||
307 | * @param int $i |
||
308 | * @param bool $raw |
||
309 | * @return string |
||
310 | */ |
||
311 | protected function getColumnSearchKeyword($i, $raw = false) |
||
320 | |||
321 | /** |
||
322 | * Apply filterColumn api search. |
||
323 | * |
||
324 | * @param mixed $query |
||
325 | * @param string $columnName |
||
326 | * @param string $keyword |
||
327 | * @param string $boolean |
||
328 | */ |
||
329 | protected function applyFilterColumn($query, $columnName, $keyword, $boolean = 'and') |
||
336 | |||
337 | /** |
||
338 | * Get eager loads keys if eloquent. |
||
339 | * |
||
340 | * @return array |
||
341 | */ |
||
342 | protected function getEagerLoads() |
||
350 | |||
351 | /** |
||
352 | * Compile queries for column search. |
||
353 | * |
||
354 | * @param int $i |
||
355 | * @param string $column |
||
356 | * @param string $keyword |
||
357 | */ |
||
358 | protected function compileColumnSearch($i, $column, $keyword) |
||
367 | |||
368 | /** |
||
369 | * Compile regex query column search. |
||
370 | * |
||
371 | * @param mixed $column |
||
372 | * @param string $keyword |
||
373 | */ |
||
374 | protected function regexColumnSearch($column, $keyword) |
||
394 | |||
395 | /** |
||
396 | * Compile query builder where clause depending on configurations. |
||
397 | * |
||
398 | * @param mixed $query |
||
399 | * @param string $column |
||
400 | * @param string $keyword |
||
401 | * @param string $relation |
||
402 | */ |
||
403 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
415 | |||
416 | /** |
||
417 | * Patch for fix about ambiguous field. |
||
418 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
419 | * |
||
420 | * @param mixed $query |
||
421 | * @param string $column |
||
422 | * @return string |
||
423 | */ |
||
424 | protected function addTablePrefix($query, $column) |
||
435 | |||
436 | /** |
||
437 | * Wrap a column and cast based on database driver. |
||
438 | * |
||
439 | * @param string $column |
||
440 | * @return string |
||
441 | */ |
||
442 | protected function castColumn($column) |
||
453 | |||
454 | /** |
||
455 | * Prepare search keyword based on configurations. |
||
456 | * |
||
457 | * @param string $keyword |
||
458 | * @return string |
||
459 | */ |
||
460 | protected function prepareKeyword($keyword) |
||
476 | |||
477 | /** |
||
478 | * Perform pagination. |
||
479 | * |
||
480 | * @return void |
||
481 | */ |
||
482 | public function paging() |
||
487 | |||
488 | /** |
||
489 | * Get paginated results. |
||
490 | * |
||
491 | * @return \Illuminate\Support\Collection |
||
492 | */ |
||
493 | public function results() |
||
497 | |||
498 | /** |
||
499 | * Add column in collection. |
||
500 | * |
||
501 | * @param string $name |
||
502 | * @param string|callable $content |
||
503 | * @param bool|int $order |
||
504 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
505 | */ |
||
506 | public function addColumn($name, $content, $order = false) |
||
512 | |||
513 | /** |
||
514 | * Get query builder instance. |
||
515 | * |
||
516 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
517 | */ |
||
518 | public function getQuery() |
||
522 | |||
523 | /** |
||
524 | * Perform global search for the given keyword. |
||
525 | * |
||
526 | * @param string $keyword |
||
527 | */ |
||
528 | protected function globalSearch($keyword) |
||
551 | |||
552 | /** |
||
553 | * Append debug parameters on output. |
||
554 | * |
||
555 | * @param array $output |
||
556 | * @return array |
||
557 | */ |
||
558 | protected function showDebugger(array $output) |
||
565 | } |
||
566 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: