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 | * Custom ordering callback. |
||
| 172 | * |
||
| 173 | * @var \Closure |
||
| 174 | */ |
||
| 175 | protected $orderCallback; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Array of data to append on json response. |
||
| 179 | * |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | private $appends = []; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Setup search keyword. |
||
| 186 | * |
||
| 187 | * @param string $value |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function setupKeyword($value) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get config use wild card status. |
||
| 204 | * |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function isWildcard() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Adds % wildcards to the given string. |
||
| 214 | * |
||
| 215 | * @param string $str |
||
| 216 | * @param bool $lowercase |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function wildcardLikeString($str, $lowercase = true) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Will prefix column if needed. |
||
| 237 | * |
||
| 238 | * @param string $column |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function prefixColumn($column) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Will look through the query and all it's joins to determine the table names. |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | public function tableNames() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get Query Builder object. |
||
| 281 | * |
||
| 282 | * @param mixed $instance |
||
| 283 | * @return mixed |
||
| 284 | */ |
||
| 285 | public function getQueryBuilder($instance = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Check query type is a builder. |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function isQueryBuilder() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Add column in collection. |
||
| 310 | * |
||
| 311 | * @param string $name |
||
| 312 | * @param string $content |
||
| 313 | * @param bool|int $order |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function addColumn($name, $content, $order = false) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Edit column's content. |
||
| 327 | * |
||
| 328 | * @param string $name |
||
| 329 | * @param string $content |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function editColumn($name, $content) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Remove column from collection. |
||
| 341 | * |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function removeColumn() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Declare columns to escape values. |
||
| 354 | * |
||
| 355 | * @param string|array $columns |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function escapeColumns($columns = '*') |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Allows previous API calls where the methods were snake_case. |
||
| 367 | * Will convert a camelCase API call to a snake_case call. |
||
| 368 | * Allow query builder method to be used by the engine. |
||
| 369 | * |
||
| 370 | * @param $name |
||
| 371 | * @param $arguments |
||
| 372 | * @return $this|mixed |
||
| 373 | */ |
||
| 374 | public function __call($name, $arguments) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Sets DT_RowClass template |
||
| 390 | * result: <tr class="output_from_your_template">. |
||
| 391 | * |
||
| 392 | * @param string|callable $content |
||
| 393 | * @return $this |
||
| 394 | */ |
||
| 395 | public function setRowClass($content) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Sets DT_RowId template |
||
| 404 | * result: <tr id="output_from_your_template">. |
||
| 405 | * |
||
| 406 | * @param string|callable $content |
||
| 407 | * @return $this |
||
| 408 | */ |
||
| 409 | public function setRowId($content) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set DT_RowData templates. |
||
| 418 | * |
||
| 419 | * @param array $data |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | public function setRowData(array $data) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Add DT_RowData template. |
||
| 431 | * |
||
| 432 | * @param string $key |
||
| 433 | * @param string|callable $value |
||
| 434 | * @return $this |
||
| 435 | */ |
||
| 436 | public function addRowData($key, $value) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Set DT_RowAttr templates |
||
| 445 | * result: <tr attr1="attr1" attr2="attr2">. |
||
| 446 | * |
||
| 447 | * @param array $data |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | public function setRowAttr(array $data) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Add DT_RowAttr template. |
||
| 459 | * |
||
| 460 | * @param string $key |
||
| 461 | * @param string|callable $value |
||
| 462 | * @return $this |
||
| 463 | */ |
||
| 464 | public function addRowAttr($key, $value) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Override default column filter search. |
||
| 473 | * |
||
| 474 | * @param string $column |
||
| 475 | * @param string $method |
||
| 476 | * @return $this |
||
| 477 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
| 478 | * @internal string $1 Special variable that returns the requested search keyword. |
||
| 479 | */ |
||
| 480 | public function filterColumn($column, $method) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Override default column ordering. |
||
| 490 | * |
||
| 491 | * @param string $column |
||
| 492 | * @param string $sql |
||
| 493 | * @param array $bindings |
||
| 494 | * @return $this |
||
| 495 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 496 | */ |
||
| 497 | public function orderColumn($column, $sql, $bindings = []) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Set data output transformer. |
||
| 506 | * |
||
| 507 | * @param \League\Fractal\TransformerAbstract $transformer |
||
| 508 | * @return $this |
||
| 509 | */ |
||
| 510 | public function setTransformer($transformer) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Set fractal serializer class. |
||
| 519 | * |
||
| 520 | * @param string $serializer |
||
| 521 | * @return $this |
||
| 522 | */ |
||
| 523 | public function setSerializer($serializer) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Organizes works. |
||
| 532 | * |
||
| 533 | * @param bool $mDataSupport |
||
| 534 | * @param bool $orderFirst |
||
| 535 | * @return \Illuminate\Http\JsonResponse |
||
| 536 | */ |
||
| 537 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Sort records. |
||
| 553 | * |
||
| 554 | * @param boolean $skip |
||
| 555 | * @return void |
||
| 556 | */ |
||
| 557 | public function orderRecords($skip) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Perform necessary filters. |
||
| 566 | * |
||
| 567 | * @return void |
||
| 568 | */ |
||
| 569 | public function filterRecords() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Apply pagination. |
||
| 585 | * |
||
| 586 | * @return void |
||
| 587 | */ |
||
| 588 | public function paginate() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Render json response. |
||
| 597 | * |
||
| 598 | * @param bool $object |
||
| 599 | * @return \Illuminate\Http\JsonResponse |
||
| 600 | */ |
||
| 601 | public function render($object = false) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Get processed data |
||
| 649 | * |
||
| 650 | * @param bool|false $object |
||
| 651 | * @return array |
||
| 652 | */ |
||
| 653 | private function getProcessedData($object = false) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Check if app is in debug mode. |
||
| 666 | * |
||
| 667 | * @return bool |
||
| 668 | */ |
||
| 669 | public function isDebugging() |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Append debug parameters on output. |
||
| 676 | * |
||
| 677 | * @param array $output |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | public function showDebugger(array $output) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Update flags to disable global search |
||
| 690 | * |
||
| 691 | * @param \Closure $callback |
||
| 692 | * @param mixed $parameters |
||
| 693 | * @return void |
||
| 694 | */ |
||
| 695 | public function overrideGlobalSearch(\Closure $callback, $parameters) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Get config is case insensitive status. |
||
| 705 | * |
||
| 706 | * @return bool |
||
| 707 | */ |
||
| 708 | public function isCaseInsensitive() |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Append data on json response. |
||
| 715 | * |
||
| 716 | * @param mixed $key |
||
| 717 | * @param mixed $value |
||
| 718 | * @return $this |
||
| 719 | */ |
||
| 720 | public function with($key, $value = '') |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Override default ordering method with a closure callback. |
||
| 735 | * |
||
| 736 | * @param \Closure $closure |
||
| 737 | * @return $this |
||
| 738 | */ |
||
| 739 | public function order(\Closure $closure) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Check if the current sql language is based on oracle syntax. |
||
| 748 | * |
||
| 749 | * @return bool |
||
| 750 | */ |
||
| 751 | public function isOracleSql() |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Get column name to be use for filtering and sorting. |
||
| 758 | * |
||
| 759 | * @param integer $index |
||
| 760 | * @param bool $wantsAlias |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | protected function getColumnName($index, $wantsAlias = false) |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Get column name by order column index. |
||
| 781 | * |
||
| 782 | * @param int $index |
||
| 783 | * @return mixed |
||
| 784 | */ |
||
| 785 | protected function getColumnNameByIndex($index) |
||
| 791 | |||
| 792 | /** |
||
| 793 | * If column name could not be resolved then use primary key. |
||
| 794 | * |
||
| 795 | * @return string |
||
| 796 | */ |
||
| 797 | protected function getPrimaryKeyName() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Check if the engine used was eloquent. |
||
| 808 | * |
||
| 809 | * @return bool |
||
| 810 | */ |
||
| 811 | protected function isEloquent() |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Get column name from string. |
||
| 818 | * |
||
| 819 | * @param string $str |
||
| 820 | * @param bool $wantsAlias |
||
| 821 | * @return string |
||
| 822 | */ |
||
| 823 | protected function extractColumnName($str, $wantsAlias) |
||
| 841 | } |
||
| 842 |