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) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Wrap a column and cast in pgsql. |
||
| 275 | * |
||
| 276 | * @param string $column |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function castColumn($column) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Perform column search. |
||
| 291 | * |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | public function columnSearch() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get proper keyword to use for search. |
||
| 342 | * |
||
| 343 | * @param int $i |
||
| 344 | * @param bool $raw |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | private function getSearchKeyword($i, $raw = false) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Compile queries for column search. |
||
| 359 | * |
||
| 360 | * @param int $i |
||
| 361 | * @param mixed $column |
||
| 362 | * @param string $keyword |
||
| 363 | * @param bool $caseSensitive |
||
| 364 | */ |
||
| 365 | protected function compileColumnSearch($i, $column, $keyword, $caseSensitive = true) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Compile regex query column search. |
||
| 380 | * |
||
| 381 | * @param mixed $column |
||
| 382 | * @param string $keyword |
||
| 383 | * @param bool $caseSensitive |
||
| 384 | */ |
||
| 385 | protected function regexColumnSearch($column, $keyword, $caseSensitive = true) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Perform sorting of columns. |
||
| 398 | * |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | public function ordering() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Join eager loaded relation and get the related column name. |
||
| 445 | * |
||
| 446 | * @param string $relation |
||
| 447 | * @param string $relationColumn |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Perform pagination |
||
| 472 | * |
||
| 473 | * @return void |
||
| 474 | */ |
||
| 475 | public function paging() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get results |
||
| 483 | * |
||
| 484 | * @return array|static[] |
||
| 485 | */ |
||
| 486 | public function results() |
||
| 490 | } |
||
| 491 |