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 | * @var \Illuminate\Contracts\Logging\Log |
||
| 30 | */ |
||
| 31 | protected $logger; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Array of result columns/fields. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $columns = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $columnDef = [ |
||
| 46 | 'index' => false, |
||
| 47 | 'append' => [], |
||
| 48 | 'edit' => [], |
||
| 49 | 'excess' => ['rn', 'row_num'], |
||
| 50 | 'filter' => [], |
||
| 51 | 'order' => [], |
||
| 52 | 'escape' => '*', |
||
| 53 | 'raw' => ['action'], |
||
| 54 | 'blacklist' => ['password', 'remember_token'], |
||
| 55 | 'whitelist' => '*', |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Extra/Added columns. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $extraColumns = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Total records. |
||
| 67 | * |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | protected $totalRecords = 0; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Total filtered records. |
||
| 74 | * |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | protected $filteredRecords = 0; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Auto-filter flag. |
||
| 81 | * |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | protected $autoFilter = true; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Callback to override global search. |
||
| 88 | * |
||
| 89 | * @var callable |
||
| 90 | */ |
||
| 91 | protected $filterCallback; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Parameters to passed on filterCallback. |
||
| 95 | * |
||
| 96 | * @var mixed |
||
| 97 | */ |
||
| 98 | protected $filterCallbackParameters; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * DT row templates container. |
||
| 102 | * |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $templates = [ |
||
| 106 | 'DT_RowId' => '', |
||
| 107 | 'DT_RowClass' => '', |
||
| 108 | 'DT_RowData' => [], |
||
| 109 | 'DT_RowAttr' => [], |
||
| 110 | ]; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Output transformer. |
||
| 114 | * |
||
| 115 | * @var \League\Fractal\TransformerAbstract |
||
| 116 | */ |
||
| 117 | protected $transformer = null; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Database prefix. |
||
| 121 | * |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | protected $prefix; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Database driver used. |
||
| 128 | * |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | protected $database; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * [internal] Track if any filter was applied for at least one column. |
||
| 135 | * |
||
| 136 | * @var boolean |
||
| 137 | */ |
||
| 138 | protected $isFilterApplied = false; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Fractal serializer class. |
||
| 142 | * |
||
| 143 | * @var string|null |
||
| 144 | */ |
||
| 145 | protected $serializer = null; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Custom ordering callback. |
||
| 149 | * |
||
| 150 | * @var callable |
||
| 151 | */ |
||
| 152 | protected $orderCallback; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Skip paginate as needed. |
||
| 156 | * |
||
| 157 | * @var bool |
||
| 158 | */ |
||
| 159 | protected $skipPaging = false; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Array of data to append on json response. |
||
| 163 | * |
||
| 164 | * @var array |
||
| 165 | */ |
||
| 166 | protected $appends = []; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Flag for ordering NULLS LAST option. |
||
| 170 | * |
||
| 171 | * @var bool |
||
| 172 | */ |
||
| 173 | protected $nullsLast = false; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Add column in collection. |
||
| 177 | * |
||
| 178 | * @param string $name |
||
| 179 | * @param string|callable $content |
||
| 180 | * @param bool|int $order |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | public function addColumn($name, $content, $order = false) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Add DT row index column on response. |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | public function addIndexColumn() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Edit column's content. |
||
| 206 | * |
||
| 207 | * @param string $name |
||
| 208 | * @param string|callable $content |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function editColumn($name, $content) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Remove column from collection. |
||
| 220 | * |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | public function removeColumn() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Declare columns to escape values. |
||
| 233 | * |
||
| 234 | * @param string|array $columns |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | public function escapeColumns($columns = '*') |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set columns that should not be escaped. |
||
| 246 | * |
||
| 247 | * @param array $columns |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | public function rawColumns(array $columns) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Sets DT_RowClass template. |
||
| 259 | * result: <tr class="output_from_your_template">. |
||
| 260 | * |
||
| 261 | * @param string|callable $content |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | public function setRowClass($content) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Sets DT_RowId template. |
||
| 273 | * result: <tr id="output_from_your_template">. |
||
| 274 | * |
||
| 275 | * @param string|callable $content |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function setRowId($content) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Set DT_RowData templates. |
||
| 287 | * |
||
| 288 | * @param array $data |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | public function setRowData(array $data) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Add DT_RowData template. |
||
| 300 | * |
||
| 301 | * @param string $key |
||
| 302 | * @param string|callable $value |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | public function addRowData($key, $value) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Set DT_RowAttr templates. |
||
| 314 | * result: <tr attr1="attr1" attr2="attr2">. |
||
| 315 | * |
||
| 316 | * @param array $data |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | public function setRowAttr(array $data) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Add DT_RowAttr template. |
||
| 328 | * |
||
| 329 | * @param string $key |
||
| 330 | * @param string|callable $value |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | public function addRowAttr($key, $value) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Add custom filter handler for the give column. |
||
| 342 | * |
||
| 343 | * @param string $column |
||
| 344 | * @param callable $callback |
||
| 345 | * @return $this |
||
| 346 | */ |
||
| 347 | public function filterColumn($column, callable $callback) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Order each given columns versus the given custom sql. |
||
| 356 | * |
||
| 357 | * @param array $columns |
||
| 358 | * @param string $sql |
||
| 359 | * @param array $bindings |
||
| 360 | * @return $this |
||
| 361 | */ |
||
| 362 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Override default column ordering. |
||
| 373 | * |
||
| 374 | * @param string $column |
||
| 375 | * @param string $sql |
||
| 376 | * @param array $bindings |
||
| 377 | * @return $this |
||
| 378 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 379 | */ |
||
| 380 | public function orderColumn($column, $sql, $bindings = []) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Set data output transformer. |
||
| 389 | * |
||
| 390 | * @param \League\Fractal\TransformerAbstract $transformer |
||
| 391 | * @return $this |
||
| 392 | */ |
||
| 393 | public function setTransformer($transformer) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Set fractal serializer class. |
||
| 402 | * |
||
| 403 | * @param string $serializer |
||
| 404 | * @return $this |
||
| 405 | */ |
||
| 406 | public function setSerializer($serializer) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Organizes works. |
||
| 415 | * |
||
| 416 | * @param bool $mDataSupport |
||
| 417 | * @param bool $orderFirst |
||
| 418 | * @return \Illuminate\Http\JsonResponse |
||
| 419 | * @throws \Exception |
||
| 420 | */ |
||
| 421 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Sort records. |
||
| 454 | * |
||
| 455 | * @param boolean $skip |
||
| 456 | * @return void |
||
| 457 | */ |
||
| 458 | protected function orderRecords($skip) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Perform necessary filters. |
||
| 467 | * |
||
| 468 | * @return void |
||
| 469 | */ |
||
| 470 | protected function filterRecords() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Apply pagination. |
||
| 486 | * |
||
| 487 | * @return void |
||
| 488 | */ |
||
| 489 | protected function paginate() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Render json response. |
||
| 498 | * |
||
| 499 | * @param bool $object |
||
| 500 | * @return \Illuminate\Http\JsonResponse |
||
| 501 | */ |
||
| 502 | protected function render($object = false) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get or create transformer serializer instance. |
||
| 550 | * |
||
| 551 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
| 552 | */ |
||
| 553 | protected function createSerializer() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get or create transformer instance. |
||
| 564 | * |
||
| 565 | * @return \League\Fractal\TransformerAbstract |
||
| 566 | */ |
||
| 567 | protected function createTransformer() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Get processed data. |
||
| 578 | * |
||
| 579 | * @param bool|false $object |
||
| 580 | * @return array |
||
| 581 | */ |
||
| 582 | protected function getProcessedData($object = false) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Get columns definition. |
||
| 596 | * |
||
| 597 | * @return array |
||
| 598 | */ |
||
| 599 | protected function getColumnsDefinition() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Check if app is in debug mode. |
||
| 609 | * |
||
| 610 | * @return bool |
||
| 611 | */ |
||
| 612 | public function isDebugging() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Append debug parameters on output. |
||
| 619 | * |
||
| 620 | * @param array $output |
||
| 621 | * @return array |
||
| 622 | */ |
||
| 623 | protected function showDebugger(array $output) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get monolog/logger instance. |
||
| 633 | * |
||
| 634 | * @return \Illuminate\Contracts\Logging\Log |
||
| 635 | */ |
||
| 636 | public function getLogger() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Set monolog/logger instance. |
||
| 645 | * |
||
| 646 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
| 647 | * @return $this |
||
| 648 | */ |
||
| 649 | public function setLogger(Log $logger) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Check if config uses case insensitive search. |
||
| 658 | * |
||
| 659 | * @return bool |
||
| 660 | */ |
||
| 661 | public function isCaseInsensitive() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Append data on json response. |
||
| 668 | * |
||
| 669 | * @param mixed $key |
||
| 670 | * @param mixed $value |
||
| 671 | * @return $this |
||
| 672 | */ |
||
| 673 | public function with($key, $value = '') |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Override default ordering method with a closure callback. |
||
| 688 | * |
||
| 689 | * @param callable $closure |
||
| 690 | * @return $this |
||
| 691 | */ |
||
| 692 | public function order(callable $closure) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Update list of columns that is not allowed for search/sort. |
||
| 701 | * |
||
| 702 | * @param array $blacklist |
||
| 703 | * @return $this |
||
| 704 | */ |
||
| 705 | public function blacklist(array $blacklist) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Update list of columns that is allowed for search/sort. |
||
| 714 | * |
||
| 715 | * @param string|array $whitelist |
||
| 716 | * @return $this |
||
| 717 | */ |
||
| 718 | public function whitelist($whitelist = '*') |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Set smart search config at runtime. |
||
| 727 | * |
||
| 728 | * @param bool $bool |
||
| 729 | * @return $this |
||
| 730 | */ |
||
| 731 | public function smart($bool = true) |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Set total records manually. |
||
| 740 | * |
||
| 741 | * @param int $total |
||
| 742 | * @return $this |
||
| 743 | */ |
||
| 744 | public function setTotalRecords($total) |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Skip pagination as needed. |
||
| 753 | * |
||
| 754 | * @return $this |
||
| 755 | */ |
||
| 756 | public function skipPaging() |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Check if the current sql language is based on oracle syntax. |
||
| 765 | * |
||
| 766 | * @return bool |
||
| 767 | */ |
||
| 768 | public function isOracleSql() |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Set datatables to do ordering with NULLS LAST option. |
||
| 775 | * |
||
| 776 | * @return $this |
||
| 777 | */ |
||
| 778 | public function orderByNullsLast() |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Push a new column name to blacklist. |
||
| 787 | * |
||
| 788 | * @param string $column |
||
| 789 | * @return $this |
||
| 790 | */ |
||
| 791 | public function pushToBlacklist($column) |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Check if column is blacklisted. |
||
| 802 | * |
||
| 803 | * @param string $column |
||
| 804 | * @return bool |
||
| 805 | */ |
||
| 806 | protected function isBlacklisted($column) |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Setup search keyword. |
||
| 821 | * |
||
| 822 | * @param string $value |
||
| 823 | * @return string |
||
| 824 | */ |
||
| 825 | protected function setupKeyword($value) |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Check if config uses smart search. |
||
| 843 | * |
||
| 844 | * @return bool |
||
| 845 | */ |
||
| 846 | public function isSmartSearch() |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Check if config uses wild card search. |
||
| 853 | * |
||
| 854 | * @return bool |
||
| 855 | */ |
||
| 856 | public function isWildcard() |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Adds % wildcards to the given string. |
||
| 863 | * |
||
| 864 | * @param string $str |
||
| 865 | * @param bool $lowercase |
||
| 866 | * @return string |
||
| 867 | */ |
||
| 868 | protected function wildcardLikeString($str, $lowercase = true) |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Update flags to disable global search. |
||
| 888 | * |
||
| 889 | * @param callable $callback |
||
| 890 | * @param mixed $parameters |
||
| 891 | * @param bool $autoFilter |
||
| 892 | */ |
||
| 893 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Get column name to be use for filtering and sorting. |
||
| 903 | * |
||
| 904 | * @param integer $index |
||
| 905 | * @param bool $wantsAlias |
||
| 906 | * @return string |
||
| 907 | */ |
||
| 908 | protected function getColumnName($index, $wantsAlias = false) |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Get column name by order column index. |
||
| 926 | * |
||
| 927 | * @param int $index |
||
| 928 | * @return mixed |
||
| 929 | */ |
||
| 930 | protected function getColumnNameByIndex($index) |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Get column name from string. |
||
| 939 | * |
||
| 940 | * @param string $str |
||
| 941 | * @param bool $wantsAlias |
||
| 942 | * @return string |
||
| 943 | */ |
||
| 944 | protected function extractColumnName($str, $wantsAlias) |
||
| 962 | } |
||
| 963 |
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: