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 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 43 | */ |
||
| 44 | public function __construct(Builder $builder) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Organizes works. |
||
| 58 | * |
||
| 59 | * @param bool $mDataSupport |
||
| 60 | * @return \Illuminate\Http\JsonResponse |
||
| 61 | * @throws \Exception |
||
| 62 | */ |
||
| 63 | public function make($mDataSupport = true) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Prepare query by executing count, filter, order and paginate. |
||
| 80 | */ |
||
| 81 | protected function prepareQuery() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Count total items. |
||
| 98 | * |
||
| 99 | * @return integer |
||
| 100 | */ |
||
| 101 | public function totalCount() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Counts current query. |
||
| 108 | * |
||
| 109 | * @return int |
||
| 110 | */ |
||
| 111 | public function count() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Prepare count query builder. |
||
| 123 | * |
||
| 124 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 125 | */ |
||
| 126 | protected function prepareCountQuery() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Check if builder query uses complex sql. |
||
| 140 | * |
||
| 141 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | protected function isComplexQuery($builder) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Wrap column with DB grammar. |
||
| 151 | * |
||
| 152 | * @param string $column |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | protected function wrap($column) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get paginated results. |
||
| 162 | * |
||
| 163 | * @return \Illuminate\Support\Collection |
||
| 164 | */ |
||
| 165 | public function results() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get filtered, ordered and paginated query. |
||
| 172 | * |
||
| 173 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 174 | */ |
||
| 175 | public function getFilteredQuery() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get query builder instance. |
||
| 184 | * |
||
| 185 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 186 | */ |
||
| 187 | public function getQuery() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Perform column search. |
||
| 194 | * |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | public function columnSearch() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Check if column has custom filter handler. |
||
| 224 | * |
||
| 225 | * @param string $columnName |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | public function hasFilterColumn($columnName) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get column keyword to use for search. |
||
| 235 | * |
||
| 236 | * @param int $i |
||
| 237 | * @param bool $raw |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Apply filterColumn api search. |
||
| 252 | * |
||
| 253 | * @param Builder $query |
||
| 254 | * @param string $columnName |
||
| 255 | * @param string $keyword |
||
| 256 | * @param string $boolean |
||
| 257 | */ |
||
| 258 | protected function applyFilterColumn(Builder $query, $columnName, $keyword, $boolean = 'and') |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the base query builder instance. |
||
| 275 | * |
||
| 276 | * @param mixed $instance |
||
| 277 | * @return \Illuminate\Database\Query\Builder |
||
| 278 | */ |
||
| 279 | protected function getBaseQueryBuilder($instance = null) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Resolve the proper column name be used. |
||
| 294 | * |
||
| 295 | * @param string $column |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | protected function resolveRelationColumn($column) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Compile queries for column search. |
||
| 305 | * |
||
| 306 | * @param int $i |
||
| 307 | * @param string $column |
||
| 308 | * @param string $keyword |
||
| 309 | */ |
||
| 310 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Compile regex query column search. |
||
| 322 | * |
||
| 323 | * @param mixed $column |
||
| 324 | * @param string $keyword |
||
| 325 | */ |
||
| 326 | protected function regexColumnSearch($column, $keyword) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Compile query builder where clause depending on configurations. |
||
| 349 | * |
||
| 350 | * @param mixed $query |
||
| 351 | * @param string $column |
||
| 352 | * @param string $keyword |
||
| 353 | * @param string $relation |
||
| 354 | */ |
||
| 355 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Patch for fix about ambiguous field. |
||
| 370 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 371 | * |
||
| 372 | * @param mixed $query |
||
| 373 | * @param string $column |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | protected function addTablePrefix($query, $column) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Wrap a column and cast based on database driver. |
||
| 390 | * |
||
| 391 | * @param string $column |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | protected function castColumn($column) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Prepare search keyword based on configurations. |
||
| 408 | * |
||
| 409 | * @param string $keyword |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | protected function prepareKeyword($keyword) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Add custom filter handler for the give column. |
||
| 431 | * |
||
| 432 | * @param string $column |
||
| 433 | * @param callable $callback |
||
| 434 | * @return $this |
||
| 435 | */ |
||
| 436 | public function filterColumn($column, callable $callback) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Order each given columns versus the given custom sql. |
||
| 445 | * |
||
| 446 | * @param array $columns |
||
| 447 | * @param string $sql |
||
| 448 | * @param array $bindings |
||
| 449 | * @return $this |
||
| 450 | */ |
||
| 451 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Override default column ordering. |
||
| 462 | * |
||
| 463 | * @param string $column |
||
| 464 | * @param string $sql |
||
| 465 | * @param array $bindings |
||
| 466 | * @return $this |
||
| 467 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 468 | */ |
||
| 469 | public function orderColumn($column, $sql, $bindings = []) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Set datatables to do ordering with NULLS LAST option. |
||
| 478 | * |
||
| 479 | * @return $this |
||
| 480 | */ |
||
| 481 | public function orderByNullsLast() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Perform pagination. |
||
| 490 | * |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | public function paging() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Add column in collection. |
||
| 501 | * |
||
| 502 | * @param string $name |
||
| 503 | * @param string|callable $content |
||
| 504 | * @param bool|int $order |
||
| 505 | * @return \Yajra\DataTables\DataTableAbstract|\Yajra\DataTables\Builders\QueryDataTable |
||
| 506 | */ |
||
| 507 | public function addColumn($name, $content, $order = false) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Resolve callback parameter instance. |
||
| 516 | * |
||
| 517 | * @return \Illuminate\Database\Query\Builder |
||
| 518 | */ |
||
| 519 | protected function resolveCallbackParameter() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Perform default query orderBy clause. |
||
| 526 | */ |
||
| 527 | protected function defaultOrdering() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Check if column has custom sort handler. |
||
| 554 | * |
||
| 555 | * @param string $column |
||
| 556 | * @return bool |
||
| 557 | */ |
||
| 558 | protected function hasOrderColumn($column) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Apply orderColumn custom query. |
||
| 565 | * |
||
| 566 | * @param string $column |
||
| 567 | * @param array $orderable |
||
| 568 | */ |
||
| 569 | protected function applyOrderColumn($column, $orderable): void |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Get NULLS LAST SQL. |
||
| 579 | * |
||
| 580 | * @param string $column |
||
| 581 | * @param string $direction |
||
| 582 | * @return string |
||
| 583 | */ |
||
| 584 | protected function getNullsLastSql($column, $direction) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Perform global search for the given keyword. |
||
| 593 | * |
||
| 594 | * @param string $keyword |
||
| 595 | */ |
||
| 596 | protected function globalSearch($keyword) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Append debug parameters on output. |
||
| 622 | * |
||
| 623 | * @param array $output |
||
| 624 | * @return array |
||
| 625 | */ |
||
| 626 | protected function showDebugger(array $output) |
||
| 633 | } |
||
| 634 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: