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 |
||
| 20 | abstract class BaseEngine implements DataTableEngineContract |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Datatables Request object. |
||
| 24 | * |
||
| 25 | * @var \Yajra\Datatables\Request |
||
| 26 | */ |
||
| 27 | public $request; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Database connection used. |
||
| 31 | * |
||
| 32 | * @var \Illuminate\Database\Connection |
||
| 33 | */ |
||
| 34 | protected $connection; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Builder object. |
||
| 38 | * |
||
| 39 | * @var \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
| 40 | */ |
||
| 41 | protected $query; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Query builder object. |
||
| 45 | * |
||
| 46 | * @var \Illuminate\Database\Query\Builder |
||
| 47 | */ |
||
| 48 | protected $builder; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var \Illuminate\Contracts\Logging\Log |
||
| 52 | */ |
||
| 53 | protected $logger; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Array of result columns/fields. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $columns = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $columnDef = [ |
||
| 68 | 'index' => false, |
||
| 69 | 'append' => [], |
||
| 70 | 'edit' => [], |
||
| 71 | 'excess' => ['rn', 'row_num'], |
||
| 72 | 'filter' => [], |
||
| 73 | 'order' => [], |
||
| 74 | 'escape' => '*', |
||
| 75 | 'raw' => ['action'], |
||
| 76 | 'blacklist' => ['password', 'remember_token'], |
||
| 77 | 'whitelist' => '*', |
||
| 78 | ]; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Query type. |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $query_type; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Extra/Added columns. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $extraColumns = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Total records. |
||
| 96 | * |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | protected $totalRecords = 0; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Total filtered records. |
||
| 103 | * |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | protected $filteredRecords = 0; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Auto-filter flag. |
||
| 110 | * |
||
| 111 | * @var bool |
||
| 112 | */ |
||
| 113 | protected $autoFilter = true; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Callback to override global search. |
||
| 117 | * |
||
| 118 | * @var callable |
||
| 119 | */ |
||
| 120 | protected $filterCallback; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Parameters to passed on filterCallback. |
||
| 124 | * |
||
| 125 | * @var mixed |
||
| 126 | */ |
||
| 127 | protected $filterCallbackParameters; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * DT row templates container. |
||
| 131 | * |
||
| 132 | * @var array |
||
| 133 | */ |
||
| 134 | protected $templates = [ |
||
| 135 | 'DT_RowId' => '', |
||
| 136 | 'DT_RowClass' => '', |
||
| 137 | 'DT_RowData' => [], |
||
| 138 | 'DT_RowAttr' => [], |
||
| 139 | ]; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Output transformer. |
||
| 143 | * |
||
| 144 | * @var \League\Fractal\TransformerAbstract |
||
| 145 | */ |
||
| 146 | protected $transformer = null; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Database prefix |
||
| 150 | * |
||
| 151 | * @var string |
||
| 152 | */ |
||
| 153 | protected $prefix; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Database driver used. |
||
| 157 | * |
||
| 158 | * @var string |
||
| 159 | */ |
||
| 160 | protected $database; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * [internal] Track if any filter was applied for at least one column |
||
| 164 | * |
||
| 165 | * @var boolean |
||
| 166 | */ |
||
| 167 | protected $isFilterApplied = false; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Fractal serializer class. |
||
| 171 | * |
||
| 172 | * @var string|null |
||
| 173 | */ |
||
| 174 | protected $serializer = null; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Custom ordering callback. |
||
| 178 | * |
||
| 179 | * @var callable |
||
| 180 | */ |
||
| 181 | protected $orderCallback; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Skip paginate as needed. |
||
| 185 | * |
||
| 186 | * @var bool |
||
| 187 | */ |
||
| 188 | protected $skipPaging = false; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Array of data to append on json response. |
||
| 192 | * |
||
| 193 | * @var array |
||
| 194 | */ |
||
| 195 | protected $appends = []; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Flag for ordering NULLS LAST option. |
||
| 199 | * |
||
| 200 | * @var bool |
||
| 201 | */ |
||
| 202 | protected $nullsLast = false; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Add column in collection. |
||
| 206 | * |
||
| 207 | * @param string $name |
||
| 208 | * @param string|callable $content |
||
| 209 | * @param bool|int $order |
||
| 210 | * @return $this |
||
| 211 | */ |
||
| 212 | public function addColumn($name, $content, $order = false) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Add DT row index column on response. |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function addIndexColumn() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Edit column's content. |
||
| 235 | * |
||
| 236 | * @param string $name |
||
| 237 | * @param string|callable $content |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | public function editColumn($name, $content) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Remove column from collection. |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | public function removeColumn() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Declare columns to escape values. |
||
| 262 | * |
||
| 263 | * @param string|array $columns |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | public function escapeColumns($columns = '*') |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Set columns that should not be escaped. |
||
| 275 | * |
||
| 276 | * @param array $columns |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | public function rawColumns(array $columns) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Allows previous API calls where the methods were snake_case. |
||
| 288 | * Will convert a camelCase API call to a snake_case call. |
||
| 289 | * Allow query builder method to be used by the engine. |
||
| 290 | * |
||
| 291 | * @param string $name |
||
| 292 | * @param array $arguments |
||
| 293 | * @return mixed |
||
| 294 | */ |
||
| 295 | public function __call($name, $arguments) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get Query Builder object. |
||
| 311 | * |
||
| 312 | * @param mixed $instance |
||
| 313 | * @return mixed |
||
| 314 | */ |
||
| 315 | public function getQueryBuilder($instance = null) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Check query type is a builder. |
||
| 330 | * |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | public function isQueryBuilder() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Sets DT_RowClass template. |
||
| 340 | * result: <tr class="output_from_your_template">. |
||
| 341 | * |
||
| 342 | * @param string|callable $content |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function setRowClass($content) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Sets DT_RowId template. |
||
| 354 | * result: <tr id="output_from_your_template">. |
||
| 355 | * |
||
| 356 | * @param string|callable $content |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | public function setRowId($content) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Set DT_RowData templates. |
||
| 368 | * |
||
| 369 | * @param array $data |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function setRowData(array $data) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Add DT_RowData template. |
||
| 381 | * |
||
| 382 | * @param string $key |
||
| 383 | * @param string|callable $value |
||
| 384 | * @return $this |
||
| 385 | */ |
||
| 386 | public function addRowData($key, $value) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set DT_RowAttr templates. |
||
| 395 | * result: <tr attr1="attr1" attr2="attr2">. |
||
| 396 | * |
||
| 397 | * @param array $data |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | public function setRowAttr(array $data) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Add DT_RowAttr template. |
||
| 409 | * |
||
| 410 | * @param string $key |
||
| 411 | * @param string|callable $value |
||
| 412 | * @return $this |
||
| 413 | */ |
||
| 414 | public function addRowAttr($key, $value) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Override default column filter search. |
||
| 423 | * |
||
| 424 | * @param string $column |
||
| 425 | * @param string|callable $method |
||
| 426 | * @return $this |
||
| 427 | * @internal param $mixed ...,... All the individual parameters required for specified $method |
||
| 428 | * @internal string $1 Special variable that returns the requested search keyword. |
||
| 429 | */ |
||
| 430 | public function filterColumn($column, $method) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Order each given columns versus the given custom sql. |
||
| 440 | * |
||
| 441 | * @param array $columns |
||
| 442 | * @param string $sql |
||
| 443 | * @param array $bindings |
||
| 444 | * @return $this |
||
| 445 | */ |
||
| 446 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Override default column ordering. |
||
| 457 | * |
||
| 458 | * @param string $column |
||
| 459 | * @param string $sql |
||
| 460 | * @param array $bindings |
||
| 461 | * @return $this |
||
| 462 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 463 | */ |
||
| 464 | public function orderColumn($column, $sql, $bindings = []) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Set data output transformer. |
||
| 473 | * |
||
| 474 | * @param \League\Fractal\TransformerAbstract $transformer |
||
| 475 | * @return $this |
||
| 476 | */ |
||
| 477 | public function setTransformer($transformer) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Set fractal serializer class. |
||
| 486 | * |
||
| 487 | * @param string $serializer |
||
| 488 | * @return $this |
||
| 489 | */ |
||
| 490 | public function setSerializer($serializer) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Organizes works. |
||
| 499 | * |
||
| 500 | * @param bool $mDataSupport |
||
| 501 | * @param bool $orderFirst |
||
| 502 | * @return \Illuminate\Http\JsonResponse |
||
| 503 | * @throws \Exception |
||
| 504 | */ |
||
| 505 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Sort records. |
||
| 538 | * |
||
| 539 | * @param boolean $skip |
||
| 540 | * @return void |
||
| 541 | */ |
||
| 542 | protected function orderRecords($skip) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Perform necessary filters. |
||
| 551 | * |
||
| 552 | * @return void |
||
| 553 | */ |
||
| 554 | protected function filterRecords() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Apply pagination. |
||
| 570 | * |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | protected function paginate() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Render json response. |
||
| 582 | * |
||
| 583 | * @param bool $object |
||
| 584 | * @return \Illuminate\Http\JsonResponse |
||
| 585 | */ |
||
| 586 | protected function render($object = false) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Get or create transformer serializer instance. |
||
| 632 | * |
||
| 633 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
| 634 | */ |
||
| 635 | protected function createSerializer() |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Get or create transformer instance. |
||
| 646 | * |
||
| 647 | * @return \League\Fractal\TransformerAbstract |
||
| 648 | */ |
||
| 649 | protected function createTransformer() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Get processed data |
||
| 660 | * |
||
| 661 | * @param bool|false $object |
||
| 662 | * @return array |
||
| 663 | */ |
||
| 664 | protected function getProcessedData($object = false) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Get columns definition. |
||
| 678 | * |
||
| 679 | * @return array |
||
| 680 | */ |
||
| 681 | protected function getColumnsDefinition() |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Check if app is in debug mode. |
||
| 691 | * |
||
| 692 | * @return bool |
||
| 693 | */ |
||
| 694 | public function isDebugging() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Append debug parameters on output. |
||
| 701 | * |
||
| 702 | * @param array $output |
||
| 703 | * @return array |
||
| 704 | */ |
||
| 705 | protected function showDebugger(array $output) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Get monolog/logger instance. |
||
| 715 | * |
||
| 716 | * @return \Illuminate\Contracts\Logging\Log |
||
| 717 | */ |
||
| 718 | public function getLogger() |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Set monolog/logger instance. |
||
| 727 | * |
||
| 728 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
| 729 | * @return $this |
||
| 730 | */ |
||
| 731 | public function setLogger(Log $logger) |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Get config is case insensitive status. |
||
| 740 | * |
||
| 741 | * @return bool |
||
| 742 | */ |
||
| 743 | public function isCaseInsensitive() |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Append data on json response. |
||
| 750 | * |
||
| 751 | * @param mixed $key |
||
| 752 | * @param mixed $value |
||
| 753 | * @return $this |
||
| 754 | */ |
||
| 755 | public function with($key, $value = '') |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Override default ordering method with a closure callback. |
||
| 770 | * |
||
| 771 | * @param callable $closure |
||
| 772 | * @return $this |
||
| 773 | */ |
||
| 774 | public function order(callable $closure) |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Update list of columns that is not allowed for search/sort. |
||
| 783 | * |
||
| 784 | * @param array $blacklist |
||
| 785 | * @return $this |
||
| 786 | */ |
||
| 787 | public function blacklist(array $blacklist) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Update list of columns that is allowed for search/sort. |
||
| 796 | * |
||
| 797 | * @param string|array $whitelist |
||
| 798 | * @return $this |
||
| 799 | */ |
||
| 800 | public function whitelist($whitelist = '*') |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Set smart search config at runtime. |
||
| 809 | * |
||
| 810 | * @param bool $bool |
||
| 811 | * @return $this |
||
| 812 | */ |
||
| 813 | public function smart($bool = true) |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Set total records manually. |
||
| 822 | * |
||
| 823 | * @param int $total |
||
| 824 | * @return $this |
||
| 825 | */ |
||
| 826 | public function setTotalRecords($total) |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Skip pagination as needed. |
||
| 835 | * |
||
| 836 | * @return $this |
||
| 837 | */ |
||
| 838 | public function skipPaging() |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Check if the current sql language is based on oracle syntax. |
||
| 847 | * |
||
| 848 | * @return bool |
||
| 849 | */ |
||
| 850 | public function isOracleSql() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Set datatables to do ordering with NULLS LAST option. |
||
| 857 | * |
||
| 858 | * @return $this |
||
| 859 | */ |
||
| 860 | public function orderByNullsLast() |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Push a new column name to blacklist |
||
| 869 | * |
||
| 870 | * @param string $column |
||
| 871 | * @return $this |
||
| 872 | */ |
||
| 873 | public function pushToBlacklist($column) |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Check if column is blacklisted. |
||
| 884 | * |
||
| 885 | * @param string $column |
||
| 886 | * @return bool |
||
| 887 | */ |
||
| 888 | protected function isBlacklisted($column) |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Setup search keyword. |
||
| 903 | * |
||
| 904 | * @param string $value |
||
| 905 | * @return string |
||
| 906 | */ |
||
| 907 | protected function setupKeyword($value) |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Check if DataTables uses smart search. |
||
| 925 | * |
||
| 926 | * @return bool |
||
| 927 | */ |
||
| 928 | public function isSmartSearch() |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Get config use wild card status. |
||
| 935 | * |
||
| 936 | * @return bool |
||
| 937 | */ |
||
| 938 | public function isWildcard() |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Adds % wildcards to the given string. |
||
| 945 | * |
||
| 946 | * @param string $str |
||
| 947 | * @param bool $lowercase |
||
| 948 | * @return string |
||
| 949 | */ |
||
| 950 | protected function wildcardLikeString($str, $lowercase = true) |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Update flags to disable global search |
||
| 970 | * |
||
| 971 | * @param callable $callback |
||
| 972 | * @param mixed $parameters |
||
| 973 | * @param bool $autoFilter |
||
| 974 | */ |
||
| 975 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Get column name to be use for filtering and sorting. |
||
| 985 | * |
||
| 986 | * @param integer $index |
||
| 987 | * @param bool $wantsAlias |
||
| 988 | * @return string |
||
| 989 | */ |
||
| 990 | protected function getColumnName($index, $wantsAlias = false) |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Get column name by order column index. |
||
| 1008 | * |
||
| 1009 | * @param int $index |
||
| 1010 | * @return mixed |
||
| 1011 | */ |
||
| 1012 | protected function getColumnNameByIndex($index) |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * If column name could not be resolved then use primary key. |
||
| 1021 | * |
||
| 1022 | * @return string |
||
| 1023 | */ |
||
| 1024 | protected function getPrimaryKeyName() |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Check if the engine used was eloquent. |
||
| 1035 | * |
||
| 1036 | * @return bool |
||
| 1037 | */ |
||
| 1038 | public function isEloquent() |
||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * Get column name from string. |
||
| 1045 | * |
||
| 1046 | * @param string $str |
||
| 1047 | * @param bool $wantsAlias |
||
| 1048 | * @return string |
||
| 1049 | */ |
||
| 1050 | protected function extractColumnName($str, $wantsAlias) |
||
| 1068 | } |
||
| 1069 |
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: