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 |
||
21 | class Grid |
||
22 | { |
||
23 | use HasElementNames; |
||
24 | |||
25 | /** |
||
26 | * The grid data model instance. |
||
27 | * |
||
28 | * @var \Encore\Admin\Grid\Model |
||
29 | */ |
||
30 | protected $model; |
||
31 | |||
32 | /** |
||
33 | * Collection of all grid columns. |
||
34 | * |
||
35 | * @var \Illuminate\Support\Collection |
||
36 | */ |
||
37 | protected $columns; |
||
38 | |||
39 | /** |
||
40 | * Collection of all data rows. |
||
41 | * |
||
42 | * @var \Illuminate\Support\Collection |
||
43 | */ |
||
44 | protected $rows; |
||
45 | |||
46 | /** |
||
47 | * Rows callable fucntion. |
||
48 | * |
||
49 | * @var \Closure |
||
50 | */ |
||
51 | protected $rowsCallback; |
||
52 | |||
53 | /** |
||
54 | * All column names of the grid. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | public $columnNames = []; |
||
59 | |||
60 | /** |
||
61 | * Grid builder. |
||
62 | * |
||
63 | * @var \Closure |
||
64 | */ |
||
65 | protected $builder; |
||
66 | |||
67 | /** |
||
68 | * Mark if the grid is builded. |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $builded = false; |
||
73 | |||
74 | /** |
||
75 | * All variables in grid view. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $variables = []; |
||
80 | |||
81 | /** |
||
82 | * The grid Filter. |
||
83 | * |
||
84 | * @var \Encore\Admin\Grid\Filter |
||
85 | */ |
||
86 | protected $filter; |
||
87 | |||
88 | /** |
||
89 | * Resource path of the grid. |
||
90 | * |
||
91 | * @var |
||
92 | */ |
||
93 | protected $resourcePath; |
||
94 | |||
95 | /** |
||
96 | * Default primary key name. |
||
97 | * |
||
98 | * @var string |
||
99 | */ |
||
100 | protected $keyName = 'id'; |
||
101 | |||
102 | /** |
||
103 | * Export driver. |
||
104 | * |
||
105 | * @var string |
||
106 | */ |
||
107 | protected $exporter; |
||
108 | |||
109 | /** |
||
110 | * View for grid to render. |
||
111 | * |
||
112 | * @var string |
||
113 | */ |
||
114 | protected $view = 'admin::grid.table'; |
||
115 | |||
116 | /** |
||
117 | * Per-page options. |
||
118 | * |
||
119 | * @var array |
||
120 | */ |
||
121 | public $perPages = [10, 20, 30, 50, 100]; |
||
122 | |||
123 | /** |
||
124 | * Default items count per-page. |
||
125 | * |
||
126 | * @var int |
||
127 | */ |
||
128 | public $perPage = 20; |
||
129 | |||
130 | /** |
||
131 | * Header tools. |
||
132 | * |
||
133 | * @var Tools |
||
134 | */ |
||
135 | public $tools; |
||
136 | |||
137 | /** |
||
138 | * Callback for grid actions. |
||
139 | * |
||
140 | * @var Closure |
||
141 | */ |
||
142 | protected $actionsCallback; |
||
143 | |||
144 | /** |
||
145 | * Options for grid. |
||
146 | * |
||
147 | * @var array |
||
148 | */ |
||
149 | protected $options = [ |
||
150 | 'usePagination' => true, |
||
151 | 'useFilter' => true, |
||
152 | 'useExporter' => true, |
||
153 | 'useActions' => true, |
||
154 | 'useRowSelector' => true, |
||
155 | 'allowCreate' => true, |
||
156 | ]; |
||
157 | |||
158 | /** |
||
159 | * @var Tools\Footer |
||
160 | */ |
||
161 | protected $footer; |
||
162 | |||
163 | /** |
||
164 | * Create a new grid instance. |
||
165 | * |
||
166 | * @param Eloquent $model |
||
167 | * @param Closure $builder |
||
168 | */ |
||
169 | public function __construct(Eloquent $model, Closure $builder = null) |
||
183 | |||
184 | /** |
||
185 | * Setup grid tools. |
||
186 | */ |
||
187 | public function setupTools() |
||
191 | |||
192 | /** |
||
193 | * Setup grid filter. |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | protected function setupFilter() |
||
201 | |||
202 | /** |
||
203 | * Setup grid exporter. |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | protected function setupExporter() |
||
219 | |||
220 | /** |
||
221 | * Get or set option for grid. |
||
222 | * |
||
223 | * @param string $key |
||
224 | * @param mixed $value |
||
225 | * |
||
226 | * @return $this|mixed |
||
227 | */ |
||
228 | public function option($key, $value = null) |
||
238 | |||
239 | /** |
||
240 | * Get primary key name of model. |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function getKeyName() |
||
248 | |||
249 | /** |
||
250 | * Add column to Grid. |
||
251 | * |
||
252 | * @param string $name |
||
253 | * @param string $label |
||
254 | * |
||
255 | * @return Column |
||
256 | */ |
||
257 | public function column($name, $label = '') |
||
280 | |||
281 | /** |
||
282 | * Batch add column to grid. |
||
283 | * |
||
284 | * @example |
||
285 | * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]); |
||
286 | * 2.$grid->columns('name', 'email' ...) |
||
287 | * |
||
288 | * @param array $columns |
||
289 | * |
||
290 | * @return Collection|null |
||
291 | */ |
||
292 | public function columns($columns = []) |
||
310 | |||
311 | /** |
||
312 | * Add column to grid. |
||
313 | * |
||
314 | * @param string $column |
||
315 | * @param string $label |
||
316 | * |
||
317 | * @return Column |
||
318 | */ |
||
319 | protected function addColumn($column = '', $label = '') |
||
326 | |||
327 | /** |
||
328 | * Get Grid model. |
||
329 | * |
||
330 | * @return Model |
||
331 | */ |
||
332 | public function model() |
||
336 | |||
337 | /** |
||
338 | * Paginate the grid. |
||
339 | * |
||
340 | * @param int $perPage |
||
341 | * |
||
342 | * @return void |
||
343 | */ |
||
344 | public function paginate($perPage = 20) |
||
350 | |||
351 | /** |
||
352 | * Get the grid paginator. |
||
353 | * |
||
354 | * @return mixed |
||
355 | */ |
||
356 | public function paginator() |
||
360 | |||
361 | /** |
||
362 | * Disable grid pagination. |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function disablePagination() |
||
374 | |||
375 | /** |
||
376 | * If this grid use pagination. |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | public function usePagination() |
||
384 | |||
385 | /** |
||
386 | * Set per-page options. |
||
387 | * |
||
388 | * @param array $perPages |
||
389 | */ |
||
390 | public function perPages(array $perPages) |
||
394 | |||
395 | /** |
||
396 | * Disable all actions. |
||
397 | * |
||
398 | * @return $this |
||
399 | */ |
||
400 | public function disableActions() |
||
404 | |||
405 | /** |
||
406 | * Set grid action callback. |
||
407 | * |
||
408 | * @param Closure $callback |
||
409 | * |
||
410 | * @return $this |
||
411 | */ |
||
412 | public function actions(Closure $callback) |
||
418 | |||
419 | /** |
||
420 | * Add `actions` column for grid. |
||
421 | * |
||
422 | * @return void |
||
423 | */ |
||
424 | protected function appendActionsColumn() |
||
440 | |||
441 | /** |
||
442 | * Disable row selector. |
||
443 | * |
||
444 | * @return Grid|mixed |
||
445 | */ |
||
446 | public function disableRowSelector() |
||
455 | |||
456 | /** |
||
457 | * Prepend checkbox column for grid. |
||
458 | * |
||
459 | * @return void |
||
460 | */ |
||
461 | protected function prependRowSelectorColumn() |
||
480 | |||
481 | /** |
||
482 | * Build the grid. |
||
483 | * |
||
484 | * @return void |
||
485 | */ |
||
486 | public function build() |
||
509 | |||
510 | /** |
||
511 | * Disable grid filter. |
||
512 | * |
||
513 | * @return $this |
||
514 | */ |
||
515 | public function disableFilter() |
||
521 | |||
522 | /** |
||
523 | * Get filter of Grid. |
||
524 | * |
||
525 | * @return Filter |
||
526 | */ |
||
527 | public function getFilter() |
||
531 | |||
532 | /** |
||
533 | * Process the grid filter. |
||
534 | * |
||
535 | * @return array |
||
536 | */ |
||
537 | public function processFilter() |
||
545 | |||
546 | /** |
||
547 | * Set the grid filter. |
||
548 | * |
||
549 | * @param Closure $callback |
||
550 | */ |
||
551 | public function filter(Closure $callback) |
||
555 | |||
556 | /** |
||
557 | * Render the grid filter. |
||
558 | * |
||
559 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
||
560 | */ |
||
561 | public function renderFilter() |
||
569 | |||
570 | /** |
||
571 | * Expand filter. |
||
572 | * |
||
573 | * @return $this |
||
574 | */ |
||
575 | public function expandFilter() |
||
581 | |||
582 | /** |
||
583 | * Build the grid rows. |
||
584 | * |
||
585 | * @param array $data |
||
586 | * |
||
587 | * @return void |
||
588 | */ |
||
589 | protected function buildRows(array $data) |
||
599 | |||
600 | /** |
||
601 | * Set grid row callback function. |
||
602 | * |
||
603 | * @param Closure $callable |
||
604 | * |
||
605 | * @return Collection|null |
||
606 | */ |
||
607 | public function rows(Closure $callable = null) |
||
615 | |||
616 | /** |
||
617 | * Setup grid tools. |
||
618 | * |
||
619 | * @param Closure $callback |
||
620 | * |
||
621 | * @return void |
||
622 | */ |
||
623 | public function tools(Closure $callback) |
||
627 | |||
628 | /** |
||
629 | * Render custom tools. |
||
630 | * |
||
631 | * @return string |
||
632 | */ |
||
633 | public function renderHeaderTools() |
||
637 | |||
638 | /** |
||
639 | * Set exporter driver for Grid to export. |
||
640 | * |
||
641 | * @param $exporter |
||
642 | * |
||
643 | * @return $this |
||
644 | */ |
||
645 | public function exporter($exporter) |
||
651 | |||
652 | /** |
||
653 | * Get the export url. |
||
654 | * |
||
655 | * @param int $scope |
||
656 | * @param null $args |
||
657 | * |
||
658 | * @return string |
||
659 | */ |
||
660 | public function getExportUrl($scope = 1, $args = null) |
||
670 | |||
671 | /** |
||
672 | * Get create url. |
||
673 | * |
||
674 | * @return string |
||
675 | */ |
||
676 | public function getCreateUrl() |
||
689 | |||
690 | /** |
||
691 | * If grid allows export.s. |
||
692 | * |
||
693 | * @return bool |
||
694 | */ |
||
695 | public function allowExport() |
||
699 | |||
700 | /** |
||
701 | * Disable export. |
||
702 | * |
||
703 | * @return $this |
||
704 | */ |
||
705 | public function disableExport() |
||
709 | |||
710 | /** |
||
711 | * Render export button. |
||
712 | * |
||
713 | * @return string |
||
714 | */ |
||
715 | public function renderExportButton() |
||
719 | |||
720 | /** |
||
721 | * Alias for method `disableCreateButton`. |
||
722 | * |
||
723 | * @return $this |
||
724 | * |
||
725 | * @deprecated |
||
726 | */ |
||
727 | public function disableCreation() |
||
731 | |||
732 | /** |
||
733 | * Remove create button on grid. |
||
734 | * |
||
735 | * @return $this |
||
736 | */ |
||
737 | public function disableCreateButton() |
||
741 | |||
742 | /** |
||
743 | * If allow creation. |
||
744 | * |
||
745 | * @return bool |
||
746 | */ |
||
747 | public function allowCreation() |
||
751 | |||
752 | /** |
||
753 | * Render create button for grid. |
||
754 | * |
||
755 | * @return string |
||
756 | */ |
||
757 | public function renderCreateButton() |
||
761 | |||
762 | /** |
||
763 | * Set grid footer. |
||
764 | * |
||
765 | * @param Closure|null $closure |
||
766 | * |
||
767 | * @return $this|Tools\Footer |
||
768 | */ |
||
769 | public function footer(Closure $closure = null) |
||
779 | |||
780 | /** |
||
781 | * Render grid footer. |
||
782 | * |
||
783 | * @return Tools\Footer|string |
||
784 | */ |
||
785 | public function renderFooter() |
||
793 | |||
794 | /** |
||
795 | * Get current resource uri. |
||
796 | * |
||
797 | * @param string $path |
||
798 | * |
||
799 | * @return string |
||
800 | */ |
||
801 | public function resource($path = null) |
||
815 | |||
816 | /** |
||
817 | * Handle get mutator column for grid. |
||
818 | * |
||
819 | * @param string $method |
||
820 | * @param string $label |
||
821 | * |
||
822 | * @return bool|Column |
||
823 | */ |
||
824 | protected function handleGetMutatorColumn($method, $label) |
||
832 | |||
833 | /** |
||
834 | * Handle relation column for grid. |
||
835 | * |
||
836 | * @param string $method |
||
837 | * @param string $label |
||
838 | * |
||
839 | * @return bool|Column |
||
840 | */ |
||
841 | 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) |
||
897 | |||
898 | /** |
||
899 | * Register column displayers. |
||
900 | * |
||
901 | * @return void. |
||
902 | */ |
||
903 | public static function registerColumnDisplayer() |
||
926 | |||
927 | /** |
||
928 | * Add variables to grid view. |
||
929 | * |
||
930 | * @param array $variables |
||
931 | * |
||
932 | * @return $this |
||
933 | */ |
||
934 | public function with($variables = []) |
||
940 | |||
941 | /** |
||
942 | * Get all variables will used in grid view. |
||
943 | * |
||
944 | * @return array |
||
945 | */ |
||
946 | protected function variables() |
||
952 | |||
953 | /** |
||
954 | * Set a view to render. |
||
955 | * |
||
956 | * @param string $view |
||
957 | * @param array $variables |
||
958 | */ |
||
959 | public function setView($view, $variables = []) |
||
967 | |||
968 | /** |
||
969 | * Set grid title. |
||
970 | * |
||
971 | * @param string $title |
||
972 | * |
||
973 | * @return $this |
||
974 | */ |
||
975 | public function setTitle($title) |
||
981 | |||
982 | /** |
||
983 | * Set relation for grid. |
||
984 | * |
||
985 | * @param Relations\Relation $relation |
||
986 | * |
||
987 | * @return $this |
||
988 | */ |
||
989 | public function setRelation(Relations\Relation $relation) |
||
995 | |||
996 | /** |
||
997 | * Set resource path for grid. |
||
998 | * |
||
999 | * @param string $path |
||
1000 | * @return $this |
||
1001 | */ |
||
1002 | public function setResource($path) |
||
1008 | |||
1009 | /** |
||
1010 | * Get the string contents of the grid view. |
||
1011 | * |
||
1012 | * @return string |
||
1013 | */ |
||
1014 | public function render() |
||
1024 | } |
||
1025 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.