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 |
||
| 51 | class Grid extends Components\Container |
||
| 52 | 1 | { |
|
| 53 | /***** DEFAULTS ****/ |
||
| 54 | const BUTTONS = 'buttons'; |
||
| 55 | |||
| 56 | const CLIENT_SIDE_OPTIONS = 'grido-options'; |
||
| 57 | |||
| 58 | /** @var int @persistent */ |
||
| 59 | public $page = 1; |
||
| 60 | |||
| 61 | /** @var int @persistent */ |
||
| 62 | public $perPage; |
||
| 63 | |||
| 64 | /** @var array @persistent */ |
||
| 65 | public $sort = []; |
||
| 66 | |||
| 67 | 1 | /** @var array @persistent */ |
|
| 68 | public $filter = []; |
||
| 69 | |||
| 70 | /** @var array event on all grid's components registered */ |
||
| 71 | public $onRegistered; |
||
| 72 | |||
| 73 | /** @var array event on render */ |
||
| 74 | public $onRender; |
||
| 75 | |||
| 76 | /** @var array event for modifying data */ |
||
| 77 | public $onFetchData; |
||
| 78 | |||
| 79 | /** @var callback returns tr html element; function($row, Html $tr) */ |
||
| 80 | protected $rowCallback; |
||
| 81 | |||
| 82 | /** @var \Nette\Utils\Html */ |
||
| 83 | protected $tablePrototype; |
||
| 84 | |||
| 85 | /** @var bool */ |
||
| 86 | protected $rememberState = FALSE; |
||
| 87 | |||
| 88 | /** @var string */ |
||
| 89 | protected $rememberStateSectionName; |
||
| 90 | |||
| 91 | /** @var string */ |
||
| 92 | protected $primaryKey = 'id'; |
||
| 93 | |||
| 94 | /** @var string */ |
||
| 95 | protected $filterRenderType; |
||
| 96 | |||
| 97 | /** @var array */ |
||
| 98 | protected $perPageList = [10, 20, 30, 50, 100]; |
||
| 99 | |||
| 100 | /** @var int */ |
||
| 101 | protected $defaultPerPage = 20; |
||
| 102 | |||
| 103 | /** @var array */ |
||
| 104 | protected $defaultFilter = []; |
||
| 105 | |||
| 106 | /** @var array */ |
||
| 107 | protected $defaultSort = []; |
||
| 108 | |||
| 109 | /** @var DataSources\IDataSource */ |
||
| 110 | protected $model; |
||
| 111 | |||
| 112 | /** @var int total count of items */ |
||
| 113 | protected $count; |
||
| 114 | |||
| 115 | /** @var mixed */ |
||
| 116 | protected $data; |
||
| 117 | |||
| 118 | /** @var Paginator */ |
||
| 119 | protected $paginator; |
||
| 120 | |||
| 121 | /** @var \Nette\Localization\ITranslator */ |
||
| 122 | protected $translator; |
||
| 123 | |||
| 124 | /** @var PropertyAccessor */ |
||
| 125 | protected $propertyAccessor; |
||
| 126 | |||
| 127 | /** @var bool */ |
||
| 128 | protected $strictMode = TRUE; |
||
| 129 | |||
| 130 | /** @var array */ |
||
| 131 | protected $options = [ |
||
| 132 | self::CLIENT_SIDE_OPTIONS => [] |
||
| 133 | ]; |
||
| 134 | |||
| 135 | /** @var Customization */ |
||
| 136 | protected $customization; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Grid constructor. |
||
| 140 | */ |
||
| 141 | public function __construct() |
||
| 151 | 1 | ||
| 152 | /** |
||
| 153 | * Sets a model that implements the interface Grido\DataSources\IDataSource or data-source object. |
||
| 154 | * @param mixed $model |
||
| 155 | * @param bool $forceWrapper |
||
| 156 | * @throws Exception |
||
| 157 | * @return Grid |
||
| 158 | */ |
||
| 159 | public function setModel($model, $forceWrapper = FALSE) |
||
| 167 | 1 | ||
| 168 | 1 | /** |
|
| 169 | 1 | * Sets the default number of items per page. |
|
| 170 | * @param int $perPage |
||
| 171 | * @return Grid |
||
| 172 | */ |
||
| 173 | public function setDefaultPerPage($perPage) |
||
| 185 | |||
| 186 | /** |
||
| 187 | 1 | * Sets default filtering. |
|
| 188 | * @param array $filter |
||
| 189 | * @return Grid |
||
| 190 | */ |
||
| 191 | 1 | public function setDefaultFilter(array $filter) |
|
| 196 | 1 | ||
| 197 | /** |
||
| 198 | * Sets default sorting. |
||
| 199 | 1 | * @param array $sort |
|
| 200 | 1 | * @return Grid |
|
| 201 | * @throws Exception |
||
| 202 | 1 | */ |
|
| 203 | 1 | public function setDefaultSort(array $sort) |
|
| 218 | 1 | ||
| 219 | /** |
||
| 220 | 1 | * Sets items to per-page select. |
|
| 221 | * @param array $perPageList |
||
| 222 | * @return Grid |
||
| 223 | */ |
||
| 224 | public function setPerPageList(array $perPageList) |
||
| 234 | |||
| 235 | /** |
||
| 236 | 1 | * Sets translator. |
|
| 237 | * @param \Nette\Localization\ITranslator $translator |
||
| 238 | * @return Grid |
||
| 239 | */ |
||
| 240 | public function setTranslator(\Nette\Localization\ITranslator $translator) |
||
| 245 | |||
| 246 | 1 | /** |
|
| 247 | 1 | * Sets type of filter rendering. |
|
| 248 | * Defaults inner (Filter::RENDER_INNER) if column does not exist then outer filter (Filter::RENDER_OUTER). |
||
| 249 | * @param string $type |
||
| 250 | * @throws Exception |
||
| 251 | * @return Grid |
||
| 252 | */ |
||
| 253 | public function setFilterRenderType($type) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Sets custom paginator. |
||
| 266 | * @param Paginator $paginator |
||
| 267 | * @return Grid |
||
| 268 | */ |
||
| 269 | 1 | public function setPaginator(Paginator $paginator) |
|
| 274 | |||
| 275 | 1 | /** |
|
| 276 | * Sets grid primary key. |
||
| 277 | * Defaults is "id". |
||
| 278 | * @param string $key |
||
| 279 | * @return Grid |
||
| 280 | 1 | */ |
|
| 281 | 1 | public function setPrimaryKey($key) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Sets file name of custom template. |
||
| 289 | * @param string $file |
||
| 290 | * @return Grid |
||
| 291 | */ |
||
| 292 | public function setTemplateFile($file) |
||
| 301 | 1 | ||
| 302 | /** |
||
| 303 | * Sets saving state to session. |
||
| 304 | * @param bool $state |
||
| 305 | * @param string $sectionName |
||
| 306 | * @return Grid |
||
| 307 | */ |
||
| 308 | public function setRememberState($state = TRUE, $sectionName = NULL) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Sets callback for customizing tr html object. |
||
| 320 | * Callback returns tr html element; function($row, Html $tr). |
||
| 321 | * @param $callback |
||
| 322 | * @return Grid |
||
| 323 | 1 | */ |
|
| 324 | 1 | public function setRowCallback($callback) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Sets client-side options. |
||
| 332 | * @param array $options |
||
| 333 | * @return Grid |
||
| 334 | 1 | */ |
|
| 335 | 1 | public function setClientSideOptions(array $options) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Determines whether any user error will cause a notice. |
||
| 343 | 1 | * @param bool $mode |
|
| 344 | 1 | * @return \Grido\Grid |
|
| 345 | */ |
||
| 346 | public function setStrictMode($mode) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param \Grido\Customization $customization |
||
| 354 | 1 | */ |
|
| 355 | 1 | public function setCustomization(Customization $customization) |
|
| 359 | |||
| 360 | /**********************************************************************************************/ |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Returns total count of data. |
||
| 364 | * @return int |
||
| 365 | */ |
||
| 366 | public function getCount() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Returns default per page. |
||
| 377 | * @return int |
||
| 378 | */ |
||
| 379 | public function getDefaultPerPage() |
||
| 387 | |||
| 388 | /** |
||
| 389 | 1 | * Returns default filter. |
|
| 390 | * @return array |
||
| 391 | 1 | */ |
|
| 392 | public function getDefaultFilter() |
||
| 396 | |||
| 397 | /** |
||
| 398 | 1 | * Returns default sort. |
|
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | public function getDefaultSort() |
||
| 405 | |||
| 406 | /** |
||
| 407 | 1 | * Returns list of possible items per page. |
|
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | public function getPerPageList() |
||
| 414 | |||
| 415 | /** |
||
| 416 | 1 | * Returns primary key. |
|
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | 1 | public function getPrimaryKey() |
|
| 423 | |||
| 424 | /** |
||
| 425 | 1 | * Returns remember state. |
|
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public function getRememberState() |
||
| 432 | |||
| 433 | /** |
||
| 434 | 1 | * Returns row callback. |
|
| 435 | 1 | * @return callback |
|
| 436 | 1 | */ |
|
| 437 | public function getRowCallback() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Returns items per page. |
||
| 444 | * @return int |
||
| 445 | */ |
||
| 446 | 1 | public function getPerPage() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Returns actual filter values. |
||
| 455 | * @param string $key |
||
| 456 | * @return mixed |
||
| 457 | */ |
||
| 458 | public function getActualFilter($key = NULL) |
||
| 463 | |||
| 464 | 1 | /** |
|
| 465 | 1 | * Returns fetched data. |
|
| 466 | 1 | * @param bool $applyPaging |
|
| 467 | 1 | * @param bool $useCache |
|
| 468 | * @param bool $fetch |
||
| 469 | 1 | * @throws Exception |
|
| 470 | 1 | * @return array|DataSources\IDataSource|\Nette\Database\Table\Selection |
|
| 471 | 1 | */ |
|
| 472 | public function getData($applyPaging = TRUE, $useCache = TRUE, $fetch = TRUE) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Returns translator. |
||
| 512 | * @return Translations\FileTranslator |
||
| 513 | */ |
||
| 514 | public function getTranslator() |
||
| 522 | |||
| 523 | 1 | /** |
|
| 524 | 1 | * Returns remember session for set expiration, etc. |
|
| 525 | 1 | * @param bool $forceStart - if TRUE, session will be started if not |
|
| 526 | * @return \Nette\Http\SessionSection|NULL |
||
| 527 | */ |
||
| 528 | public function getRememberSession($forceStart = FALSE) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns table html element of grid. |
||
| 544 | * @return \Nette\Utils\Html |
||
| 545 | */ |
||
| 546 | public function getTablePrototype() |
||
| 555 | |||
| 556 | 1 | /** |
|
| 557 | 1 | * @return string |
|
| 558 | 1 | * @internal |
|
| 559 | 1 | */ |
|
| 560 | 1 | public function getFilterRenderType() |
|
| 581 | |||
| 582 | 1 | /** |
|
| 583 | 1 | * @return DataSources\IDataSource |
|
| 584 | 1 | */ |
|
| 585 | 1 | public function getModel() |
|
| 589 | |||
| 590 | /** |
||
| 591 | * @return Paginator |
||
| 592 | * @internal |
||
| 593 | */ |
||
| 594 | public function getPaginator() |
||
| 604 | 1 | ||
| 605 | 1 | /** |
|
| 606 | 1 | * A simple wrapper around symfony/property-access with Nette Database dot notation support. |
|
| 607 | * @param array|object $object |
||
| 608 | 1 | * @param string $name |
|
| 609 | * @return mixed |
||
| 610 | * @internal |
||
| 611 | 1 | */ |
|
| 612 | 1 | public function getProperty($object, $name) |
|
| 631 | |||
| 632 | /** |
||
| 633 | * @return PropertyAccessor |
||
| 634 | * @internal |
||
| 635 | */ |
||
| 636 | public function getPropertyAccessor() |
||
| 644 | 1 | ||
| 645 | 1 | /** |
|
| 646 | * @param mixed $row item from db |
||
| 647 | 1 | * @return \Nette\Utils\Html |
|
| 648 | 1 | * @internal |
|
| 649 | 1 | */ |
|
| 650 | public function getRowPrototype($row) |
||
| 667 | |||
| 668 | 1 | /** |
|
| 669 | * Returns client-side options. |
||
| 670 | * @return array |
||
| 671 | */ |
||
| 672 | public function getClientSideOptions() |
||
| 676 | 1 | ||
| 677 | 1 | /** |
|
| 678 | 1 | * @return bool |
|
| 679 | */ |
||
| 680 | 1 | public function isStrictMode() |
|
| 684 | |||
| 685 | /** |
||
| 686 | * @return Customization |
||
| 687 | */ |
||
| 688 | public function getCustomization() |
||
| 696 | 1 | ||
| 697 | 1 | /**********************************************************************************************/ |
|
| 698 | 1 | ||
| 699 | /** |
||
| 700 | 1 | * Loads state informations. |
|
| 701 | 1 | * @param array $params |
|
| 702 | * @internal |
||
| 703 | */ |
||
| 704 | public function loadState(array $params) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Saves state informations for next request. |
||
| 719 | * @param array $params |
||
| 720 | * @param \Nette\Application\UI\PresenterComponentReflection $reflection (internal, used by Presenter) |
||
| 721 | * @internal |
||
| 722 | */ |
||
| 723 | public function saveState(array &$params, $reflection = NULL) |
||
| 728 | |||
| 729 | /** |
||
| 730 | 1 | * Ajax method. |
|
| 731 | * @internal |
||
| 732 | */ |
||
| 733 | public function handleRefresh() |
||
| 737 | |||
| 738 | /** |
||
| 739 | 1 | * @param int $page |
|
| 740 | 1 | * @internal |
|
| 741 | */ |
||
| 742 | public function handlePage($page) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * @param array $sort |
||
| 749 | 1 | * @internal |
|
| 750 | 1 | */ |
|
| 751 | 1 | public function handleSort(array $sort) |
|
| 756 | 1 | ||
| 757 | 1 | /** |
|
| 758 | 1 | * @param \Nette\Forms\Controls\SubmitButton $button |
|
| 759 | 1 | * @internal |
|
| 760 | */ |
||
| 761 | public function handleFilter(\Nette\Forms\Controls\SubmitButton $button) |
||
| 781 | |||
| 782 | 1 | /** |
|
| 783 | * @param \Nette\Forms\Controls\SubmitButton $button |
||
| 784 | 1 | * @internal |
|
| 785 | 1 | */ |
|
| 786 | public function handleReset(\Nette\Forms\Controls\SubmitButton $button) |
||
| 801 | |||
| 802 | /** |
||
| 803 | * @param \Nette\Forms\Controls\SubmitButton $button |
||
| 804 | * @internal |
||
| 805 | */ |
||
| 806 | public function handlePerPage(\Nette\Forms\Controls\SubmitButton $button) |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Refresh wrapper. |
||
| 819 | * @return void |
||
| 820 | * @internal |
||
| 821 | */ |
||
| 822 | public function reload() |
||
| 831 | |||
| 832 | /**********************************************************************************************/ |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @return \Nette\Templating\FileTemplate |
||
| 836 | * @internal |
||
| 837 | */ |
||
| 838 | public function createTemplate() |
||
| 846 | 1 | ||
| 847 | 1 | /** |
|
| 848 | 1 | * @internal |
|
| 849 | * @throws Exception |
||
| 850 | 1 | */ |
|
| 851 | public function render() |
||
| 894 | 1 | ||
| 895 | 1 | protected function saveRememberState() |
|
| 905 | 1 | ||
| 906 | 1 | protected function applyFiltering() |
|
| 911 | |||
| 912 | /** |
||
| 913 | * @param array $filter |
||
| 914 | * @return array |
||
| 915 | * @internal |
||
| 916 | */ |
||
| 917 | 1 | public function __getConditions(array $filter) |
|
| 944 | 1 | ||
| 945 | 1 | protected function applySorting() |
|
| 984 | |||
| 985 | protected function applyPaging() |
||
| 998 | |||
| 999 | 1 | protected function createComponentForm($name) |
|
| 1017 | |||
| 1018 | 1 | /** |
|
| 1019 | 1 | * @return array |
|
| 1020 | 1 | */ |
|
| 1021 | protected function getItemsForCountSelect() |
||
| 1025 | 1 | ||
| 1026 | /** |
||
| 1027 | * @internal |
||
| 1028 | * @param string $message |
||
| 1029 | */ |
||
| 1030 | public function __triggerUserNotice($message) |
||
| 1038 | } |
||
| 1039 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.