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 |
||
28 | class Grid |
||
29 | { |
||
30 | /** |
||
31 | * The grid data model instance. |
||
32 | * |
||
33 | * @var \Encore\Admin\Grid\Model |
||
34 | */ |
||
35 | protected $model; |
||
36 | |||
37 | /** |
||
38 | * Collection of all grid columns. |
||
39 | * |
||
40 | * @var \Illuminate\Support\Collection |
||
41 | */ |
||
42 | protected $columns; |
||
43 | |||
44 | /** |
||
45 | * Collection of table columns. |
||
46 | * |
||
47 | * @var \Illuminate\Support\Collection |
||
48 | */ |
||
49 | protected $dbColumns; |
||
50 | |||
51 | /** |
||
52 | * Collection of all data rows. |
||
53 | * |
||
54 | * @var \Illuminate\Support\Collection |
||
55 | */ |
||
56 | protected $rows; |
||
57 | |||
58 | /** |
||
59 | * Rows callable fucntion. |
||
60 | * |
||
61 | * @var \Closure |
||
62 | */ |
||
63 | protected $rowsCallback; |
||
64 | |||
65 | /** |
||
66 | * All column names of the grid. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | public $columnNames = []; |
||
71 | |||
72 | /** |
||
73 | * Grid builder. |
||
74 | * |
||
75 | * @var \Closure |
||
76 | */ |
||
77 | protected $builder; |
||
78 | |||
79 | /** |
||
80 | * Mark if the grid is builded. |
||
81 | * |
||
82 | * @var bool |
||
83 | */ |
||
84 | protected $builded = false; |
||
85 | |||
86 | /** |
||
87 | * All variables in grid view. |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $variables = []; |
||
92 | |||
93 | /** |
||
94 | * The grid Filter. |
||
95 | * |
||
96 | * @var \Encore\Admin\Grid\Filter |
||
97 | */ |
||
98 | protected $filter; |
||
99 | |||
100 | /** |
||
101 | * Resource path of the grid. |
||
102 | * |
||
103 | * @var |
||
104 | */ |
||
105 | protected $resourcePath; |
||
106 | |||
107 | /** |
||
108 | * Default primary key name. |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | protected $keyName = 'id'; |
||
113 | |||
114 | /** |
||
115 | * Export driver. |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | protected $exporter; |
||
120 | |||
121 | /** |
||
122 | * View for grid to render. |
||
123 | * |
||
124 | * @var string |
||
125 | */ |
||
126 | protected $view = 'admin::grid.table'; |
||
127 | |||
128 | /** |
||
129 | * Per-page options. |
||
130 | * |
||
131 | * @var array |
||
132 | */ |
||
133 | public $perPages = [10, 20, 30, 50, 100]; |
||
134 | |||
135 | /** |
||
136 | * Default items count per-page. |
||
137 | * |
||
138 | * @var int |
||
139 | */ |
||
140 | public $perPage = 20; |
||
141 | |||
142 | /** |
||
143 | * Header tools. |
||
144 | * |
||
145 | * @var Tools |
||
146 | */ |
||
147 | public $tools; |
||
148 | |||
149 | /** |
||
150 | * Callback for grid actions. |
||
151 | * |
||
152 | * @var Closure |
||
153 | */ |
||
154 | protected $actionsCallback; |
||
155 | |||
156 | /** |
||
157 | * Options for grid. |
||
158 | * |
||
159 | * @var array |
||
160 | */ |
||
161 | protected $options = [ |
||
162 | 'usePagination' => true, |
||
163 | 'useFilter' => true, |
||
164 | 'useExporter' => true, |
||
165 | 'useActions' => true, |
||
166 | 'useRowSelector' => true, |
||
167 | 'allowCreate' => true, |
||
168 | ]; |
||
169 | |||
170 | /** |
||
171 | * @var Tools\Footer |
||
172 | */ |
||
173 | protected $footer; |
||
174 | |||
175 | /** |
||
176 | * Create a new grid instance. |
||
177 | * |
||
178 | * @param Eloquent $model |
||
179 | * @param Closure $builder |
||
180 | */ |
||
181 | public function __construct(Eloquent $model, Closure $builder) |
||
193 | |||
194 | /** |
||
195 | * Setup grid tools. |
||
196 | */ |
||
197 | public function setupTools() |
||
201 | |||
202 | /** |
||
203 | * Setup grid filter. |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | protected function setupFilter() |
||
211 | |||
212 | /** |
||
213 | * Setup grid exporter. |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | protected function setupExporter() |
||
227 | |||
228 | /** |
||
229 | * Get or set option for grid. |
||
230 | * |
||
231 | * @param string $key |
||
232 | * @param mixed $value |
||
233 | * |
||
234 | * @return $this|mixed |
||
235 | */ |
||
236 | public function option($key, $value = null) |
||
246 | |||
247 | /** |
||
248 | * Get primary key name of model. |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getKeyName() |
||
256 | |||
257 | /** |
||
258 | * Add column to Grid. |
||
259 | * |
||
260 | * @param string $name |
||
261 | * @param string $label |
||
262 | * |
||
263 | * @return Column |
||
264 | */ |
||
265 | public function column($name, $label = '') |
||
288 | |||
289 | public function setLabel($label , $relationColumn) |
||
303 | |||
304 | /** |
||
305 | * Batch add column to grid. |
||
306 | * |
||
307 | * @example |
||
308 | * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]); |
||
309 | * 2.$grid->columns('name', 'email' ...) |
||
310 | * |
||
311 | * @param array $columns |
||
312 | * |
||
313 | * @return Collection|null |
||
314 | */ |
||
315 | public function columns($columns = []) |
||
333 | |||
334 | /** |
||
335 | * Add column to grid. |
||
336 | * |
||
337 | * @param string $column |
||
338 | * @param string $label |
||
339 | * |
||
340 | * @return Column |
||
341 | */ |
||
342 | protected function addColumn($column = '', $label = '') |
||
349 | |||
350 | /** |
||
351 | * Get Grid model. |
||
352 | * |
||
353 | * @return Model |
||
354 | */ |
||
355 | public function model() |
||
359 | |||
360 | /** |
||
361 | * Paginate the grid. |
||
362 | * |
||
363 | * @param int $perPage |
||
364 | * |
||
365 | * @return void |
||
366 | */ |
||
367 | public function paginate($perPage = 20) |
||
373 | |||
374 | /** |
||
375 | * Get the grid paginator. |
||
376 | * |
||
377 | * @return mixed |
||
378 | */ |
||
379 | public function paginator() |
||
383 | |||
384 | /** |
||
385 | * Disable grid pagination. |
||
386 | * |
||
387 | * @return $this |
||
388 | */ |
||
389 | public function disablePagination() |
||
397 | |||
398 | /** |
||
399 | * If this grid use pagination. |
||
400 | * |
||
401 | * @return bool |
||
402 | */ |
||
403 | public function usePagination() |
||
407 | |||
408 | /** |
||
409 | * Set per-page options. |
||
410 | * |
||
411 | * @param array $perPages |
||
412 | */ |
||
413 | public function perPages(array $perPages) |
||
417 | |||
418 | /** |
||
419 | * Disable all actions. |
||
420 | * |
||
421 | * @return $this |
||
422 | */ |
||
423 | public function disableActions() |
||
427 | |||
428 | /** |
||
429 | * Set grid action callback. |
||
430 | * |
||
431 | * @param Closure $callback |
||
432 | * |
||
433 | * @return $this |
||
434 | */ |
||
435 | public function actions(Closure $callback) |
||
441 | |||
442 | /** |
||
443 | * Add `actions` column for grid. |
||
444 | * |
||
445 | * @return void |
||
446 | */ |
||
447 | protected function appendActionsColumn() |
||
463 | |||
464 | /** |
||
465 | * Disable row selector. |
||
466 | * |
||
467 | * @return Grid|mixed |
||
468 | */ |
||
469 | public function disableRowSelector() |
||
478 | |||
479 | /** |
||
480 | * Prepend checkbox column for grid. |
||
481 | * |
||
482 | * @return void |
||
483 | */ |
||
484 | protected function prependRowSelectorColumn() |
||
503 | |||
504 | /** |
||
505 | * Build the grid. |
||
506 | * |
||
507 | * @return void |
||
508 | */ |
||
509 | public function build() |
||
532 | |||
533 | /** |
||
534 | * Disable grid filter. |
||
535 | * |
||
536 | * @return $this |
||
537 | */ |
||
538 | public function disableFilter() |
||
544 | |||
545 | /** |
||
546 | * Get filter of Grid. |
||
547 | * |
||
548 | * @return Filter |
||
549 | */ |
||
550 | public function getFilter() |
||
554 | |||
555 | /** |
||
556 | * Process the grid filter. |
||
557 | * |
||
558 | * @return array |
||
559 | */ |
||
560 | public function processFilter() |
||
566 | |||
567 | /** |
||
568 | * Set the grid filter. |
||
569 | * |
||
570 | * @param Closure $callback |
||
571 | */ |
||
572 | public function filter(Closure $callback) |
||
576 | |||
577 | /** |
||
578 | * Render the grid filter. |
||
579 | * |
||
580 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
||
581 | */ |
||
582 | public function renderFilter() |
||
590 | |||
591 | /** |
||
592 | * Build the grid rows. |
||
593 | * |
||
594 | * @param array $data |
||
595 | * |
||
596 | * @return void |
||
597 | */ |
||
598 | protected function buildRows(array $data) |
||
608 | |||
609 | /** |
||
610 | * Set grid row callback function. |
||
611 | * |
||
612 | * @param Closure $callable |
||
613 | * |
||
614 | * @return Collection|null |
||
615 | */ |
||
616 | public function rows(Closure $callable = null) |
||
624 | |||
625 | /** |
||
626 | * Setup grid tools. |
||
627 | * |
||
628 | * @param Closure $callback |
||
629 | * |
||
630 | * @return void |
||
631 | */ |
||
632 | public function tools(Closure $callback) |
||
636 | |||
637 | /** |
||
638 | * Render custom tools. |
||
639 | * |
||
640 | * @return string |
||
641 | */ |
||
642 | public function renderHeaderTools() |
||
646 | |||
647 | /** |
||
648 | * Set exporter driver for Grid to export. |
||
649 | * |
||
650 | * @param $exporter |
||
651 | * |
||
652 | * @return $this |
||
653 | */ |
||
654 | public function exporter($exporter) |
||
660 | |||
661 | /** |
||
662 | * Get the export url. |
||
663 | * |
||
664 | * @param int $scope |
||
665 | * @param null $args |
||
666 | * |
||
667 | * @return string |
||
668 | */ |
||
669 | public function exportUrl($scope = 1, $args = null) |
||
675 | |||
676 | /** |
||
677 | * If grid allows export.s. |
||
678 | * |
||
679 | * @return bool |
||
680 | */ |
||
681 | public function allowExport() |
||
685 | |||
686 | /** |
||
687 | * Disable export. |
||
688 | * |
||
689 | * @return $this |
||
690 | */ |
||
691 | public function disableExport() |
||
695 | |||
696 | /** |
||
697 | * Render export button. |
||
698 | * |
||
699 | * @return Tools\ExportButton |
||
700 | */ |
||
701 | public function renderExportButton() |
||
705 | |||
706 | /** |
||
707 | * Alias for method `disableCreateButton`. |
||
708 | * |
||
709 | * @return $this |
||
710 | * |
||
711 | * @deprecated |
||
712 | */ |
||
713 | public function disableCreation() |
||
717 | |||
718 | /** |
||
719 | * Remove create button on grid. |
||
720 | * |
||
721 | * @return $this |
||
722 | */ |
||
723 | public function disableCreateButton() |
||
727 | |||
728 | /** |
||
729 | * If allow creation. |
||
730 | * |
||
731 | * @return bool |
||
732 | */ |
||
733 | public function allowCreation() |
||
737 | |||
738 | /** |
||
739 | * Render create button for grid. |
||
740 | * |
||
741 | * @return Tools\CreateButton |
||
742 | */ |
||
743 | public function renderCreateButton() |
||
747 | |||
748 | /** |
||
749 | * Set grid footer. |
||
750 | * |
||
751 | * @param Closure|null $closure |
||
752 | * |
||
753 | * @return $this|Tools\Footer |
||
754 | */ |
||
755 | public function footer(Closure $closure = null) |
||
765 | |||
766 | /** |
||
767 | * Render grid footer. |
||
768 | * |
||
769 | * @return Tools\Footer|string |
||
770 | */ |
||
771 | public function renderFooter() |
||
779 | |||
780 | /** |
||
781 | * Get current resource uri. |
||
782 | * |
||
783 | * @param string $path |
||
784 | * |
||
785 | * @return string |
||
786 | */ |
||
787 | public function resource($path = null) |
||
801 | |||
802 | /** |
||
803 | * Get the table columns for grid. |
||
804 | * |
||
805 | * @return void |
||
806 | */ |
||
807 | protected function setDbColumns() |
||
813 | |||
814 | /** |
||
815 | * Handle table column for grid. |
||
816 | * |
||
817 | * @param string $method |
||
818 | * @param string $label |
||
819 | * |
||
820 | * @return bool|Column |
||
821 | */ |
||
822 | protected function handleTableColumn($method, $label) |
||
834 | |||
835 | /** |
||
836 | * Handle get mutator column for grid. |
||
837 | * |
||
838 | * @param string $method |
||
839 | * @param string $label |
||
840 | * |
||
841 | * @return bool|Column |
||
842 | */ |
||
843 | protected function handleGetMutatorColumn($method, $label) |
||
851 | |||
852 | /** |
||
853 | * Handle relation column for grid. |
||
854 | * |
||
855 | * @param string $method |
||
856 | * @param string $label |
||
857 | * |
||
858 | * @return bool|Column |
||
859 | */ |
||
860 | protected function handleRelationColumn($method, $label) |
||
886 | |||
887 | /** |
||
888 | * Dynamically add columns to the grid view. |
||
889 | * |
||
890 | * @param $method |
||
891 | * @param $arguments |
||
892 | * |
||
893 | * @return Column |
||
894 | */ |
||
895 | public function __call($method, $arguments) |
||
918 | |||
919 | /** |
||
920 | * Register column displayers. |
||
921 | * |
||
922 | * @return void. |
||
923 | */ |
||
924 | public static function registerColumnDisplayer() |
||
946 | |||
947 | /** |
||
948 | * Add variables to grid view. |
||
949 | * |
||
950 | * @param array $variables |
||
951 | * |
||
952 | * @return $this |
||
953 | */ |
||
954 | public function with($variables = []) |
||
960 | |||
961 | /** |
||
962 | * Get all variables will used in grid view. |
||
963 | * |
||
964 | * @return array |
||
965 | */ |
||
966 | protected function variables() |
||
972 | |||
973 | /** |
||
974 | * Set a view to render. |
||
975 | * |
||
976 | * @param string $view |
||
977 | * @param array $variables |
||
978 | */ |
||
979 | public function setView($view, $variables = []) |
||
987 | |||
988 | /** |
||
989 | * Get the string contents of the grid view. |
||
990 | * |
||
991 | * @return string |
||
992 | */ |
||
993 | public function render() |
||
1003 | |||
1004 | /** |
||
1005 | * Get the string contents of the grid view. |
||
1006 | * |
||
1007 | * @return string |
||
1008 | */ |
||
1009 | public function __toString() |
||
1013 | } |
||
1014 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.