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 AdminLte 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 AdminLte, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class AdminLte |
||
12 | { |
||
13 | protected $menu; |
||
14 | |||
15 | protected $filters; |
||
16 | |||
17 | protected $events; |
||
18 | |||
19 | protected $container; |
||
20 | |||
21 | 7 | public function __construct( |
|
30 | |||
31 | 5 | public function menu($filterOpt = null) |
|
32 | { |
||
33 | 5 | if (! $this->menu) { |
|
34 | 5 | $this->menu = $this->buildMenu(); |
|
35 | } |
||
36 | |||
37 | // Check for filter option. |
||
38 | |||
39 | 5 | if ($filterOpt == 'sidebar') { |
|
40 | 1 | return array_filter($this->menu, [$this, 'sidebarFilter']); |
|
41 | 4 | } elseif ($filterOpt == 'navbar-left') { |
|
42 | 1 | return array_filter($this->menu, [$this, 'navbarLeftFilter']); |
|
43 | 3 | } elseif ($filterOpt == 'navbar-right') { |
|
44 | 1 | return array_filter($this->menu, [$this, 'navbarRightFilter']); |
|
45 | 2 | } elseif ($filterOpt == 'navbar-user') { |
|
46 | 1 | return array_filter($this->menu, [$this, 'navbarUserMenuFilter']); |
|
47 | } else { |
||
48 | 1 | return $this->menu; |
|
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Gets the body classes, in relation to the config options. |
||
54 | */ |
||
55 | 1 | public function getBodyClasses() |
|
56 | { |
||
57 | 1 | $body_classes = []; |
|
58 | 1 | $screen_sizes = ['xs', 'sm', 'md', 'lg', 'xl']; |
|
59 | |||
60 | // Add classes related to the "sidebar_mini" configuration. |
||
61 | |||
62 | 1 | if (config('adminlte.sidebar_mini', true) === true) { |
|
63 | 1 | $body_classes[] = 'sidebar-mini'; |
|
64 | 1 | } elseif (config('adminlte.sidebar_mini', true) == 'md') { |
|
65 | 1 | $body_classes[] = 'sidebar-mini sidebar-mini-md'; |
|
66 | } |
||
67 | |||
68 | // Add classes related to the "layout_topnav" configuration. |
||
69 | |||
70 | 1 | if (config('adminlte.layout_topnav') || View::getSection('layout_topnav')) { |
|
71 | 1 | $body_classes[] = 'layout-top-nav'; |
|
72 | } |
||
73 | |||
74 | // Add classes related to the "layout_boxed" configuration. |
||
75 | |||
76 | 1 | if (config('adminlte.layout_boxed') || View::getSection('layout_boxed')) { |
|
77 | 1 | $body_classes[] = 'layout-boxed'; |
|
78 | } |
||
79 | |||
80 | // Add classes related to the "sidebar_collapse" configuration. |
||
81 | |||
82 | 1 | if (config('adminlte.sidebar_collapse') || View::getSection('sidebar_collapse')) { |
|
83 | 1 | $body_classes[] = 'sidebar-collapse'; |
|
84 | } |
||
85 | |||
86 | // Add classes related to the "right_sidebar" configuration. |
||
87 | |||
88 | 1 | if (config('adminlte.right_sidebar') && config('adminlte.right_sidebar_push')) { |
|
89 | 1 | $body_classes[] = 'control-sidebar-push'; |
|
90 | } |
||
91 | |||
92 | // Add classes related to fixed sidebar, these are not compatible with |
||
93 | // "layout_topnav". |
||
94 | |||
95 | 1 | if (! config('adminlte.layout_topnav') && ! View::getSection('layout_topnav')) { |
|
96 | |||
97 | // Check for fixed sidebar configuration. |
||
98 | |||
99 | 1 | if (config('adminlte.layout_fixed_sidebar')) { |
|
100 | 1 | $body_classes[] = 'layout-fixed'; |
|
101 | } |
||
102 | } |
||
103 | |||
104 | // Add classes related to fixed footer and navbar, these are not |
||
105 | // compatible with "layout_boxed". |
||
106 | |||
107 | 1 | if (! config('adminlte.layout_boxed') && ! View::getSection('layout_boxed')) { |
|
108 | |||
109 | // Check for fixed navbar configuration. |
||
110 | |||
111 | 1 | $fixed_navbar_cfg = config('adminlte.layout_fixed_navbar'); |
|
112 | |||
113 | 1 | View Code Duplication | if ($fixed_navbar_cfg === true) { |
|
|||
114 | 1 | $body_classes[] = 'layout-navbar-fixed'; |
|
115 | 1 | } elseif (is_array($fixed_navbar_cfg)) { |
|
116 | 1 | foreach ($fixed_navbar_cfg as $size => $enabled) { |
|
117 | 1 | if (in_array($size, $screen_sizes)) { |
|
118 | 1 | $size = $size == 'xs' ? '' : '-'.$size; |
|
119 | 1 | $body_classes[] = $enabled == true ? |
|
120 | 1 | 'layout'.$size.'-navbar-fixed' : |
|
121 | 1 | 'layout'.$size.'-navbar-not-fixed'; |
|
122 | } |
||
123 | } |
||
124 | } |
||
125 | |||
126 | // Check for fixed footer configuration. |
||
127 | |||
128 | 1 | $fixed_footer_cfg = config('adminlte.layout_fixed_footer'); |
|
129 | |||
130 | 1 | View Code Duplication | if ($fixed_footer_cfg === true) { |
131 | 1 | $body_classes[] = 'layout-footer-fixed'; |
|
132 | 1 | } elseif (is_array($fixed_footer_cfg)) { |
|
133 | 1 | foreach ($fixed_footer_cfg as $size => $enabled) { |
|
134 | 1 | if (in_array($size, $screen_sizes)) { |
|
135 | 1 | $size = $size == 'xs' ? '' : '-'.$size; |
|
136 | 1 | $body_classes[] = $enabled == true ? |
|
137 | 1 | 'layout'.$size.'-footer-fixed' : |
|
138 | 1 | 'layout'.$size.'-footer-not-fixed'; |
|
139 | } |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | // Add custom classes, related to the "classes_body" configuration. |
||
145 | |||
146 | 1 | $body_classes[] = config('adminlte.classes_body', ''); |
|
147 | |||
148 | // Return the set of configured classes for the body tag. |
||
149 | |||
150 | 1 | return trim(implode(' ', $body_classes)); |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Gets the body data attributes, in relation to the config options. |
||
155 | */ |
||
156 | 1 | public function getBodyData() |
|
157 | { |
||
158 | 1 | $body_data = []; |
|
159 | |||
160 | // Add data related to the "sidebar_scrollbar_theme" configuration. |
||
161 | |||
162 | 1 | $sb_theme_cfg = config('adminlte.sidebar_scrollbar_theme', 'os-theme-light'); |
|
163 | |||
164 | 1 | if ($sb_theme_cfg != 'os-theme-light') { |
|
165 | 1 | $body_data[] = 'data-scrollbar-theme='.$sb_theme_cfg; |
|
166 | } |
||
167 | |||
168 | // Add data related to the "sidebar_scrollbar_auto_hide" configuration. |
||
169 | |||
170 | 1 | $sb_auto_hide = config('adminlte.sidebar_scrollbar_auto_hide', 'l'); |
|
171 | |||
172 | 1 | if ($sb_auto_hide != 'l') { |
|
173 | 1 | $body_data[] = 'data-scrollbar-auto-hide='.$sb_auto_hide; |
|
174 | } |
||
175 | |||
176 | 1 | return trim(implode(' ', $body_data)); |
|
177 | } |
||
178 | |||
179 | 5 | protected function buildMenu() |
|
187 | |||
188 | 5 | protected function buildFilters() |
|
192 | |||
193 | /** |
||
194 | * Filter method for sidebar menu items. |
||
195 | */ |
||
196 | 1 | private function sidebarFilter($item) |
|
212 | |||
213 | /** |
||
214 | * Filter method for navbar top left menu items. |
||
215 | */ |
||
216 | 1 | private function navbarLeftFilter($item) |
|
232 | |||
233 | /** |
||
234 | * Filter method for navbar top right menu items. |
||
235 | */ |
||
236 | 1 | private function navbarRightFilter($item) |
|
244 | |||
245 | /** |
||
246 | * Filter method for navbar dropdown user menu items. |
||
247 | */ |
||
248 | 1 | private function navbarUserMenuFilter($item) |
|
256 | } |
||
257 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.