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 ModuleSeedCommand extends Command |
||
11 | { |
||
12 | /** |
||
13 | * The console command name. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name = 'module:seed'; |
||
18 | |||
19 | /** |
||
20 | * The console command description. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $description = 'Seed the database with records for a specific or all modules'; |
||
25 | |||
26 | /** |
||
27 | * @var Modules |
||
28 | */ |
||
29 | protected $module; |
||
30 | |||
31 | /** |
||
32 | * Create a new command instance. |
||
33 | * |
||
34 | * @param Modules $module |
||
35 | */ |
||
36 | public function __construct(Modules $module) |
||
42 | |||
43 | /** |
||
44 | * Execute the console command. |
||
45 | * |
||
46 | * @return mixed |
||
47 | */ |
||
48 | View Code Duplication | public function fire() |
|
76 | |||
77 | /** |
||
78 | * Seed the specific module. |
||
79 | * |
||
80 | * @param string $module |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | protected function seed($slug) |
||
85 | { |
||
86 | $module = $this->module->where('slug', $slug)->first(); |
||
87 | $params = []; |
||
88 | $namespacePath = $this->module->getNamespace(); |
||
89 | $rootSeeder = $module['namespace'].'DatabaseSeeder'; |
||
90 | $fullPath = $namespacePath.'\\'.$module['namespace'].'\Database\Seeds\\'.$rootSeeder; |
||
91 | |||
92 | if (class_exists($fullPath)) { |
||
93 | if ($this->option('class')) { |
||
94 | $params['--class'] = $this->option('class'); |
||
95 | } else { |
||
96 | $params['--class'] = $fullPath; |
||
97 | } |
||
98 | |||
99 | if ($option = $this->option('database')) { |
||
100 | $params['--database'] = $option; |
||
101 | } |
||
102 | |||
103 | if ($option = $this->option('force')) { |
||
104 | $params['--force'] = $option; |
||
105 | } |
||
106 | |||
107 | $this->call('db:seed', $params); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get the console command arguments. |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | protected function getArguments() |
||
120 | |||
121 | /** |
||
122 | * Get the console command options. |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | View Code Duplication | protected function getOptions() |
|
134 | } |
||
135 |
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.