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 |
||
| 26 | class Grid |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * The grid data model instance. |
||
| 30 | * |
||
| 31 | * @var \Encore\Admin\Grid\Model |
||
| 32 | */ |
||
| 33 | protected $model; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Collection of all grid columns. |
||
| 37 | * |
||
| 38 | * @var \Illuminate\Support\Collection |
||
| 39 | */ |
||
| 40 | protected $columns; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Collection of all data rows. |
||
| 44 | * |
||
| 45 | * @var \Illuminate\Support\Collection |
||
| 46 | */ |
||
| 47 | protected $rows; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Rows callable fucntion. |
||
| 51 | * |
||
| 52 | * @var \Closure |
||
| 53 | */ |
||
| 54 | protected $rowsCallback; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * All column names of the grid. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | public $columnNames = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Grid builder. |
||
| 65 | * |
||
| 66 | * @var \Closure |
||
| 67 | */ |
||
| 68 | protected $builder; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Mark if the grid is builded. |
||
| 72 | * |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | protected $builded = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * All variables in grid view. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $variables = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The grid Filter. |
||
| 86 | * |
||
| 87 | * @var \Encore\Admin\Grid\Filter |
||
| 88 | */ |
||
| 89 | protected $filter; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Resource path of the grid. |
||
| 93 | * |
||
| 94 | * @var |
||
| 95 | */ |
||
| 96 | protected $resourcePath; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Default primary key name. |
||
| 100 | * |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | protected $keyName = 'id'; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Allow batch deletion. |
||
| 107 | * |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | protected $allowBatchDeletion = true; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Allow creation. |
||
| 114 | * |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | protected $allowCreation = true; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Allow actions. |
||
| 121 | * |
||
| 122 | * @var bool |
||
| 123 | */ |
||
| 124 | protected $allowActions = true; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Allow export data. |
||
| 128 | * |
||
| 129 | * @var bool |
||
| 130 | */ |
||
| 131 | protected $allowExport = true; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Is grid rows orderable. |
||
| 135 | * |
||
| 136 | * @var bool |
||
| 137 | */ |
||
| 138 | protected $orderable = false; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var Exporter |
||
| 142 | */ |
||
| 143 | protected $exporter; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Create a new grid instance. |
||
| 147 | * |
||
| 148 | * @param Eloquent $model |
||
| 149 | * @param callable $builder |
||
| 150 | */ |
||
| 151 | public function __construct(Eloquent $model, Closure $builder) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get primary key name of model. |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getKeyName() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Add column to Grid. |
||
| 175 | * |
||
| 176 | * @param string $name |
||
| 177 | * @param string $label |
||
| 178 | * |
||
| 179 | * @return Column |
||
| 180 | */ |
||
| 181 | public function column($name, $label = '') |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Batch add column to grid. |
||
| 205 | * |
||
| 206 | * @example |
||
| 207 | * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]); |
||
| 208 | * 2.$grid->columns('name', 'email' ...) |
||
| 209 | * |
||
| 210 | * @param array $columns |
||
| 211 | * |
||
| 212 | * @return Collection|void |
||
| 213 | */ |
||
| 214 | public function columns($columns = []) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Add column to grid. |
||
| 235 | * |
||
| 236 | * @param string $column |
||
| 237 | * @param string $label |
||
| 238 | * |
||
| 239 | * @return Column |
||
| 240 | */ |
||
| 241 | protected function addColumn($column = '', $label = '') |
||
| 248 | |||
| 249 | public function blank($label) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get Grid model. |
||
| 256 | * |
||
| 257 | * @return Model |
||
| 258 | */ |
||
| 259 | public function model() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Paginate the grid. |
||
| 266 | * |
||
| 267 | * @param int $perPage |
||
| 268 | * |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | public function paginate($perPage = null) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the grid paginator. |
||
| 278 | * |
||
| 279 | * @return mixed |
||
| 280 | */ |
||
| 281 | public function paginator() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Build the grid. |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | public function build() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Process the grid filter. |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function processFilter() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Build the grid rows. |
||
| 328 | * |
||
| 329 | * @param array $data |
||
| 330 | * |
||
| 331 | * @return void |
||
| 332 | */ |
||
| 333 | protected function buildRows(array $data) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set grid row callback function. |
||
| 351 | * |
||
| 352 | * @param callable $callable |
||
| 353 | * |
||
| 354 | * @return Collection|void |
||
| 355 | */ |
||
| 356 | public function rows(Closure $callable = null) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Setup grid filter. |
||
| 367 | * |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | protected function setupFilter() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Setup grid exporter. |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | protected function setupExporter() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Export url. |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public function exportUrl() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * If allow batch delete. |
||
| 404 | * |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | public function allowBatchDeletion() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Disable batch deletion. |
||
| 414 | */ |
||
| 415 | public function disableBatchDeletion() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Disable creation. |
||
| 422 | */ |
||
| 423 | public function disableCreation() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * If allow creation. |
||
| 430 | * |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | public function allowCreation() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * If allow actions. |
||
| 440 | * |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | public function allowActions() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Disable all actions. |
||
| 450 | */ |
||
| 451 | public function disableActions() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * If grid allows export.s. |
||
| 458 | * |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function allowExport() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Disable export. |
||
| 468 | */ |
||
| 469 | public function disableExport() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Set grid as orderable. |
||
| 476 | * |
||
| 477 | * @return $this |
||
| 478 | */ |
||
| 479 | public function orderable() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Is the grid orderable. |
||
| 488 | * |
||
| 489 | * @return bool |
||
| 490 | */ |
||
| 491 | public function isOrderable() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Set the grid filter. |
||
| 498 | * |
||
| 499 | * @param callable $callback |
||
| 500 | */ |
||
| 501 | public function filter(Closure $callback) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Render the grid filter. |
||
| 508 | * |
||
| 509 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 510 | */ |
||
| 511 | public function renderFilter() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get current resource uri. |
||
| 518 | * |
||
| 519 | * @param string $path |
||
| 520 | * |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public function resource($path = null) |
||
| 537 | |||
| 538 | public function pathOfCreate() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Add variables to grid view. |
||
| 549 | * |
||
| 550 | * @param array $variables |
||
| 551 | * |
||
| 552 | * @return $this |
||
| 553 | */ |
||
| 554 | public function with($variables = []) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get all variables will used in grid view. |
||
| 563 | * |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | protected function variables() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get the string contents of the grid view. |
||
| 575 | * |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public function render() |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Dynamically add columns to the grid view. |
||
| 591 | * |
||
| 592 | * @param $method |
||
| 593 | * @param $arguments |
||
| 594 | * |
||
| 595 | * @return $this|Column |
||
| 596 | */ |
||
| 597 | public function __call($method, $arguments) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Js code for grid. |
||
| 627 | * |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | public function script() |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Get the string contents of the grid view. |
||
| 706 | * |
||
| 707 | * @return string |
||
| 708 | */ |
||
| 709 | public function __toString() |
||
| 715 | } |
||
| 716 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: