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 | * Grid's box-title |
||
143 | */ |
||
144 | public $title = ''; |
||
145 | |||
146 | /** |
||
147 | * Header tools. |
||
148 | * |
||
149 | * @var Tools |
||
150 | */ |
||
151 | public $tools; |
||
152 | |||
153 | /** |
||
154 | * Callback for grid actions. |
||
155 | * |
||
156 | * @var Closure |
||
157 | */ |
||
158 | protected $actionsCallback; |
||
159 | |||
160 | /** |
||
161 | * Options for grid. |
||
162 | * |
||
163 | * @var array |
||
164 | */ |
||
165 | protected $options = [ |
||
166 | 'usePagination' => true, |
||
167 | 'useFilter' => true, |
||
168 | 'useExporter' => true, |
||
169 | 'useActions' => true, |
||
170 | 'useRowSelector' => true, |
||
171 | 'allowCreate' => true, |
||
172 | ]; |
||
173 | |||
174 | /** |
||
175 | * @var Tools\Footer |
||
176 | */ |
||
177 | protected $footer; |
||
178 | |||
179 | /** |
||
180 | * Create a new grid instance. |
||
181 | * |
||
182 | * @param Eloquent $model |
||
183 | * @param Closure $builder |
||
184 | */ |
||
185 | public function __construct(Eloquent $model, Closure $builder) |
||
197 | |||
198 | /** |
||
199 | * Setup grid tools. |
||
200 | */ |
||
201 | public function setupTools() |
||
205 | |||
206 | /** |
||
207 | * Setup grid filter. |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | protected function setupFilter() |
||
215 | |||
216 | /** |
||
217 | * Setup grid exporter. |
||
218 | * |
||
219 | * @return void |
||
220 | */ |
||
221 | protected function setupExporter() |
||
231 | |||
232 | /** |
||
233 | * Get or set option for grid. |
||
234 | * |
||
235 | * @param string $key |
||
236 | * @param mixed $value |
||
237 | * |
||
238 | * @return $this|mixed |
||
239 | */ |
||
240 | public function option($key, $value = null) |
||
250 | |||
251 | /** |
||
252 | * Get primary key name of model. |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getKeyName() |
||
260 | |||
261 | /** |
||
262 | * Add column to Grid. |
||
263 | * |
||
264 | * @param string $name |
||
265 | * @param string $label |
||
266 | * |
||
267 | * @return Column |
||
268 | */ |
||
269 | public function column($name, $label = '') |
||
292 | |||
293 | /** |
||
294 | * Batch add column to grid. |
||
295 | * |
||
296 | * @example |
||
297 | * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]); |
||
298 | * 2.$grid->columns('name', 'email' ...) |
||
299 | * |
||
300 | * @param array $columns |
||
301 | * |
||
302 | * @return Collection|null |
||
303 | */ |
||
304 | public function columns($columns = []) |
||
322 | |||
323 | /** |
||
324 | * Add column to grid. |
||
325 | * |
||
326 | * @param string $column |
||
327 | * @param string $label |
||
328 | * |
||
329 | * @return Column |
||
330 | */ |
||
331 | protected function addColumn($column = '', $label = '') |
||
338 | |||
339 | /** |
||
340 | * Get Grid model. |
||
341 | * |
||
342 | * @return Model |
||
343 | */ |
||
344 | public function model() |
||
348 | |||
349 | /** |
||
350 | * Set the Grid's box-title |
||
351 | * |
||
352 | * @param string $title |
||
353 | */ |
||
354 | public function title($title) |
||
358 | |||
359 | /** |
||
360 | * Paginate the grid. |
||
361 | * |
||
362 | * @param int $perPage |
||
363 | * |
||
364 | * @return void |
||
365 | */ |
||
366 | public function paginate($perPage = 20) |
||
372 | |||
373 | /** |
||
374 | * Get the grid paginator. |
||
375 | * |
||
376 | * @return mixed |
||
377 | */ |
||
378 | public function paginator() |
||
382 | |||
383 | /** |
||
384 | * Disable grid pagination. |
||
385 | * |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function disablePagination() |
||
396 | |||
397 | /** |
||
398 | * If this grid use pagination. |
||
399 | * |
||
400 | * @return bool |
||
401 | */ |
||
402 | public function usePagination() |
||
406 | |||
407 | /** |
||
408 | * Set per-page options. |
||
409 | * |
||
410 | * @param array $perPages |
||
411 | */ |
||
412 | public function perPages(array $perPages) |
||
416 | |||
417 | /** |
||
418 | * Disable all actions. |
||
419 | * |
||
420 | * @return $this |
||
421 | */ |
||
422 | public function disableActions() |
||
426 | |||
427 | /** |
||
428 | * Set grid action callback. |
||
429 | * |
||
430 | * @param Closure $callback |
||
431 | * |
||
432 | * @return $this |
||
433 | */ |
||
434 | public function actions(Closure $callback) |
||
440 | |||
441 | /** |
||
442 | * Add `actions` column for grid. |
||
443 | * |
||
444 | * @return void |
||
445 | */ |
||
446 | protected function appendActionsColumn() |
||
462 | |||
463 | /** |
||
464 | * Disable row selector. |
||
465 | * |
||
466 | * @return Grid|mixed |
||
467 | */ |
||
468 | public function disableRowSelector() |
||
477 | |||
478 | /** |
||
479 | * Prepend checkbox column for grid. |
||
480 | * |
||
481 | * @return void |
||
482 | */ |
||
483 | protected function prependRowSelectorColumn() |
||
502 | |||
503 | /** |
||
504 | * Build the grid. |
||
505 | * |
||
506 | * @return void |
||
507 | */ |
||
508 | public function build() |
||
531 | |||
532 | /** |
||
533 | * Disable grid filter. |
||
534 | * |
||
535 | * @return $this |
||
536 | */ |
||
537 | public function disableFilter() |
||
543 | |||
544 | /** |
||
545 | * Get filter of Grid. |
||
546 | * |
||
547 | * @return Filter |
||
548 | */ |
||
549 | public function getFilter() |
||
553 | |||
554 | /** |
||
555 | * Process the grid filter. |
||
556 | * |
||
557 | * @return array |
||
558 | */ |
||
559 | public function processFilter() |
||
565 | |||
566 | /** |
||
567 | * Set the grid filter. |
||
568 | * |
||
569 | * @param Closure $callback |
||
570 | */ |
||
571 | public function filter(Closure $callback) |
||
575 | |||
576 | /** |
||
577 | * Render the grid filter. |
||
578 | * |
||
579 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
||
580 | */ |
||
581 | public function renderFilter() |
||
589 | |||
590 | /** |
||
591 | * Build the grid rows. |
||
592 | * |
||
593 | * @param array $data |
||
594 | * |
||
595 | * @return void |
||
596 | */ |
||
597 | protected function buildRows(array $data) |
||
607 | |||
608 | /** |
||
609 | * Set grid row callback function. |
||
610 | * |
||
611 | * @param Closure $callable |
||
612 | * |
||
613 | * @return Collection|null |
||
614 | */ |
||
615 | public function rows(Closure $callable = null) |
||
623 | |||
624 | /** |
||
625 | * Setup grid tools. |
||
626 | * |
||
627 | * @param Closure $callback |
||
628 | * |
||
629 | * @return void |
||
630 | */ |
||
631 | public function tools(Closure $callback) |
||
635 | |||
636 | /** |
||
637 | * Render custom tools. |
||
638 | * |
||
639 | * @return string |
||
640 | */ |
||
641 | public function renderHeaderTools() |
||
645 | |||
646 | /** |
||
647 | * Set exporter driver for Grid to export. |
||
648 | * |
||
649 | * @param $exporter |
||
650 | * |
||
651 | * @return $this |
||
652 | */ |
||
653 | public function exporter($exporter) |
||
659 | |||
660 | /** |
||
661 | * Get the export url. |
||
662 | * |
||
663 | * @param int $scope |
||
664 | * @param null $args |
||
665 | * |
||
666 | * @return string |
||
667 | */ |
||
668 | public function exportUrl($scope = 1, $args = null) |
||
674 | |||
675 | /** |
||
676 | * If grid allows export.s. |
||
677 | * |
||
678 | * @return bool |
||
679 | */ |
||
680 | public function allowExport() |
||
684 | |||
685 | /** |
||
686 | * Disable export. |
||
687 | * |
||
688 | * @return $this |
||
689 | */ |
||
690 | public function disableExport() |
||
694 | |||
695 | /** |
||
696 | * Render export button. |
||
697 | * |
||
698 | * @return Tools\ExportButton |
||
699 | */ |
||
700 | public function renderExportButton() |
||
704 | |||
705 | /** |
||
706 | * Alias for method `disableCreateButton`. |
||
707 | * |
||
708 | * @return $this |
||
709 | * |
||
710 | * @deprecated |
||
711 | */ |
||
712 | public function disableCreation() |
||
716 | |||
717 | /** |
||
718 | * Remove create button on grid. |
||
719 | * |
||
720 | * @return $this |
||
721 | */ |
||
722 | public function disableCreateButton() |
||
726 | |||
727 | /** |
||
728 | * If allow creation. |
||
729 | * |
||
730 | * @return bool |
||
731 | */ |
||
732 | public function allowCreation() |
||
736 | |||
737 | /** |
||
738 | * Render create button for grid. |
||
739 | * |
||
740 | * @return Tools\CreateButton |
||
741 | */ |
||
742 | public function renderCreateButton() |
||
746 | |||
747 | /** |
||
748 | * Set grid footer. |
||
749 | * |
||
750 | * @param Closure|null $closure |
||
751 | * |
||
752 | * @return $this|Tools\Footer |
||
753 | */ |
||
754 | public function footer(Closure $closure = null) |
||
764 | |||
765 | /** |
||
766 | * Render grid footer. |
||
767 | * |
||
768 | * @return Tools\Footer|string |
||
769 | */ |
||
770 | public function renderFooter() |
||
778 | |||
779 | /** |
||
780 | * Get current resource uri. |
||
781 | * |
||
782 | * @param string $path |
||
783 | * |
||
784 | * @return string |
||
785 | */ |
||
786 | public function resource($path = null) |
||
800 | |||
801 | /** |
||
802 | * Get the table columns for grid. |
||
803 | * |
||
804 | * @return void |
||
805 | */ |
||
806 | protected function setDbColumns() |
||
812 | |||
813 | /** |
||
814 | * Handle table column for grid. |
||
815 | * |
||
816 | * @param string $method |
||
817 | * @param string $label |
||
818 | * |
||
819 | * @return bool|Column |
||
820 | */ |
||
821 | protected function handleTableColumn($method, $label) |
||
833 | |||
834 | /** |
||
835 | * Handle get mutator column for grid. |
||
836 | * |
||
837 | * @param string $method |
||
838 | * @param string $label |
||
839 | * |
||
840 | * @return bool|Column |
||
841 | */ |
||
842 | protected function handleGetMutatorColumn($method, $label) |
||
850 | |||
851 | /** |
||
852 | * Handle relation column for grid. |
||
853 | * |
||
854 | * @param string $method |
||
855 | * @param string $label |
||
856 | * |
||
857 | * @return bool|Column |
||
858 | */ |
||
859 | protected function handleRelationColumn($method, $label) |
||
885 | |||
886 | /** |
||
887 | * Dynamically add columns to the grid view. |
||
888 | * |
||
889 | * @param $method |
||
890 | * @param $arguments |
||
891 | * |
||
892 | * @return Column |
||
893 | */ |
||
894 | public function __call($method, $arguments) |
||
916 | |||
917 | /** |
||
918 | * Register column displayers. |
||
919 | * |
||
920 | * @return void. |
||
921 | */ |
||
922 | public static function registerColumnDisplayer() |
||
944 | |||
945 | /** |
||
946 | * Add variables to grid view. |
||
947 | * |
||
948 | * @param array $variables |
||
949 | * |
||
950 | * @return $this |
||
951 | */ |
||
952 | public function with($variables = []) |
||
958 | |||
959 | /** |
||
960 | * Get all variables will used in grid view. |
||
961 | * |
||
962 | * @return array |
||
963 | */ |
||
964 | protected function variables() |
||
970 | |||
971 | /** |
||
972 | * Set a view to render. |
||
973 | * |
||
974 | * @param string $view |
||
975 | * @param array $variables |
||
976 | */ |
||
977 | public function setView($view, $variables = []) |
||
985 | |||
986 | /** |
||
987 | * Get the string contents of the grid view. |
||
988 | * |
||
989 | * @return string |
||
990 | */ |
||
991 | public function render() |
||
1001 | |||
1002 | /** |
||
1003 | * Get the string contents of the grid view. |
||
1004 | * |
||
1005 | * @return string |
||
1006 | */ |
||
1007 | public function __toString() |
||
1011 | } |
||
1012 |
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: