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 |
||
| 48 | class Grid extends Components\Container |
||
| 49 | 1 | { |
|
| 50 | 1 | /***** DEFAULTS ****/ |
|
| 51 | const BUTTONS = 'buttons'; |
||
| 52 | |||
| 53 | const CLIENT_SIDE_OPTIONS = 'grido-options'; |
||
| 54 | |||
| 55 | /** @var int @persistent */ |
||
| 56 | public $page = 1; |
||
| 57 | |||
| 58 | /** @var int @persistent */ |
||
| 59 | public $perPage; |
||
| 60 | |||
| 61 | /** @var array @persistent */ |
||
| 62 | public $sort = []; |
||
| 63 | |||
| 64 | /** @var array @persistent */ |
||
| 65 | public $filter = []; |
||
| 66 | 1 | ||
| 67 | /** @var array event on all grid's components registered */ |
||
| 68 | public $onRegistered; |
||
| 69 | |||
| 70 | /** @var array event on render */ |
||
| 71 | public $onRender; |
||
| 72 | |||
| 73 | /** @var array event for modifying data */ |
||
| 74 | public $onFetchData; |
||
| 75 | |||
| 76 | /** @var callback returns tr html element; function($row, Html $tr) */ |
||
| 77 | protected $rowCallback; |
||
| 78 | 1 | ||
| 79 | /** @var \Nette\Utils\Html */ |
||
| 80 | protected $tablePrototype; |
||
| 81 | |||
| 82 | /** @var bool */ |
||
| 83 | protected $rememberState = FALSE; |
||
| 84 | |||
| 85 | /** @var string */ |
||
| 86 | protected $rememberStateSectionName; |
||
| 87 | |||
| 88 | /** @var string */ |
||
| 89 | protected $primaryKey = 'id'; |
||
| 90 | |||
| 91 | /** @var string */ |
||
| 92 | protected $filterRenderType; |
||
| 93 | |||
| 94 | /** @var array */ |
||
| 95 | protected $perPageList = [10, 20, 30, 50, 100]; |
||
| 96 | |||
| 97 | /** @var int */ |
||
| 98 | protected $defaultPerPage = 20; |
||
| 99 | |||
| 100 | /** @var array */ |
||
| 101 | protected $defaultFilter = []; |
||
| 102 | |||
| 103 | /** @var array */ |
||
| 104 | protected $defaultSort = []; |
||
| 105 | |||
| 106 | /** @var DataSources\IDataSource */ |
||
| 107 | protected $model; |
||
| 108 | |||
| 109 | /** @var int total count of items */ |
||
| 110 | protected $count; |
||
| 111 | |||
| 112 | /** @var mixed */ |
||
| 113 | protected $data; |
||
| 114 | |||
| 115 | /** @var Paginator */ |
||
| 116 | protected $paginator; |
||
| 117 | |||
| 118 | /** @var \Nette\Localization\ITranslator */ |
||
| 119 | protected $translator; |
||
| 120 | |||
| 121 | /** @var PropertyAccessor */ |
||
| 122 | protected $propertyAccessor; |
||
| 123 | |||
| 124 | /** @var bool */ |
||
| 125 | protected $strictMode = TRUE; |
||
| 126 | |||
| 127 | 1 | /** @var array */ |
|
| 128 | protected $options = [ |
||
| 129 | self::CLIENT_SIDE_OPTIONS => [] |
||
| 130 | ]; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Sets a model that implements the interface Grido\DataSources\IDataSource or data-source object. |
||
| 134 | * @param mixed $model |
||
| 135 | * @param bool $forceWrapper |
||
| 136 | * @throws Exception |
||
| 137 | * @return Grid |
||
| 138 | */ |
||
| 139 | public function setModel($model, $forceWrapper = FALSE) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Sets the default number of items per page. |
||
| 150 | * @param int $perPage |
||
| 151 | * @return Grid |
||
| 152 | */ |
||
| 153 | public function setDefaultPerPage($perPage) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Sets default filtering. |
||
| 168 | * @param array $filter |
||
| 169 | * @return Grid |
||
| 170 | */ |
||
| 171 | public function setDefaultFilter(array $filter) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Sets default sorting. |
||
| 179 | * @param array $sort |
||
| 180 | * @return Grid |
||
| 181 | * @throws Exception |
||
| 182 | */ |
||
| 183 | public function setDefaultSort(array $sort) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Sets items to per-page select. |
||
| 201 | * @param array $perPageList |
||
| 202 | * @return Grid |
||
| 203 | */ |
||
| 204 | public function setPerPageList(array $perPageList) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Sets translator. |
||
| 217 | * @param \Nette\Localization\ITranslator $translator |
||
| 218 | * @return Grid |
||
| 219 | */ |
||
| 220 | public function setTranslator(\Nette\Localization\ITranslator $translator) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Sets type of filter rendering. |
||
| 228 | * Defaults inner (Filter::RENDER_INNER) if column does not exist then outer filter (Filter::RENDER_OUTER). |
||
| 229 | * @param string $type |
||
| 230 | * @throws Exception |
||
| 231 | * @return Grid |
||
| 232 | */ |
||
| 233 | public function setFilterRenderType($type) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Sets custom paginator. |
||
| 246 | * @param Paginator $paginator |
||
| 247 | * @return Grid |
||
| 248 | */ |
||
| 249 | public function setPaginator(Paginator $paginator) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Sets grid primary key. |
||
| 257 | * Defaults is "id". |
||
| 258 | * @param string $key |
||
| 259 | * @return Grid |
||
| 260 | */ |
||
| 261 | public function setPrimaryKey($key) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Sets file name of custom template. |
||
| 269 | * @param string $file |
||
| 270 | * @return Grid |
||
| 271 | */ |
||
| 272 | public function setTemplateFile($file) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Sets saving state to session. |
||
| 284 | * @param bool $state |
||
| 285 | * @param string $sectionName |
||
| 286 | * @return Grid |
||
| 287 | */ |
||
| 288 | public function setRememberState($state = TRUE, $sectionName = NULL) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Sets callback for customizing tr html object. |
||
| 300 | * Callback returns tr html element; function($row, Html $tr). |
||
| 301 | * @param $callback |
||
| 302 | * @return Grid |
||
| 303 | */ |
||
| 304 | public function setRowCallback($callback) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Sets client-side options. |
||
| 312 | * @param array $options |
||
| 313 | * @return Grid |
||
| 314 | */ |
||
| 315 | public function setClientSideOptions(array $options) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Determines whether any user error will cause a notice. |
||
| 323 | * @param bool $mode |
||
| 324 | * @return \Grido\Grid |
||
| 325 | */ |
||
| 326 | public function setStrictMode($mode) |
||
| 331 | |||
| 332 | /**********************************************************************************************/ |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns total count of data. |
||
| 336 | * @return int |
||
| 337 | */ |
||
| 338 | public function getCount() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns default per page. |
||
| 349 | * @return int |
||
| 350 | */ |
||
| 351 | public function getDefaultPerPage() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Returns default filter. |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | public function getDefaultFilter() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns default sort. |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public function getDefaultSort() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns list of possible items per page. |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | public function getPerPageList() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Returns primary key. |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public function getPrimaryKey() |
||
| 395 | |||
| 396 | 1 | /** |
|
| 397 | * Returns remember state. |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | public function getRememberState() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns row callback. |
||
| 407 | * @return callback |
||
| 408 | */ |
||
| 409 | public function getRowCallback() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Returns items per page. |
||
| 416 | * @return int |
||
| 417 | */ |
||
| 418 | public function getPerPage() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Returns actual filter values. |
||
| 427 | * @param string $key |
||
| 428 | * @return mixed |
||
| 429 | */ |
||
| 430 | public function getActualFilter($key = NULL) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns fetched data. |
||
| 438 | * @param bool $applyPaging |
||
| 439 | * @param bool $useCache |
||
| 440 | * @param bool $fetch |
||
| 441 | * @throws Exception |
||
| 442 | * @return array|DataSources\IDataSource|\Nette\Database\Table\Selection |
||
| 443 | */ |
||
| 444 | public function getData($applyPaging = TRUE, $useCache = TRUE, $fetch = TRUE) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Returns translator. |
||
| 484 | * @return Translations\FileTranslator |
||
| 485 | */ |
||
| 486 | public function getTranslator() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Returns remember session for set expiration, etc. |
||
| 497 | * @param bool $forceStart - if TRUE, session will be started if not |
||
| 498 | * @return \Nette\Http\SessionSection|NULL |
||
| 499 | */ |
||
| 500 | public function getRememberSession($forceStart = FALSE) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Returns table html element of grid. |
||
| 516 | * @return \Nette\Utils\Html |
||
| 517 | */ |
||
| 518 | public function getTablePrototype() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @return string |
||
| 531 | * @internal |
||
| 532 | */ |
||
| 533 | public function getFilterRenderType() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @return DataSources\IDataSource |
||
| 557 | */ |
||
| 558 | public function getModel() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @return Paginator |
||
| 565 | * @internal |
||
| 566 | */ |
||
| 567 | public function getPaginator() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * A simple wrapper around symfony/property-access with Nette Database dot notation support. |
||
| 580 | * @param array|object $object |
||
| 581 | * @param string $name |
||
| 582 | * @return mixed |
||
| 583 | * @internal |
||
| 584 | */ |
||
| 585 | public function getProperty($object, $name) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @return PropertyAccessor |
||
| 607 | * @internal |
||
| 608 | */ |
||
| 609 | public function getPropertyAccessor() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @param mixed $row item from db |
||
| 620 | * @return \Nette\Utils\Html |
||
| 621 | * @internal |
||
| 622 | */ |
||
| 623 | public function getRowPrototype($row) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Returns client-side options. |
||
| 643 | * @return array |
||
| 644 | */ |
||
| 645 | public function getClientSideOptions() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @return bool |
||
| 652 | */ |
||
| 653 | public function isStrictMode() |
||
| 657 | |||
| 658 | /**********************************************************************************************/ |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Loads state informations. |
||
| 662 | * @param array $params |
||
| 663 | * @internal |
||
| 664 | */ |
||
| 665 | public function loadState(array $params) |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Saves state informations for next request. |
||
| 680 | * @param array $params |
||
| 681 | * @param \Nette\Application\UI\PresenterComponentReflection $reflection (internal, used by Presenter) |
||
| 682 | * @internal |
||
| 683 | */ |
||
| 684 | public function saveState(array &$params, $reflection = NULL) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Ajax method. |
||
| 692 | * @internal |
||
| 693 | */ |
||
| 694 | public function handleRefresh() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @param int $page |
||
| 701 | * @internal |
||
| 702 | */ |
||
| 703 | public function handlePage($page) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * @param array $sort |
||
| 710 | * @internal |
||
| 711 | */ |
||
| 712 | public function handleSort(array $sort) |
||
| 717 | |||
| 718 | /** |
||
| 719 | * @param \Nette\Forms\Controls\SubmitButton $button |
||
| 720 | * @internal |
||
| 721 | */ |
||
| 722 | public function handleFilter(\Nette\Forms\Controls\SubmitButton $button) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @param \Nette\Forms\Controls\SubmitButton $button |
||
| 745 | * @internal |
||
| 746 | */ |
||
| 747 | public function handleReset(\Nette\Forms\Controls\SubmitButton $button) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @param \Nette\Forms\Controls\SubmitButton $button |
||
| 765 | * @internal |
||
| 766 | */ |
||
| 767 | public function handlePerPage(\Nette\Forms\Controls\SubmitButton $button) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Refresh wrapper. |
||
| 780 | * @return void |
||
| 781 | * @internal |
||
| 782 | */ |
||
| 783 | public function reload() |
||
| 792 | |||
| 793 | /**********************************************************************************************/ |
||
| 794 | |||
| 795 | /** |
||
| 796 | * @return \Nette\Templating\FileTemplate |
||
| 797 | * @internal |
||
| 798 | */ |
||
| 799 | public function createTemplate() |
||
| 807 | |||
| 808 | /** |
||
| 809 | * @internal |
||
| 810 | * @throws Exception |
||
| 811 | */ |
||
| 812 | public function render() |
||
| 837 | |||
| 838 | protected function saveRememberState() |
||
| 848 | |||
| 849 | protected function applyFiltering() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * @param array $filter |
||
| 857 | * @return array |
||
| 858 | * @internal |
||
| 859 | */ |
||
| 860 | public function __getConditions(array $filter) |
||
| 887 | |||
| 888 | protected function applySorting() |
||
| 927 | |||
| 928 | protected function applyPaging() |
||
| 941 | |||
| 942 | protected function createComponentForm($name) |
||
| 960 | |||
| 961 | /** |
||
| 962 | * @return array |
||
| 963 | */ |
||
| 964 | protected function getItemsForCountSelect() |
||
| 968 | |||
| 969 | /** |
||
| 970 | * @internal |
||
| 971 | * @param string $message |
||
| 972 | */ |
||
| 973 | public function __triggerUserNotice($message) |
||
| 977 | } |
||
| 978 |
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: