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 DataTableEngine |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Datatables Request object. |
||
| 24 | * |
||
| 25 | * @var \Yajra\Datatables\Request |
||
| 26 | */ |
||
| 27 | public $request; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \Illuminate\Contracts\Logging\Log |
||
| 31 | */ |
||
| 32 | protected $logger; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Array of result columns/fields. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $columns = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * DT columns definitions container (add/edit/remove/filter/order/escape). |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $columnDef = [ |
||
| 47 | 'index' => false, |
||
| 48 | 'append' => [], |
||
| 49 | 'edit' => [], |
||
| 50 | 'filter' => [], |
||
| 51 | 'order' => [], |
||
| 52 | ]; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Extra/Added columns. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $extraColumns = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Total records. |
||
| 63 | * |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | protected $totalRecords = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Total filtered records. |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | protected $filteredRecords = 0; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Auto-filter flag. |
||
| 77 | * |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | protected $autoFilter = true; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Callback to override global search. |
||
| 84 | * |
||
| 85 | * @var callable |
||
| 86 | */ |
||
| 87 | protected $filterCallback; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Parameters to passed on filterCallback. |
||
| 91 | * |
||
| 92 | * @var mixed |
||
| 93 | */ |
||
| 94 | protected $filterCallbackParameters; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * DT row templates container. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $templates = [ |
||
| 102 | 'DT_RowId' => '', |
||
| 103 | 'DT_RowClass' => '', |
||
| 104 | 'DT_RowData' => [], |
||
| 105 | 'DT_RowAttr' => [], |
||
| 106 | ]; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Output transformer. |
||
| 110 | * |
||
| 111 | * @var \League\Fractal\TransformerAbstract |
||
| 112 | */ |
||
| 113 | protected $transformer = null; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * [internal] Track if any filter was applied for at least one column. |
||
| 117 | * |
||
| 118 | * @var boolean |
||
| 119 | */ |
||
| 120 | protected $isFilterApplied = false; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Fractal serializer class. |
||
| 124 | * |
||
| 125 | * @var string|null |
||
| 126 | */ |
||
| 127 | protected $serializer = null; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Custom ordering callback. |
||
| 131 | * |
||
| 132 | * @var callable |
||
| 133 | */ |
||
| 134 | protected $orderCallback; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Skip paginate as needed. |
||
| 138 | * |
||
| 139 | * @var bool |
||
| 140 | */ |
||
| 141 | protected $skipPaging = false; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Array of data to append on json response. |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $appends = []; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Flag for ordering NULLS LAST option. |
||
| 152 | * |
||
| 153 | * @var bool |
||
| 154 | */ |
||
| 155 | protected $nullsLast = false; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var \Yajra\Datatables\Config |
||
| 159 | */ |
||
| 160 | protected $config; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Add column in collection. |
||
| 164 | * |
||
| 165 | * @param string $name |
||
| 166 | * @param string|callable $content |
||
| 167 | * @param bool|int $order |
||
| 168 | * @return $this |
||
| 169 | */ |
||
| 170 | public function addColumn($name, $content, $order = false) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Add DT row index column on response. |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function addIndexColumn() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Edit column's content. |
||
| 193 | * |
||
| 194 | * @param string $name |
||
| 195 | * @param string|callable $content |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function editColumn($name, $content) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Remove column from collection. |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function removeColumn() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Declare columns to escape values. |
||
| 220 | * |
||
| 221 | * @param string|array $columns |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | public function escapeColumns($columns = '*') |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set columns that should not be escaped. |
||
| 233 | * |
||
| 234 | * @param array $columns |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | public function rawColumns(array $columns) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Sets DT_RowClass template. |
||
| 246 | * result: <tr class="output_from_your_template">. |
||
| 247 | * |
||
| 248 | * @param string|callable $content |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | public function setRowClass($content) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Sets DT_RowId template. |
||
| 260 | * result: <tr id="output_from_your_template">. |
||
| 261 | * |
||
| 262 | * @param string|callable $content |
||
| 263 | * @return $this |
||
| 264 | */ |
||
| 265 | public function setRowId($content) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set DT_RowData templates. |
||
| 274 | * |
||
| 275 | * @param array $data |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function setRowData(array $data) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Add DT_RowData template. |
||
| 287 | * |
||
| 288 | * @param string $key |
||
| 289 | * @param string|callable $value |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function addRowData($key, $value) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set DT_RowAttr templates. |
||
| 301 | * result: <tr attr1="attr1" attr2="attr2">. |
||
| 302 | * |
||
| 303 | * @param array $data |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | public function setRowAttr(array $data) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Add DT_RowAttr template. |
||
| 315 | * |
||
| 316 | * @param string $key |
||
| 317 | * @param string|callable $value |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function addRowAttr($key, $value) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Add custom filter handler for the give column. |
||
| 329 | * |
||
| 330 | * @param string $column |
||
| 331 | * @param callable $callback |
||
| 332 | * @return $this |
||
| 333 | */ |
||
| 334 | public function filterColumn($column, callable $callback) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Order each given columns versus the given custom sql. |
||
| 343 | * |
||
| 344 | * @param array $columns |
||
| 345 | * @param string $sql |
||
| 346 | * @param array $bindings |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function orderColumns(array $columns, $sql, $bindings = []) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Override default column ordering. |
||
| 360 | * |
||
| 361 | * @param string $column |
||
| 362 | * @param string $sql |
||
| 363 | * @param array $bindings |
||
| 364 | * @return $this |
||
| 365 | * @internal string $1 Special variable that returns the requested order direction of the column. |
||
| 366 | */ |
||
| 367 | public function orderColumn($column, $sql, $bindings = []) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Set data output transformer. |
||
| 376 | * |
||
| 377 | * @param \League\Fractal\TransformerAbstract $transformer |
||
| 378 | * @return $this |
||
| 379 | */ |
||
| 380 | public function setTransformer($transformer) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Set fractal serializer class. |
||
| 389 | * |
||
| 390 | * @param string $serializer |
||
| 391 | * @return $this |
||
| 392 | */ |
||
| 393 | public function setSerializer($serializer) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Append data on json response. |
||
| 402 | * |
||
| 403 | * @param mixed $key |
||
| 404 | * @param mixed $value |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | public function with($key, $value = '') |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Override default ordering method with a closure callback. |
||
| 422 | * |
||
| 423 | * @param callable $closure |
||
| 424 | * @return $this |
||
| 425 | */ |
||
| 426 | public function order(callable $closure) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Update list of columns that is not allowed for search/sort. |
||
| 435 | * |
||
| 436 | * @param array $blacklist |
||
| 437 | * @return $this |
||
| 438 | */ |
||
| 439 | public function blacklist(array $blacklist) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Update list of columns that is allowed for search/sort. |
||
| 448 | * |
||
| 449 | * @param string|array $whitelist |
||
| 450 | * @return $this |
||
| 451 | */ |
||
| 452 | public function whitelist($whitelist = '*') |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Set smart search config at runtime. |
||
| 461 | * |
||
| 462 | * @param bool $bool |
||
| 463 | * @return $this |
||
| 464 | */ |
||
| 465 | public function smart($bool = true) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Set total records manually. |
||
| 474 | * |
||
| 475 | * @param int $total |
||
| 476 | * @return $this |
||
| 477 | */ |
||
| 478 | public function setTotalRecords($total) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Skip pagination as needed. |
||
| 487 | * |
||
| 488 | * @return $this |
||
| 489 | */ |
||
| 490 | public function skipPaging() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Set datatables to do ordering with NULLS LAST option. |
||
| 499 | * |
||
| 500 | * @return $this |
||
| 501 | */ |
||
| 502 | public function orderByNullsLast() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Push a new column name to blacklist. |
||
| 511 | * |
||
| 512 | * @param string $column |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | public function pushToBlacklist($column) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Check if column is blacklisted. |
||
| 526 | * |
||
| 527 | * @param string $column |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | protected function isBlacklisted($column) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get columns definition. |
||
| 547 | * |
||
| 548 | * @return array |
||
| 549 | */ |
||
| 550 | protected function getColumnsDefinition() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Perform necessary filters. |
||
| 560 | * |
||
| 561 | * @return void |
||
| 562 | */ |
||
| 563 | protected function filterRecords() |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Perform global search. |
||
| 579 | * |
||
| 580 | * @return void |
||
| 581 | */ |
||
| 582 | public function filtering() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Perform multi-term search by splitting keyword into |
||
| 597 | * individual words and searches for each of them. |
||
| 598 | * |
||
| 599 | * @param string $keyword |
||
| 600 | */ |
||
| 601 | protected function smartGlobalSearch($keyword) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Perform global search for the given keyword. |
||
| 612 | * |
||
| 613 | * @param string $keyword |
||
| 614 | */ |
||
| 615 | abstract protected function globalSearch($keyword); |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Apply pagination. |
||
| 619 | * |
||
| 620 | * @return void |
||
| 621 | */ |
||
| 622 | protected function paginate() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Transform output. |
||
| 631 | * |
||
| 632 | * @param mixed $output |
||
| 633 | * @return array |
||
| 634 | */ |
||
| 635 | protected function transform($output) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Get processed data. |
||
| 646 | * |
||
| 647 | * @param bool|false $object |
||
| 648 | * @return array |
||
| 649 | */ |
||
| 650 | protected function getProcessedData($object = false) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Render json response. |
||
| 664 | * |
||
| 665 | * @param array $data |
||
| 666 | * @return \Illuminate\Http\JsonResponse |
||
| 667 | */ |
||
| 668 | protected function render(array $data) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Append debug parameters on output. |
||
| 691 | * |
||
| 692 | * @param array $output |
||
| 693 | * @return array |
||
| 694 | */ |
||
| 695 | abstract protected function showDebugger(array $output); |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Return an error json response. |
||
| 699 | * |
||
| 700 | * @param \Exception $exception |
||
| 701 | * @return \Illuminate\Http\JsonResponse |
||
| 702 | * @throws \Yajra\Datatables\Exception |
||
| 703 | */ |
||
| 704 | protected function errorResponse(\Exception $exception) |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Get monolog/logger instance. |
||
| 724 | * |
||
| 725 | * @return \Illuminate\Contracts\Logging\Log |
||
| 726 | */ |
||
| 727 | public function getLogger() |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Set monolog/logger instance. |
||
| 736 | * |
||
| 737 | * @param \Illuminate\Contracts\Logging\Log $logger |
||
| 738 | * @return $this |
||
| 739 | */ |
||
| 740 | public function setLogger(Log $logger) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Setup search keyword. |
||
| 749 | * |
||
| 750 | * @param string $value |
||
| 751 | * @return string |
||
| 752 | */ |
||
| 753 | protected function setupKeyword($value) |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Adds % wildcards to the given string. |
||
| 771 | * |
||
| 772 | * @param string $str |
||
| 773 | * @param bool $lowercase |
||
| 774 | * @return string |
||
| 775 | */ |
||
| 776 | protected function wildcardLikeString($str, $lowercase = true) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Update flags to disable global search. |
||
| 796 | * |
||
| 797 | * @param callable $callback |
||
| 798 | * @param mixed $parameters |
||
| 799 | * @param bool $autoFilter |
||
| 800 | */ |
||
| 801 | protected function overrideGlobalSearch(callable $callback, $parameters, $autoFilter = false) |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Get column name to be use for filtering and sorting. |
||
| 811 | * |
||
| 812 | * @param integer $index |
||
| 813 | * @param bool $wantsAlias |
||
| 814 | * @return string |
||
| 815 | */ |
||
| 816 | protected function getColumnName($index, $wantsAlias = false) |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Get column name by order column index. |
||
| 834 | * |
||
| 835 | * @param int $index |
||
| 836 | * @return string |
||
| 837 | */ |
||
| 838 | protected function getColumnNameByIndex($index) |
||
| 844 | |||
| 845 | /** |
||
| 846 | * If column name could not be resolved then use primary key. |
||
| 847 | * |
||
| 848 | * @return string |
||
| 849 | */ |
||
| 850 | protected function getPrimaryKeyName() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Get column name from string. |
||
| 857 | * |
||
| 858 | * @param string $str |
||
| 859 | * @param bool $wantsAlias |
||
| 860 | * @return string |
||
| 861 | */ |
||
| 862 | protected function extractColumnName($str, $wantsAlias) |
||
| 880 | } |
||
| 881 |