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 |
||
26 | class Grid |
||
27 | { |
||
28 | /** |
||
29 | * The grid data model instance. |
||
30 | * |
||
31 | * @var \Encore\Admin\Grid\Model |
||
32 | */ |
||
33 | protected $model; |
||
34 | |||
35 | /** |
||
36 | * Collection of all grid columns. |
||
37 | * |
||
38 | * @var \Illuminate\Support\Collection |
||
39 | */ |
||
40 | protected $columns; |
||
41 | |||
42 | /** |
||
43 | * Collection of all data rows. |
||
44 | * |
||
45 | * @var \Illuminate\Support\Collection |
||
46 | */ |
||
47 | protected $rows; |
||
48 | |||
49 | /** |
||
50 | * Rows callable fucntion. |
||
51 | * |
||
52 | * @var \Closure |
||
53 | */ |
||
54 | protected $rowsCallback; |
||
55 | |||
56 | /** |
||
57 | * All column names of the grid. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | public $columnNames = []; |
||
62 | |||
63 | /** |
||
64 | * Grid builder. |
||
65 | * |
||
66 | * @var \Closure |
||
67 | */ |
||
68 | protected $builder; |
||
69 | |||
70 | /** |
||
71 | * Mark if the grid is builded. |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | protected $builded = false; |
||
76 | |||
77 | /** |
||
78 | * All variables in grid view. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $variables = []; |
||
83 | |||
84 | /** |
||
85 | * The grid Filter. |
||
86 | * |
||
87 | * @var \Encore\Admin\Grid\Filter |
||
88 | */ |
||
89 | protected $filter; |
||
90 | |||
91 | /** |
||
92 | * Resource path of the grid. |
||
93 | * |
||
94 | * @var |
||
95 | */ |
||
96 | protected $resourcePath; |
||
97 | |||
98 | /** |
||
99 | * Default primary key name. |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $keyName = 'id'; |
||
104 | |||
105 | /** |
||
106 | * Allow batch deletion. |
||
107 | * |
||
108 | * @var bool |
||
109 | */ |
||
110 | protected $allowBatchDeletion = true; |
||
111 | |||
112 | /** |
||
113 | * Allow creation. |
||
114 | * |
||
115 | * @var bool |
||
116 | */ |
||
117 | protected $allowCreation = true; |
||
118 | |||
119 | /** |
||
120 | * Allow actions. |
||
121 | * |
||
122 | * @var bool |
||
123 | */ |
||
124 | protected $allowActions = true; |
||
125 | |||
126 | /** |
||
127 | * Allow export data. |
||
128 | * |
||
129 | * @var bool |
||
130 | */ |
||
131 | protected $allowExport = true; |
||
132 | |||
133 | /** |
||
134 | * If use grid filter. |
||
135 | * |
||
136 | * @var bool |
||
137 | */ |
||
138 | protected $useFilter = true; |
||
139 | |||
140 | /** |
||
141 | * If grid use pagination. |
||
142 | * |
||
143 | * @var bool |
||
144 | */ |
||
145 | protected $usePagination = true; |
||
146 | |||
147 | /** |
||
148 | * If grid use per-page selector. |
||
149 | * |
||
150 | * @var bool |
||
151 | */ |
||
152 | protected $usePerPageSelector = true; |
||
153 | |||
154 | /** |
||
155 | * Is grid rows orderable. |
||
156 | * |
||
157 | * @var bool |
||
158 | */ |
||
159 | protected $orderable = false; |
||
160 | |||
161 | /** |
||
162 | * @var Exporter |
||
163 | */ |
||
164 | protected $exporter; |
||
165 | |||
166 | /** |
||
167 | * View for grid to render. |
||
168 | * |
||
169 | * @var string |
||
170 | */ |
||
171 | protected $view = 'admin::grid.table'; |
||
172 | |||
173 | /** |
||
174 | * Per-page options. |
||
175 | * |
||
176 | * @var array |
||
177 | */ |
||
178 | protected $perPages = [10, 20, 30, 50, 100]; |
||
179 | |||
180 | /** |
||
181 | * Default items count per-page. |
||
182 | * |
||
183 | * @var int |
||
184 | */ |
||
185 | protected $perPage = 20; |
||
186 | |||
187 | /** |
||
188 | * Create a new grid instance. |
||
189 | * |
||
190 | * @param Eloquent $model |
||
191 | * @param callable $builder |
||
192 | */ |
||
193 | public function __construct(Eloquent $model, Closure $builder) |
||
204 | |||
205 | /** |
||
206 | * Get primary key name of model. |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getKeyName() |
||
214 | |||
215 | /** |
||
216 | * Add column to Grid. |
||
217 | * |
||
218 | * @param string $name |
||
219 | * @param string $label |
||
220 | * |
||
221 | * @return Column |
||
222 | */ |
||
223 | public function column($name, $label = '') |
||
244 | |||
245 | /** |
||
246 | * Batch add column to grid. |
||
247 | * |
||
248 | * @example |
||
249 | * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]); |
||
250 | * 2.$grid->columns('name', 'email' ...) |
||
251 | * |
||
252 | * @param array $columns |
||
253 | * |
||
254 | * @return Collection|void |
||
255 | */ |
||
256 | public function columns($columns = []) |
||
274 | |||
275 | /** |
||
276 | * Add column to grid. |
||
277 | * |
||
278 | * @param string $column |
||
279 | * @param string $label |
||
280 | * |
||
281 | * @return Column |
||
282 | */ |
||
283 | protected function addColumn($column = '', $label = '') |
||
290 | |||
291 | /** |
||
292 | * Add a blank column. |
||
293 | * |
||
294 | * @param $label |
||
295 | * |
||
296 | * @return Column |
||
297 | */ |
||
298 | public function blank($label) |
||
302 | |||
303 | /** |
||
304 | * Get Grid model. |
||
305 | * |
||
306 | * @return Model |
||
307 | */ |
||
308 | public function model() |
||
312 | |||
313 | /** |
||
314 | * Paginate the grid. |
||
315 | * |
||
316 | * @param int $perPage |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | public function paginate($perPage = 20) |
||
326 | |||
327 | /** |
||
328 | * Get the grid paginator. |
||
329 | * |
||
330 | * @return mixed |
||
331 | */ |
||
332 | public function paginator() |
||
344 | |||
345 | /** |
||
346 | * Build the grid. |
||
347 | * |
||
348 | * @return void |
||
349 | */ |
||
350 | public function build() |
||
368 | |||
369 | /** |
||
370 | * Process the grid filter. |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | public function processFilter() |
||
380 | |||
381 | /** |
||
382 | * Build the grid rows. |
||
383 | * |
||
384 | * @param array $data |
||
385 | * |
||
386 | * @return void |
||
387 | */ |
||
388 | protected function buildRows(array $data) |
||
403 | |||
404 | /** |
||
405 | * Set grid row callback function. |
||
406 | * |
||
407 | * @param callable $callable |
||
408 | * |
||
409 | * @return Collection|void |
||
410 | */ |
||
411 | public function rows(Closure $callable = null) |
||
419 | |||
420 | /** |
||
421 | * Setup grid filter. |
||
422 | * |
||
423 | * @return void |
||
424 | */ |
||
425 | protected function setupFilter() |
||
429 | |||
430 | /** |
||
431 | * Setup grid exporter. |
||
432 | * |
||
433 | * @return void |
||
434 | */ |
||
435 | protected function setupExporter() |
||
443 | |||
444 | /** |
||
445 | * Export url. |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | public function exportUrl() |
||
456 | |||
457 | /** |
||
458 | * If allow batch delete. |
||
459 | * |
||
460 | * @return bool |
||
461 | */ |
||
462 | public function allowBatchDeletion() |
||
466 | |||
467 | /** |
||
468 | * Disable batch deletion. |
||
469 | * |
||
470 | * @return $this |
||
471 | */ |
||
472 | public function disableBatchDeletion() |
||
478 | |||
479 | /** |
||
480 | * Disable creation. |
||
481 | * |
||
482 | * @return $this |
||
483 | */ |
||
484 | public function disableCreation() |
||
490 | |||
491 | /** |
||
492 | * If allow creation. |
||
493 | * |
||
494 | * @return bool |
||
495 | */ |
||
496 | public function allowCreation() |
||
500 | |||
501 | /** |
||
502 | * If allow actions. |
||
503 | * |
||
504 | * @return bool |
||
505 | */ |
||
506 | public function allowActions() |
||
510 | |||
511 | /** |
||
512 | * Disable all actions. |
||
513 | * |
||
514 | * @return $this |
||
515 | */ |
||
516 | public function disableActions() |
||
522 | |||
523 | /** |
||
524 | * If grid allows export.s. |
||
525 | * |
||
526 | * @return bool |
||
527 | */ |
||
528 | public function allowExport() |
||
532 | |||
533 | /** |
||
534 | * Disable export. |
||
535 | * |
||
536 | * @return $this |
||
537 | */ |
||
538 | public function disableExport() |
||
544 | |||
545 | /** |
||
546 | * Disable grid filter. |
||
547 | * |
||
548 | * @return $this |
||
549 | */ |
||
550 | public function disableFilter() |
||
556 | |||
557 | /** |
||
558 | * Disable grid pagination. |
||
559 | * |
||
560 | * @return $this |
||
561 | */ |
||
562 | public function disablePagination() |
||
570 | |||
571 | /** |
||
572 | * If this grid use pagination. |
||
573 | * |
||
574 | * @return bool |
||
575 | */ |
||
576 | public function usePagination() |
||
580 | |||
581 | /** |
||
582 | * Disable grid per-page selector. |
||
583 | */ |
||
584 | public function disablePerPageSelector() |
||
590 | |||
591 | /** |
||
592 | * If this grid use per-page selector. |
||
593 | * |
||
594 | * @return bool |
||
595 | */ |
||
596 | public function usePerPageSelector() |
||
600 | |||
601 | /** |
||
602 | * Set grid as orderable. |
||
603 | * |
||
604 | * @return $this |
||
605 | */ |
||
606 | public function orderable() |
||
612 | |||
613 | /** |
||
614 | * Is the grid orderable. |
||
615 | * |
||
616 | * @return bool |
||
617 | */ |
||
618 | public function isOrderable() |
||
622 | |||
623 | /** |
||
624 | * Set the grid filter. |
||
625 | * |
||
626 | * @param callable $callback |
||
627 | */ |
||
628 | public function filter(Closure $callback) |
||
632 | |||
633 | /** |
||
634 | * Render the grid filter. |
||
635 | * |
||
636 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
637 | */ |
||
638 | public function renderFilter() |
||
646 | |||
647 | /** |
||
648 | * Set per-page options. |
||
649 | * |
||
650 | * @param array $perPages |
||
651 | */ |
||
652 | public function perPages(array $perPages) |
||
656 | |||
657 | /** |
||
658 | * Generate per-page options. |
||
659 | * |
||
660 | * @return string |
||
661 | */ |
||
662 | public function perPageOptions() |
||
681 | |||
682 | /** |
||
683 | * Get current resource uri. |
||
684 | * |
||
685 | * @param string $path |
||
686 | * |
||
687 | * @return string |
||
688 | */ |
||
689 | public function resource($path = null) |
||
703 | |||
704 | /** |
||
705 | * Handle table column for grid. |
||
706 | * |
||
707 | * @param string $method |
||
708 | * @param string $label |
||
709 | * |
||
710 | * @return bool|Column |
||
711 | */ |
||
712 | protected function handleTableColumn($method, $label) |
||
722 | |||
723 | /** |
||
724 | * Handle get mutator column for grid. |
||
725 | * |
||
726 | * @param string $method |
||
727 | * @param string $label |
||
728 | * |
||
729 | * @return bool|Column |
||
730 | */ |
||
731 | protected function handleGetMutatorColumn($method, $label) |
||
739 | |||
740 | /** |
||
741 | * Handle relation column for grid. |
||
742 | * |
||
743 | * @param string $method |
||
744 | * @param string $label |
||
745 | * |
||
746 | * @return bool|Column |
||
747 | */ |
||
748 | protected function handleRelationColumn($method, $label) |
||
774 | |||
775 | /** |
||
776 | * Dynamically add columns to the grid view. |
||
777 | * |
||
778 | * @param $method |
||
779 | * @param $arguments |
||
780 | * |
||
781 | * @return $this|Column |
||
782 | */ |
||
783 | public function __call($method, $arguments) |
||
805 | |||
806 | /** |
||
807 | * Add variables to grid view. |
||
808 | * |
||
809 | * @param array $variables |
||
810 | * |
||
811 | * @return $this |
||
812 | */ |
||
813 | public function with($variables = []) |
||
819 | |||
820 | /** |
||
821 | * Get all variables will used in grid view. |
||
822 | * |
||
823 | * @return array |
||
824 | */ |
||
825 | protected function variables() |
||
831 | |||
832 | /** |
||
833 | * Set a view to render. |
||
834 | * |
||
835 | * @param string $view |
||
836 | * @param array $variables |
||
837 | */ |
||
838 | public function view($view, $variables = []) |
||
846 | |||
847 | /** |
||
848 | * Get the string contents of the grid view. |
||
849 | * |
||
850 | * @return string |
||
851 | */ |
||
852 | public function render() |
||
864 | |||
865 | /** |
||
866 | * Js code for grid. |
||
867 | * |
||
868 | * @return string |
||
869 | */ |
||
870 | public function script() |
||
947 | |||
948 | /** |
||
949 | * Get the string contents of the grid view. |
||
950 | * |
||
951 | * @return string |
||
952 | */ |
||
953 | public function __toString() |
||
957 | } |
||
958 |
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: