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 |
||
52 | 1 | class Grid extends Components\Container |
|
53 | { |
||
54 | /***** DEFAULTS ****/ |
||
55 | const BUTTONS = 'buttons'; |
||
56 | |||
57 | const CLIENT_SIDE_OPTIONS = 'grido-options'; |
||
58 | |||
59 | /** @var int @persistent */ |
||
60 | public $page = 1; |
||
61 | |||
62 | /** @var int @persistent */ |
||
63 | public $perPage; |
||
64 | |||
65 | /** @var array @persistent */ |
||
66 | public $sort = []; |
||
67 | 1 | ||
68 | /** @var array @persistent */ |
||
69 | public $filter = []; |
||
70 | |||
71 | /** @var array event on all grid's components registered */ |
||
72 | public $onRegistered; |
||
73 | |||
74 | /** @var array event on render */ |
||
75 | public $onRender; |
||
76 | |||
77 | /** @var array event for modifying data */ |
||
78 | public $onFetchData; |
||
79 | |||
80 | /** @var callback returns tr html element; function($row, Html $tr) */ |
||
81 | protected $rowCallback; |
||
82 | |||
83 | /** @var \Nette\Utils\Html */ |
||
84 | protected $tablePrototype; |
||
85 | |||
86 | /** @var bool */ |
||
87 | protected $rememberState = FALSE; |
||
88 | |||
89 | /** @var string */ |
||
90 | protected $rememberStateSectionName; |
||
91 | |||
92 | /** @var string */ |
||
93 | protected $primaryKey = 'id'; |
||
94 | |||
95 | /** @var string */ |
||
96 | protected $filterRenderType; |
||
97 | |||
98 | /** @var array */ |
||
99 | protected $perPageList = [10, 20, 30, 50, 100]; |
||
100 | |||
101 | /** @var int */ |
||
102 | protected $defaultPerPage = 20; |
||
103 | |||
104 | /** @var array */ |
||
105 | protected $defaultFilter = []; |
||
106 | |||
107 | /** @var array */ |
||
108 | protected $defaultSort = []; |
||
109 | |||
110 | /** @var DataSources\IDataSource */ |
||
111 | protected $model; |
||
112 | |||
113 | /** @var int total count of items */ |
||
114 | protected $count; |
||
115 | |||
116 | /** @var mixed */ |
||
117 | protected $data; |
||
118 | |||
119 | /** @var Paginator */ |
||
120 | protected $paginator; |
||
121 | |||
122 | /** @var \Nette\Localization\ITranslator */ |
||
123 | protected $translator; |
||
124 | |||
125 | /** @var PropertyAccessor */ |
||
126 | protected $propertyAccessor; |
||
127 | |||
128 | /** @var bool */ |
||
129 | protected $strictMode = TRUE; |
||
130 | |||
131 | /** @var array */ |
||
132 | protected $options = [ |
||
133 | self::CLIENT_SIDE_OPTIONS => [] |
||
134 | ]; |
||
135 | |||
136 | /** @var Customization */ |
||
137 | protected $customization; |
||
138 | |||
139 | /** |
||
140 | * Grid constructor. |
||
141 | */ |
||
142 | public function __construct() |
||
143 | { |
||
144 | list($parent, $name) = func_get_args() + [NULL, NULL]; |
||
145 | if ($parent !== NULL) { |
||
146 | $parent->addComponent($this, $name); |
||
147 | 1 | } elseif (is_string($name)) { |
|
148 | 1 | $this->name = $name; |
|
149 | 1 | } |
|
150 | 1 | } |
|
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): void |
||
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): void |
||
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 | * @internal |
||
836 | */ |
||
837 | public function createTemplate(): \Nette\Application\UI\ITemplate |
||
845 | |||
846 | 1 | /** |
|
847 | 1 | * @internal |
|
848 | 1 | * @throws Exception |
|
849 | */ |
||
850 | 1 | public function render() |
|
893 | |||
894 | 1 | protected function saveRememberState() |
|
904 | |||
905 | 1 | protected function applyFiltering() |
|
910 | |||
911 | /** |
||
912 | * @param array $filter |
||
913 | * @return array |
||
914 | * @internal |
||
915 | */ |
||
916 | public function __getConditions(array $filter) |
||
943 | |||
944 | 1 | protected function applySorting() |
|
983 | 1 | ||
984 | protected function applyPaging() |
||
997 | 1 | ||
998 | protected function createComponentForm($name) |
||
1016 | |||
1017 | /** |
||
1018 | 1 | * @return array |
|
1019 | 1 | */ |
|
1020 | 1 | protected function getItemsForCountSelect() |
|
1024 | |||
1025 | 1 | /** |
|
1026 | * @internal |
||
1027 | * @param string $message |
||
1028 | */ |
||
1029 | public function __triggerUserNotice($message) |
||
1037 | } |
||
1038 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.