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 | /** @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 | 1 | ||
| 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 | * Sets a model that implements the interface Grido\DataSources\IDataSource or data-source object. |
||
| 140 | * @param mixed $model |
||
| 141 | * @param bool $forceWrapper |
||
| 142 | 1 | * @throws Exception |
|
| 143 | * @return Grid |
||
| 144 | */ |
||
| 145 | public function setModel($model, $forceWrapper = FALSE) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Sets the default number of items per page. |
||
| 156 | * @param int $perPage |
||
| 157 | * @return Grid |
||
| 158 | */ |
||
| 159 | public function setDefaultPerPage($perPage) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Sets default filtering. |
||
| 174 | * @param array $filter |
||
| 175 | * @return Grid |
||
| 176 | */ |
||
| 177 | public function setDefaultFilter(array $filter) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Sets default sorting. |
||
| 185 | * @param array $sort |
||
| 186 | * @return Grid |
||
| 187 | 1 | * @throws Exception |
|
| 188 | */ |
||
| 189 | public function setDefaultSort(array $sort) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Sets items to per-page select. |
||
| 207 | * @param array $perPageList |
||
| 208 | 1 | * @return Grid |
|
| 209 | */ |
||
| 210 | public function setPerPageList(array $perPageList) |
||
| 220 | 1 | ||
| 221 | /** |
||
| 222 | * Sets translator. |
||
| 223 | * @param \Nette\Localization\ITranslator $translator |
||
| 224 | * @return Grid |
||
| 225 | */ |
||
| 226 | public function setTranslator(\Nette\Localization\ITranslator $translator) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Sets type of filter rendering. |
||
| 234 | * Defaults inner (Filter::RENDER_INNER) if column does not exist then outer filter (Filter::RENDER_OUTER). |
||
| 235 | * @param string $type |
||
| 236 | 1 | * @throws Exception |
|
| 237 | * @return Grid |
||
| 238 | */ |
||
| 239 | public function setFilterRenderType($type) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Sets custom paginator. |
||
| 252 | * @param Paginator $paginator |
||
| 253 | * @return Grid |
||
| 254 | */ |
||
| 255 | public function setPaginator(Paginator $paginator) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Sets grid primary key. |
||
| 263 | * Defaults is "id". |
||
| 264 | * @param string $key |
||
| 265 | * @return Grid |
||
| 266 | */ |
||
| 267 | public function setPrimaryKey($key) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Sets file name of custom template. |
||
| 275 | * @param string $file |
||
| 276 | * @return Grid |
||
| 277 | */ |
||
| 278 | public function setTemplateFile($file) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Sets saving state to session. |
||
| 290 | * @param bool $state |
||
| 291 | * @param string $sectionName |
||
| 292 | * @return Grid |
||
| 293 | */ |
||
| 294 | public function setRememberState($state = TRUE, $sectionName = NULL) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Sets callback for customizing tr html object. |
||
| 308 | * Callback returns tr html element; function($row, Html $tr). |
||
| 309 | * @param $callback |
||
| 310 | * @return Grid |
||
| 311 | */ |
||
| 312 | public function setRowCallback($callback) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Sets client-side options. |
||
| 320 | * @param array $options |
||
| 321 | * @return Grid |
||
| 322 | */ |
||
| 323 | public function setClientSideOptions(array $options) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Determines whether any user error will cause a notice. |
||
| 331 | * @param bool $mode |
||
| 332 | * @return \Grido\Grid |
||
| 333 | */ |
||
| 334 | public function setStrictMode($mode) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param \Grido\Customization $customization |
||
| 342 | */ |
||
| 343 | public function setCustomization(Customization $customization) |
||
| 347 | |||
| 348 | /**********************************************************************************************/ |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns total count of data. |
||
| 352 | * @return int |
||
| 353 | */ |
||
| 354 | public function getCount() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Returns default per page. |
||
| 365 | * @return int |
||
| 366 | 1 | */ |
|
| 367 | public function getDefaultPerPage() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Returns default filter. |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | public function getDefaultFilter() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns default sort. |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function getDefaultSort() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Returns list of possible items per page. |
||
| 396 | 1 | * @return array |
|
| 397 | */ |
||
| 398 | public function getPerPageList() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Returns primary key. |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function getPrimaryKey() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Returns remember state. |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | public function getRememberState() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Returns row callback. |
||
| 423 | * @return callback |
||
| 424 | */ |
||
| 425 | public function getRowCallback() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Returns items per page. |
||
| 432 | * @return int |
||
| 433 | */ |
||
| 434 | 1 | public function getPerPage() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Returns actual filter values. |
||
| 443 | * @param string $key |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | public function getActualFilter($key = NULL) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Returns fetched data. |
||
| 454 | * @param bool $applyPaging |
||
| 455 | * @param bool $useCache |
||
| 456 | * @param bool $fetch |
||
| 457 | * @throws Exception |
||
| 458 | * @return array|DataSources\IDataSource|\Nette\Database\Table\Selection |
||
| 459 | */ |
||
| 460 | public function getData($applyPaging = TRUE, $useCache = TRUE, $fetch = TRUE) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Returns translator. |
||
| 500 | * @return Translations\FileTranslator |
||
| 501 | */ |
||
| 502 | public function getTranslator() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Returns remember session for set expiration, etc. |
||
| 513 | * @param bool $forceStart - if TRUE, session will be started if not |
||
| 514 | * @return \Nette\Http\SessionSection|NULL |
||
| 515 | */ |
||
| 516 | public function getRememberSession($forceStart = FALSE) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Returns table html element of grid. |
||
| 532 | * @return \Nette\Utils\Html |
||
| 533 | */ |
||
| 534 | public function getTablePrototype() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @return string |
||
| 546 | * @internal |
||
| 547 | */ |
||
| 548 | public function getFilterRenderType() |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @return DataSources\IDataSource |
||
| 572 | */ |
||
| 573 | public function getModel() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @return Paginator |
||
| 580 | * @internal |
||
| 581 | */ |
||
| 582 | public function getPaginator() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * A simple wrapper around symfony/property-access with Nette Database dot notation support. |
||
| 595 | * @param array|object $object |
||
| 596 | * @param string $name |
||
| 597 | * @return mixed |
||
| 598 | * @internal |
||
| 599 | */ |
||
| 600 | public function getProperty($object, $name) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * @return PropertyAccessor |
||
| 622 | * @internal |
||
| 623 | */ |
||
| 624 | public function getPropertyAccessor() |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @param mixed $row item from db |
||
| 635 | * @return \Nette\Utils\Html |
||
| 636 | * @internal |
||
| 637 | */ |
||
| 638 | public function getRowPrototype($row) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Returns client-side options. |
||
| 658 | * @return array |
||
| 659 | */ |
||
| 660 | public function getClientSideOptions() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @return bool |
||
| 667 | */ |
||
| 668 | public function isStrictMode() |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @return Customization |
||
| 675 | */ |
||
| 676 | public function getCustomization() |
||
| 684 | |||
| 685 | /**********************************************************************************************/ |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Loads state informations. |
||
| 689 | * @param array $params |
||
| 690 | * @internal |
||
| 691 | */ |
||
| 692 | public function loadState(array $params) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Saves state informations for next request. |
||
| 707 | * @param array $params |
||
| 708 | * @param \Nette\Application\UI\PresenterComponentReflection $reflection (internal, used by Presenter) |
||
| 709 | * @internal |
||
| 710 | */ |
||
| 711 | public function saveState(array &$params, $reflection = NULL) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Ajax method. |
||
| 719 | * @internal |
||
| 720 | */ |
||
| 721 | public function handleRefresh() |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @param int $page |
||
| 728 | * @internal |
||
| 729 | */ |
||
| 730 | public function handlePage($page) |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @param array $sort |
||
| 737 | * @internal |
||
| 738 | */ |
||
| 739 | public function handleSort(array $sort) |
||
| 744 | |||
| 745 | /** |
||
| 746 | * @param \Nette\Forms\Controls\SubmitButton $button |
||
| 747 | * @internal |
||
| 748 | */ |
||
| 749 | public function handleFilter(\Nette\Forms\Controls\SubmitButton $button) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * @param \Nette\Forms\Controls\SubmitButton $button |
||
| 772 | * @internal |
||
| 773 | */ |
||
| 774 | public function handleReset(\Nette\Forms\Controls\SubmitButton $button) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * @param \Nette\Forms\Controls\SubmitButton $button |
||
| 792 | * @internal |
||
| 793 | */ |
||
| 794 | public function handlePerPage(\Nette\Forms\Controls\SubmitButton $button) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Refresh wrapper. |
||
| 807 | * @return void |
||
| 808 | * @internal |
||
| 809 | */ |
||
| 810 | public function reload() |
||
| 819 | |||
| 820 | /**********************************************************************************************/ |
||
| 821 | |||
| 822 | /** |
||
| 823 | * @return \Nette\Templating\FileTemplate |
||
| 824 | * @internal |
||
| 825 | */ |
||
| 826 | public function createTemplate() |
||
| 834 | |||
| 835 | /** |
||
| 836 | * @internal |
||
| 837 | * @throws Exception |
||
| 838 | */ |
||
| 839 | public function render() |
||
| 870 | |||
| 871 | /** |
||
| 872 | * This method will be called when the component (or component's parent) |
||
| 873 | * becomes attached to a monitored object. Do not call this method yourself. |
||
| 874 | * @param Nette\ComponentModel\IComponent |
||
| 875 | * @return void |
||
| 876 | * @internal |
||
| 877 | */ |
||
| 878 | protected function attached($presenter) |
||
| 888 | |||
| 889 | protected function saveRememberState() |
||
| 899 | |||
| 900 | protected function applyFiltering() |
||
| 905 | |||
| 906 | /** |
||
| 907 | * @param array $filter |
||
| 908 | * @return array |
||
| 909 | * @internal |
||
| 910 | */ |
||
| 911 | public function __getConditions(array $filter) |
||
| 938 | |||
| 939 | protected function applySorting() |
||
| 978 | |||
| 979 | protected function applyPaging() |
||
| 992 | |||
| 993 | protected function createComponentForm($name) |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * @return array |
||
| 1014 | */ |
||
| 1015 | protected function getItemsForCountSelect() |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * @internal |
||
| 1022 | * @param string $message |
||
| 1023 | */ |
||
| 1024 | public function __triggerUserNotice($message) |
||
| 1032 | } |
||
| 1033 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: