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 |
||
| 19 | abstract class BaseEngine implements DataTableEngineContract |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Datatables Request object. |
||
| 23 | * |
||
| 24 | * @var \Yajra\Datatables\Request |
||
| 25 | */ |
||
| 26 | public $request; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Database connection used. |
||
| 30 | * |
||
| 31 | * @var \Illuminate\Database\Connection |
||
| 32 | */ |
||
| 33 | protected $connection; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Builder object. |
||
| 37 | * |
||
| 38 | * @var \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
| 39 | */ |
||
| 40 | protected $query; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Query builder object. |
||
| 44 | * |
||
| 45 | * @var \Illuminate\Database\Query\Builder |
||
| 46 | */ |
||
| 47 | protected $builder; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Array of result columns/fields. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $columns = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $columnDef = [ |
||
| 62 | 'index' => false, |
||
| 63 | 'append' => [], |
||
| 64 | 'edit' => [], |
||
| 65 | 'excess' => ['rn', 'row_num'], |
||
| 66 | 'filter' => [], |
||
| 67 | 'order' => [], |
||
| 68 | 'escape' => [], |
||
| 69 | 'blacklist' => ['password', 'remember_token'], |
||
| 70 | 'whitelist' => '*', |
||
| 71 | ]; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Query type. |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $query_type; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Extra/Added columns. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $extraColumns = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Total records. |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | protected $totalRecords = 0; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Total filtered records. |
||
| 96 | * |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | protected $filteredRecords = 0; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Auto-filter flag. |
||
| 103 | * |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | protected $autoFilter = true; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Select trashed records in count function for models with soft deletes trait. |
||
| 110 | * By default we do not select soft deleted records |
||
| 111 | * |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | protected $withTrashed = false; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Callback to override global search. |
||
| 118 | * |
||
| 119 | * @var callable |
||
| 120 | */ |
||
| 121 | protected $filterCallback; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Parameters to passed on filterCallback. |
||
| 125 | * |
||
| 126 | * @var mixed |
||
| 127 | */ |
||
| 128 | protected $filterCallbackParameters; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * DT row templates container. |
||
| 132 | * |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | protected $templates = [ |
||
| 136 | 'DT_RowId' => '', |
||
| 137 | 'DT_RowClass' => '', |
||
| 138 | 'DT_RowData' => [], |
||
| 139 | 'DT_RowAttr' => [], |
||
| 140 | ]; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Output transformer. |
||
| 144 | * |
||
| 145 | * @var \League\Fractal\TransformerAbstract |
||
| 146 | */ |
||
| 147 | protected $transformer = null; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Database prefix |
||
| 151 | * |
||
| 152 | * @var string |
||
| 153 | */ |
||
| 154 | protected $prefix; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Database driver used. |
||
| 158 | * |
||
| 159 | * @var string |
||
| 160 | */ |
||
| 161 | protected $database; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * [internal] Track if any filter was applied for at least one column |
||
| 165 | * |
||
| 166 | * @var boolean |
||
| 167 | */ |
||
| 168 | protected $isFilterApplied = false; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Fractal serializer class. |
||
| 172 | * |
||
| 173 | * @var string|null |
||
| 174 | */ |
||
| 175 | protected $serializer = null; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Custom ordering callback. |
||
| 179 | * |
||
| 180 | * @var callable |
||
| 181 | */ |
||
| 182 | protected $orderCallback; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Skip paginate as needed. |
||
| 186 | * |
||
| 187 | * @var bool |
||
| 188 | */ |
||
| 189 | protected $skipPaging = false; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Array of data to append on json response. |
||
| 193 | * |
||
| 194 | * @var array |
||
| 195 | */ |
||
| 196 | protected $appends = []; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Flag for ordering NULLS LAST option. |
||
| 200 | * |
||
| 201 | * @var bool |
||
| 202 | */ |
||
| 203 | protected $nullsLast = false; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Add column in collection. |
||
| 207 | * |
||
| 208 | * @param string $name |
||
| 209 | * @param string|callable $content |
||
| 210 | * @param bool|int $order |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function addColumn($name, $content, $order = false) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Add DT row index column on response. |
||
| 224 | * |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function addIndexColumn() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Edit column's content. |
||
| 236 | * |
||
| 237 | * @param string $name |
||
| 238 | * @param string|callable $content |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | public function editColumn($name, $content) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Remove column from collection. |
||
| 250 | * |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | public function removeColumn() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Declare columns to escape values. |
||
| 263 | * |
||
| 264 | * @param string|array $columns |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function escapeColumns($columns = '*') |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Change withTrashed flag value. |
||
| 276 | * |
||
| 277 | * @param bool $withTrashed |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function withTrashed($withTrashed = true) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Allows previous API calls where the methods were snake_case. |
||
| 289 | * Will convert a camelCase API call to a snake_case call. |
||
| 290 | * Allow query builder method to be used by the engine. |
||
| 291 | * |
||
| 292 | * @param string $name |
||
| 293 | * @param array $arguments |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | public function __call($name, $arguments) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get Query Builder object. |
||
| 312 | * |
||
| 313 | * @param mixed $instance |
||
| 314 | * @return mixed |
||
| 315 | */ |
||
| 316 | public function getQueryBuilder($instance = null) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Check query type is a builder. |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function isQueryBuilder() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Sets DT_RowClass template. |
||
| 341 | * result: <tr class="output_from_your_template">. |
||
| 342 | * |
||
| 343 | * @param string|callable $content |
||
| 344 | * @return $this |
||
| 345 | */ |
||
| 346 | public function setRowClass($content) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Sets DT_RowId template. |
||
| 355 | * result: <tr id="output_from_your_template">. |
||
| 356 | * |
||
| 357 | * @param string|callable $content |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | public function setRowId($content) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set DT_RowData templates. |
||
| 369 | * |
||
| 370 | * @param array $data |
||
| 371 | * @return $this |
||
| 372 | */ |
||
| 373 | public function setRowData(array $data) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Add DT_RowData template. |
||
| 382 | * |
||
| 383 | * @param string $key |
||
| 384 | * @param string|callable $value |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | public function addRowData($key, $value) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Set DT_RowAttr templates. |
||
| 396 | * result: <tr attr1="attr1" attr2="attr2">. |
||
| 397 | * |
||
| 398 | * @param array $data |
||
| 399 | * @return $this |
||
| 400 | */ |
||
| 401 | public function setRowAttr(array $data) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Add DT_RowAttr template. |
||
| 410 | * |
||
| 411 | * @param string $key |
||
| 412 | * @param string|callable $value |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | public function addRowAttr($key, $value) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Override default column filter search. |
||
| 424 | * |
||
| 425 | * @param string $column |
||
| 426 | * @param string|callable $method |
||
| 427 | * @return $this |
||
| 428 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
| 429 | * @internal string $1 Special variable that returns the requested search keyword. |
||
| 430 | */ |
||
| 431 | public function filterColumn($column, $method) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Order each given columns versus the given custom sql. |
||
| 441 | * |
||
| 442 | * @param array $columns |
||
| 443 | * @param string $sql |
||
| 444 | * @param array $bindings |
||
| 445 | * @return $this |
||
| 446 | */ |
||
| 447 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Override default column ordering. |
||
| 458 | * |
||
| 459 | * @param string $column |
||
| 460 | * @param string $sql |
||
| 461 | * @param array $bindings |
||
| 462 | * @return $this |
||
| 463 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 464 | */ |
||
| 465 | public function orderColumn($column, $sql, $bindings = []) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Set data output transformer. |
||
| 474 | * |
||
| 475 | * @param \League\Fractal\TransformerAbstract $transformer |
||
| 476 | * @return $this |
||
| 477 | */ |
||
| 478 | public function setTransformer($transformer) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Set fractal serializer class. |
||
| 487 | * |
||
| 488 | * @param string $serializer |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | public function setSerializer($serializer) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Organizes works. |
||
| 500 | * |
||
| 501 | * @param bool $mDataSupport |
||
| 502 | * @param bool $orderFirst |
||
| 503 | * @return \Illuminate\Http\JsonResponse |
||
| 504 | */ |
||
| 505 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Sort records. |
||
| 521 | * |
||
| 522 | * @param boolean $skip |
||
| 523 | * @return void |
||
| 524 | */ |
||
| 525 | protected function orderRecords($skip) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Perform necessary filters. |
||
| 534 | * |
||
| 535 | * @return void |
||
| 536 | */ |
||
| 537 | protected function filterRecords() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Apply pagination. |
||
| 553 | * |
||
| 554 | * @return void |
||
| 555 | */ |
||
| 556 | protected function paginate() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Render json response. |
||
| 565 | * |
||
| 566 | * @param bool $object |
||
| 567 | * @return \Illuminate\Http\JsonResponse |
||
| 568 | */ |
||
| 569 | protected function render($object = false) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Get or create transformer serializer instance. |
||
| 615 | * |
||
| 616 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
| 617 | */ |
||
| 618 | protected function createSerializer() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Get or create transformer instance. |
||
| 629 | * |
||
| 630 | * @return \League\Fractal\TransformerAbstract |
||
| 631 | */ |
||
| 632 | protected function createTransformer() |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Get processed data |
||
| 643 | * |
||
| 644 | * @param bool|false $object |
||
| 645 | * @return array |
||
| 646 | */ |
||
| 647 | protected function getProcessedData($object = false) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Check if app is in debug mode. |
||
| 661 | * |
||
| 662 | * @return bool |
||
| 663 | */ |
||
| 664 | public function isDebugging() |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Append debug parameters on output. |
||
| 671 | * |
||
| 672 | * @param array $output |
||
| 673 | * @return array |
||
| 674 | */ |
||
| 675 | protected function showDebugger(array $output) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Get config is case insensitive status. |
||
| 685 | * |
||
| 686 | * @return bool |
||
| 687 | */ |
||
| 688 | public function isCaseInsensitive() |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Append data on json response. |
||
| 695 | * |
||
| 696 | * @param mixed $key |
||
| 697 | * @param mixed $value |
||
| 698 | * @return $this |
||
| 699 | */ |
||
| 700 | public function with($key, $value = '') |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Override default ordering method with a closure callback. |
||
| 715 | * |
||
| 716 | * @param callable $closure |
||
| 717 | * @return $this |
||
| 718 | */ |
||
| 719 | public function order(callable $closure) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Update list of columns that is not allowed for search/sort. |
||
| 728 | * |
||
| 729 | * @param array $blacklist |
||
| 730 | * @return $this |
||
| 731 | */ |
||
| 732 | public function blacklist(array $blacklist) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Update list of columns that is not allowed for search/sort. |
||
| 741 | * |
||
| 742 | * @param string|array $whitelist |
||
| 743 | * @return $this |
||
| 744 | */ |
||
| 745 | public function whitelist($whitelist = '*') |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Set smart search config at runtime. |
||
| 754 | * |
||
| 755 | * @param bool $bool |
||
| 756 | * @return $this |
||
| 757 | */ |
||
| 758 | public function smart($bool = true) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Set total records manually. |
||
| 767 | * |
||
| 768 | * @param int $total |
||
| 769 | * @return $this |
||
| 770 | */ |
||
| 771 | public function setTotalRecords($total) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Skip pagination as needed. |
||
| 780 | * |
||
| 781 | * @return $this |
||
| 782 | */ |
||
| 783 | public function skipPaging() |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Check if the current sql language is based on oracle syntax. |
||
| 792 | * |
||
| 793 | * @return bool |
||
| 794 | */ |
||
| 795 | public function isOracleSql() |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Set datatables to do ordering with NULLS LAST option. |
||
| 802 | * |
||
| 803 | * @return $this |
||
| 804 | */ |
||
| 805 | public function orderByNullsLast() |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Setup search keyword. |
||
| 814 | * |
||
| 815 | * @param string $value |
||
| 816 | * @return string |
||
| 817 | */ |
||
| 818 | protected function setupKeyword($value) |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Check if DataTables uses smart search. |
||
| 836 | * |
||
| 837 | * @return bool |
||
| 838 | */ |
||
| 839 | public function isSmartSearch() |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Get config use wild card status. |
||
| 846 | * |
||
| 847 | * @return bool |
||
| 848 | */ |
||
| 849 | public function isWildcard() |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Adds % wildcards to the given string. |
||
| 856 | * |
||
| 857 | * @param string $str |
||
| 858 | * @param bool $lowercase |
||
| 859 | * @return string |
||
| 860 | */ |
||
| 861 | protected function wildcardLikeString($str, $lowercase = true) |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Update flags to disable global search |
||
| 879 | * |
||
| 880 | * @param callable $callback |
||
| 881 | * @param mixed $parameters |
||
| 882 | * @param bool $autoFilter |
||
| 883 | */ |
||
| 884 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Check if column is blacklisted. |
||
| 894 | * |
||
| 895 | * @param string $column |
||
| 896 | * @return bool |
||
| 897 | */ |
||
| 898 | protected function isBlacklisted($column) |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Get column name to be use for filtering and sorting. |
||
| 913 | * |
||
| 914 | * @param integer $index |
||
| 915 | * @param bool $wantsAlias |
||
| 916 | * @return string |
||
| 917 | */ |
||
| 918 | protected function getColumnName($index, $wantsAlias = false) |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Get column name by order column index. |
||
| 936 | * |
||
| 937 | * @param int $index |
||
| 938 | * @return mixed |
||
| 939 | */ |
||
| 940 | protected function getColumnNameByIndex($index) |
||
| 946 | |||
| 947 | /** |
||
| 948 | * If column name could not be resolved then use primary key. |
||
| 949 | * |
||
| 950 | * @return string |
||
| 951 | */ |
||
| 952 | protected function getPrimaryKeyName() |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Check if the engine used was eloquent. |
||
| 963 | * |
||
| 964 | * @return bool |
||
| 965 | */ |
||
| 966 | public function isEloquent() |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Get column name from string. |
||
| 973 | * |
||
| 974 | * @param string $str |
||
| 975 | * @param bool $wantsAlias |
||
| 976 | * @return string |
||
| 977 | */ |
||
| 978 | protected function extractColumnName($str, $wantsAlias) |
||
| 996 | } |
||
| 997 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: