Complex classes like BaseEngine 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 BaseEngine, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | abstract class BaseEngine implements DataTableEngine |
||
| 26 | { |
||
| 27 | use Macroable; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Datatables Request object. |
||
| 31 | * |
||
| 32 | * @var \Yajra\Datatables\Request |
||
| 33 | */ |
||
| 34 | public $request; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \Illuminate\Contracts\Logging\Log |
||
| 38 | */ |
||
| 39 | protected $logger; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Array of result columns/fields. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $columns = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $columnDef = [ |
||
| 54 | 'index' => false, |
||
| 55 | 'append' => [], |
||
| 56 | 'edit' => [], |
||
| 57 | 'filter' => [], |
||
| 58 | 'order' => [], |
||
| 59 | ]; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Extra/Added columns. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $extraColumns = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Total records. |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | protected $totalRecords = 0; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Total filtered records. |
||
| 77 | * |
||
| 78 | * @var int |
||
| 79 | */ |
||
| 80 | protected $filteredRecords = 0; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Auto-filter flag. |
||
| 84 | * |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | protected $autoFilter = true; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Callback to override global search. |
||
| 91 | * |
||
| 92 | * @var callable |
||
| 93 | */ |
||
| 94 | protected $filterCallback; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * DT row templates container. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $templates = [ |
||
| 102 | 'DT_RowId' => '', |
||
| 103 | 'DT_RowClass' => '', |
||
| 104 | 'DT_RowData' => [], |
||
| 105 | 'DT_RowAttr' => [], |
||
| 106 | ]; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * [internal] Track if any filter was applied for at least one column. |
||
| 110 | * |
||
| 111 | * @var boolean |
||
| 112 | */ |
||
| 113 | protected $isFilterApplied = false; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Custom ordering callback. |
||
| 117 | * |
||
| 118 | * @var callable |
||
| 119 | */ |
||
| 120 | protected $orderCallback; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Skip paginate as needed. |
||
| 124 | * |
||
| 125 | * @var bool |
||
| 126 | */ |
||
| 127 | protected $skipPaging = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Array of data to append on json response. |
||
| 131 | * |
||
| 132 | * @var array |
||
| 133 | */ |
||
| 134 | protected $appends = []; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var \Yajra\Datatables\Config |
||
| 138 | */ |
||
| 139 | protected $config; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Add column in collection. |
||
| 143 | * |
||
| 144 | * @param string $name |
||
| 145 | * @param string|callable $content |
||
| 146 | * @param bool|int $order |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | public function addColumn($name, $content, $order = false) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add DT row index column on response. |
||
| 160 | * |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | public function addIndexColumn() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Edit column's content. |
||
| 172 | * |
||
| 173 | * @param string $name |
||
| 174 | * @param string|callable $content |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function editColumn($name, $content) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Remove column from collection. |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function removeColumn() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Declare columns to escape values. |
||
| 199 | * |
||
| 200 | * @param string|array $columns |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | public function escapeColumns($columns = '*') |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Set columns that should not be escaped. |
||
| 212 | * |
||
| 213 | * @param array $columns |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | public function rawColumns(array $columns) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Sets DT_RowClass template. |
||
| 225 | * result: <tr class="output_from_your_template">. |
||
| 226 | * |
||
| 227 | * @param string|callable $content |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | public function setRowClass($content) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Sets DT_RowId template. |
||
| 239 | * result: <tr id="output_from_your_template">. |
||
| 240 | * |
||
| 241 | * @param string|callable $content |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function setRowId($content) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set DT_RowData templates. |
||
| 253 | * |
||
| 254 | * @param array $data |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | public function setRowData(array $data) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Add DT_RowData template. |
||
| 266 | * |
||
| 267 | * @param string $key |
||
| 268 | * @param string|callable $value |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function addRowData($key, $value) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set DT_RowAttr templates. |
||
| 280 | * result: <tr attr1="attr1" attr2="attr2">. |
||
| 281 | * |
||
| 282 | * @param array $data |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | public function setRowAttr(array $data) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Add DT_RowAttr template. |
||
| 294 | * |
||
| 295 | * @param string $key |
||
| 296 | * @param string|callable $value |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function addRowAttr($key, $value) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Append data on json response. |
||
| 308 | * |
||
| 309 | * @param mixed $key |
||
| 310 | * @param mixed $value |
||
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | public function with($key, $value = '') |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Override default ordering method with a closure callback. |
||
| 328 | * |
||
| 329 | * @param callable $closure |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function order(callable $closure) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Update list of columns that is not allowed for search/sort. |
||
| 341 | * |
||
| 342 | * @param array $blacklist |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function blacklist(array $blacklist) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Update list of columns that is allowed for search/sort. |
||
| 354 | * |
||
| 355 | * @param string|array $whitelist |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function whitelist($whitelist = '*') |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Set smart search config at runtime. |
||
| 367 | * |
||
| 368 | * @param bool $bool |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function smart($bool = true) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Set total records manually. |
||
| 380 | * |
||
| 381 | * @param int $total |
||
| 382 | * @return $this |
||
| 383 | */ |
||
| 384 | public function setTotalRecords($total) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Skip pagination as needed. |
||
| 393 | * |
||
| 394 | * @return $this |
||
| 395 | */ |
||
| 396 | public function skipPaging() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Push a new column name to blacklist. |
||
| 405 | * |
||
| 406 | * @param string $column |
||
| 407 | * @return $this |
||
| 408 | */ |
||
| 409 | public function pushToBlacklist($column) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Check if column is blacklisted. |
||
| 420 | * |
||
| 421 | * @param string $column |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | protected function isBlacklisted($column) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get columns definition. |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | protected function getColumnsDefinition() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Perform sorting of columns. |
||
| 454 | */ |
||
| 455 | public function ordering() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Resolve callback parameter instance. |
||
| 466 | * |
||
| 467 | * @return mixed |
||
| 468 | */ |
||
| 469 | abstract protected function resolveCallbackParameter(); |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Perform default query orderBy clause. |
||
| 473 | */ |
||
| 474 | abstract protected function defaultOrdering(); |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Set auto filter off and run your own filter. |
||
| 478 | * Overrides global search. |
||
| 479 | * |
||
| 480 | * @param callable $callback |
||
| 481 | * @param bool $globalSearch |
||
| 482 | * @return $this |
||
| 483 | */ |
||
| 484 | public function filter(callable $callback, $globalSearch = false) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Perform necessary filters. |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | protected function filterRecords() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Perform global search. |
||
| 514 | * |
||
| 515 | * @return void |
||
| 516 | */ |
||
| 517 | public function filtering() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Perform multi-term search by splitting keyword into |
||
| 532 | * individual words and searches for each of them. |
||
| 533 | * |
||
| 534 | * @param string $keyword |
||
| 535 | */ |
||
| 536 | protected function smartGlobalSearch($keyword) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Perform global search for the given keyword. |
||
| 547 | * |
||
| 548 | * @param string $keyword |
||
| 549 | */ |
||
| 550 | abstract protected function globalSearch($keyword); |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Apply pagination. |
||
| 554 | * |
||
| 555 | * @return void |
||
| 556 | */ |
||
| 557 | protected function paginate() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Transform output. |
||
| 566 | * |
||
| 567 | * @param mixed $results |
||
| 568 | * @param mixed $processed |
||
| 569 | * @return array |
||
| 570 | */ |
||
| 571 | protected function transform($results, $processed) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Get processed data. |
||
| 582 | * |
||
| 583 | * @param mixed $results |
||
| 584 | * @param bool $object |
||
| 585 | * @return array |
||
| 586 | */ |
||
| 587 | protected function processResults($results, $object = false) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Render json response. |
||
| 601 | * |
||
| 602 | * @param array $data |
||
| 603 | * @return \Illuminate\Http\JsonResponse |
||
| 604 | */ |
||
| 605 | protected function render(array $data) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Append debug parameters on output. |
||
| 628 | * |
||
| 629 | * @param array $output |
||
| 630 | * @return array |
||
| 631 | */ |
||
| 632 | abstract protected function showDebugger(array $output); |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Return an error json response. |
||
| 636 | * |
||
| 637 | * @param \Exception $exception |
||
| 638 | * @return \Illuminate\Http\JsonResponse |
||
| 639 | * @throws \Yajra\Datatables\Exception |
||
| 640 | */ |
||
| 641 | protected function errorResponse(\Exception $exception) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Get monolog/logger instance. |
||
| 661 | * |
||
| 662 | * @return \Illuminate\Contracts\Logging\Log |
||
| 663 | */ |
||
| 664 | public function getLogger() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Set monolog/logger instance. |
||
| 673 | * |
||
| 674 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
| 675 | * @return $this |
||
| 676 | */ |
||
| 677 | public function setLogger(Log $logger) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Setup search keyword. |
||
| 686 | * |
||
| 687 | * @param string $value |
||
| 688 | * @return string |
||
| 689 | */ |
||
| 690 | protected function setupKeyword($value) |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Get column name to be use for filtering and sorting. |
||
| 708 | * |
||
| 709 | * @param integer $index |
||
| 710 | * @param bool $wantsAlias |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | protected function getColumnName($index, $wantsAlias = false) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Get column name by order column index. |
||
| 731 | * |
||
| 732 | * @param int $index |
||
| 733 | * @return string |
||
| 734 | */ |
||
| 735 | protected function getColumnNameByIndex($index) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * If column name could not be resolved then use primary key. |
||
| 744 | * |
||
| 745 | * @return string |
||
| 746 | */ |
||
| 747 | protected function getPrimaryKeyName() |
||
| 751 | } |
||
| 752 |