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 | * Builder object. |
||
| 21 | * |
||
| 22 | * @var \Illuminate\Database\Query\Builder |
||
| 23 | */ |
||
| 24 | protected $query; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Database connection used. |
||
| 28 | * |
||
| 29 | * @var \Illuminate\Database\Connection |
||
| 30 | */ |
||
| 31 | protected $connection; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Flag for ordering NULLS LAST option. |
||
| 35 | * |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $nullsLast = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Flag to check if query preparation was already done. |
||
| 42 | * |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $prepared = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 49 | */ |
||
| 50 | public function __construct(Builder $builder) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Organizes works. |
||
| 64 | * |
||
| 65 | * @param bool $mDataSupport |
||
| 66 | * @return \Illuminate\Http\JsonResponse |
||
| 67 | * @throws \Exception |
||
| 68 | */ |
||
| 69 | public function make($mDataSupport = true) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Prepare query by executing count, filter, order and paginate. |
||
| 86 | */ |
||
| 87 | protected function prepareQuery() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Count total items. |
||
| 104 | * |
||
| 105 | * @return integer |
||
| 106 | */ |
||
| 107 | public function totalCount() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Counts current query. |
||
| 114 | * |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | public function count() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Prepare count query builder. |
||
| 129 | * |
||
| 130 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 131 | */ |
||
| 132 | protected function prepareCountQuery() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Check if builder query uses complex sql. |
||
| 146 | * |
||
| 147 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | protected function isComplexQuery($builder) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Wrap column with DB grammar. |
||
| 157 | * |
||
| 158 | * @param string $column |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | protected function wrap($column) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get paginated results. |
||
| 168 | * |
||
| 169 | * @return \Illuminate\Support\Collection |
||
| 170 | */ |
||
| 171 | public function results() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get filtered, ordered and paginated query. |
||
| 178 | * |
||
| 179 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 180 | */ |
||
| 181 | public function getFilteredQuery() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get query builder instance. |
||
| 190 | * |
||
| 191 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 192 | */ |
||
| 193 | public function getQuery() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Perform column search. |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function columnSearch() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Check if column has custom filter handler. |
||
| 230 | * |
||
| 231 | * @param string $columnName |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | public function hasFilterColumn($columnName) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get column keyword to use for search. |
||
| 241 | * |
||
| 242 | * @param int $i |
||
| 243 | * @param bool $raw |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Apply filterColumn api search. |
||
| 258 | * |
||
| 259 | * @param Builder $query |
||
| 260 | * @param string $columnName |
||
| 261 | * @param string $keyword |
||
| 262 | * @param string $boolean |
||
| 263 | */ |
||
| 264 | protected function applyFilterColumn(Builder $query, $columnName, $keyword, $boolean = 'and') |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the base query builder instance. |
||
| 281 | * |
||
| 282 | * @param mixed $instance |
||
| 283 | * @return \Illuminate\Database\Query\Builder |
||
| 284 | */ |
||
| 285 | protected function getBaseQueryBuilder($instance = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Resolve the proper column name be used. |
||
| 300 | * |
||
| 301 | * @param string $column |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | protected function resolveRelationColumn($column) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Compile queries for column search. |
||
| 311 | * |
||
| 312 | * @param int $i |
||
| 313 | * @param string $column |
||
| 314 | * @param string $keyword |
||
| 315 | */ |
||
| 316 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Compile regex query column search. |
||
| 328 | * |
||
| 329 | * @param mixed $column |
||
| 330 | * @param string $keyword |
||
| 331 | */ |
||
| 332 | protected function regexColumnSearch($column, $keyword) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Compile query builder where clause depending on configurations. |
||
| 355 | * |
||
| 356 | * @param mixed $query |
||
| 357 | * @param string $column |
||
| 358 | * @param string $keyword |
||
| 359 | * @param string $relation |
||
| 360 | */ |
||
| 361 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Patch for fix about ambiguous field. |
||
| 376 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 377 | * |
||
| 378 | * @param mixed $query |
||
| 379 | * @param string $column |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | protected function addTablePrefix($query, $column) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Wrap a column and cast based on database driver. |
||
| 396 | * |
||
| 397 | * @param string $column |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | protected function castColumn($column) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Prepare search keyword based on configurations. |
||
| 414 | * |
||
| 415 | * @param string $keyword |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | protected function prepareKeyword($keyword) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Add custom filter handler for the give column. |
||
| 437 | * |
||
| 438 | * @param string $column |
||
| 439 | * @param callable $callback |
||
| 440 | * @return $this |
||
| 441 | */ |
||
| 442 | public function filterColumn($column, callable $callback) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Order each given columns versus the given custom sql. |
||
| 451 | * |
||
| 452 | * @param array $columns |
||
| 453 | * @param string $sql |
||
| 454 | * @param array $bindings |
||
| 455 | * @return $this |
||
| 456 | */ |
||
| 457 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Override default column ordering. |
||
| 468 | * |
||
| 469 | * @param string $column |
||
| 470 | * @param string $sql |
||
| 471 | * @param array $bindings |
||
| 472 | * @return $this |
||
| 473 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 474 | */ |
||
| 475 | public function orderColumn($column, $sql, $bindings = []) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Set datatables to do ordering with NULLS LAST option. |
||
| 484 | * |
||
| 485 | * @return $this |
||
| 486 | */ |
||
| 487 | public function orderByNullsLast() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Perform pagination. |
||
| 496 | * |
||
| 497 | * @return void |
||
| 498 | */ |
||
| 499 | public function paging() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Add column in collection. |
||
| 507 | * |
||
| 508 | * @param string $name |
||
| 509 | * @param string|callable $content |
||
| 510 | * @param bool|int $order |
||
| 511 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
| 512 | */ |
||
| 513 | public function addColumn($name, $content, $order = false) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Resolve callback parameter instance. |
||
| 522 | * |
||
| 523 | * @return \Illuminate\Database\Query\Builder |
||
| 524 | */ |
||
| 525 | protected function resolveCallbackParameter() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Perform default query orderBy clause. |
||
| 532 | */ |
||
| 533 | protected function defaultOrdering() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Check if column has custom sort handler. |
||
| 560 | * |
||
| 561 | * @param string $column |
||
| 562 | * @return bool |
||
| 563 | */ |
||
| 564 | protected function hasOrderColumn($column) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Apply orderColumn custom query. |
||
| 571 | * |
||
| 572 | * @param string $column |
||
| 573 | * @param array $orderable |
||
| 574 | */ |
||
| 575 | protected function applyOrderColumn($column, $orderable): void |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Get NULLS LAST SQL. |
||
| 585 | * |
||
| 586 | * @param string $column |
||
| 587 | * @param string $direction |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | protected function getNullsLastSql($column, $direction) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Perform global search for the given keyword. |
||
| 599 | * |
||
| 600 | * @param string $keyword |
||
| 601 | */ |
||
| 602 | protected function globalSearch($keyword) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Append debug parameters on output. |
||
| 628 | * |
||
| 629 | * @param array $output |
||
| 630 | * @return array |
||
| 631 | */ |
||
| 632 | protected function showDebugger(array $output) |
||
| 639 | } |
||
| 640 |