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 |
||
16 | class PublishAssignment extends Command |
||
17 | { |
||
18 | |||
19 | use InteractsWithLocalGithub, InteractsWithEnvironment; |
||
20 | |||
21 | use AborstIfEnvVariableIsnotInstalled; |
||
22 | |||
23 | /** |
||
24 | * The name and signature of the console command. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $signature = 'publish:assignment {name? : The name description} {repository_uri? : The repository URI} {repository_type? : The repository type} {forge_site? : The Laravel Forge site id} {forge_server? : The Laravel Forge Server} '; |
||
29 | |||
30 | /** |
||
31 | * The console command description. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $description = 'Create a new assignment using current project'; |
||
36 | |||
37 | /** |
||
38 | * Server names. |
||
39 | * |
||
40 | * @var Client |
||
41 | */ |
||
42 | protected $http; |
||
43 | |||
44 | /** |
||
45 | * Assignment name. |
||
46 | * |
||
47 | * @var String |
||
48 | */ |
||
49 | protected $assignmentName; |
||
50 | |||
51 | /** |
||
52 | * Repository uri. |
||
53 | * |
||
54 | * @var String |
||
55 | */ |
||
56 | protected $repository_uri; |
||
57 | |||
58 | /** |
||
59 | * Repository type. |
||
60 | * |
||
61 | * @var String |
||
62 | */ |
||
63 | protected $repository_type; |
||
64 | |||
65 | /** |
||
66 | * Laravel Forge site id. |
||
67 | * |
||
68 | * @var String |
||
69 | */ |
||
70 | protected $forge_site; |
||
71 | |||
72 | /** |
||
73 | * Existing assignment |
||
74 | * @var |
||
75 | */ |
||
76 | protected $existingAssignment; |
||
77 | |||
78 | /** |
||
79 | * Laravel Forge server id. |
||
80 | * |
||
81 | * @var String |
||
82 | */ |
||
83 | protected $forge_server; |
||
84 | |||
85 | /** |
||
86 | * SaveEnvVariable constructor. |
||
87 | * |
||
88 | */ |
||
89 | public function __construct(Client $http) |
||
94 | |||
95 | /** |
||
96 | * Execute the console command. |
||
97 | * |
||
98 | */ |
||
99 | public function handle() |
||
121 | |||
122 | /** |
||
123 | * Create assignment. |
||
124 | * |
||
125 | * @return array|mixed |
||
126 | */ |
||
127 | View Code Duplication | protected function createAssignment() |
|
155 | |||
156 | /** |
||
157 | * Update assignment. |
||
158 | * |
||
159 | * @return array|mixed |
||
160 | */ |
||
161 | View Code Duplication | protected function updateAssignment() |
|
188 | |||
189 | /** |
||
190 | * Ask forge site. |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | protected function askForgeSite() |
||
199 | |||
200 | /** |
||
201 | * Ask forge server. |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | protected function askForgeServer() |
||
210 | |||
211 | /** |
||
212 | * Default forge site |
||
213 | */ |
||
214 | protected function defaultForgeSite() |
||
218 | |||
219 | /** |
||
220 | * Default forge server. |
||
221 | */ |
||
222 | protected function defaultForgeServer() |
||
226 | |||
227 | /** |
||
228 | * Ask name. |
||
229 | */ |
||
230 | protected function askName() |
||
235 | |||
236 | /** |
||
237 | * Ask repository Uri. |
||
238 | */ |
||
239 | protected function askRepositoryUri() { |
||
243 | |||
244 | /** |
||
245 | * Ask repository Uri. |
||
246 | */ |
||
247 | protected function askRepositoryType() { |
||
251 | |||
252 | /** |
||
253 | * Default repository type. |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | protected function defaultRepositoryType() |
||
261 | |||
262 | /** |
||
263 | * Default repository URI. |
||
264 | * |
||
265 | * @return null |
||
266 | */ |
||
267 | protected function defaultRepositoryUri() |
||
271 | |||
272 | /** |
||
273 | * Default name. |
||
274 | * |
||
275 | * @return null|string |
||
276 | */ |
||
277 | protected function defaultName() |
||
281 | |||
282 | /** |
||
283 | * Abort command execution. |
||
284 | */ |
||
285 | protected function abortCommandExecution() |
||
289 | } |
||
290 |
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.