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 |
||
15 | class PublishMySQLUsers extends Command |
||
16 | { |
||
17 | use ChecksEnv, DiesIfEnvVariableIsnotInstalled; |
||
18 | |||
19 | /** |
||
20 | * The name and signature of the console command. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $signature = 'publish:mysql-users {name?} {user?} {databases?} {--server=} {--dump}'; |
||
25 | |||
26 | /** |
||
27 | * The console command description. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $description = 'Manage MySQL user databases'; |
||
32 | |||
33 | /** |
||
34 | * Server forge id. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $server; |
||
39 | |||
40 | /** |
||
41 | * Guzzle http client. |
||
42 | * |
||
43 | * @var Client |
||
44 | */ |
||
45 | protected $http; |
||
46 | |||
47 | /** |
||
48 | * Create a new command instance. |
||
49 | * |
||
50 | */ |
||
51 | public function __construct(Client $http) |
||
56 | |||
57 | /** |
||
58 | * Execute the console command. |
||
59 | * |
||
60 | */ |
||
61 | public function handle() |
||
71 | |||
72 | /** |
||
73 | * Create MySQL user. |
||
74 | */ |
||
75 | View Code Duplication | protected function createMySQLUser() |
|
88 | |||
89 | /** |
||
90 | * List MySQL users. |
||
91 | */ |
||
92 | protected function listMySQLUsers() |
||
130 | |||
131 | /** |
||
132 | * Check parameters. |
||
133 | */ |
||
134 | protected function checkParameters() |
||
143 | |||
144 | /** |
||
145 | * Get data. |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | View Code Duplication | protected function getData() |
|
163 | |||
164 | /** |
||
165 | * Obtain API URL endpoint. |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | protected function obtainAPIURLEndpoint() |
||
174 | |||
175 | /** |
||
176 | * Obtain API URL endpoint. |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | protected function obtainAPIURLEndpointForList() |
||
185 | |||
186 | /** |
||
187 | * Abort command execution. |
||
188 | */ |
||
189 | protected function abortCommandExecution() |
||
194 | } |
||
195 |
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.