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 |
||
14 | class ModuleMigrateCommand extends Command |
||
15 | { |
||
16 | use ConfirmableTrait; |
||
17 | |||
18 | /** |
||
19 | * The console command name. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $name = 'module:migrate'; |
||
24 | |||
25 | /** |
||
26 | * The console command description. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $description = 'Run the database migrations for a specific or all modules'; |
||
31 | |||
32 | /** |
||
33 | * @var Modules |
||
34 | */ |
||
35 | protected $module; |
||
36 | |||
37 | /** |
||
38 | * @var Migrator |
||
39 | */ |
||
40 | protected $migrator; |
||
41 | |||
42 | /** |
||
43 | * Create a new command instance. |
||
44 | * |
||
45 | * @param Migrator $migrator |
||
46 | * @param Modules $module |
||
47 | */ |
||
48 | public function __construct(Migrator $migrator, Modules $module) |
||
55 | |||
56 | /** |
||
57 | * Execute the console command. |
||
58 | * |
||
59 | * @return mixed |
||
60 | */ |
||
61 | public function fire() |
||
87 | |||
88 | /** |
||
89 | * Run migrations for the specified module. |
||
90 | * |
||
91 | * @param string $slug |
||
92 | * |
||
93 | * @return mixed |
||
94 | */ |
||
95 | protected function migrate($slug) |
||
126 | |||
127 | /** |
||
128 | * Get migration directory path. |
||
129 | * |
||
130 | * @param string $slug |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | protected function getMigrationPath($slug) |
||
140 | |||
141 | /** |
||
142 | * Prepare the migration database for running. |
||
143 | */ |
||
144 | protected function prepareDatabase() |
||
154 | |||
155 | /** |
||
156 | * Get the console command arguments. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | protected function getArguments() |
||
164 | |||
165 | /** |
||
166 | * Get the console command options. |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | View Code Duplication | protected function getOptions() |
|
179 | } |
||
180 |
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.