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 AdminLtePluginCommand 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 AdminLtePluginCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class AdminLtePluginCommand extends Command |
||
9 | { |
||
10 | protected $signature = 'adminlte:plugins '. |
||
11 | '{operation? : Operation command, Available commands; list, install, update & remove}'. |
||
12 | '{--plugin=* : Plugin Key}'. |
||
13 | '{--interactive : The installation will guide you through the process}'; |
||
14 | |||
15 | protected $description = 'Manages additional plugin files for AdminLTE'; |
||
16 | |||
17 | protected $plugins = [ |
||
18 | 'bootstrapColorpicker' => [ |
||
19 | 'name' => 'Bootstrap Colorpicker', |
||
20 | 'package_path' => 'bootstrap-colorpicker', |
||
21 | 'assets_path' => 'bootstrap-colorpicker', |
||
22 | ], |
||
23 | 'bootstrapSlider' => [ |
||
24 | 'name' => 'Bootstrap Slider', |
||
25 | 'package_path' => 'bootstrap-slider', |
||
26 | 'assets_path' => 'bootstrap-slider', |
||
27 | ], |
||
28 | 'bootstrap4Duallistbox' => [ |
||
29 | 'name' => 'Bootstrap4 Duallistbox', |
||
30 | 'package_path' => 'bootstrap4-duallistbox', |
||
31 | 'assets_path' => 'bootstrap4-duallistbox', |
||
32 | ], |
||
33 | 'chartJs' => [ |
||
34 | 'name' => 'Chart.js', |
||
35 | 'package_path' => 'chart.js', |
||
36 | 'assets_path' => 'chart.js', |
||
37 | ], |
||
38 | 'datatables' => [ |
||
39 | 'name' => 'Datatables', |
||
40 | 'package_path' => [ |
||
41 | 'datatables', |
||
42 | 'datatables-bs4', |
||
43 | ], |
||
44 | 'assets_path' => [ |
||
45 | 'datatables/js', |
||
46 | 'datatables', |
||
47 | ], |
||
48 | ], |
||
49 | 'datatablesPlugins' => [ |
||
50 | 'name' => 'Datatables Plugins', |
||
51 | 'package_path' => [ |
||
52 | 'datatables-autofill', |
||
53 | 'datatables-buttons', |
||
54 | 'datatables-colreorder', |
||
55 | 'datatables-fixedcolumns', |
||
56 | 'datatables-fixedheader', |
||
57 | 'datatables-keytable', |
||
58 | 'datatables-responsive', |
||
59 | 'datatables-rowgroup', |
||
60 | 'datatables-rowreorder', |
||
61 | 'datatables-scroller', |
||
62 | 'datatables-select', |
||
63 | 'pdfmake', |
||
64 | 'jszip', |
||
65 | ], |
||
66 | 'assets_path' => [ |
||
67 | 'datatables-plugins/autofill', |
||
68 | 'datatables-plugins/buttons', |
||
69 | 'datatables-plugins/colreorder', |
||
70 | 'datatables-plugins/fixedcolumns', |
||
71 | 'datatables-plugins/fixedheader', |
||
72 | 'datatables-plugins/keytable', |
||
73 | 'datatables-plugins/responsive', |
||
74 | 'datatables-plugins/rowgroup', |
||
75 | 'datatables-plugins/rowreorder', |
||
76 | 'datatables-plugins/scroller', |
||
77 | 'datatables-plugins/select', |
||
78 | 'datatables-plugins/pdfmake', |
||
79 | 'datatables-plugins/jszip', |
||
80 | ], |
||
81 | ], |
||
82 | 'daterangepicker' => [ |
||
83 | 'name' => 'DateRangePicker', |
||
84 | 'package_path' => [ |
||
85 | 'daterangepicker', |
||
86 | 'moment', |
||
87 | ], |
||
88 | 'assets_path' => [ |
||
89 | 'daterangepicker', |
||
90 | 'moment', |
||
91 | ], |
||
92 | ], |
||
93 | 'ekkoLightbox' => [ |
||
94 | 'name' => 'Ekko Lightbox', |
||
95 | 'package_path' => 'ekko-lightbox', |
||
96 | 'assets_path' => 'ekko-lightbox', |
||
97 | ], |
||
98 | 'fastclick' => [ |
||
99 | 'name' => 'Fastclick', |
||
100 | 'package_path' => 'fastclick', |
||
101 | 'assets_path' => 'fastclick', |
||
102 | ], |
||
103 | 'filterizr' => [ |
||
104 | 'name' => 'Filterizr', |
||
105 | 'package_path' => 'filterizr', |
||
106 | 'assets_path' => 'filterizr', |
||
107 | 'ignore_ending' => '*.d.ts', |
||
108 | 'recursive' => false, |
||
109 | ], |
||
110 | 'flagIconCss' => [ |
||
111 | 'name' => 'Flag Icon Css', |
||
112 | 'package_path' => 'flag-icon-css', |
||
113 | 'assets_path' => 'flag-icon-css', |
||
114 | ], |
||
115 | 'flot' => [ |
||
116 | 'name' => 'Flot', |
||
117 | 'package_path' => 'flot', |
||
118 | 'assets_path' => 'flot', |
||
119 | ], |
||
120 | 'fullcalendar' => [ |
||
121 | 'name' => 'Fullcalendar', |
||
122 | 'package_path' => 'fullcalendar', |
||
123 | 'assets_path' => 'fullcalendar', |
||
124 | 'ignore_ending' => [ |
||
125 | '*.d.ts', '*.json', '*.md', |
||
126 | ], |
||
127 | ], |
||
128 | 'fullcalendarPlugins' => [ |
||
129 | 'name' => 'Fullcalendar Plugins', |
||
130 | 'package_path' => [ |
||
131 | 'fullcalendar-bootstrap', |
||
132 | 'fullcalendar-daygrid', |
||
133 | 'fullcalendar-interaction', |
||
134 | 'fullcalendar-timegrid', |
||
135 | ], |
||
136 | 'assets_path' => [ |
||
137 | 'fullcalendar-plugins/bootstrap', |
||
138 | 'fullcalendar-plugins/daygrid', |
||
139 | 'fullcalendar-plugins/interaction', |
||
140 | 'fullcalendar-plugins/timegrid', |
||
141 | ], |
||
142 | 'ignore_ending' => [ |
||
143 | '*.d.ts', '*.json', '*.md', |
||
144 | ], |
||
145 | ], |
||
146 | 'icheckBootstrap' => [ |
||
147 | 'name' => 'iCheck Bootstrap', |
||
148 | 'package_path' => 'icheck-bootstrap', |
||
149 | 'assets_path' => 'icheck-bootstrap', |
||
150 | 'ignore_ending' => [ |
||
151 | '*.json', '*.md', |
||
152 | ], |
||
153 | ], |
||
154 | 'inputmask' => [ |
||
155 | 'name' => 'InputMask', |
||
156 | 'package_path' => 'inputmask', |
||
157 | 'assets_path' => 'inputmask', |
||
158 | ], |
||
159 | 'ionRangslider' => [ |
||
160 | 'name' => 'ion RangeSlider', |
||
161 | 'package_path' => 'ion-rangeslider', |
||
162 | 'assets_path' => 'ion-rangeslider', |
||
163 | 'ignore_ending' => [ |
||
164 | '*.json', '*.md', '.editorconfig', |
||
165 | ], |
||
166 | ], |
||
167 | 'jqueryKnob' => [ |
||
168 | 'name' => 'jQuery Knob', |
||
169 | 'package_path' => 'jquery-knob', |
||
170 | 'assets_path' => 'jquery-knob', |
||
171 | ], |
||
172 | 'jqueryMapael' => [ |
||
173 | 'name' => 'jQuery Mapael', |
||
174 | 'package_path' => [ |
||
175 | 'jquery-mapael', |
||
176 | 'raphael', |
||
177 | 'jquery-mousewheel', |
||
178 | ], |
||
179 | 'assets_path' => [ |
||
180 | 'jquery-mapael', |
||
181 | 'raphael', |
||
182 | 'jquery-mousewheel', |
||
183 | ], |
||
184 | 'ignore_ending' => [ |
||
185 | '*.json', '*.md', '.editorconfig', |
||
186 | ], |
||
187 | ], |
||
188 | 'jqueryUi' => [ |
||
189 | 'name' => 'jQuery UI', |
||
190 | 'package_path' => [ |
||
191 | 'jquery-ui', |
||
192 | 'jquery-ui/images', |
||
193 | ], |
||
194 | 'assets_path' => [ |
||
195 | 'jquery-ui', |
||
196 | 'jquery-ui/images', |
||
197 | ], |
||
198 | 'recursive' => false, |
||
199 | 'ignore_ending' => [ |
||
200 | '*.json', '*.md', '*.html', '.editorconfig', |
||
201 | ], |
||
202 | ], |
||
203 | 'jqvmap' => [ |
||
204 | 'name' => 'jQVMap', |
||
205 | 'package_path' => 'jqvmap', |
||
206 | 'assets_path' => 'jqvmap', |
||
207 | ], |
||
208 | 'jsgrid' => [ |
||
209 | 'name' => 'jsGrid', |
||
210 | 'package_path' => [ |
||
211 | 'jsgrid', |
||
212 | 'jsgrid/i18n', |
||
213 | ], |
||
214 | 'assets_path' => [ |
||
215 | 'jsgrid', |
||
216 | 'jsgrid/i18n', |
||
217 | ], |
||
218 | 'recursive' => false, |
||
219 | ], |
||
220 | 'paceProgress' => [ |
||
221 | 'name' => 'Pace Progress', |
||
222 | 'package_path' => 'pace-progress', |
||
223 | 'assets_path' => 'pace-progress', |
||
224 | ], |
||
225 | 'select2' => [ |
||
226 | 'name' => 'Select 2 with Bootstrap 4 Theme', |
||
227 | 'package_path' => [ |
||
228 | 'select2', |
||
229 | 'select2-bootstrap4-theme', |
||
230 | ], |
||
231 | 'assets_path' => [ |
||
232 | 'select2', |
||
233 | 'select2-bootstrap4-theme', |
||
234 | ], |
||
235 | 'ignore_ending' => [ |
||
236 | '*.json', '*.md', |
||
237 | ], |
||
238 | ], |
||
239 | 'sparklines' => [ |
||
240 | 'name' => 'Sparklines', |
||
241 | 'package_path' => 'sparklines', |
||
242 | 'assets_path' => 'sparklines', |
||
243 | ], |
||
244 | 'summernote' => [ |
||
245 | 'name' => 'Summernote', |
||
246 | 'package_path' => 'summernote', |
||
247 | 'assets_path' => 'summernote', |
||
248 | ], |
||
249 | 'sweetalert2' => [ |
||
250 | 'name' => 'Sweetalert 2 with Bootstrap 4 Theme', |
||
251 | 'package_path' => [ |
||
252 | 'sweetalert2', |
||
253 | 'sweetalert2-theme-bootstrap-4', |
||
254 | ], |
||
255 | 'assets_path' => [ |
||
256 | 'sweetalert2', |
||
257 | 'sweetalert2-theme-bootstrap-4', |
||
258 | ], |
||
259 | ], |
||
260 | 'tempusdominusBootstrap4' => [ |
||
261 | 'name' => 'Tempusdominus Bootstrap 4', |
||
262 | 'package_path' => 'tempusdominus-bootstrap-4', |
||
263 | 'assets_path' => 'tempusdominus-bootstrap-4', |
||
264 | ], |
||
265 | 'toastr' => [ |
||
266 | 'name' => 'Toastr', |
||
267 | 'package_path' => 'toastr', |
||
268 | 'assets_path' => 'toastr', |
||
269 | ], |
||
270 | ]; |
||
271 | |||
272 | protected $assets_path = 'vendor/'; |
||
273 | |||
274 | protected $package_path = 'vendor/almasaeed2010/adminlte/'; |
||
275 | |||
276 | /** |
||
277 | * Execute the console command. |
||
278 | * |
||
279 | * @return void |
||
280 | */ |
||
281 | public function handle() |
||
318 | |||
319 | /** |
||
320 | * List Plugins. |
||
321 | * |
||
322 | * @return void |
||
323 | */ |
||
324 | protected function listPlugins() |
||
347 | |||
348 | /** |
||
349 | * Check Plugin. |
||
350 | * |
||
351 | * @return void |
||
352 | */ |
||
353 | protected function checkPlugin($plugin_key) |
||
400 | |||
401 | /** |
||
402 | * Copy all Plugin Assets to Public Directory. |
||
403 | */ |
||
404 | protected function copyPlugins($force = null) |
||
453 | |||
454 | /** |
||
455 | * Removes all Plugin Assets to Public Directory. |
||
456 | */ |
||
457 | protected function removePlugins() |
||
501 | } |
||
502 |
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.