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:
1 | <?php |
||
10 | class ModuleMigrateRefreshCommand extends Command |
||
11 | { |
||
12 | use ConfirmableTrait; |
||
13 | |||
14 | /** |
||
15 | * The console command name. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $name = 'module:migrate:refresh'; |
||
20 | |||
21 | /** |
||
22 | * The console command description. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $description = 'Reset and re-run all migrations for a specific or all modules'; |
||
27 | |||
28 | /** |
||
29 | * Execute the console command. |
||
30 | * |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public function fire() |
||
34 | { |
||
35 | if (!$this->confirmToProceed()) { |
||
36 | return; |
||
37 | } |
||
38 | |||
39 | $slug = $this->argument('slug'); |
||
40 | |||
41 | $this->call('module:migrate:reset', [ |
||
42 | 'slug' => $slug, |
||
43 | '--database' => $this->option('database'), |
||
44 | '--force' => $this->option('force'), |
||
45 | '--pretend' => $this->option('pretend'), |
||
46 | ]); |
||
47 | |||
48 | $this->call('module:migrate', [ |
||
49 | 'slug' => $slug, |
||
50 | '--database' => $this->option('database'), |
||
51 | ]); |
||
52 | |||
53 | if ($this->needsSeeding()) { |
||
54 | $this->runSeeder($slug, $this->option('database')); |
||
55 | } |
||
56 | |||
57 | if (isset($slug)) { |
||
58 | $this->info('Module has been refreshed.'); |
||
59 | } else { |
||
60 | $this->info('All modules have been refreshed.'); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Determine if the developer has requested database seeding. |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | protected function needsSeeding() |
||
73 | |||
74 | /** |
||
75 | * Run the module seeder command. |
||
76 | * |
||
77 | * @param string $database |
||
78 | */ |
||
79 | protected function runSeeder($slug = null, $database = null) |
||
86 | |||
87 | /** |
||
88 | * Get the console command arguments. |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | protected function getArguments() |
||
96 | |||
97 | /** |
||
98 | * Get the console command options. |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | View Code Duplication | protected function getOptions() |
|
111 | } |
||
112 |
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.