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 |
||
27 | class Grid |
||
28 | { |
||
29 | /** |
||
30 | * The grid data model instance. |
||
31 | * |
||
32 | * @var \Encore\Admin\Grid\Model |
||
33 | */ |
||
34 | protected $model; |
||
35 | |||
36 | /** |
||
37 | * Collection of all grid columns. |
||
38 | * |
||
39 | * @var \Illuminate\Support\Collection |
||
40 | */ |
||
41 | protected $columns; |
||
42 | |||
43 | /** |
||
44 | * Collection of table columns. |
||
45 | * |
||
46 | * @var \Illuminate\Support\Collection |
||
47 | */ |
||
48 | protected $dbColumns; |
||
49 | |||
50 | /** |
||
51 | * Collection of all data rows. |
||
52 | * |
||
53 | * @var \Illuminate\Support\Collection |
||
54 | */ |
||
55 | protected $rows; |
||
56 | |||
57 | /** |
||
58 | * Rows callable fucntion. |
||
59 | * |
||
60 | * @var \Closure |
||
61 | */ |
||
62 | protected $rowsCallback; |
||
63 | |||
64 | /** |
||
65 | * All column names of the grid. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | public $columnNames = []; |
||
70 | |||
71 | /** |
||
72 | * Grid builder. |
||
73 | * |
||
74 | * @var \Closure |
||
75 | */ |
||
76 | protected $builder; |
||
77 | |||
78 | /** |
||
79 | * Mark if the grid is builded. |
||
80 | * |
||
81 | * @var bool |
||
82 | */ |
||
83 | protected $builded = false; |
||
84 | |||
85 | /** |
||
86 | * All variables in grid view. |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $variables = []; |
||
91 | |||
92 | /** |
||
93 | * The grid Filter. |
||
94 | * |
||
95 | * @var \Encore\Admin\Grid\Filter |
||
96 | */ |
||
97 | protected $filter; |
||
98 | |||
99 | /** |
||
100 | * Resource path of the grid. |
||
101 | * |
||
102 | * @var |
||
103 | */ |
||
104 | protected $resourcePath; |
||
105 | |||
106 | /** |
||
107 | * Default primary key name. |
||
108 | * |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $keyName = 'id'; |
||
112 | |||
113 | /** |
||
114 | * Export driver. |
||
115 | * |
||
116 | * @var string |
||
117 | */ |
||
118 | protected $exporter; |
||
119 | |||
120 | /** |
||
121 | * View for grid to render. |
||
122 | * |
||
123 | * @var string |
||
124 | */ |
||
125 | protected $view = 'admin::grid.table'; |
||
126 | |||
127 | /** |
||
128 | * Per-page options. |
||
129 | * |
||
130 | * @var array |
||
131 | */ |
||
132 | public $perPages = [10, 20, 30, 50, 100]; |
||
133 | |||
134 | /** |
||
135 | * Default items count per-page. |
||
136 | * |
||
137 | * @var int |
||
138 | */ |
||
139 | public $perPage = 20; |
||
140 | |||
141 | /** |
||
142 | * Header tools. |
||
143 | * |
||
144 | * @var Tools |
||
145 | */ |
||
146 | public $tools; |
||
147 | |||
148 | /** |
||
149 | * Callback for grid actions. |
||
150 | * |
||
151 | * @var Closure |
||
152 | */ |
||
153 | protected $actionsCallback; |
||
154 | |||
155 | /** |
||
156 | * Options for grid. |
||
157 | * |
||
158 | * @var array |
||
159 | */ |
||
160 | protected $options = [ |
||
161 | 'usePagination' => true, |
||
162 | 'useFilter' => true, |
||
163 | 'useExporter' => true, |
||
164 | 'useActions' => true, |
||
165 | 'useRowSelector' => true, |
||
166 | 'allowCreate' => true, |
||
167 | ]; |
||
168 | |||
169 | /** |
||
170 | * @var Tools\Footer |
||
171 | */ |
||
172 | protected $footer; |
||
173 | |||
174 | /** |
||
175 | * Create a new grid instance. |
||
176 | * |
||
177 | * @param Eloquent $model |
||
178 | * @param Closure $builder |
||
179 | */ |
||
180 | public function __construct(Eloquent $model, Closure $builder) |
||
192 | |||
193 | /** |
||
194 | * Setup grid tools. |
||
195 | */ |
||
196 | public function setupTools() |
||
200 | |||
201 | /** |
||
202 | * Setup grid filter. |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | protected function setupFilter() |
||
210 | |||
211 | /** |
||
212 | * Setup grid exporter. |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | protected function setupExporter() |
||
226 | |||
227 | /** |
||
228 | * Get or set option for grid. |
||
229 | * |
||
230 | * @param string $key |
||
231 | * @param mixed $value |
||
232 | * |
||
233 | * @return $this|mixed |
||
234 | */ |
||
235 | public function option($key, $value = null) |
||
245 | |||
246 | /** |
||
247 | * Get primary key name of model. |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getKeyName() |
||
255 | |||
256 | /** |
||
257 | * Add column to Grid. |
||
258 | * |
||
259 | * @param string $name |
||
260 | * @param string $label |
||
261 | * |
||
262 | * @return Column |
||
263 | */ |
||
264 | public function column($name, $label = '') |
||
287 | |||
288 | /** |
||
289 | * Batch add column to grid. |
||
290 | * |
||
291 | * @example |
||
292 | * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]); |
||
293 | * 2.$grid->columns('name', 'email' ...) |
||
294 | * |
||
295 | * @param array $columns |
||
296 | * |
||
297 | * @return Collection|null |
||
298 | */ |
||
299 | public function columns($columns = []) |
||
317 | |||
318 | /** |
||
319 | * Add column to grid. |
||
320 | * |
||
321 | * @param string $column |
||
322 | * @param string $label |
||
323 | * |
||
324 | * @return Column |
||
325 | */ |
||
326 | protected function addColumn($column = '', $label = '') |
||
333 | |||
334 | /** |
||
335 | * Get Grid model. |
||
336 | * |
||
337 | * @return Model |
||
338 | */ |
||
339 | public function model() |
||
343 | |||
344 | /** |
||
345 | * Paginate the grid. |
||
346 | * |
||
347 | * @param int $perPage |
||
348 | * |
||
349 | * @return void |
||
350 | */ |
||
351 | public function paginate($perPage = 20) |
||
357 | |||
358 | /** |
||
359 | * Get the grid paginator. |
||
360 | * |
||
361 | * @return mixed |
||
362 | */ |
||
363 | public function paginator() |
||
367 | |||
368 | /** |
||
369 | * Disable grid pagination. |
||
370 | * |
||
371 | * @return $this |
||
372 | */ |
||
373 | public function disablePagination() |
||
381 | |||
382 | /** |
||
383 | * If this grid use pagination. |
||
384 | * |
||
385 | * @return bool |
||
386 | */ |
||
387 | public function usePagination() |
||
391 | |||
392 | /** |
||
393 | * Set per-page options. |
||
394 | * |
||
395 | * @param array $perPages |
||
396 | */ |
||
397 | public function perPages(array $perPages) |
||
401 | |||
402 | /** |
||
403 | * Disable all actions. |
||
404 | * |
||
405 | * @return $this |
||
406 | */ |
||
407 | public function disableActions() |
||
411 | |||
412 | /** |
||
413 | * Set grid action callback. |
||
414 | * |
||
415 | * @param Closure $callback |
||
416 | * |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function actions(Closure $callback) |
||
425 | |||
426 | /** |
||
427 | * Add `actions` column for grid. |
||
428 | * |
||
429 | * @return void |
||
430 | */ |
||
431 | protected function appendActionsColumn() |
||
447 | |||
448 | /** |
||
449 | * Disable row selector. |
||
450 | * |
||
451 | * @return Grid|mixed |
||
452 | */ |
||
453 | public function disableRowSelector() |
||
462 | |||
463 | /** |
||
464 | * Prepend checkbox column for grid. |
||
465 | * |
||
466 | * @return void |
||
467 | */ |
||
468 | protected function prependRowSelectorColumn() |
||
487 | |||
488 | /** |
||
489 | * Build the grid. |
||
490 | * |
||
491 | * @return void |
||
492 | */ |
||
493 | public function build() |
||
516 | |||
517 | /** |
||
518 | * Disable grid filter. |
||
519 | * |
||
520 | * @return $this |
||
521 | */ |
||
522 | public function disableFilter() |
||
528 | |||
529 | /** |
||
530 | * Get filter of Grid. |
||
531 | * |
||
532 | * @return Filter |
||
533 | */ |
||
534 | public function getFilter() |
||
538 | |||
539 | /** |
||
540 | * Process the grid filter. |
||
541 | * |
||
542 | * @return array |
||
543 | */ |
||
544 | public function processFilter() |
||
550 | |||
551 | /** |
||
552 | * Set the grid filter. |
||
553 | * |
||
554 | * @param Closure $callback |
||
555 | */ |
||
556 | public function filter(Closure $callback) |
||
560 | |||
561 | /** |
||
562 | * Render the grid filter. |
||
563 | * |
||
564 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
||
565 | */ |
||
566 | public function renderFilter() |
||
574 | |||
575 | /** |
||
576 | * Build the grid rows. |
||
577 | * |
||
578 | * @param array $data |
||
579 | * |
||
580 | * @return void |
||
581 | */ |
||
582 | protected function buildRows(array $data) |
||
592 | |||
593 | /** |
||
594 | * Set grid row callback function. |
||
595 | * |
||
596 | * @param Closure $callable |
||
597 | * |
||
598 | * @return Collection|null |
||
599 | */ |
||
600 | public function rows(Closure $callable = null) |
||
608 | |||
609 | /** |
||
610 | * Setup grid tools. |
||
611 | * |
||
612 | * @param Closure $callback |
||
613 | * |
||
614 | * @return void |
||
615 | */ |
||
616 | public function tools(Closure $callback) |
||
620 | |||
621 | /** |
||
622 | * Render custom tools. |
||
623 | * |
||
624 | * @return string |
||
625 | */ |
||
626 | public function renderHeaderTools() |
||
630 | |||
631 | /** |
||
632 | * Set exporter driver for Grid to export. |
||
633 | * |
||
634 | * @param $exporter |
||
635 | * |
||
636 | * @return $this |
||
637 | */ |
||
638 | public function exporter($exporter) |
||
644 | |||
645 | /** |
||
646 | * Get the export url. |
||
647 | * |
||
648 | * @param int $scope |
||
649 | * @param null $args |
||
650 | * |
||
651 | * @return string |
||
652 | */ |
||
653 | public function exportUrl($scope = 1, $args = null) |
||
659 | |||
660 | /** |
||
661 | * If grid allows export.s. |
||
662 | * |
||
663 | * @return bool |
||
664 | */ |
||
665 | public function allowExport() |
||
669 | |||
670 | /** |
||
671 | * Disable export. |
||
672 | * |
||
673 | * @return $this |
||
674 | */ |
||
675 | public function disableExport() |
||
679 | |||
680 | /** |
||
681 | * Render export button. |
||
682 | * |
||
683 | * @return Tools\ExportButton |
||
684 | */ |
||
685 | public function renderExportButton() |
||
689 | |||
690 | /** |
||
691 | * Alias for method `disableCreateButton`. |
||
692 | * |
||
693 | * @return $this |
||
694 | * |
||
695 | * @deprecated |
||
696 | */ |
||
697 | public function disableCreation() |
||
701 | |||
702 | /** |
||
703 | * Remove create button on grid. |
||
704 | * |
||
705 | * @return $this |
||
706 | */ |
||
707 | public function disableCreateButton() |
||
711 | |||
712 | /** |
||
713 | * If allow creation. |
||
714 | * |
||
715 | * @return bool |
||
716 | */ |
||
717 | public function allowCreation() |
||
721 | |||
722 | /** |
||
723 | * Render create button for grid. |
||
724 | * |
||
725 | * @return Tools\CreateButton |
||
726 | */ |
||
727 | public function renderCreateButton() |
||
731 | |||
732 | /** |
||
733 | * Set grid footer. |
||
734 | * |
||
735 | * @param Closure|null $closure |
||
736 | * |
||
737 | * @return $this|Tools\Footer |
||
738 | */ |
||
739 | public function footer(Closure $closure = null) |
||
749 | |||
750 | /** |
||
751 | * Render grid footer. |
||
752 | * |
||
753 | * @return Tools\Footer|string |
||
754 | */ |
||
755 | public function renderFooter() |
||
763 | |||
764 | /** |
||
765 | * Get current resource uri. |
||
766 | * |
||
767 | * @param string $path |
||
768 | * |
||
769 | * @return string |
||
770 | */ |
||
771 | public function resource($path = null) |
||
785 | |||
786 | /** |
||
787 | * Get the table columns for grid. |
||
788 | * |
||
789 | * @return void |
||
790 | */ |
||
791 | protected function setDbColumns() |
||
797 | |||
798 | /** |
||
799 | * Handle table column for grid. |
||
800 | * |
||
801 | * @param string $method |
||
802 | * @param string $label |
||
803 | * |
||
804 | * @return bool|Column |
||
805 | */ |
||
806 | protected function handleTableColumn($method, $label) |
||
818 | |||
819 | /** |
||
820 | * Handle get mutator column for grid. |
||
821 | * |
||
822 | * @param string $method |
||
823 | * @param string $label |
||
824 | * |
||
825 | * @return bool|Column |
||
826 | */ |
||
827 | protected function handleGetMutatorColumn($method, $label) |
||
835 | |||
836 | /** |
||
837 | * Handle relation column for grid. |
||
838 | * |
||
839 | * @param string $method |
||
840 | * @param string $label |
||
841 | * |
||
842 | * @return bool|Column |
||
843 | */ |
||
844 | protected function handleRelationColumn($method, $label) |
||
870 | |||
871 | /** |
||
872 | * Dynamically add columns to the grid view. |
||
873 | * |
||
874 | * @param $method |
||
875 | * @param $arguments |
||
876 | * |
||
877 | * @return Column |
||
878 | */ |
||
879 | public function __call($method, $arguments) |
||
901 | |||
902 | /** |
||
903 | * Register column displayers. |
||
904 | * |
||
905 | * @return void. |
||
906 | */ |
||
907 | public static function registerColumnDisplayer() |
||
929 | |||
930 | /** |
||
931 | * Add variables to grid view. |
||
932 | * |
||
933 | * @param array $variables |
||
934 | * |
||
935 | * @return $this |
||
936 | */ |
||
937 | public function with($variables = []) |
||
943 | |||
944 | /** |
||
945 | * Get all variables will used in grid view. |
||
946 | * |
||
947 | * @return array |
||
948 | */ |
||
949 | protected function variables() |
||
955 | |||
956 | /** |
||
957 | * Set a view to render. |
||
958 | * |
||
959 | * @param string $view |
||
960 | * @param array $variables |
||
961 | */ |
||
962 | public function setView($view, $variables = []) |
||
970 | |||
971 | /** |
||
972 | * Get the string contents of the grid view. |
||
973 | * |
||
974 | * @return string |
||
975 | */ |
||
976 | public function render() |
||
986 | |||
987 | /** |
||
988 | * Get the string contents of the grid view. |
||
989 | * |
||
990 | * @return string |
||
991 | */ |
||
992 | public function __toString() |
||
996 | } |
||
997 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: