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 |
||
| 23 | abstract class BaseEngine implements DataTableEngineContract |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Datatables Request object. |
||
| 27 | * |
||
| 28 | * @var \Yajra\Datatables\Request |
||
| 29 | */ |
||
| 30 | public $request; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Database connection used. |
||
| 34 | * |
||
| 35 | * @var \Illuminate\Database\Connection |
||
| 36 | */ |
||
| 37 | protected $connection; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Builder object. |
||
| 41 | * |
||
| 42 | * @var mixed |
||
| 43 | */ |
||
| 44 | protected $query; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Query builder object. |
||
| 48 | * |
||
| 49 | * @var \Illuminate\Database\Query\Builder |
||
| 50 | */ |
||
| 51 | protected $builder; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Array of result columns/fields. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $columns = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $columnDef = [ |
||
| 66 | 'append' => [], |
||
| 67 | 'edit' => [], |
||
| 68 | 'excess' => ['rn', 'row_num'], |
||
| 69 | 'filter' => [], |
||
| 70 | 'order' => [], |
||
| 71 | 'escape' => [], |
||
| 72 | ]; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Query type. |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $query_type; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Extra/Added columns. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $extraColumns = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Total records. |
||
| 90 | * |
||
| 91 | * @var int |
||
| 92 | */ |
||
| 93 | protected $totalRecords = 0; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Total filtered records. |
||
| 97 | * |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | protected $filteredRecords = 0; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Auto-filter flag. |
||
| 104 | * |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | protected $autoFilter = true; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Callback to override global search. |
||
| 111 | * |
||
| 112 | * @var \Closure |
||
| 113 | */ |
||
| 114 | protected $filterCallback; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Parameters to passed on filterCallback. |
||
| 118 | * |
||
| 119 | * @var mixed |
||
| 120 | */ |
||
| 121 | protected $filterCallbackParameters; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * DT row templates container. |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $templates = [ |
||
| 129 | 'DT_RowId' => '', |
||
| 130 | 'DT_RowClass' => '', |
||
| 131 | 'DT_RowData' => [], |
||
| 132 | 'DT_RowAttr' => [], |
||
| 133 | ]; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Output transformer. |
||
| 137 | * |
||
| 138 | * @var \League\Fractal\TransformerAbstract |
||
| 139 | */ |
||
| 140 | protected $transformer = null; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Database prefix |
||
| 144 | * |
||
| 145 | * @var string |
||
| 146 | */ |
||
| 147 | protected $prefix; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Database driver used. |
||
| 151 | * |
||
| 152 | * @var string |
||
| 153 | */ |
||
| 154 | protected $database; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * [internal] Track if any filter was applied for at least one column |
||
| 158 | * |
||
| 159 | * @var boolean |
||
| 160 | */ |
||
| 161 | protected $isFilterApplied = false; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Fractal serializer class. |
||
| 165 | * |
||
| 166 | * @var string |
||
| 167 | */ |
||
| 168 | protected $serializer; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Array of data to append on json response. |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | private $appends = []; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Setup search keyword. |
||
| 179 | * |
||
| 180 | * @param string $value |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function setupKeyword($value) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get config use wild card status. |
||
| 197 | * |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function isWildcard() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Adds % wildcards to the given string. |
||
| 207 | * |
||
| 208 | * @param string $str |
||
| 209 | * @param bool $lowercase |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function wildcardLikeString($str, $lowercase = true) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Setup column name to be use for filtering. |
||
| 230 | * |
||
| 231 | * @param integer $i |
||
| 232 | * @param bool $wantsAlias |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function setupColumnName($i, $wantsAlias = false) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get column name by order column index. |
||
| 247 | * |
||
| 248 | * @param int $column |
||
| 249 | * @return mixed |
||
| 250 | */ |
||
| 251 | protected function getColumnName($column) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get column name from string. |
||
| 260 | * |
||
| 261 | * @param string $str |
||
| 262 | * @param bool $wantsAlias |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function extractColumnName($str, $wantsAlias) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Will prefix column if needed. |
||
| 286 | * |
||
| 287 | * @param string $column |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function prefixColumn($column) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Will look through the query and all it's joins to determine the table names. |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function tableNames() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get Query Builder object. |
||
| 330 | * |
||
| 331 | * @param mixed $instance |
||
| 332 | * @return mixed |
||
| 333 | */ |
||
| 334 | public function getQueryBuilder($instance = null) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Check query type is a builder. |
||
| 349 | * |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function isQueryBuilder() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Add column in collection. |
||
| 359 | * |
||
| 360 | * @param string $name |
||
| 361 | * @param string $content |
||
| 362 | * @param bool|int $order |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function addColumn($name, $content, $order = false) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Edit column's content. |
||
| 376 | * |
||
| 377 | * @param string $name |
||
| 378 | * @param string $content |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | public function editColumn($name, $content) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Remove column from collection. |
||
| 390 | * |
||
| 391 | * @return $this |
||
| 392 | */ |
||
| 393 | public function removeColumn() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Declare columns to escape values. |
||
| 403 | * |
||
| 404 | * @param string|array $columns |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | public function escapeColumns($columns = '*') |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Allows previous API calls where the methods were snake_case. |
||
| 416 | * Will convert a camelCase API call to a snake_case call. |
||
| 417 | * |
||
| 418 | * @param $name |
||
| 419 | * @param $arguments |
||
| 420 | * @return $this|mixed |
||
| 421 | */ |
||
| 422 | public function __call($name, $arguments) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Sets DT_RowClass template |
||
| 438 | * result: <tr class="output_from_your_template">. |
||
| 439 | * |
||
| 440 | * @param string|callable $content |
||
| 441 | * @return $this |
||
| 442 | */ |
||
| 443 | public function setRowClass($content) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Sets DT_RowId template |
||
| 452 | * result: <tr id="output_from_your_template">. |
||
| 453 | * |
||
| 454 | * @param string|callable $content |
||
| 455 | * @return $this |
||
| 456 | */ |
||
| 457 | public function setRowId($content) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Set DT_RowData templates. |
||
| 466 | * |
||
| 467 | * @param array $data |
||
| 468 | * @return $this |
||
| 469 | */ |
||
| 470 | public function setRowData(array $data) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Add DT_RowData template. |
||
| 479 | * |
||
| 480 | * @param string $key |
||
| 481 | * @param string|callable $value |
||
| 482 | * @return $this |
||
| 483 | */ |
||
| 484 | public function addRowData($key, $value) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Set DT_RowAttr templates |
||
| 493 | * result: <tr attr1="attr1" attr2="attr2">. |
||
| 494 | * |
||
| 495 | * @param array $data |
||
| 496 | * @return $this |
||
| 497 | */ |
||
| 498 | public function setRowAttr(array $data) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Add DT_RowAttr template. |
||
| 507 | * |
||
| 508 | * @param string $key |
||
| 509 | * @param string|callable $value |
||
| 510 | * @return $this |
||
| 511 | */ |
||
| 512 | public function addRowAttr($key, $value) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Override default column filter search. |
||
| 521 | * |
||
| 522 | * @param string $column |
||
| 523 | * @param string $method |
||
| 524 | * @return $this |
||
| 525 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
| 526 | * @internal string $1 Special variable that returns the requested search keyword. |
||
| 527 | */ |
||
| 528 | public function filterColumn($column, $method) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Override default column ordering. |
||
| 538 | * |
||
| 539 | * @param string $column |
||
| 540 | * @param string $sql |
||
| 541 | * @param array $bindings |
||
| 542 | * @return $this |
||
| 543 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 544 | */ |
||
| 545 | public function orderColumn($column, $sql, $bindings = []) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Set data output transformer. |
||
| 554 | * |
||
| 555 | * @param \League\Fractal\TransformerAbstract $transformer |
||
| 556 | * @return $this |
||
| 557 | */ |
||
| 558 | public function setTransformer($transformer) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Set fractal serializer class. |
||
| 567 | * |
||
| 568 | * @param string $serializer |
||
| 569 | * @return $this |
||
| 570 | */ |
||
| 571 | public function setSerializer($serializer) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Organizes works. |
||
| 580 | * |
||
| 581 | * @param bool $mDataSupport |
||
| 582 | * @param bool $orderFirst |
||
| 583 | * @return \Illuminate\Http\JsonResponse |
||
| 584 | */ |
||
| 585 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Count results. |
||
| 601 | * |
||
| 602 | * @return integer |
||
| 603 | */ |
||
| 604 | abstract public function count(); |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Sort records. |
||
| 608 | * |
||
| 609 | * @param boolean $skip |
||
| 610 | * @return void |
||
| 611 | */ |
||
| 612 | public function orderRecords($skip) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Perform sorting of columns. |
||
| 621 | * |
||
| 622 | * @return void |
||
| 623 | */ |
||
| 624 | abstract public function ordering(); |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Perform necessary filters. |
||
| 628 | * |
||
| 629 | * @return void |
||
| 630 | */ |
||
| 631 | public function filterRecords() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Perform global search. |
||
| 647 | * |
||
| 648 | * @return void |
||
| 649 | */ |
||
| 650 | abstract public function filtering(); |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Perform column search. |
||
| 654 | * |
||
| 655 | * @return void |
||
| 656 | */ |
||
| 657 | abstract public function columnSearch(); |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Apply pagination. |
||
| 661 | * |
||
| 662 | * @return void |
||
| 663 | */ |
||
| 664 | public function paginate() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Perform pagination |
||
| 673 | * |
||
| 674 | * @return void |
||
| 675 | */ |
||
| 676 | abstract public function paging(); |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Render json response. |
||
| 680 | * |
||
| 681 | * @param bool $object |
||
| 682 | * @return \Illuminate\Http\JsonResponse |
||
| 683 | */ |
||
| 684 | public function render($object = false) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Get results |
||
| 732 | * |
||
| 733 | * @return array |
||
| 734 | */ |
||
| 735 | abstract public function results(); |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Get processed data |
||
| 739 | * |
||
| 740 | * @param bool|false $object |
||
| 741 | * @return array |
||
| 742 | */ |
||
| 743 | private function getProcessedData($object = false) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Check if app is in debug mode. |
||
| 756 | * |
||
| 757 | * @return bool |
||
| 758 | */ |
||
| 759 | public function isDebugging() |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Append debug parameters on output. |
||
| 766 | * |
||
| 767 | * @param array $output |
||
| 768 | * @return array |
||
| 769 | */ |
||
| 770 | public function showDebugger(array $output) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Set auto filter off and run your own filter. |
||
| 780 | * Overrides global search |
||
| 781 | * |
||
| 782 | * @param \Closure $callback |
||
| 783 | * @return $this |
||
| 784 | */ |
||
| 785 | abstract public function filter(\Closure $callback); |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Update flags to disable global search |
||
| 789 | * |
||
| 790 | * @param \Closure $callback |
||
| 791 | * @param mixed $parameters |
||
| 792 | * @return void |
||
| 793 | */ |
||
| 794 | public function overrideGlobalSearch(\Closure $callback, $parameters) |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Get config is case insensitive status. |
||
| 804 | * |
||
| 805 | * @return bool |
||
| 806 | */ |
||
| 807 | public function isCaseInsensitive() |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Append data on json response. |
||
| 814 | * |
||
| 815 | * @param mixed $key |
||
| 816 | * @param mixed $value |
||
| 817 | * @return $this |
||
| 818 | */ |
||
| 819 | public function with($key, $value = '') |
||
| 831 | } |
||
| 832 |