Complex classes like QueryDataTable 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 QueryDataTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class QueryDataTable extends DataTableAbstract |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Builder object. |
||
| 15 | * |
||
| 16 | * @var \Illuminate\Database\Query\Builder |
||
| 17 | */ |
||
| 18 | protected $query; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Database connection used. |
||
| 22 | * |
||
| 23 | * @var \Illuminate\Database\Connection |
||
| 24 | */ |
||
| 25 | protected $connection; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Flag for ordering NULLS LAST option. |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $nullsLast = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Flag to check if query preparation was already done. |
||
| 36 | * |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $prepared = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Query callback for custom pagination using limit without offset. |
||
| 43 | * |
||
| 44 | * @var callable |
||
| 45 | */ |
||
| 46 | protected $limitCallback; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Can the DataTable engine be created with these parameters. |
||
| 50 | * |
||
| 51 | * @param mixed $source |
||
| 52 | * @return boolean |
||
| 53 | */ |
||
| 54 | public static function canCreate($source) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 61 | */ |
||
| 62 | public function __construct(Builder $builder) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Organizes works. |
||
| 76 | * |
||
| 77 | * @param bool $mDataSupport |
||
| 78 | * @return \Illuminate\Http\JsonResponse |
||
| 79 | * @throws \Exception |
||
| 80 | */ |
||
| 81 | public function make($mDataSupport = true) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Prepare query by executing count, filter, order and paginate. |
||
| 98 | */ |
||
| 99 | protected function prepareQuery() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Count total items. |
||
| 116 | * |
||
| 117 | * @return integer |
||
| 118 | */ |
||
| 119 | public function totalCount() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Counts current query. |
||
| 126 | * |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | public function count() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Prepare count query builder. |
||
| 141 | * |
||
| 142 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 143 | */ |
||
| 144 | protected function prepareCountQuery() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Check if builder query uses complex sql. |
||
| 158 | * |
||
| 159 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | protected function isComplexQuery($builder) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Wrap column with DB grammar. |
||
| 169 | * |
||
| 170 | * @param string $column |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | protected function wrap($column) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get paginated results. |
||
| 180 | * |
||
| 181 | * @return \Illuminate\Support\Collection |
||
| 182 | */ |
||
| 183 | public function results() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get filtered, ordered and paginated query. |
||
| 190 | * |
||
| 191 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 192 | */ |
||
| 193 | public function getFilteredQuery() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get query builder instance. |
||
| 202 | * |
||
| 203 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 204 | */ |
||
| 205 | public function getQuery() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Perform column search. |
||
| 212 | * |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | public function columnSearch() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Check if column has custom filter handler. |
||
| 241 | * |
||
| 242 | * @param string $columnName |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function hasFilterColumn($columnName) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get column keyword to use for search. |
||
| 252 | * |
||
| 253 | * @param int $i |
||
| 254 | * @param bool $raw |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Apply filterColumn api search. |
||
| 269 | * |
||
| 270 | * @param mixed $query |
||
| 271 | * @param string $columnName |
||
| 272 | * @param string $keyword |
||
| 273 | * @param string $boolean |
||
| 274 | */ |
||
| 275 | protected function applyFilterColumn($query, $columnName, $keyword, $boolean = 'and') |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the base query builder instance. |
||
| 293 | * |
||
| 294 | * @param mixed $instance |
||
| 295 | * @return \Illuminate\Database\Query\Builder |
||
| 296 | */ |
||
| 297 | protected function getBaseQueryBuilder($instance = null) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Resolve the proper column name be used. |
||
| 312 | * |
||
| 313 | * @param string $column |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | protected function resolveRelationColumn($column) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Compile queries for column search. |
||
| 323 | * |
||
| 324 | * @param int $i |
||
| 325 | * @param string $column |
||
| 326 | * @param string $keyword |
||
| 327 | */ |
||
| 328 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Compile regex query column search. |
||
| 340 | * |
||
| 341 | * @param mixed $column |
||
| 342 | * @param string $keyword |
||
| 343 | */ |
||
| 344 | protected function regexColumnSearch($column, $keyword) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Wrap a column and cast based on database driver. |
||
| 370 | * |
||
| 371 | * @param string $column |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | protected function castColumn($column) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Compile query builder where clause depending on configurations. |
||
| 388 | * |
||
| 389 | * @param mixed $query |
||
| 390 | * @param string $column |
||
| 391 | * @param string $keyword |
||
| 392 | * @param string $boolean |
||
| 393 | */ |
||
| 394 | protected function compileQuerySearch($query, $column, $keyword, $boolean = 'or') |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Patch for fix about ambiguous field. |
||
| 409 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 410 | * |
||
| 411 | * @param mixed $query |
||
| 412 | * @param string $column |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | protected function addTablePrefix($query, $column) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Prepare search keyword based on configurations. |
||
| 429 | * |
||
| 430 | * @param string $keyword |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | protected function prepareKeyword($keyword) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Add custom filter handler for the give column. |
||
| 452 | * |
||
| 453 | * @param string $column |
||
| 454 | * @param callable $callback |
||
| 455 | * @return $this |
||
| 456 | */ |
||
| 457 | public function filterColumn($column, callable $callback) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Order each given columns versus the given custom sql. |
||
| 466 | * |
||
| 467 | * @param array $columns |
||
| 468 | * @param string $sql |
||
| 469 | * @param array $bindings |
||
| 470 | * @return $this |
||
| 471 | */ |
||
| 472 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Override default column ordering. |
||
| 483 | * |
||
| 484 | * @param string $column |
||
| 485 | * @param string $sql |
||
| 486 | * @param array $bindings |
||
| 487 | * @return $this |
||
| 488 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 489 | */ |
||
| 490 | public function orderColumn($column, $sql, $bindings = []) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Set datatables to do ordering with NULLS LAST option. |
||
| 499 | * |
||
| 500 | * @return $this |
||
| 501 | */ |
||
| 502 | public function orderByNullsLast() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Paginate dataTable using limit without offset |
||
| 511 | * with additional where clause via callback. |
||
| 512 | * |
||
| 513 | * @param callable $callback |
||
| 514 | * @return $this |
||
| 515 | */ |
||
| 516 | public function limit(callable $callback) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Perform pagination. |
||
| 524 | * |
||
| 525 | * @return void |
||
| 526 | */ |
||
| 527 | public function paging() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Add column in collection. |
||
| 540 | * |
||
| 541 | * @param string $name |
||
| 542 | * @param string|callable $content |
||
| 543 | * @param bool|int $order |
||
| 544 | * @return $this |
||
| 545 | */ |
||
| 546 | public function addColumn($name, $content, $order = false) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Resolve callback parameter instance. |
||
| 555 | * |
||
| 556 | * @return \Illuminate\Database\Query\Builder |
||
| 557 | */ |
||
| 558 | protected function resolveCallbackParameter() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Perform default query orderBy clause. |
||
| 565 | */ |
||
| 566 | protected function defaultOrdering() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Check if column has custom sort handler. |
||
| 593 | * |
||
| 594 | * @param string $column |
||
| 595 | * @return bool |
||
| 596 | */ |
||
| 597 | protected function hasOrderColumn($column) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Apply orderColumn custom query. |
||
| 604 | * |
||
| 605 | * @param string $column |
||
| 606 | * @param array $orderable |
||
| 607 | */ |
||
| 608 | protected function applyOrderColumn($column, $orderable) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get NULLS LAST SQL. |
||
| 618 | * |
||
| 619 | * @param string $column |
||
| 620 | * @param string $direction |
||
| 621 | * @return string |
||
| 622 | */ |
||
| 623 | protected function getNullsLastSql($column, $direction) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Perform global search for the given keyword. |
||
| 632 | * |
||
| 633 | * @param string $keyword |
||
| 634 | */ |
||
| 635 | protected function globalSearch($keyword) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Append debug parameters on output. |
||
| 659 | * |
||
| 660 | * @param array $output |
||
| 661 | * @return array |
||
| 662 | */ |
||
| 663 | protected function showDebugger(array $output) |
||
| 670 | } |
||
| 671 |