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 |
||
| 21 | abstract class BaseEngine implements DataTableEngineContract |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Datatables Request object. |
||
| 25 | * |
||
| 26 | * @var \Yajra\Datatables\Request |
||
| 27 | */ |
||
| 28 | public $request; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Database connection used. |
||
| 32 | * |
||
| 33 | * @var \Illuminate\Database\Connection |
||
| 34 | */ |
||
| 35 | protected $connection; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Builder object. |
||
| 39 | * |
||
| 40 | * @var \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
| 41 | */ |
||
| 42 | protected $query; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Query builder object. |
||
| 46 | * |
||
| 47 | * @var \Illuminate\Database\Query\Builder |
||
| 48 | */ |
||
| 49 | protected $builder; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Array of result columns/fields. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $columns = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $columnDef = [ |
||
| 64 | 'index' => false, |
||
| 65 | 'append' => [], |
||
| 66 | 'edit' => [], |
||
| 67 | 'excess' => ['rn', 'row_num'], |
||
| 68 | 'filter' => [], |
||
| 69 | 'order' => [], |
||
| 70 | 'escape' => [], |
||
| 71 | 'blacklist' => ['password', 'remember_token'], |
||
| 72 | 'whitelist' => '*', |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Query type. |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $query_type; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Extra/Added columns. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $extraColumns = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Total records. |
||
| 91 | * |
||
| 92 | * @var int |
||
| 93 | */ |
||
| 94 | protected $totalRecords = 0; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Total filtered records. |
||
| 98 | * |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | protected $filteredRecords = 0; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Auto-filter flag. |
||
| 105 | * |
||
| 106 | * @var bool |
||
| 107 | */ |
||
| 108 | protected $autoFilter = true; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Callback to override global search. |
||
| 112 | * |
||
| 113 | * @var \Closure |
||
| 114 | */ |
||
| 115 | protected $filterCallback; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Parameters to passed on filterCallback. |
||
| 119 | * |
||
| 120 | * @var mixed |
||
| 121 | */ |
||
| 122 | protected $filterCallbackParameters; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * DT row templates container. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | protected $templates = [ |
||
| 130 | 'DT_RowId' => '', |
||
| 131 | 'DT_RowClass' => '', |
||
| 132 | 'DT_RowData' => [], |
||
| 133 | 'DT_RowAttr' => [], |
||
| 134 | ]; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Output transformer. |
||
| 138 | * |
||
| 139 | * @var \League\Fractal\TransformerAbstract |
||
| 140 | */ |
||
| 141 | protected $transformer = null; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Database prefix |
||
| 145 | * |
||
| 146 | * @var string |
||
| 147 | */ |
||
| 148 | protected $prefix; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Database driver used. |
||
| 152 | * |
||
| 153 | * @var string |
||
| 154 | */ |
||
| 155 | protected $database; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * [internal] Track if any filter was applied for at least one column |
||
| 159 | * |
||
| 160 | * @var boolean |
||
| 161 | */ |
||
| 162 | protected $isFilterApplied = false; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Fractal serializer class. |
||
| 166 | * |
||
| 167 | * @var string |
||
| 168 | */ |
||
| 169 | protected $serializer; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Custom ordering callback. |
||
| 173 | * |
||
| 174 | * @var \Closure |
||
| 175 | */ |
||
| 176 | protected $orderCallback; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Array of data to append on json response. |
||
| 180 | * |
||
| 181 | * @var array |
||
| 182 | */ |
||
| 183 | private $appends = []; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Setup search keyword. |
||
| 187 | * |
||
| 188 | * @param string $value |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function setupKeyword($value) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Check if DataTables uses smart search. |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | protected function isSmartSearch() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get config use wild card status. |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function isWildcard() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Adds % wildcards to the given string. |
||
| 229 | * |
||
| 230 | * @param string $str |
||
| 231 | * @param bool $lowercase |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function wildcardLikeString($str, $lowercase = true) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Will prefix column if needed. |
||
| 252 | * |
||
| 253 | * @param string $column |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function prefixColumn($column) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Will look through the query and all it's joins to determine the table names. |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function tableNames() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get Query Builder object. |
||
| 296 | * |
||
| 297 | * @param mixed $instance |
||
| 298 | * @return mixed |
||
| 299 | */ |
||
| 300 | public function getQueryBuilder($instance = null) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Check query type is a builder. |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public function isQueryBuilder() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Add column in collection. |
||
| 325 | * |
||
| 326 | * @param string $name |
||
| 327 | * @param string|callable $content |
||
| 328 | * @param bool|int $order |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | public function addColumn($name, $content, $order = false) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Add DT row index column on response. |
||
| 342 | * |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function addIndexColumn() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Edit column's content. |
||
| 354 | * |
||
| 355 | * @param string $name |
||
| 356 | * @param string|callable $content |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | public function editColumn($name, $content) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Remove column from collection. |
||
| 368 | * |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function removeColumn() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Declare columns to escape values. |
||
| 381 | * |
||
| 382 | * @param string|array $columns |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | public function escapeColumns($columns = '*') |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Allows previous API calls where the methods were snake_case. |
||
| 394 | * Will convert a camelCase API call to a snake_case call. |
||
| 395 | * Allow query builder method to be used by the engine. |
||
| 396 | * |
||
| 397 | * @param string $name |
||
| 398 | * @param array $arguments |
||
| 399 | * @return mixed |
||
| 400 | */ |
||
| 401 | public function __call($name, $arguments) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Sets DT_RowClass template. |
||
| 417 | * result: <tr class="output_from_your_template">. |
||
| 418 | * |
||
| 419 | * @param string|callable $content |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | public function setRowClass($content) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Sets DT_RowId template. |
||
| 431 | * result: <tr id="output_from_your_template">. |
||
| 432 | * |
||
| 433 | * @param string|callable $content |
||
| 434 | * @return $this |
||
| 435 | */ |
||
| 436 | public function setRowId($content) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Set DT_RowData templates. |
||
| 445 | * |
||
| 446 | * @param array $data |
||
| 447 | * @return $this |
||
| 448 | */ |
||
| 449 | public function setRowData(array $data) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Add DT_RowData template. |
||
| 458 | * |
||
| 459 | * @param string $key |
||
| 460 | * @param string|callable $value |
||
| 461 | * @return $this |
||
| 462 | */ |
||
| 463 | public function addRowData($key, $value) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set DT_RowAttr templates. |
||
| 472 | * result: <tr attr1="attr1" attr2="attr2">. |
||
| 473 | * |
||
| 474 | * @param array $data |
||
| 475 | * @return $this |
||
| 476 | */ |
||
| 477 | public function setRowAttr(array $data) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Add DT_RowAttr template. |
||
| 486 | * |
||
| 487 | * @param string $key |
||
| 488 | * @param string|callable $value |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | public function addRowAttr($key, $value) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Override default column filter search. |
||
| 500 | * |
||
| 501 | * @param string $column |
||
| 502 | * @param string|Closure $method |
||
| 503 | * @return $this |
||
| 504 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
| 505 | * @internal string $1 Special variable that returns the requested search keyword. |
||
| 506 | */ |
||
| 507 | public function filterColumn($column, $method) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Override default column ordering. |
||
| 517 | * |
||
| 518 | * @param string $column |
||
| 519 | * @param string $sql |
||
| 520 | * @param array $bindings |
||
| 521 | * @return $this |
||
| 522 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 523 | */ |
||
| 524 | public function orderColumn($column, $sql, $bindings = []) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Set data output transformer. |
||
| 533 | * |
||
| 534 | * @param \League\Fractal\TransformerAbstract $transformer |
||
| 535 | * @return $this |
||
| 536 | */ |
||
| 537 | public function setTransformer($transformer) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Set fractal serializer class. |
||
| 546 | * |
||
| 547 | * @param string $serializer |
||
| 548 | * @return $this |
||
| 549 | */ |
||
| 550 | public function setSerializer($serializer) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Organizes works. |
||
| 559 | * |
||
| 560 | * @param bool $mDataSupport |
||
| 561 | * @param bool $orderFirst |
||
| 562 | * @return \Illuminate\Http\JsonResponse |
||
| 563 | */ |
||
| 564 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Sort records. |
||
| 580 | * |
||
| 581 | * @param boolean $skip |
||
| 582 | * @return void |
||
| 583 | */ |
||
| 584 | public function orderRecords($skip) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Perform necessary filters. |
||
| 593 | * |
||
| 594 | * @return void |
||
| 595 | */ |
||
| 596 | public function filterRecords() |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Apply pagination. |
||
| 612 | * |
||
| 613 | * @return void |
||
| 614 | */ |
||
| 615 | public function paginate() |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Render json response. |
||
| 624 | * |
||
| 625 | * @param bool $object |
||
| 626 | * @return \Illuminate\Http\JsonResponse |
||
| 627 | */ |
||
| 628 | public function render($object = false) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Get processed data |
||
| 676 | * |
||
| 677 | * @param bool|false $object |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | private function getProcessedData($object = false) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Check if app is in debug mode. |
||
| 694 | * |
||
| 695 | * @return bool |
||
| 696 | */ |
||
| 697 | public function isDebugging() |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Append debug parameters on output. |
||
| 704 | * |
||
| 705 | * @param array $output |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | public function showDebugger(array $output) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Update flags to disable global search |
||
| 718 | * |
||
| 719 | * @param \Closure $callback |
||
| 720 | * @param mixed $parameters |
||
| 721 | * @return void |
||
| 722 | */ |
||
| 723 | public function overrideGlobalSearch(\Closure $callback, $parameters) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Get config is case insensitive status. |
||
| 733 | * |
||
| 734 | * @return bool |
||
| 735 | */ |
||
| 736 | public function isCaseInsensitive() |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Append data on json response. |
||
| 743 | * |
||
| 744 | * @param mixed $key |
||
| 745 | * @param mixed $value |
||
| 746 | * @return $this |
||
| 747 | */ |
||
| 748 | public function with($key, $value = '') |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Override default ordering method with a closure callback. |
||
| 763 | * |
||
| 764 | * @param \Closure $closure |
||
| 765 | * @return $this |
||
| 766 | */ |
||
| 767 | public function order(\Closure $closure) |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Update list of columns that is not allowed for search/sort. |
||
| 776 | * |
||
| 777 | * @param array $blacklist |
||
| 778 | * @return $this |
||
| 779 | */ |
||
| 780 | public function blacklist(array $blacklist) |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Update list of columns that is not allowed for search/sort. |
||
| 789 | * |
||
| 790 | * @param string|array $whitelist |
||
| 791 | * @return $this |
||
| 792 | */ |
||
| 793 | public function whitelist($whitelist = '*') |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Set smart search config at runtime. |
||
| 802 | * |
||
| 803 | * @param bool $bool |
||
| 804 | * @return $this |
||
| 805 | */ |
||
| 806 | public function smart($bool = true) |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Check if column is blacklisted. |
||
| 815 | * |
||
| 816 | * @param string $column |
||
| 817 | * @return bool |
||
| 818 | */ |
||
| 819 | protected function isBlacklisted($column) |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Get column name to be use for filtering and sorting. |
||
| 834 | * |
||
| 835 | * @param integer $index |
||
| 836 | * @param bool $wantsAlias |
||
| 837 | * @return string |
||
| 838 | */ |
||
| 839 | protected function getColumnName($index, $wantsAlias = false) |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Get column name by order column index. |
||
| 857 | * |
||
| 858 | * @param int $index |
||
| 859 | * @return mixed |
||
| 860 | */ |
||
| 861 | protected function getColumnNameByIndex($index) |
||
| 867 | |||
| 868 | /** |
||
| 869 | * If column name could not be resolved then use primary key. |
||
| 870 | * |
||
| 871 | * @return string |
||
| 872 | */ |
||
| 873 | protected function getPrimaryKeyName() |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Check if the engine used was eloquent. |
||
| 884 | * |
||
| 885 | * @return bool |
||
| 886 | */ |
||
| 887 | protected function isEloquent() |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Get column name from string. |
||
| 894 | * |
||
| 895 | * @param string $str |
||
| 896 | * @param bool $wantsAlias |
||
| 897 | * @return string |
||
| 898 | */ |
||
| 899 | protected function extractColumnName($str, $wantsAlias) |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Check if the current sql language is based on oracle syntax. |
||
| 920 | * |
||
| 921 | * @return bool |
||
| 922 | */ |
||
| 923 | protected function isOracleSql() |
||
| 927 | } |
||
| 928 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: