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 AdminLteInstallCommand 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 AdminLteInstallCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class AdminLteInstallCommand extends Command |
||
9 | { |
||
10 | protected $signature = 'adminlte:install '. |
||
11 | '{--force : Overwrite existing views by default}'. |
||
12 | '{--type= : Installation type, Available type: none, enhanced & full.}'. |
||
13 | '{--only= : Install only specific part, Available parts: assets, config, translations, auth_views, basic_views, basic_routes & main_views. This option can not used with the with option.}'. |
||
14 | '{--with=* : Install basic assets with specific parts, Available parts: auth_views, basic_views, basic_routes & main_views}'. |
||
15 | '{--interactive : The installation will guide you through the process}'; |
||
16 | |||
17 | protected $description = 'Install all the required files for AdminLTE and the authentication views and routes'; |
||
18 | |||
19 | protected $authViews = [ |
||
20 | 'auth/login.blade.php' => '@extends(\'adminlte::login\')', |
||
21 | 'auth/register.blade.php' => '@extends(\'adminlte::register\')', |
||
22 | 'auth/passwords/confirm.blade.php' => '@extends(\'adminlte::passwords.confirm\')', |
||
23 | 'auth/passwords/email.blade.php' => '@extends(\'adminlte::passwords.email\')', |
||
24 | 'auth/passwords/reset.blade.php' => '@extends(\'adminlte::passwords.reset\')', |
||
25 | ]; |
||
26 | |||
27 | protected $basicViews = [ |
||
28 | 'home.stub' => 'home.blade.php', |
||
29 | ]; |
||
30 | |||
31 | protected $package_path = __DIR__.'/../../'; |
||
32 | |||
33 | protected $assets_path = 'vendor/'; |
||
34 | |||
35 | protected $assets_package_path = 'vendor/almasaeed2010/adminlte/'; |
||
36 | |||
37 | protected $assets = [ |
||
38 | 'adminlte' => [ |
||
39 | 'name' => 'AdminLTE v3', |
||
40 | 'package_path' => [ |
||
41 | 'dist/css/', |
||
42 | 'dist/js/', |
||
43 | ], |
||
44 | 'assets_path' => [ |
||
45 | 'adminlte/dist/css', |
||
46 | 'adminlte/dist/js', |
||
47 | ], |
||
48 | 'images_path' => 'adminlte/dist/img/', |
||
49 | 'images' => [ |
||
50 | 'dist/img/AdminLTELogo.png' => 'AdminLTELogo.png', |
||
51 | ], |
||
52 | 'recursive' => false, |
||
53 | 'ignore' => [ |
||
54 | 'demo.js', |
||
55 | ], |
||
56 | ], |
||
57 | 'fontawesomeFree' => [ |
||
58 | 'name' => 'FontAwesome 5 Free', |
||
59 | 'package_path' => 'plugins/fontawesome-free', |
||
60 | 'assets_path' => 'fontawesome-free', |
||
61 | ], |
||
62 | 'bootstrap' => [ |
||
63 | 'name' => 'Bootstrap 4 (js files only)', |
||
64 | 'package_path' => 'plugins/bootstrap', |
||
65 | 'assets_path' => 'bootstrap', |
||
66 | ], |
||
67 | 'popper' => [ |
||
68 | 'name' => 'Popper.js (Bootstrap 4 requirement)', |
||
69 | 'package_path' => 'plugins/popper', |
||
70 | 'assets_path' => 'popper', |
||
71 | ], |
||
72 | 'jquery' => [ |
||
73 | 'name' => 'jQuery (Bootstrap 4 requirement)', |
||
74 | 'package_path' => 'plugins/jquery', |
||
75 | 'assets_path' => 'jquery', |
||
76 | 'ignore' => [ |
||
77 | 'core.js', 'jquery.slim.js', 'jquery.slim.min.js', 'jquery.slim.min.map', |
||
78 | ], |
||
79 | ], |
||
80 | 'overlayScrollbars' => [ |
||
81 | 'name' => 'Overlay Scrollbars', |
||
82 | 'package_path' => 'plugins/overlayScrollbars', |
||
83 | 'assets_path' => 'overlayScrollbars', |
||
84 | ], |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * Execute the console command. |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | public function handle() |
||
171 | |||
172 | /** |
||
173 | * Export the main views. |
||
174 | * |
||
175 | * @return void |
||
176 | */ |
||
177 | View Code Duplication | protected function exportMainViews() |
|
189 | |||
190 | /** |
||
191 | * Export the authentication views. |
||
192 | * |
||
193 | * @return void |
||
194 | */ |
||
195 | protected function exportAuthViews() |
||
208 | |||
209 | /** |
||
210 | * Export the basic views. |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | protected function exportBasicViews() |
||
234 | |||
235 | /** |
||
236 | * Export the authentication routes. |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | protected function exportBasicRoutes() |
||
263 | |||
264 | /** |
||
265 | * Export the translation files. |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | View Code Duplication | protected function exportTranslations() |
|
281 | |||
282 | /** |
||
283 | * Copy all the content of the Assets Folder to Public Directory. |
||
284 | */ |
||
285 | protected function exportAssets() |
||
299 | |||
300 | /** |
||
301 | * Install the config file. |
||
302 | */ |
||
303 | protected function exportConfig() |
||
322 | |||
323 | /** |
||
324 | * Get Package Path. |
||
325 | */ |
||
326 | protected function packagePath($path) |
||
330 | |||
331 | /** |
||
332 | * Get full view path relative to the application's configured view path. |
||
333 | * |
||
334 | * @param string $path |
||
335 | * @return string |
||
336 | */ |
||
337 | public function getViewPath($path) |
||
343 | |||
344 | /** |
||
345 | * Copy Assets Data. |
||
346 | * |
||
347 | * @param string $asset_name |
||
348 | * @param bool $force |
||
349 | * @return void |
||
350 | */ |
||
351 | protected function copyAssets($asset_name, $force = false) |
||
378 | |||
379 | /** |
||
380 | * Get Protected. |
||
381 | * |
||
382 | * @return array |
||
383 | */ |
||
384 | public function getProtected($var) |
||
388 | } |
||
389 |
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.