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 |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Builder object. |
||
| 24 | * |
||
| 25 | * @var \Illuminate\Database\Query\Builder |
||
| 26 | */ |
||
| 27 | protected $query; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Database connection used. |
||
| 31 | * |
||
| 32 | * @var \Illuminate\Database\Connection |
||
| 33 | */ |
||
| 34 | protected $connection; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 38 | */ |
||
| 39 | public function __construct(Builder $builder) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Set auto filter off and run your own filter. |
||
| 53 | * Overrides global search. |
||
| 54 | * |
||
| 55 | * @param callable $callback |
||
| 56 | * @param bool $globalSearch |
||
| 57 | * @return $this |
||
| 58 | */ |
||
| 59 | public function filter(callable $callback, $globalSearch = false) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Perform global search for the given keyword. |
||
| 68 | * |
||
| 69 | * @param string $keyword |
||
| 70 | */ |
||
| 71 | protected function globalSearch($keyword) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the base query builder instance. |
||
| 95 | * |
||
| 96 | * @param mixed $instance |
||
| 97 | * @return \Illuminate\Database\Query\Builder |
||
| 98 | */ |
||
| 99 | protected function getBaseQueryBuilder($instance = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Check if column has custom filter handler. |
||
| 114 | * |
||
| 115 | * @param string $columnName |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public function hasCustomFilter($columnName) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Apply filterColumn api search. |
||
| 125 | * |
||
| 126 | * @param mixed $query |
||
| 127 | * @param string $columnName |
||
| 128 | * @param string $keyword |
||
| 129 | * @param string $boolean |
||
| 130 | */ |
||
| 131 | protected function applyFilterColumn($query, $columnName, $keyword, $boolean = 'and') |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Compile query builder where clause depending on configurations. |
||
| 141 | * |
||
| 142 | * @param mixed $query |
||
| 143 | * @param string $column |
||
| 144 | * @param string $keyword |
||
| 145 | * @param string $relation |
||
| 146 | */ |
||
| 147 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Patch for fix about ambiguous field. |
||
| 162 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 163 | * |
||
| 164 | * @param mixed $query |
||
| 165 | * @param string $column |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | protected function addTablePrefix($query, $column) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Wrap column with DB grammar. |
||
| 182 | * |
||
| 183 | * @param string $column |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | protected function wrap($column) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Wrap a column and cast based on database driver. |
||
| 193 | * |
||
| 194 | * @param string $column |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | protected function castColumn($column) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Prepare search keyword based on configurations. |
||
| 211 | * |
||
| 212 | * @param string $keyword |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | protected function prepareKeyword($keyword) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Organizes works. |
||
| 234 | * |
||
| 235 | * @param bool $mDataSupport |
||
| 236 | * @return \Illuminate\Http\JsonResponse |
||
| 237 | * @throws \Exception |
||
| 238 | */ |
||
| 239 | public function make($mDataSupport = false) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Count total items. |
||
| 260 | * |
||
| 261 | * @return integer |
||
| 262 | */ |
||
| 263 | public function totalCount() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Counts current query. |
||
| 270 | * |
||
| 271 | * @return int |
||
| 272 | */ |
||
| 273 | public function count() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Prepare count query builder. |
||
| 285 | * |
||
| 286 | * @return \Illuminate\Database\Query\Builder |
||
| 287 | */ |
||
| 288 | protected function prepareCountQuery() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Check if builder query uses complex sql. |
||
| 302 | * |
||
| 303 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | protected function isComplexQuery($builder) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Perform sorting of columns. |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | public function ordering() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Check if column has custom sort handler. |
||
| 380 | * |
||
| 381 | * @param string $column |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | protected function hasCustomOrder($column) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get eager loads keys if eloquent. |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | protected function getEagerLoads() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Join eager loaded relation and get the related column name. |
||
| 405 | * |
||
| 406 | * @param string $relation |
||
| 407 | * @param string $relationColumn |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Perform join query. |
||
| 460 | * |
||
| 461 | * @param string $table |
||
| 462 | * @param string $foreign |
||
| 463 | * @param string $other |
||
| 464 | */ |
||
| 465 | protected function performJoin($table, $foreign, $other) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get NULLS LAST SQL. |
||
| 479 | * |
||
| 480 | * @param string $column |
||
| 481 | * @param string $direction |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | protected function getNullsLastSql($column, $direction) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Perform column search. |
||
| 493 | * |
||
| 494 | * @return void |
||
| 495 | */ |
||
| 496 | public function columnSearch() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Get column keyword to use for search. |
||
| 531 | * |
||
| 532 | * @param int $i |
||
| 533 | * @param bool $raw |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Compile queries for column search. |
||
| 548 | * |
||
| 549 | * @param int $i |
||
| 550 | * @param string $column |
||
| 551 | * @param string $keyword |
||
| 552 | */ |
||
| 553 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Compile regex query column search. |
||
| 565 | * |
||
| 566 | * @param mixed $column |
||
| 567 | * @param string $keyword |
||
| 568 | */ |
||
| 569 | protected function regexColumnSearch($column, $keyword) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Perform pagination. |
||
| 592 | * |
||
| 593 | * @return void |
||
| 594 | */ |
||
| 595 | public function paging() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Get paginated results. |
||
| 603 | * |
||
| 604 | * @return \Illuminate\Support\Collection |
||
| 605 | */ |
||
| 606 | public function results() |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Add column in collection. |
||
| 613 | * |
||
| 614 | * @param string $name |
||
| 615 | * @param string|callable $content |
||
| 616 | * @param bool|int $order |
||
| 617 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
| 618 | */ |
||
| 619 | public function addColumn($name, $content, $order = false) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Get query builder instance. |
||
| 628 | * |
||
| 629 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 630 | */ |
||
| 631 | public function getQuery() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Append debug parameters on output. |
||
| 638 | * |
||
| 639 | * @param array $output |
||
| 640 | * @return array |
||
| 641 | */ |
||
| 642 | protected function showDebugger(array $output) |
||
| 649 | } |
||
| 650 |