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 |
||
23 | class Grid |
||
24 | { |
||
25 | use Concerns\HasElementNames, |
||
26 | Concerns\HasHeader, |
||
27 | Concerns\HasFooter, |
||
28 | Concerns\HasFilter, |
||
29 | Concerns\HasTools, |
||
30 | Concerns\HasTotalRow, |
||
31 | Concerns\HasHotKeys, |
||
32 | Concerns\HasQuickCreate, |
||
33 | Concerns\HasActions, |
||
34 | Concerns\HasSelector, |
||
35 | Concerns\CanHidesColumns, |
||
36 | Concerns\CanFixColumns, |
||
37 | Macroable { |
||
38 | __call as macroCall; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * The grid data model instance. |
||
43 | * |
||
44 | * @var \Encore\Admin\Grid\Model |
||
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 | * Export driver. |
||
113 | * |
||
114 | * @var string |
||
115 | */ |
||
116 | protected $exporter; |
||
117 | |||
118 | /** |
||
119 | * View for grid to render. |
||
120 | * |
||
121 | * @var string |
||
122 | */ |
||
123 | protected $view = 'admin::grid.table'; |
||
124 | |||
125 | /** |
||
126 | * Per-page options. |
||
127 | * |
||
128 | * @var array |
||
129 | */ |
||
130 | public $perPages = [10, 20, 30, 50, 100]; |
||
131 | |||
132 | /** |
||
133 | * Default items count per-page. |
||
134 | * |
||
135 | * @var int |
||
136 | */ |
||
137 | public $perPage = 20; |
||
138 | |||
139 | /** |
||
140 | * @var []callable |
||
141 | */ |
||
142 | protected $renderingCallbacks = []; |
||
143 | |||
144 | /** |
||
145 | * Options for grid. |
||
146 | * |
||
147 | * @var array |
||
148 | */ |
||
149 | protected $options = [ |
||
150 | 'show_pagination' => true, |
||
151 | 'show_tools' => true, |
||
152 | 'show_filter' => true, |
||
153 | 'show_exporter' => true, |
||
154 | 'show_actions' => true, |
||
155 | 'show_row_selector' => true, |
||
156 | 'show_create_btn' => true, |
||
157 | 'show_column_selector' => true, |
||
158 | ]; |
||
159 | |||
160 | /** |
||
161 | * @var string |
||
162 | */ |
||
163 | public $tableID; |
||
164 | |||
165 | /** |
||
166 | * Initialization closure array. |
||
167 | * |
||
168 | * @var []Closure |
||
169 | */ |
||
170 | protected static $initCallbacks = []; |
||
171 | |||
172 | /** |
||
173 | * Create a new grid instance. |
||
174 | * |
||
175 | * @param Eloquent $model |
||
176 | * @param Closure $builder |
||
177 | */ |
||
178 | public function __construct(Eloquent $model, Closure $builder = null) |
||
179 | { |
||
180 | $this->model = new Model($model, $this); |
||
181 | $this->keyName = $model->getKeyName(); |
||
182 | $this->builder = $builder; |
||
183 | |||
184 | $this->initialize(); |
||
185 | |||
186 | $this->handleExportRequest(); |
||
187 | |||
188 | $this->callInitCallbacks(); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Initialize. |
||
193 | */ |
||
194 | protected function initialize() |
||
195 | { |
||
196 | $this->tableID = uniqid('grid-table'); |
||
197 | |||
198 | $this->columns = Collection::make(); |
||
199 | $this->rows = Collection::make(); |
||
200 | |||
201 | $this->initTools() |
||
202 | ->initFilter(); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Initialize with user pre-defined default disables and exporter, etc. |
||
207 | * |
||
208 | * @param Closure $callback |
||
209 | */ |
||
210 | public static function init(Closure $callback = null) |
||
211 | { |
||
212 | static::$initCallbacks[] = $callback; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Call the initialization closure array in sequence. |
||
217 | */ |
||
218 | protected function callInitCallbacks() |
||
219 | { |
||
220 | if (empty(static::$initCallbacks)) { |
||
221 | return; |
||
222 | } |
||
223 | |||
224 | foreach (static::$initCallbacks as $callback) { |
||
225 | call_user_func($callback, $this); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Handle export request. |
||
231 | * |
||
232 | * @param bool $forceExport |
||
233 | */ |
||
234 | protected function handleExportRequest($forceExport = false) |
||
257 | |||
258 | /** |
||
259 | * @param string $scope |
||
260 | * |
||
261 | * @return AbstractExporter |
||
262 | */ |
||
263 | protected function getExporter($scope) |
||
267 | |||
268 | /** |
||
269 | * Get or set option for grid. |
||
270 | * |
||
271 | * @param string $key |
||
272 | * @param mixed $value |
||
273 | * |
||
274 | * @return $this|mixed |
||
275 | */ |
||
276 | public function option($key, $value = null) |
||
286 | |||
287 | /** |
||
288 | * Get primary key name of model. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | public function getKeyName() |
||
296 | |||
297 | /** |
||
298 | * Add a column to Grid. |
||
299 | * |
||
300 | * @param string $name |
||
301 | * @param string $label |
||
302 | * |
||
303 | * @return Column |
||
304 | */ |
||
305 | public function column($name, $label = '') |
||
317 | |||
318 | /** |
||
319 | * Batch add column to grid. |
||
320 | * |
||
321 | * @example |
||
322 | * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]); |
||
323 | * 2.$grid->columns('name', 'email' ...) |
||
324 | * |
||
325 | * @param array $columns |
||
326 | * |
||
327 | * @return Collection|null |
||
328 | */ |
||
329 | public function columns($columns = []) |
||
347 | |||
348 | /** |
||
349 | * Add column to grid. |
||
350 | * |
||
351 | * @param string $column |
||
352 | * @param string $label |
||
353 | * |
||
354 | * @return Column |
||
355 | */ |
||
356 | View Code Duplication | protected function addColumn($column = '', $label = '') |
|
365 | |||
366 | /** |
||
367 | * Add a relation column to grid. |
||
368 | * |
||
369 | * @param string $name |
||
370 | * @param string $label |
||
371 | * |
||
372 | * @return $this|bool|Column |
||
373 | */ |
||
374 | protected function addRelationColumn($name, $label = '') |
||
394 | |||
395 | /** |
||
396 | * Add a json type column to grid. |
||
397 | * |
||
398 | * @param string $name |
||
399 | * @param string $label |
||
400 | * |
||
401 | * @return Column |
||
402 | */ |
||
403 | protected function addJsonColumn($name, $label = '') |
||
411 | |||
412 | /** |
||
413 | * Prepend column to grid. |
||
414 | * |
||
415 | * @param string $column |
||
416 | * @param string $label |
||
417 | * |
||
418 | * @return Column |
||
419 | */ |
||
420 | View Code Duplication | protected function prependColumn($column = '', $label = '') |
|
429 | |||
430 | /** |
||
431 | * Get Grid model. |
||
432 | * |
||
433 | * @return Model |
||
434 | */ |
||
435 | public function model() |
||
439 | |||
440 | /** |
||
441 | * Paginate the grid. |
||
442 | * |
||
443 | * @param int $perPage |
||
444 | * |
||
445 | * @return void |
||
446 | */ |
||
447 | public function paginate($perPage = 20) |
||
453 | |||
454 | /** |
||
455 | * Get the grid paginator. |
||
456 | * |
||
457 | * @return mixed |
||
458 | */ |
||
459 | public function paginator() |
||
463 | |||
464 | /** |
||
465 | * Disable grid pagination. |
||
466 | * |
||
467 | * @return $this |
||
468 | */ |
||
469 | public function disablePagination(bool $disable = true) |
||
475 | |||
476 | /** |
||
477 | * If this grid use pagination. |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | public function showPagination() |
||
485 | |||
486 | /** |
||
487 | * Set per-page options. |
||
488 | * |
||
489 | * @param array $perPages |
||
490 | */ |
||
491 | public function perPages(array $perPages) |
||
495 | |||
496 | /** |
||
497 | * Disable row selector. |
||
498 | * |
||
499 | * @return Grid|mixed |
||
500 | */ |
||
501 | public function disableRowSelector(bool $disable = true) |
||
505 | |||
506 | /** |
||
507 | * Prepend checkbox column for grid. |
||
508 | * |
||
509 | * @return void |
||
510 | */ |
||
511 | protected function prependRowSelectorColumn() |
||
523 | |||
524 | /** |
||
525 | * Apply column filter to grid query. |
||
526 | * |
||
527 | * @return void |
||
528 | */ |
||
529 | protected function applyColumnFilter() |
||
533 | |||
534 | /** |
||
535 | * @return array|Collection|mixed |
||
536 | */ |
||
537 | protected function applyQuery() |
||
547 | |||
548 | /** |
||
549 | * Add row selector columns and action columns before and after the grid. |
||
550 | * |
||
551 | * @return void |
||
552 | */ |
||
553 | protected function addDefaultColumns() |
||
559 | |||
560 | /** |
||
561 | * Build the grid. |
||
562 | * |
||
563 | * @return void |
||
564 | */ |
||
565 | public function build() |
||
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 | * Set exporter driver for Grid to export. |
||
626 | * |
||
627 | * @param $exporter |
||
628 | * |
||
629 | * @return $this |
||
630 | */ |
||
631 | public function exporter($exporter) |
||
637 | |||
638 | /** |
||
639 | * Get the export url. |
||
640 | * |
||
641 | * @param int $scope |
||
642 | * @param null $args |
||
643 | * |
||
644 | * @return string |
||
645 | */ |
||
646 | public function getExportUrl($scope = 1, $args = null) |
||
656 | |||
657 | /** |
||
658 | * Get create url. |
||
659 | * |
||
660 | * @return string |
||
661 | */ |
||
662 | public function getCreateUrl() |
||
675 | |||
676 | /** |
||
677 | * If grid show export btn. |
||
678 | * |
||
679 | * @return bool |
||
680 | */ |
||
681 | public function showExportBtn() |
||
685 | |||
686 | /** |
||
687 | * Disable export. |
||
688 | * |
||
689 | * @return $this |
||
690 | */ |
||
691 | public function disableExport(bool $disable = true) |
||
695 | |||
696 | /** |
||
697 | * Render export button. |
||
698 | * |
||
699 | * @return string |
||
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(bool $disable = true) |
||
727 | |||
728 | /** |
||
729 | * If allow creation. |
||
730 | * |
||
731 | * @return bool |
||
732 | */ |
||
733 | public function showCreateBtn() |
||
737 | |||
738 | /** |
||
739 | * Render create button for grid. |
||
740 | * |
||
741 | * @return string |
||
742 | */ |
||
743 | public function renderCreateButton() |
||
747 | |||
748 | /** |
||
749 | * Get current resource uri. |
||
750 | * |
||
751 | * @param string $path |
||
752 | * |
||
753 | * @return string |
||
754 | */ |
||
755 | public function resource($path = null) |
||
769 | |||
770 | /** |
||
771 | * Handle get mutator column for grid. |
||
772 | * |
||
773 | * @param string $method |
||
774 | * @param string $label |
||
775 | * |
||
776 | * @return bool|Column |
||
777 | */ |
||
778 | protected function handleGetMutatorColumn($method, $label) |
||
786 | |||
787 | /** |
||
788 | * Handle relation column for grid. |
||
789 | * |
||
790 | * @param string $method |
||
791 | * @param string $label |
||
792 | * |
||
793 | * @return bool|Column |
||
794 | */ |
||
795 | protected function handleRelationColumn($method, $label) |
||
828 | |||
829 | /** |
||
830 | * Dynamically add columns to the grid view. |
||
831 | * |
||
832 | * @param $method |
||
833 | * @param $arguments |
||
834 | * |
||
835 | * @return Column |
||
836 | */ |
||
837 | public function __call($method, $arguments) |
||
859 | |||
860 | /** |
||
861 | * Add variables to grid view. |
||
862 | * |
||
863 | * @param array $variables |
||
864 | * |
||
865 | * @return $this |
||
866 | */ |
||
867 | public function with($variables = []) |
||
873 | |||
874 | /** |
||
875 | * Get all variables will used in grid view. |
||
876 | * |
||
877 | * @return array |
||
878 | */ |
||
879 | protected function variables() |
||
885 | |||
886 | /** |
||
887 | * Set a view to render. |
||
888 | * |
||
889 | * @param string $view |
||
890 | * @param array $variables |
||
891 | */ |
||
892 | public function setView($view, $variables = []) |
||
900 | |||
901 | /** |
||
902 | * Set grid title. |
||
903 | * |
||
904 | * @param string $title |
||
905 | * |
||
906 | * @return $this |
||
907 | */ |
||
908 | public function setTitle($title) |
||
914 | |||
915 | /** |
||
916 | * Set relation for grid. |
||
917 | * |
||
918 | * @param Relations\Relation $relation |
||
919 | * |
||
920 | * @return $this |
||
921 | */ |
||
922 | public function setRelation(Relations\Relation $relation) |
||
928 | |||
929 | /** |
||
930 | * Set resource path for grid. |
||
931 | * |
||
932 | * @param string $path |
||
933 | * |
||
934 | * @return $this |
||
935 | */ |
||
936 | public function setResource($path) |
||
942 | |||
943 | /** |
||
944 | * Set rendering callback. |
||
945 | * |
||
946 | * @param callable $callback |
||
947 | * |
||
948 | * @return $this |
||
949 | */ |
||
950 | public function rendering(callable $callback) |
||
956 | |||
957 | /** |
||
958 | * Call callbacks before render. |
||
959 | * |
||
960 | * @return void |
||
961 | */ |
||
962 | protected function callRenderingCallback() |
||
968 | |||
969 | /** |
||
970 | * Get the string contents of the grid view. |
||
971 | * |
||
972 | * @return string |
||
973 | */ |
||
974 | public function render() |
||
988 | } |
||
989 |