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 | * @inheritdoc |
||
| 99 | */ |
||
| 100 | public function filtering() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get eager loads keys if eloquent. |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | private function getEagerLoads() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Perform filter column on selected field. |
||
| 154 | * |
||
| 155 | * @param mixed $query |
||
| 156 | * @param string $method |
||
| 157 | * @param mixed $parameters |
||
| 158 | * @param string $column |
||
| 159 | * @param string $keyword |
||
| 160 | */ |
||
| 161 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Build Query Builder Parameters. |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | protected function parameterize() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Add relation query on global search. |
||
| 199 | * |
||
| 200 | * @param mixed $query |
||
| 201 | * @param string $column |
||
| 202 | * @param string $keyword |
||
| 203 | */ |
||
| 204 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Add a query on global search. |
||
| 217 | * |
||
| 218 | * @param mixed $query |
||
| 219 | * @param string $column |
||
| 220 | * @param string $keyword |
||
| 221 | */ |
||
| 222 | protected function compileGlobalSearch($query, $column, $keyword) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Wrap a column and cast in pgsql. |
||
| 236 | * |
||
| 237 | * @param string $column |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function castColumn($column) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @inheritdoc |
||
| 252 | */ |
||
| 253 | public function columnSearch() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get proper keyword to use for search. |
||
| 290 | * |
||
| 291 | * @param int $i |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | private function getSearchKeyword($i) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @inheritdoc |
||
| 305 | */ |
||
| 306 | public function ordering() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get order by column name. |
||
| 339 | * |
||
| 340 | * @param array $orderable |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | private function setupOrderColumn(array $orderable) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @inheritdoc |
||
| 358 | */ |
||
| 359 | public function paging() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get results |
||
| 367 | * |
||
| 368 | * @return array|static[] |
||
| 369 | */ |
||
| 370 | public function results() |
||
| 374 | } |
||
| 375 |