Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 Concerns\HasElementNames, |
||
24 | Concerns\HasHeader, |
||
25 | Concerns\HasFooter, |
||
26 | Concerns\HasFilter, |
||
27 | Concerns\HasTools, |
||
28 | Concerns\HasTotalRow, |
||
29 | Concerns\HasHotKeys, |
||
30 | Concerns\HasQuickCreate, |
||
31 | Concerns\HasActions, |
||
32 | Concerns\HasSelector, |
||
33 | Concerns\CanHidesColumns, |
||
34 | Concerns\CanFixColumns, |
||
35 | Concerns\CanExportGrid, |
||
36 | ShouldSnakeAttributes, |
||
37 | Macroable { |
||
38 | __call as macroCall; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * The grid data model instance. |
||
43 | * |
||
44 | * @var \Encore\Admin\Grid\Model|\Illuminate\Database\Eloquent\Builder |
||
45 | */ |
||
46 | protected $model; |
||
47 | |||
48 | /** |
||
49 | * Collection of all grid columns. |
||
50 | * |
||
51 | * @var \Illuminate\Support\Collection |
||
52 | */ |
||
53 | protected $columns; |
||
54 | |||
55 | /** |
||
56 | * Collection of all data rows. |
||
57 | * |
||
58 | * @var \Illuminate\Support\Collection |
||
59 | */ |
||
60 | protected $rows; |
||
61 | |||
62 | /** |
||
63 | * Rows callable fucntion. |
||
64 | * |
||
65 | * @var \Closure |
||
66 | */ |
||
67 | protected $rowsCallback; |
||
68 | |||
69 | /** |
||
70 | * All column names of the grid. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | public $columnNames = []; |
||
75 | |||
76 | /** |
||
77 | * Grid builder. |
||
78 | * |
||
79 | * @var \Closure |
||
80 | */ |
||
81 | protected $builder; |
||
82 | |||
83 | /** |
||
84 | * Mark if the grid is builded. |
||
85 | * |
||
86 | * @var bool |
||
87 | */ |
||
88 | protected $builded = false; |
||
89 | |||
90 | /** |
||
91 | * All variables in grid view. |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $variables = []; |
||
96 | |||
97 | /** |
||
98 | * Resource path of the grid. |
||
99 | * |
||
100 | * @var |
||
101 | */ |
||
102 | protected $resourcePath; |
||
103 | |||
104 | /** |
||
105 | * Default primary key name. |
||
106 | * |
||
107 | * @var string |
||
108 | */ |
||
109 | protected $keyName = 'id'; |
||
110 | |||
111 | /** |
||
112 | * View for grid to render. |
||
113 | * |
||
114 | * @var string |
||
115 | */ |
||
116 | protected $view = 'admin::grid.table'; |
||
117 | |||
118 | /** |
||
119 | * Per-page options. |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | public $perPages = [10, 20, 30, 50, 100]; |
||
124 | |||
125 | /** |
||
126 | * Default items count per-page. |
||
127 | * |
||
128 | * @var int |
||
129 | */ |
||
130 | public $perPage = 20; |
||
131 | |||
132 | /** |
||
133 | * @var []callable |
||
134 | */ |
||
135 | protected $renderingCallbacks = []; |
||
136 | |||
137 | /** |
||
138 | * Options for grid. |
||
139 | * |
||
140 | * @var array |
||
141 | */ |
||
142 | protected $options = [ |
||
143 | 'show_pagination' => true, |
||
144 | 'show_tools' => true, |
||
145 | 'show_filter' => true, |
||
146 | 'show_exporter' => true, |
||
147 | 'show_actions' => true, |
||
148 | 'show_row_selector' => true, |
||
149 | 'show_create_btn' => true, |
||
150 | 'show_column_selector' => true, |
||
151 | 'show_define_empty_page' => true, |
||
152 | ]; |
||
153 | |||
154 | /** |
||
155 | * @var string |
||
156 | */ |
||
157 | public $tableID; |
||
158 | |||
159 | /** |
||
160 | * Initialization closure array. |
||
161 | * |
||
162 | * @var []Closure |
||
163 | */ |
||
164 | protected static $initCallbacks = []; |
||
165 | |||
166 | /** |
||
167 | * Create a new grid instance. |
||
168 | * |
||
169 | * @param Eloquent $model |
||
170 | * @param Closure $builder |
||
171 | */ |
||
172 | public function __construct(Eloquent $model, Closure $builder = null) |
||
173 | { |
||
174 | $this->model = new Model($model, $this); |
||
175 | $this->keyName = $model->getKeyName(); |
||
176 | $this->builder = $builder; |
||
177 | |||
178 | $this->initialize(); |
||
179 | |||
180 | $this->callInitCallbacks(); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Initialize. |
||
185 | */ |
||
186 | protected function initialize() |
||
187 | { |
||
188 | $this->tableID = uniqid('grid-table'); |
||
189 | |||
190 | $this->columns = Collection::make(); |
||
191 | $this->rows = Collection::make(); |
||
192 | |||
193 | $this->initTools() |
||
194 | ->initFilter(); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Initialize with user pre-defined default disables and exporter, etc. |
||
199 | * |
||
200 | * @param Closure $callback |
||
201 | */ |
||
202 | public static function init(Closure $callback = null) |
||
206 | |||
207 | /** |
||
208 | * Call the initialization closure array in sequence. |
||
209 | */ |
||
210 | protected function callInitCallbacks() |
||
220 | |||
221 | /** |
||
222 | * Get or set option for grid. |
||
223 | * |
||
224 | * @param string $key |
||
225 | * @param mixed $value |
||
226 | * |
||
227 | * @return $this|mixed |
||
228 | */ |
||
229 | public function option($key, $value = null) |
||
239 | |||
240 | /** |
||
241 | * Get primary key name of model. |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | public function getKeyName() |
||
249 | |||
250 | /** |
||
251 | * Add a column to Grid. |
||
252 | * |
||
253 | * @param string $name |
||
254 | * @param string $label |
||
255 | * |
||
256 | * @return Column |
||
257 | */ |
||
258 | public function column($name, $label = '') |
||
270 | |||
271 | /** |
||
272 | * Batch add column to grid. |
||
273 | * |
||
274 | * @example |
||
275 | * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]); |
||
276 | * 2.$grid->columns('name', 'email' ...) |
||
277 | * |
||
278 | * @param array $columns |
||
279 | * |
||
280 | * @return Collection|null |
||
281 | */ |
||
282 | public function columns($columns = []) |
||
300 | |||
301 | /** |
||
302 | * Add column to grid. |
||
303 | * |
||
304 | * @param string $column |
||
305 | * @param string $label |
||
306 | * |
||
307 | * @return Column |
||
308 | */ |
||
309 | View Code Duplication | protected function addColumn($column = '', $label = '') |
|
318 | |||
319 | /** |
||
320 | * Get all columns object. |
||
321 | * |
||
322 | * @return Collection |
||
323 | */ |
||
324 | public function getColumns() |
||
328 | |||
329 | /** |
||
330 | * Add a relation column to grid. |
||
331 | * |
||
332 | * @param string $name |
||
333 | * @param string $label |
||
334 | * |
||
335 | * @return $this|bool|Column |
||
336 | */ |
||
337 | protected function addRelationColumn($name, $label = '') |
||
357 | |||
358 | /** |
||
359 | * Add a json type column to grid. |
||
360 | * |
||
361 | * @param string $name |
||
362 | * @param string $label |
||
363 | * |
||
364 | * @return Column |
||
365 | */ |
||
366 | protected function addJsonColumn($name, $label = '') |
||
374 | |||
375 | /** |
||
376 | * Prepend column to grid. |
||
377 | * |
||
378 | * @param string $column |
||
379 | * @param string $label |
||
380 | * |
||
381 | * @return Column |
||
382 | */ |
||
383 | View Code Duplication | protected function prependColumn($column = '', $label = '') |
|
392 | |||
393 | /** |
||
394 | * Get Grid model. |
||
395 | * |
||
396 | * @return Model|\Illuminate\Database\Eloquent\Builder |
||
397 | */ |
||
398 | public function model() |
||
402 | |||
403 | /** |
||
404 | * Paginate the grid. |
||
405 | * |
||
406 | * @param int $perPage |
||
407 | * |
||
408 | * @return void |
||
409 | */ |
||
410 | public function paginate($perPage = 20) |
||
416 | |||
417 | /** |
||
418 | * Get the grid paginator. |
||
419 | * |
||
420 | * @return mixed |
||
421 | */ |
||
422 | public function paginator() |
||
426 | |||
427 | /** |
||
428 | * Disable grid pagination. |
||
429 | * |
||
430 | * @return $this |
||
431 | */ |
||
432 | public function disablePagination(bool $disable = true) |
||
438 | |||
439 | /** |
||
440 | * If this grid use pagination. |
||
441 | * |
||
442 | * @return bool |
||
443 | */ |
||
444 | public function showPagination() |
||
448 | |||
449 | /** |
||
450 | * Set per-page options. |
||
451 | * |
||
452 | * @param array $perPages |
||
453 | */ |
||
454 | public function perPages(array $perPages) |
||
458 | |||
459 | /** |
||
460 | * Disable row selector. |
||
461 | * |
||
462 | * @return Grid|mixed |
||
463 | */ |
||
464 | public function disableRowSelector(bool $disable = true) |
||
468 | |||
469 | /** |
||
470 | * Prepend checkbox column for grid. |
||
471 | * |
||
472 | * @return void |
||
473 | */ |
||
474 | protected function prependRowSelectorColumn() |
||
486 | |||
487 | /** |
||
488 | * Apply column filter to grid query. |
||
489 | * |
||
490 | * @return void |
||
491 | */ |
||
492 | protected function applyColumnFilter() |
||
496 | |||
497 | /** |
||
498 | * Apply column search to grid query. |
||
499 | * |
||
500 | * @return void |
||
501 | */ |
||
502 | protected function applyColumnSearch() |
||
506 | |||
507 | /** |
||
508 | * @return array|Collection|mixed |
||
509 | */ |
||
510 | public function applyQuery() |
||
520 | |||
521 | /** |
||
522 | * Add row selector columns and action columns before and after the grid. |
||
523 | * |
||
524 | * @return void |
||
525 | */ |
||
526 | protected function addDefaultColumns() |
||
532 | |||
533 | /** |
||
534 | * Build the grid. |
||
535 | * |
||
536 | * @return void |
||
537 | */ |
||
538 | public function build() |
||
564 | |||
565 | /** |
||
566 | * Build the grid rows. |
||
567 | * |
||
568 | * @param array $data |
||
569 | * |
||
570 | * @return void |
||
571 | */ |
||
572 | protected function buildRows(array $data) |
||
582 | |||
583 | /** |
||
584 | * Set grid row callback function. |
||
585 | * |
||
586 | * @param Closure $callable |
||
587 | * |
||
588 | * @return Collection|null |
||
589 | */ |
||
590 | public function rows(Closure $callable = null) |
||
598 | |||
599 | /** |
||
600 | * Get create url. |
||
601 | * |
||
602 | * @return string |
||
603 | */ |
||
604 | public function getCreateUrl() |
||
618 | |||
619 | /** |
||
620 | * Alias for method `disableCreateButton`. |
||
621 | * |
||
622 | * @return $this |
||
623 | * |
||
624 | * @deprecated |
||
625 | */ |
||
626 | public function disableCreation() |
||
630 | |||
631 | /** |
||
632 | * Remove create button on grid. |
||
633 | * |
||
634 | * @return $this |
||
635 | */ |
||
636 | public function disableCreateButton(bool $disable = true) |
||
640 | |||
641 | /** |
||
642 | * Remove define empty page on grid. |
||
643 | * |
||
644 | * @return $this |
||
645 | */ |
||
646 | public function disableDefineEmptyPage(bool $disable = true) |
||
650 | |||
651 | /** |
||
652 | * If grid show define empty page on grid. |
||
653 | * |
||
654 | * @return bool |
||
655 | */ |
||
656 | public function showDefineEmptyPage() |
||
660 | |||
661 | /** |
||
662 | * If allow creation. |
||
663 | * |
||
664 | * @return bool |
||
665 | */ |
||
666 | public function showCreateBtn() |
||
670 | |||
671 | /** |
||
672 | * Render create button for grid. |
||
673 | * |
||
674 | * @return string |
||
675 | */ |
||
676 | public function renderCreateButton() |
||
680 | |||
681 | /** |
||
682 | * Get current resource url. |
||
683 | * |
||
684 | * @param string $path |
||
685 | * |
||
686 | * @return string |
||
687 | */ |
||
688 | public function resource($path = null) |
||
702 | |||
703 | /** |
||
704 | * Handle get mutator column for grid. |
||
705 | * |
||
706 | * @param string $method |
||
707 | * @param string $label |
||
708 | * |
||
709 | * @return bool|Column |
||
710 | */ |
||
711 | protected function handleGetMutatorColumn($method, $label) |
||
719 | |||
720 | /** |
||
721 | * Handle relation column for grid. |
||
722 | * |
||
723 | * @param string $method |
||
724 | * @param string $label |
||
725 | * |
||
726 | * @return bool|Column |
||
727 | */ |
||
728 | protected function handleRelationColumn($method, $label) |
||
763 | |||
764 | /** |
||
765 | * Dynamically add columns to the grid view. |
||
766 | * |
||
767 | * @param $method |
||
768 | * @param $arguments |
||
769 | * |
||
770 | * @return Column |
||
771 | */ |
||
772 | public function __call($method, $arguments) |
||
794 | |||
795 | /** |
||
796 | * Add variables to grid view. |
||
797 | * |
||
798 | * @param array $variables |
||
799 | * |
||
800 | * @return $this |
||
801 | */ |
||
802 | public function with($variables = []) |
||
808 | |||
809 | /** |
||
810 | * Get all variables will used in grid view. |
||
811 | * |
||
812 | * @return array |
||
813 | */ |
||
814 | protected function variables() |
||
820 | |||
821 | /** |
||
822 | * Set a view to render. |
||
823 | * |
||
824 | * @param string $view |
||
825 | * @param array $variables |
||
826 | */ |
||
827 | public function setView($view, $variables = []) |
||
835 | |||
836 | /** |
||
837 | * Set grid title. |
||
838 | * |
||
839 | * @param string $title |
||
840 | * |
||
841 | * @return $this |
||
842 | */ |
||
843 | public function setTitle($title) |
||
849 | |||
850 | /** |
||
851 | * Set relation for grid. |
||
852 | * |
||
853 | * @param Relations\Relation $relation |
||
854 | * |
||
855 | * @return $this |
||
856 | */ |
||
857 | public function setRelation(Relations\Relation $relation) |
||
863 | |||
864 | /** |
||
865 | * Set resource path for grid. |
||
866 | * |
||
867 | * @param string $path |
||
868 | * |
||
869 | * @return $this |
||
870 | */ |
||
871 | public function setResource($path) |
||
877 | |||
878 | /** |
||
879 | * Set rendering callback. |
||
880 | * |
||
881 | * @param callable $callback |
||
882 | * |
||
883 | * @return $this |
||
884 | */ |
||
885 | public function rendering(callable $callback) |
||
891 | |||
892 | /** |
||
893 | * Call callbacks before render. |
||
894 | * |
||
895 | * @return void |
||
896 | */ |
||
897 | protected function callRenderingCallback() |
||
903 | |||
904 | /** |
||
905 | * Get the string contents of the grid view. |
||
906 | * |
||
907 | * @return string |
||
908 | */ |
||
909 | public function render() |
||
923 | } |
||
924 |