Complex classes like DataTableAbstract 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 DataTableAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class DataTableAbstract implements DataTable, Arrayable, Jsonable |
||
| 24 | { |
||
| 25 | use Macroable; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Datatables Request object. |
||
| 29 | * |
||
| 30 | * @var \Yajra\DataTables\Utilities\Request |
||
| 31 | */ |
||
| 32 | public $request; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \Illuminate\Contracts\Logging\Log |
||
| 36 | */ |
||
| 37 | protected $logger; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Array of result columns/fields. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $columns = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $columnDef = [ |
||
| 52 | 'index' => false, |
||
| 53 | 'append' => [], |
||
| 54 | 'edit' => [], |
||
| 55 | 'filter' => [], |
||
| 56 | 'order' => [], |
||
| 57 | ]; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Extra/Added columns. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $extraColumns = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Total records. |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $totalRecords = 0; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Total filtered records. |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | protected $filteredRecords = 0; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Auto-filter flag. |
||
| 82 | * |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | protected $autoFilter = true; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Callback to override global search. |
||
| 89 | * |
||
| 90 | * @var callable |
||
| 91 | */ |
||
| 92 | protected $filterCallback; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * DT row templates container. |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $templates = [ |
||
| 100 | 'DT_RowId' => '', |
||
| 101 | 'DT_RowClass' => '', |
||
| 102 | 'DT_RowData' => [], |
||
| 103 | 'DT_RowAttr' => [], |
||
| 104 | ]; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * [internal] Track if any filter was applied for at least one column. |
||
| 108 | * |
||
| 109 | * @var boolean |
||
| 110 | */ |
||
| 111 | protected $isFilterApplied = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Custom ordering callback. |
||
| 115 | * |
||
| 116 | * @var callable |
||
| 117 | */ |
||
| 118 | protected $orderCallback; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Skip paginate as needed. |
||
| 122 | * |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | protected $skipPaging = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Array of data to append on json response. |
||
| 129 | * |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | protected $appends = []; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var \Yajra\DataTables\Utilities\Config |
||
| 136 | */ |
||
| 137 | protected $config; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Add column in collection. |
||
| 141 | * |
||
| 142 | * @param string $name |
||
| 143 | * @param string|callable $content |
||
| 144 | * @param bool|int $order |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function addColumn($name, $content, $order = false) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Add DT row index column on response. |
||
| 158 | * |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | public function addIndexColumn() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Edit column's content. |
||
| 170 | * |
||
| 171 | * @param string $name |
||
| 172 | * @param string|callable $content |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function editColumn($name, $content) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Remove column from collection. |
||
| 184 | * |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | public function removeColumn() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Declare columns to escape values. |
||
| 197 | * |
||
| 198 | * @param string|array $columns |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function escapeColumns($columns = '*') |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set columns that should not be escaped. |
||
| 210 | * |
||
| 211 | * @param array $columns |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | public function rawColumns(array $columns) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Sets DT_RowClass template. |
||
| 223 | * result: <tr class="output_from_your_template">. |
||
| 224 | * |
||
| 225 | * @param string|callable $content |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | public function setRowClass($content) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Sets DT_RowId template. |
||
| 237 | * result: <tr id="output_from_your_template">. |
||
| 238 | * |
||
| 239 | * @param string|callable $content |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function setRowId($content) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set DT_RowData templates. |
||
| 251 | * |
||
| 252 | * @param array $data |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | public function setRowData(array $data) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Add DT_RowData template. |
||
| 264 | * |
||
| 265 | * @param string $key |
||
| 266 | * @param string|callable $value |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | public function addRowData($key, $value) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set DT_RowAttr templates. |
||
| 278 | * result: <tr attr1="attr1" attr2="attr2">. |
||
| 279 | * |
||
| 280 | * @param array $data |
||
| 281 | * @return $this |
||
| 282 | */ |
||
| 283 | public function setRowAttr(array $data) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Add DT_RowAttr template. |
||
| 292 | * |
||
| 293 | * @param string $key |
||
| 294 | * @param string|callable $value |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | public function addRowAttr($key, $value) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Append data on json response. |
||
| 306 | * |
||
| 307 | * @param mixed $key |
||
| 308 | * @param mixed $value |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | public function with($key, $value = '') |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Override default ordering method with a closure callback. |
||
| 326 | * |
||
| 327 | * @param callable $closure |
||
| 328 | * @return $this |
||
| 329 | */ |
||
| 330 | public function order(callable $closure) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Update list of columns that is not allowed for search/sort. |
||
| 339 | * |
||
| 340 | * @param array $blacklist |
||
| 341 | * @return $this |
||
| 342 | */ |
||
| 343 | public function blacklist(array $blacklist) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Update list of columns that is allowed for search/sort. |
||
| 352 | * |
||
| 353 | * @param string|array $whitelist |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | public function whitelist($whitelist = '*') |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Set smart search config at runtime. |
||
| 365 | * |
||
| 366 | * @param bool $bool |
||
| 367 | * @return $this |
||
| 368 | */ |
||
| 369 | public function smart($bool = true) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Set total records manually. |
||
| 378 | * |
||
| 379 | * @param int $total |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | public function setTotalRecords($total) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Skip pagination as needed. |
||
| 391 | * |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | public function skipPaging() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Push a new column name to blacklist. |
||
| 403 | * |
||
| 404 | * @param string $column |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | public function pushToBlacklist($column) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Check if column is blacklisted. |
||
| 418 | * |
||
| 419 | * @param string $column |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | protected function isBlacklisted($column) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get columns definition. |
||
| 439 | * |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | protected function getColumnsDefinition() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Perform sorting of columns. |
||
| 452 | */ |
||
| 453 | public function ordering() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Resolve callback parameter instance. |
||
| 464 | * |
||
| 465 | * @return mixed |
||
| 466 | */ |
||
| 467 | abstract protected function resolveCallbackParameter(); |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Perform default query orderBy clause. |
||
| 471 | */ |
||
| 472 | abstract protected function defaultOrdering(); |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Set auto filter off and run your own filter. |
||
| 476 | * Overrides global search. |
||
| 477 | * |
||
| 478 | * @param callable $callback |
||
| 479 | * @param bool $globalSearch |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function filter(callable $callback, $globalSearch = false) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Convert instance to array. |
||
| 493 | * |
||
| 494 | * @return array |
||
| 495 | */ |
||
| 496 | public function toArray() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Convert the object to its JSON representation. |
||
| 503 | * |
||
| 504 | * @param int $options |
||
| 505 | * @return \Illuminate\Http\JsonResponse |
||
| 506 | */ |
||
| 507 | public function toJson($options = 0) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Perform necessary filters. |
||
| 518 | * |
||
| 519 | * @return void |
||
| 520 | */ |
||
| 521 | protected function filterRecords() |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Perform global search. |
||
| 537 | * |
||
| 538 | * @return void |
||
| 539 | */ |
||
| 540 | public function filtering() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Perform multi-term search by splitting keyword into |
||
| 555 | * individual words and searches for each of them. |
||
| 556 | * |
||
| 557 | * @param string $keyword |
||
| 558 | */ |
||
| 559 | protected function smartGlobalSearch($keyword) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Perform global search for the given keyword. |
||
| 570 | * |
||
| 571 | * @param string $keyword |
||
| 572 | */ |
||
| 573 | abstract protected function globalSearch($keyword); |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Apply pagination. |
||
| 577 | * |
||
| 578 | * @return void |
||
| 579 | */ |
||
| 580 | protected function paginate() |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Transform output. |
||
| 589 | * |
||
| 590 | * @param mixed $results |
||
| 591 | * @param mixed $processed |
||
| 592 | * @return array |
||
| 593 | */ |
||
| 594 | protected function transform($results, $processed) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Get processed data. |
||
| 609 | * |
||
| 610 | * @param mixed $results |
||
| 611 | * @param bool $object |
||
| 612 | * @return array |
||
| 613 | */ |
||
| 614 | protected function processResults($results, $object = false) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Render json response. |
||
| 628 | * |
||
| 629 | * @param array $data |
||
| 630 | * @return \Illuminate\Http\JsonResponse |
||
| 631 | */ |
||
| 632 | protected function render(array $data) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Append debug parameters on output. |
||
| 655 | * |
||
| 656 | * @param array $output |
||
| 657 | * @return array |
||
| 658 | */ |
||
| 659 | abstract protected function showDebugger(array $output); |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Return an error json response. |
||
| 663 | * |
||
| 664 | * @param \Exception $exception |
||
| 665 | * @return \Illuminate\Http\JsonResponse |
||
| 666 | * @throws \Yajra\DataTables\Exceptions\Exception |
||
| 667 | */ |
||
| 668 | protected function errorResponse(\Exception $exception) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Get monolog/logger instance. |
||
| 688 | * |
||
| 689 | * @return \Illuminate\Contracts\Logging\Log |
||
| 690 | */ |
||
| 691 | public function getLogger() |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Set monolog/logger instance. |
||
| 700 | * |
||
| 701 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
| 702 | * @return $this |
||
| 703 | */ |
||
| 704 | public function setLogger(Log $logger) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Setup search keyword. |
||
| 713 | * |
||
| 714 | * @param string $value |
||
| 715 | * @return string |
||
| 716 | */ |
||
| 717 | protected function setupKeyword($value) |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Get column name to be use for filtering and sorting. |
||
| 735 | * |
||
| 736 | * @param integer $index |
||
| 737 | * @param bool $wantsAlias |
||
| 738 | * @return string |
||
| 739 | */ |
||
| 740 | protected function getColumnName($index, $wantsAlias = false) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Get column name by order column index. |
||
| 758 | * |
||
| 759 | * @param int $index |
||
| 760 | * @return string |
||
| 761 | */ |
||
| 762 | protected function getColumnNameByIndex($index) |
||
| 768 | |||
| 769 | /** |
||
| 770 | * If column name could not be resolved then use primary key. |
||
| 771 | * |
||
| 772 | * @return string |
||
| 773 | */ |
||
| 774 | protected function getPrimaryKeyName() |
||
| 778 | } |
||
| 779 |
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: