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) |
||
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') |
||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function filter(Closure $callback) |
||
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 | * @inheritdoc |
||
99 | */ |
||
100 | public function filtering() |
||
123 | |||
124 | /** |
||
125 | * Perform filter column on selected field. |
||
126 | * |
||
127 | * @param mixed $query |
||
128 | * @param string $method |
||
129 | * @param mixed $parameters |
||
130 | * @param string $column |
||
131 | * @param string $keyword |
||
132 | */ |
||
133 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
153 | |||
154 | /** |
||
155 | * Build Query Builder Parameters. |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | protected function parameterize() |
||
168 | |||
169 | /** |
||
170 | * Add a query on global search. |
||
171 | * |
||
172 | * @param mixed $query |
||
173 | * @param string $column |
||
174 | * @param string $keyword |
||
175 | */ |
||
176 | protected function compileGlobalSearch($query, $column, $keyword) |
||
187 | |||
188 | /** |
||
189 | * Wrap a column and cast in pgsql |
||
190 | * |
||
191 | * @param string $column |
||
192 | * @return string |
||
193 | */ |
||
194 | public function castColumn($column) |
||
203 | |||
204 | /** |
||
205 | * @inheritdoc |
||
206 | */ |
||
207 | public function columnSearch() |
||
241 | |||
242 | /** |
||
243 | * Get proper keyword to use for search. |
||
244 | * |
||
245 | * @param int $i |
||
246 | * @return string |
||
247 | */ |
||
248 | private function getSearchKeyword($i) |
||
256 | |||
257 | /** |
||
258 | * @inheritdoc |
||
259 | */ |
||
260 | public function ordering() |
||
284 | |||
285 | /** |
||
286 | * Get order by column name. |
||
287 | * |
||
288 | * @param array $orderable |
||
289 | * @return string |
||
290 | */ |
||
291 | private function setupOrderColumn(array $orderable) |
||
303 | |||
304 | /** |
||
305 | * @inheritdoc |
||
306 | */ |
||
307 | public function paging() |
||
312 | |||
313 | /** |
||
314 | * Get results |
||
315 | * |
||
316 | * @return array|static[] |
||
317 | */ |
||
318 | public function results() |
||
322 | } |
||
323 |