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 |
||
| 16 | class QueryBuilderEngine extends BaseEngine |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Builder object. |
||
| 20 | * |
||
| 21 | * @var \Illuminate\Database\Query\Builder |
||
| 22 | */ |
||
| 23 | protected $query; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Database connection used. |
||
| 27 | * |
||
| 28 | * @var \Illuminate\Database\Connection |
||
| 29 | */ |
||
| 30 | protected $connection; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 34 | */ |
||
| 35 | public function __construct(Builder $builder) |
||
| 36 | { |
||
| 37 | $this->query = $builder; |
||
| 38 | $this->request = resolve('datatables.request'); |
||
| 39 | $this->config = resolve('datatables.config'); |
||
| 40 | $this->columns = $builder->columns; |
||
| 41 | $this->connection = $builder->getConnection(); |
||
| 42 | if ($this->config->isDebugging()) { |
||
| 43 | $this->connection->enableQueryLog(); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Set auto filter off and run your own filter. |
||
| 49 | * Overrides global search. |
||
| 50 | * |
||
| 51 | * @param callable $callback |
||
| 52 | * @param bool $globalSearch |
||
| 53 | * @return $this |
||
| 54 | */ |
||
| 55 | public function filter(callable $callback, $globalSearch = false) |
||
| 56 | { |
||
| 57 | $this->overrideGlobalSearch($callback, $this->query, $globalSearch); |
||
| 58 | |||
| 59 | return $this; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Organizes works. |
||
| 64 | * |
||
| 65 | * @param bool $mDataSupport |
||
| 66 | * @return \Illuminate\Http\JsonResponse |
||
| 67 | * @throws \Exception |
||
| 68 | */ |
||
| 69 | public function make($mDataSupport = false) |
||
| 70 | { |
||
| 71 | try { |
||
| 72 | $this->totalRecords = $this->totalCount(); |
||
| 73 | |||
| 74 | if ($this->totalRecords) { |
||
| 75 | $this->filterRecords(); |
||
| 76 | $this->ordering(); |
||
| 77 | $this->paginate(); |
||
| 78 | } |
||
| 79 | |||
| 80 | $data = $this->transform($this->getProcessedData($mDataSupport)); |
||
| 81 | |||
| 82 | return $this->render($data); |
||
| 83 | } catch (\Exception $exception) { |
||
| 84 | return $this->errorResponse($exception); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Count total items. |
||
| 90 | * |
||
| 91 | * @return integer |
||
| 92 | */ |
||
| 93 | public function totalCount() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Counts current query. |
||
| 100 | * |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | public function count() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Prepare count query builder. |
||
| 115 | * |
||
| 116 | * @return \Illuminate\Database\Query\Builder |
||
| 117 | */ |
||
| 118 | protected function prepareCountQuery() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Check if builder query uses complex sql. |
||
| 132 | * |
||
| 133 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | protected function isComplexQuery($builder) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Wrap column with DB grammar. |
||
| 143 | * |
||
| 144 | * @param string $column |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | protected function wrap($column) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Perform sorting of columns. |
||
| 154 | * |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | public function ordering() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Check if column has custom sort handler. |
||
| 190 | * |
||
| 191 | * @param string $column |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | protected function hasOrderColumn($column) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Resolve the proper column name be used. |
||
| 201 | * |
||
| 202 | * @param string $column |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | protected function resolveRelationColumn($column) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Apply orderColumn custom query. |
||
| 212 | * |
||
| 213 | * @param string $column |
||
| 214 | * @param array $orderable |
||
| 215 | */ |
||
| 216 | protected function applyOrderColumn($column, $orderable): void |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get NULLS LAST SQL. |
||
| 226 | * |
||
| 227 | * @param string $column |
||
| 228 | * @param string $direction |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | protected function getNullsLastSql($column, $direction) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Perform column search. |
||
| 240 | * |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | public function columnSearch() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Check if column has custom filter handler. |
||
| 270 | * |
||
| 271 | * @param string $columnName |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function hasFilterColumn($columnName) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get column keyword to use for search. |
||
| 281 | * |
||
| 282 | * @param int $i |
||
| 283 | * @param bool $raw |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Apply filterColumn api search. |
||
| 298 | * |
||
| 299 | * @param Builder $query |
||
| 300 | * @param string $columnName |
||
| 301 | * @param string $keyword |
||
| 302 | * @param string $boolean |
||
| 303 | */ |
||
| 304 | protected function applyFilterColumn(Builder $query, $columnName, $keyword, $boolean = 'and') |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the base query builder instance. |
||
| 314 | * |
||
| 315 | * @param mixed $instance |
||
| 316 | * @return \Illuminate\Database\Query\Builder |
||
| 317 | */ |
||
| 318 | protected function getBaseQueryBuilder($instance = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Compile queries for column search. |
||
| 333 | * |
||
| 334 | * @param int $i |
||
| 335 | * @param string $column |
||
| 336 | * @param string $keyword |
||
| 337 | */ |
||
| 338 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Compile regex query column search. |
||
| 350 | * |
||
| 351 | * @param mixed $column |
||
| 352 | * @param string $keyword |
||
| 353 | */ |
||
| 354 | protected function regexColumnSearch($column, $keyword) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Compile query builder where clause depending on configurations. |
||
| 377 | * |
||
| 378 | * @param mixed $query |
||
| 379 | * @param string $column |
||
| 380 | * @param string $keyword |
||
| 381 | * @param string $relation |
||
| 382 | */ |
||
| 383 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Patch for fix about ambiguous field. |
||
| 398 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 399 | * |
||
| 400 | * @param mixed $query |
||
| 401 | * @param string $column |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | protected function addTablePrefix($query, $column) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Wrap a column and cast based on database driver. |
||
| 418 | * |
||
| 419 | * @param string $column |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | protected function castColumn($column) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Prepare search keyword based on configurations. |
||
| 436 | * |
||
| 437 | * @param string $keyword |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | protected function prepareKeyword($keyword) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Perform pagination. |
||
| 459 | * |
||
| 460 | * @return void |
||
| 461 | */ |
||
| 462 | public function paging() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Get paginated results. |
||
| 470 | * |
||
| 471 | * @return \Illuminate\Support\Collection |
||
| 472 | */ |
||
| 473 | public function results() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Add column in collection. |
||
| 480 | * |
||
| 481 | * @param string $name |
||
| 482 | * @param string|callable $content |
||
| 483 | * @param bool|int $order |
||
| 484 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
| 485 | */ |
||
| 486 | public function addColumn($name, $content, $order = false) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Get query builder instance. |
||
| 495 | * |
||
| 496 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 497 | */ |
||
| 498 | public function getQuery() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Perform global search for the given keyword. |
||
| 505 | * |
||
| 506 | * @param string $keyword |
||
| 507 | */ |
||
| 508 | protected function globalSearch($keyword) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Append debug parameters on output. |
||
| 534 | * |
||
| 535 | * @param array $output |
||
| 536 | * @return array |
||
| 537 | */ |
||
| 538 | protected function showDebugger(array $output) |
||
| 545 | } |
||
| 546 |