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 MigrateStatusCommand extends Command |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * The console command name. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $name = 'module:migrate-status'; |
||
19 | |||
20 | /** |
||
21 | * The console command description. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $description = 'Status for all migrations'; |
||
26 | |||
27 | /** |
||
28 | * @var \Nwidart\Modules\Repository |
||
29 | */ |
||
30 | protected $module; |
||
31 | |||
32 | /** |
||
33 | * Execute the console command. |
||
34 | * |
||
35 | * @return mixed |
||
36 | */ |
||
37 | View Code Duplication | public function handle() |
|
1 ignored issue
–
show
|
|||
38 | { |
||
39 | $this->module = $this->laravel['modules']; |
||
40 | |||
41 | $name = $this->argument('module'); |
||
42 | |||
43 | if ($name) { |
||
44 | $module = $this->module->findOrFail($name); |
||
45 | return $this->migrateStatus($module); |
||
46 | } |
||
47 | |||
48 | foreach ($this->module->getOrdered($this->option('direction')) as $module) { |
||
49 | $this->line('Running for module: <info>' . $module->getName() . '</info>'); |
||
50 | $this->migrateStatus($module); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Run the migration from the specified module. |
||
56 | * |
||
57 | * @param Module $module |
||
58 | */ |
||
59 | protected function migrateStatus(Module $module) |
||
68 | |||
69 | /** |
||
70 | * Get the console command arguments. |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 91 | protected function getArguments() |
|
80 | |||
81 | /** |
||
82 | * Get the console command options. |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 91 | View Code Duplication | protected function getOptions() |
93 | } |
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.