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 bool |
||
| 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 int |
||
| 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() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Check if builder query uses complex sql. |
||
| 159 | * |
||
| 160 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | protected function isComplexQuery($builder) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Wrap column with DB grammar. |
||
| 170 | * |
||
| 171 | * @param string $column |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | protected function wrap($column) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get paginated results. |
||
| 181 | * |
||
| 182 | * @return \Illuminate\Support\Collection |
||
| 183 | */ |
||
| 184 | public function results() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get filtered, ordered and paginated query. |
||
| 191 | * |
||
| 192 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 193 | */ |
||
| 194 | public function getFilteredQuery() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get query builder instance. |
||
| 203 | * |
||
| 204 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 205 | */ |
||
| 206 | public function getQuery() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Perform column search. |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public function columnSearch() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Check if column has custom filter handler. |
||
| 242 | * |
||
| 243 | * @param string $columnName |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function hasFilterColumn($columnName) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get column keyword to use for search. |
||
| 253 | * |
||
| 254 | * @param int $i |
||
| 255 | * @param bool $raw |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Apply filterColumn api search. |
||
| 270 | * |
||
| 271 | * @param mixed $query |
||
| 272 | * @param string $columnName |
||
| 273 | * @param string $keyword |
||
| 274 | * @param string $boolean |
||
| 275 | */ |
||
| 276 | protected function applyFilterColumn($query, $columnName, $keyword, $boolean = 'and') |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get the base query builder instance. |
||
| 294 | * |
||
| 295 | * @param mixed $instance |
||
| 296 | * @return \Illuminate\Database\Query\Builder |
||
| 297 | */ |
||
| 298 | protected function getBaseQueryBuilder($instance = null) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Resolve the proper column name be used. |
||
| 313 | * |
||
| 314 | * @param string $column |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | protected function resolveRelationColumn($column) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Compile queries for column search. |
||
| 324 | * |
||
| 325 | * @param int $i |
||
| 326 | * @param string $column |
||
| 327 | * @param string $keyword |
||
| 328 | */ |
||
| 329 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Compile regex query column search. |
||
| 341 | * |
||
| 342 | * @param mixed $column |
||
| 343 | * @param string $keyword |
||
| 344 | */ |
||
| 345 | protected function regexColumnSearch($column, $keyword) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Wrap a column and cast based on database driver. |
||
| 371 | * |
||
| 372 | * @param string $column |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | protected function castColumn($column) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Compile query builder where clause depending on configurations. |
||
| 389 | * |
||
| 390 | * @param mixed $query |
||
| 391 | * @param string $column |
||
| 392 | * @param string $keyword |
||
| 393 | * @param string $boolean |
||
| 394 | */ |
||
| 395 | protected function compileQuerySearch($query, $column, $keyword, $boolean = 'or') |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Patch for fix about ambiguous field. |
||
| 410 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 411 | * |
||
| 412 | * @param mixed $query |
||
| 413 | * @param string $column |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | protected function addTablePrefix($query, $column) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Prepare search keyword based on configurations. |
||
| 430 | * |
||
| 431 | * @param string $keyword |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | protected function prepareKeyword($keyword) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Add custom filter handler for the give column. |
||
| 453 | * |
||
| 454 | * @param string $column |
||
| 455 | * @param callable $callback |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | public function filterColumn($column, callable $callback) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Order each given columns versus the given custom sql. |
||
| 467 | * |
||
| 468 | * @param array $columns |
||
| 469 | * @param string $sql |
||
| 470 | * @param array $bindings |
||
| 471 | * @return $this |
||
| 472 | */ |
||
| 473 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Override default column ordering. |
||
| 484 | * |
||
| 485 | * @param string $column |
||
| 486 | * @param string $sql |
||
| 487 | * @param array $bindings |
||
| 488 | * @return $this |
||
| 489 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 490 | */ |
||
| 491 | public function orderColumn($column, $sql, $bindings = []) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Set datatables to do ordering with NULLS LAST option. |
||
| 500 | * |
||
| 501 | * @return $this |
||
| 502 | */ |
||
| 503 | public function orderByNullsLast() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Paginate dataTable using limit without offset |
||
| 512 | * with additional where clause via callback. |
||
| 513 | * |
||
| 514 | * @param callable $callback |
||
| 515 | * @return $this |
||
| 516 | */ |
||
| 517 | public function limit(callable $callback) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Perform pagination. |
||
| 526 | * |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | public function paging() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Add column in collection. |
||
| 542 | * |
||
| 543 | * @param string $name |
||
| 544 | * @param string|callable $content |
||
| 545 | * @param bool|int $order |
||
| 546 | * @return $this |
||
| 547 | */ |
||
| 548 | public function addColumn($name, $content, $order = false) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Resolve callback parameter instance. |
||
| 557 | * |
||
| 558 | * @return \Illuminate\Database\Query\Builder |
||
| 559 | */ |
||
| 560 | protected function resolveCallbackParameter() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Perform default query orderBy clause. |
||
| 567 | */ |
||
| 568 | protected function defaultOrdering() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Check if column has custom sort handler. |
||
| 595 | * |
||
| 596 | * @param string $column |
||
| 597 | * @return bool |
||
| 598 | */ |
||
| 599 | protected function hasOrderColumn($column) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Apply orderColumn custom query. |
||
| 606 | * |
||
| 607 | * @param string $column |
||
| 608 | * @param array $orderable |
||
| 609 | */ |
||
| 610 | protected function applyOrderColumn($column, $orderable) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Get NULLS LAST SQL. |
||
| 620 | * |
||
| 621 | * @param string $column |
||
| 622 | * @param string $direction |
||
| 623 | * @return string |
||
| 624 | */ |
||
| 625 | protected function getNullsLastSql($column, $direction) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Perform global search for the given keyword. |
||
| 634 | * |
||
| 635 | * @param string $keyword |
||
| 636 | */ |
||
| 637 | protected function globalSearch($keyword) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Append debug parameters on output. |
||
| 661 | * |
||
| 662 | * @param array $output |
||
| 663 | * @return array |
||
| 664 | */ |
||
| 665 | protected function showDebugger(array $output) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Attach custom with meta on response. |
||
| 675 | * |
||
| 676 | * @param array $data |
||
| 677 | * @return array |
||
| 678 | */ |
||
| 679 | protected function attachAppends(array $data) |
||
| 692 | } |
||
| 693 |