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 |
||
| 19 | class QueryBuilderEngine extends BaseEngine |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 23 | * @param \Yajra\Datatables\Request $request |
||
| 24 | */ |
||
| 25 | public function __construct(Builder $builder, Request $request) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Initialize attributes. |
||
| 33 | * |
||
| 34 | * @param \Yajra\Datatables\Request $request |
||
| 35 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 36 | * @param string $type |
||
| 37 | */ |
||
| 38 | protected function init($request, $builder, $type = 'builder') |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | */ |
||
| 54 | public function filter(Closure $callback) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @inheritdoc |
||
| 63 | */ |
||
| 64 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @inheritdoc |
||
| 71 | */ |
||
| 72 | public function totalCount() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Counts current query. |
||
| 79 | * |
||
| 80 | * @return int |
||
| 81 | */ |
||
| 82 | public function count() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Perform global search. |
||
| 98 | * |
||
| 99 | * @return void |
||
| 100 | */ |
||
| 101 | public function filtering() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Perform filter column on selected field. |
||
| 161 | * |
||
| 162 | * @param mixed $query |
||
| 163 | * @param string|Closure $method |
||
| 164 | * @param mixed $parameters |
||
| 165 | * @param string $column |
||
| 166 | * @param string $keyword |
||
| 167 | */ |
||
| 168 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Build Query Builder Parameters. |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | protected function parameterize() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get eager loads keys if eloquent. |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | protected function getEagerLoads() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Add relation query on global search. |
||
| 220 | * |
||
| 221 | * @param mixed $query |
||
| 222 | * @param string $relation |
||
| 223 | * @param string $column |
||
| 224 | * @param string $keyword |
||
| 225 | */ |
||
| 226 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Add a query on global search. |
||
| 240 | * |
||
| 241 | * @param mixed $query |
||
| 242 | * @param string $column |
||
| 243 | * @param string $keyword |
||
| 244 | */ |
||
| 245 | protected function compileGlobalSearch($query, $column, $keyword) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Wrap a column and cast in pgsql. |
||
| 259 | * |
||
| 260 | * @param string $column |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function castColumn($column) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Perform column search. |
||
| 275 | * |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | public function columnSearch() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get proper keyword to use for search. |
||
| 326 | * |
||
| 327 | * @param int $i |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | private function getSearchKeyword($i, $raw = false) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Compile queries for column search. |
||
| 342 | * |
||
| 343 | * @param int $i |
||
| 344 | * @param mixed $column |
||
| 345 | * @param string $keyword |
||
| 346 | * @param bool $caseSensitive |
||
| 347 | */ |
||
| 348 | protected function compileColumnSearch($i, $column, $keyword, $caseSensitive = true) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Compile regex query column search. |
||
| 361 | * |
||
| 362 | * @param mixed $column |
||
| 363 | * @param string $keyword |
||
| 364 | * @param bool $caseSensitive |
||
| 365 | */ |
||
| 366 | protected function regexColumnSearch($column, $keyword, $caseSensitive = true) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Check if the current sql language is based on oracle syntax. |
||
| 379 | * |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | protected function isOracleSql() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Perform sorting of columns. |
||
| 389 | * |
||
| 390 | * @return void |
||
| 391 | */ |
||
| 392 | public function ordering() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Join eager loaded relation and get the related column name. |
||
| 431 | * |
||
| 432 | * @param string $relation |
||
| 433 | * @param string $relationColumn |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Perform pagination |
||
| 458 | * |
||
| 459 | * @return void |
||
| 460 | */ |
||
| 461 | public function paging() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get results |
||
| 469 | * |
||
| 470 | * @return array|static[] |
||
| 471 | */ |
||
| 472 | public function results() |
||
| 476 | } |
||
| 477 |