Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Grid 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 Grid, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Grid |
||
| 24 | { |
||
| 25 | use Concerns\HasElementNames, |
||
| 26 | Concerns\HasHeader, |
||
| 27 | Concerns\HasFooter, |
||
| 28 | Concerns\HasFilter, |
||
| 29 | Concerns\HasTools, |
||
| 30 | Concerns\HasTotalRow, |
||
| 31 | Concerns\HasHotKeys, |
||
| 32 | Concerns\HasQuickCreate, |
||
| 33 | Concerns\HasActions, |
||
| 34 | Concerns\CanHidesColumns, |
||
| 35 | Concerns\CanFixColumns, |
||
| 36 | Macroable { |
||
| 37 | __call as macroCall; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The grid data model instance. |
||
| 42 | * |
||
| 43 | * @var \Encore\Admin\Grid\Model |
||
| 44 | */ |
||
| 45 | protected $model; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Collection of all grid columns. |
||
| 49 | * |
||
| 50 | * @var \Illuminate\Support\Collection |
||
| 51 | */ |
||
| 52 | protected $columns; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Collection of all data rows. |
||
| 56 | * |
||
| 57 | * @var \Illuminate\Support\Collection |
||
| 58 | */ |
||
| 59 | protected $rows; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Rows callable fucntion. |
||
| 63 | * |
||
| 64 | * @var \Closure |
||
| 65 | */ |
||
| 66 | protected $rowsCallback; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * All column names of the grid. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | public $columnNames = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Grid builder. |
||
| 77 | * |
||
| 78 | * @var \Closure |
||
| 79 | */ |
||
| 80 | protected $builder; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Mark if the grid is builded. |
||
| 84 | * |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | protected $builded = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * All variables in grid view. |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | protected $variables = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Resource path of the grid. |
||
| 98 | * |
||
| 99 | * @var |
||
| 100 | */ |
||
| 101 | protected $resourcePath; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Default primary key name. |
||
| 105 | * |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | protected $keyName = 'id'; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Export driver. |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | protected $exporter; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * View for grid to render. |
||
| 119 | * |
||
| 120 | * @var string |
||
| 121 | */ |
||
| 122 | protected $view = 'admin::grid.table'; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Per-page options. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | public $perPages = [10, 20, 30, 50, 100]; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Default items count per-page. |
||
| 133 | * |
||
| 134 | * @var int |
||
| 135 | */ |
||
| 136 | public $perPage = 20; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var []callable |
||
| 140 | */ |
||
| 141 | protected $renderingCallbacks = []; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Options for grid. |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $options = [ |
||
| 149 | 'show_pagination' => true, |
||
| 150 | 'show_tools' => true, |
||
| 151 | 'show_filter' => true, |
||
| 152 | 'show_exporter' => true, |
||
| 153 | 'show_actions' => true, |
||
| 154 | 'show_row_selector' => true, |
||
| 155 | 'show_create_btn' => true, |
||
| 156 | 'show_column_selector' => true, |
||
| 157 | ]; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var string |
||
| 161 | */ |
||
| 162 | public $tableID; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Initialization closure array. |
||
| 166 | * |
||
| 167 | * @var []Closure |
||
| 168 | */ |
||
| 169 | protected static $initCallbacks = []; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Create a new grid instance. |
||
| 173 | * |
||
| 174 | * @param Eloquent $model |
||
| 175 | * @param Closure $builder |
||
| 176 | */ |
||
| 177 | public function __construct(Eloquent $model, Closure $builder = null) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Initialize. |
||
| 192 | */ |
||
| 193 | protected function initialize() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Initialize with user pre-defined default disables and exporter, etc. |
||
| 206 | * |
||
| 207 | * @param Closure $callback |
||
| 208 | */ |
||
| 209 | public static function init(Closure $callback = null) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Call the initialization closure array in sequence. |
||
| 216 | */ |
||
| 217 | protected function callInitCallbacks() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Handle export request. |
||
| 230 | * |
||
| 231 | * @param bool $forceExport |
||
| 232 | */ |
||
| 233 | protected function handleExportRequest($forceExport = false) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $scope |
||
| 259 | * |
||
| 260 | * @return AbstractExporter |
||
| 261 | */ |
||
| 262 | protected function getExporter($scope) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get or set option for grid. |
||
| 269 | * |
||
| 270 | * @param string $key |
||
| 271 | * @param mixed $value |
||
| 272 | * |
||
| 273 | * @return $this|mixed |
||
| 274 | */ |
||
| 275 | public function option($key, $value = null) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get primary key name of model. |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getKeyName() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Add a column to Grid. |
||
| 298 | * |
||
| 299 | * @param string $name |
||
| 300 | * @param string $label |
||
| 301 | * |
||
| 302 | * @return Column |
||
| 303 | */ |
||
| 304 | public function column($name, $label = '') |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Batch add column to grid. |
||
| 319 | * |
||
| 320 | * @example |
||
| 321 | * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]); |
||
| 322 | * 2.$grid->columns('name', 'email' ...) |
||
| 323 | * |
||
| 324 | * @param array $columns |
||
| 325 | * |
||
| 326 | * @return Collection|null |
||
| 327 | */ |
||
| 328 | public function columns($columns = []) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Add column to grid. |
||
| 349 | * |
||
| 350 | * @param string $column |
||
| 351 | * @param string $label |
||
| 352 | * |
||
| 353 | * @return Column |
||
| 354 | */ |
||
| 355 | View Code Duplication | protected function addColumn($column = '', $label = '') |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Add a relation column to grid. |
||
| 367 | * |
||
| 368 | * @param string $name |
||
| 369 | * @param string $label |
||
| 370 | * |
||
| 371 | * @return $this|bool|Column |
||
| 372 | */ |
||
| 373 | protected function addRelationColumn($name, $label = '') |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Add a json type column to grid. |
||
| 396 | * |
||
| 397 | * @param string $name |
||
| 398 | * @param string $label |
||
| 399 | * |
||
| 400 | * @return Column |
||
| 401 | */ |
||
| 402 | protected function addJsonColumn($name, $label = '') |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Prepend column to grid. |
||
| 413 | * |
||
| 414 | * @param string $column |
||
| 415 | * @param string $label |
||
| 416 | * |
||
| 417 | * @return Column |
||
| 418 | */ |
||
| 419 | View Code Duplication | protected function prependColumn($column = '', $label = '') |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Get Grid model. |
||
| 431 | * |
||
| 432 | * @return Model |
||
| 433 | */ |
||
| 434 | public function model() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Paginate the grid. |
||
| 441 | * |
||
| 442 | * @param int $perPage |
||
| 443 | * |
||
| 444 | * @return void |
||
| 445 | */ |
||
| 446 | public function paginate($perPage = 20) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Get the grid paginator. |
||
| 455 | * |
||
| 456 | * @return mixed |
||
| 457 | */ |
||
| 458 | public function paginator() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Disable grid pagination. |
||
| 465 | * |
||
| 466 | * @return $this |
||
| 467 | */ |
||
| 468 | public function disablePagination(bool $disable = true) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * If this grid use pagination. |
||
| 477 | * |
||
| 478 | * @return bool |
||
| 479 | */ |
||
| 480 | public function showPagination() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Set per-page options. |
||
| 487 | * |
||
| 488 | * @param array $perPages |
||
| 489 | */ |
||
| 490 | public function perPages(array $perPages) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Disable row selector. |
||
| 497 | * |
||
| 498 | * @return Grid|mixed |
||
| 499 | */ |
||
| 500 | public function disableRowSelector(bool $disable = true) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Prepend checkbox column for grid. |
||
| 507 | * |
||
| 508 | * @return void |
||
| 509 | */ |
||
| 510 | protected function prependRowSelectorColumn() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Apply column filter to grid query. |
||
| 525 | * |
||
| 526 | * @return void |
||
| 527 | */ |
||
| 528 | protected function applyColumnFilter() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Build the grid. |
||
| 535 | * |
||
| 536 | * @return void |
||
| 537 | */ |
||
| 538 | public function build() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Build the grid rows. |
||
| 570 | * |
||
| 571 | * @param array $data |
||
| 572 | * |
||
| 573 | * @return void |
||
| 574 | */ |
||
| 575 | protected function buildRows(array $data) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Set grid row callback function. |
||
| 588 | * |
||
| 589 | * @param Closure $callable |
||
| 590 | * |
||
| 591 | * @return Collection|null |
||
| 592 | */ |
||
| 593 | public function rows(Closure $callable = null) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Set exporter driver for Grid to export. |
||
| 604 | * |
||
| 605 | * @param $exporter |
||
| 606 | * |
||
| 607 | * @return $this |
||
| 608 | */ |
||
| 609 | public function exporter($exporter) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get the export url. |
||
| 618 | * |
||
| 619 | * @param int $scope |
||
| 620 | * @param null $args |
||
| 621 | * |
||
| 622 | * @return string |
||
| 623 | */ |
||
| 624 | public function getExportUrl($scope = 1, $args = null) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Get create url. |
||
| 637 | * |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | public function getCreateUrl() |
||
| 653 | |||
| 654 | /** |
||
| 655 | * If grid show export btn. |
||
| 656 | * |
||
| 657 | * @return bool |
||
| 658 | */ |
||
| 659 | public function showExportBtn() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Disable export. |
||
| 666 | * |
||
| 667 | * @return $this |
||
| 668 | */ |
||
| 669 | public function disableExport(bool $disable = true) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Render export button. |
||
| 676 | * |
||
| 677 | * @return string |
||
| 678 | */ |
||
| 679 | public function renderExportButton() |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Alias for method `disableCreateButton`. |
||
| 686 | * |
||
| 687 | * @return $this |
||
| 688 | * |
||
| 689 | * @deprecated |
||
| 690 | */ |
||
| 691 | public function disableCreation() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Remove create button on grid. |
||
| 698 | * |
||
| 699 | * @return $this |
||
| 700 | */ |
||
| 701 | public function disableCreateButton(bool $disable = true) |
||
| 705 | |||
| 706 | /** |
||
| 707 | * If allow creation. |
||
| 708 | * |
||
| 709 | * @return bool |
||
| 710 | */ |
||
| 711 | public function showCreateBtn() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Render create button for grid. |
||
| 718 | * |
||
| 719 | * @return string |
||
| 720 | */ |
||
| 721 | public function renderCreateButton() |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Get current resource uri. |
||
| 728 | * |
||
| 729 | * @param string $path |
||
| 730 | * |
||
| 731 | * @return string |
||
| 732 | */ |
||
| 733 | public function resource($path = null) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Handle get mutator column for grid. |
||
| 750 | * |
||
| 751 | * @param string $method |
||
| 752 | * @param string $label |
||
| 753 | * |
||
| 754 | * @return bool|Column |
||
| 755 | */ |
||
| 756 | protected function handleGetMutatorColumn($method, $label) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Handle relation column for grid. |
||
| 767 | * |
||
| 768 | * @param string $method |
||
| 769 | * @param string $label |
||
| 770 | * |
||
| 771 | * @return bool|Column |
||
| 772 | */ |
||
| 773 | protected function handleRelationColumn($method, $label) |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Dynamically add columns to the grid view. |
||
| 809 | * |
||
| 810 | * @param $method |
||
| 811 | * @param $arguments |
||
| 812 | * |
||
| 813 | * @return Column |
||
| 814 | */ |
||
| 815 | public function __call($method, $arguments) |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Add variables to grid view. |
||
| 840 | * |
||
| 841 | * @param array $variables |
||
| 842 | * |
||
| 843 | * @return $this |
||
| 844 | */ |
||
| 845 | public function with($variables = []) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Get all variables will used in grid view. |
||
| 854 | * |
||
| 855 | * @return array |
||
| 856 | */ |
||
| 857 | protected function variables() |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Set a view to render. |
||
| 866 | * |
||
| 867 | * @param string $view |
||
| 868 | * @param array $variables |
||
| 869 | */ |
||
| 870 | public function setView($view, $variables = []) |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Set grid title. |
||
| 881 | * |
||
| 882 | * @param string $title |
||
| 883 | * |
||
| 884 | * @return $this |
||
| 885 | */ |
||
| 886 | public function setTitle($title) |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Set relation for grid. |
||
| 895 | * |
||
| 896 | * @param Relations\Relation $relation |
||
| 897 | * |
||
| 898 | * @return $this |
||
| 899 | */ |
||
| 900 | public function setRelation(Relations\Relation $relation) |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Set resource path for grid. |
||
| 909 | * |
||
| 910 | * @param string $path |
||
| 911 | * |
||
| 912 | * @return $this |
||
| 913 | */ |
||
| 914 | public function setResource($path) |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Set rendering callback. |
||
| 923 | * |
||
| 924 | * @param callable $callback |
||
| 925 | * |
||
| 926 | * @return $this |
||
| 927 | */ |
||
| 928 | public function rendering(callable $callback) |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Call callbacks before render. |
||
| 937 | * |
||
| 938 | * @return void |
||
| 939 | */ |
||
| 940 | protected function callRenderingCallback() |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Get the string contents of the grid view. |
||
| 949 | * |
||
| 950 | * @return string |
||
| 951 | */ |
||
| 952 | public function render() |
||
| 966 | } |
||
| 967 |