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 |
||
| 17 | class QueryBuilderEngine extends BaseEngine |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 21 | * @param \Yajra\Datatables\Request $request |
||
| 22 | */ |
||
| 23 | public function __construct(Builder $builder, Request $request) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Initialize attributes. |
||
| 31 | * |
||
| 32 | * @param \Yajra\Datatables\Request $request |
||
| 33 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 34 | * @param string $type |
||
| 35 | */ |
||
| 36 | protected function init($request, $builder, $type = 'builder') |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Set auto filter off and run your own filter. |
||
| 51 | * Overrides global search |
||
| 52 | * |
||
| 53 | * @param \Closure $callback |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | public function filter(Closure $callback) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Organizes works |
||
| 65 | * |
||
| 66 | * @param bool $mDataSupport |
||
| 67 | * @param bool $orderFirst |
||
| 68 | * @return \Illuminate\Http\JsonResponse |
||
| 69 | */ |
||
| 70 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Count total items. |
||
| 77 | * |
||
| 78 | * @return integer |
||
| 79 | */ |
||
| 80 | public function totalCount() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Counts current query. |
||
| 87 | * |
||
| 88 | * @return int |
||
| 89 | */ |
||
| 90 | public function count() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Perform global search. |
||
| 106 | * |
||
| 107 | * @return void |
||
| 108 | */ |
||
| 109 | public function filtering() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Perform filter column on selected field. |
||
| 173 | * |
||
| 174 | * @param mixed $query |
||
| 175 | * @param string|Closure $method |
||
| 176 | * @param mixed $parameters |
||
| 177 | * @param string $column |
||
| 178 | * @param string $keyword |
||
| 179 | */ |
||
| 180 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Build Query Builder Parameters. |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | protected function parameterize() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get eager loads keys if eloquent. |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | protected function getEagerLoads() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Add relation query on global search. |
||
| 232 | * |
||
| 233 | * @param mixed $query |
||
| 234 | * @param string $relation |
||
| 235 | * @param string $column |
||
| 236 | * @param string $keyword |
||
| 237 | */ |
||
| 238 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Add a query on global search. |
||
| 252 | * |
||
| 253 | * @param mixed $query |
||
| 254 | * @param string $column |
||
| 255 | * @param string $keyword |
||
| 256 | */ |
||
| 257 | protected function compileGlobalSearch($query, $column, $keyword) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Wrap a column and cast in pgsql. |
||
| 271 | * |
||
| 272 | * @param string $column |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function castColumn($column) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Perform column search. |
||
| 287 | * |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | public function columnSearch() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get proper keyword to use for search. |
||
| 338 | * |
||
| 339 | * @param int $i |
||
| 340 | * @param bool $raw |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | private function getSearchKeyword($i, $raw = false) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Compile queries for column search. |
||
| 355 | * |
||
| 356 | * @param int $i |
||
| 357 | * @param mixed $column |
||
| 358 | * @param string $keyword |
||
| 359 | * @param bool $caseSensitive |
||
| 360 | */ |
||
| 361 | protected function compileColumnSearch($i, $column, $keyword, $caseSensitive = true) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Compile regex query column search. |
||
| 374 | * |
||
| 375 | * @param mixed $column |
||
| 376 | * @param string $keyword |
||
| 377 | * @param bool $caseSensitive |
||
| 378 | */ |
||
| 379 | protected function regexColumnSearch($column, $keyword, $caseSensitive = true) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Check if the current sql language is based on oracle syntax. |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | protected function isOracleSql() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Perform sorting of columns. |
||
| 402 | * |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | public function ordering() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Join eager loaded relation and get the related column name. |
||
| 449 | * |
||
| 450 | * @param string $relation |
||
| 451 | * @param string $relationColumn |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Perform pagination |
||
| 476 | * |
||
| 477 | * @return void |
||
| 478 | */ |
||
| 479 | public function paging() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Get results |
||
| 487 | * |
||
| 488 | * @return array|static[] |
||
| 489 | */ |
||
| 490 | public function results() |
||
| 494 | } |
||
| 495 |