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 |
||
41 | class InstallProjectController extends AbstractController |
||
42 | { |
||
43 | /** |
||
44 | * Create a project. |
||
45 | * |
||
46 | * @param Request $request The request. |
||
47 | * |
||
48 | * @return JsonResponse |
||
49 | * |
||
50 | * @ApiDoc( |
||
51 | * section="install", |
||
52 | * statusCodes = { |
||
53 | * 201 = "When everything worked out ok" |
||
54 | * }, |
||
55 | * ) |
||
56 | * @ApiDescription( |
||
57 | * request={ |
||
58 | * "project" = { |
||
59 | * "description" = "The name of the project to install.", |
||
60 | * "children" = { |
||
61 | * "name" = { |
||
62 | * "dataType" = "string", |
||
63 | * "description" = "The name of the project to install.", |
||
64 | * "required" = true |
||
65 | * }, |
||
66 | * "version" = { |
||
67 | * "dataType" = "string", |
||
68 | * "description" = "The name of the project to install.", |
||
69 | * "required" = false |
||
70 | * } |
||
71 | * } |
||
72 | * }, |
||
73 | * "credentials" = { |
||
74 | * "description" = "The name of the project to install.", |
||
75 | * "children" = { |
||
76 | * "secret" = { |
||
77 | * "dataType" = "string", |
||
78 | * "description" = "The secret to use for encryption and signing.", |
||
79 | * "required" = true |
||
80 | * }, |
||
81 | * "username" = { |
||
82 | * "dataType" = "string", |
||
83 | * "description" = "The name of the admin user.", |
||
84 | * "required" = true |
||
85 | * }, |
||
86 | * "password" = { |
||
87 | * "dataType" = "string", |
||
88 | * "description" = "The password to use for the admin.", |
||
89 | * "required" = false |
||
90 | * } |
||
91 | * } |
||
92 | * } |
||
93 | * }, |
||
94 | * response={ |
||
95 | * "token" = { |
||
96 | * "dataType" = "string", |
||
97 | * "description" = "The API token for the created user" |
||
98 | * }, |
||
99 | * "task" = { |
||
100 | * "dataType" = "string", |
||
101 | * "description" = "The id of the created install task" |
||
102 | * } |
||
103 | * } |
||
104 | * ) |
||
105 | */ |
||
106 | public function createProjectAction(Request $request) |
||
192 | |||
193 | /** |
||
194 | * This is a gateway to the self test controller available only at install time. |
||
195 | * |
||
196 | * This is just here as the other route is protected with login. |
||
197 | * This method is inaccessible as soon as the installation is complete. |
||
198 | * |
||
199 | * @return JsonResponse |
||
200 | * |
||
201 | * @ApiDoc( |
||
202 | * section="install", |
||
203 | * description="Install time - self test." |
||
204 | * ) |
||
205 | * @ApiDescription( |
||
206 | * response={ |
||
207 | * "results" = { |
||
208 | * "actualType" = "collection", |
||
209 | * "subType" = "object", |
||
210 | * "description" = "The test results.", |
||
211 | * "children" = { |
||
212 | * "name" = { |
||
213 | * "dataType" = "string", |
||
214 | * "description" = "The name of the test" |
||
215 | * }, |
||
216 | * "state" = { |
||
217 | * "dataType" = "choice", |
||
218 | * "description" = "The test result state.", |
||
219 | * "format" = "[FAIL|SKIPPED|SUCCESS|WARNING]" |
||
220 | * }, |
||
221 | * "message" = { |
||
222 | * "dataType" = "string", |
||
223 | * "description" = "The detailed message of the test result." |
||
224 | * }, |
||
225 | * "explain" = { |
||
226 | * "dataType" = "string", |
||
227 | * "description" = "Optional description that could hint any problems and/or explain the error further." |
||
228 | * } |
||
229 | * } |
||
230 | * } |
||
231 | * } |
||
232 | * ) |
||
233 | */ |
||
234 | public function getSelfTestAction() |
||
240 | |||
241 | /** |
||
242 | * Install time gateway to the auto config. |
||
243 | * |
||
244 | * This is just here as the other route is protected with login. |
||
245 | * This method is inaccessible as soon as the installation is complete. |
||
246 | * |
||
247 | * @return JsonResponse |
||
248 | * |
||
249 | * @ApiDoc( |
||
250 | * section="install", |
||
251 | * description="Install time - auto config." |
||
252 | * ) |
||
253 | * @ApiDescription( |
||
254 | * response={ |
||
255 | * "php_cli" = { |
||
256 | * "dataType" = "string", |
||
257 | * "description" = "The PHP interpreter to run on command line." |
||
258 | * }, |
||
259 | * "php_cli_arguments" = { |
||
260 | * "dataType" = "string", |
||
261 | * "description" = "Command line arguments to add." |
||
262 | * } |
||
263 | * } |
||
264 | * ) |
||
265 | */ |
||
266 | public function getAutoConfigAction() |
||
272 | |||
273 | /** |
||
274 | * Retrieve the available versions of a package. |
||
275 | * |
||
276 | * @param string $vendor The vendor name of the package. |
||
277 | * |
||
278 | * @param string $project The name of the package. |
||
279 | * |
||
280 | * @return JsonResponse |
||
281 | * |
||
282 | * @ApiDoc( |
||
283 | * section="install", |
||
284 | * statusCodes = { |
||
285 | * 200 = "When everything worked out ok" |
||
286 | * } |
||
287 | * ) |
||
288 | * @ApiDescription( |
||
289 | * response={ |
||
290 | * "versions" = { |
||
291 | * "actualType" = "collection", |
||
292 | * "subType" = "object", |
||
293 | * "description" = "The list of versions", |
||
294 | * "children" = { |
||
295 | * "name" = { |
||
296 | * "dataType" = "string", |
||
297 | * "description" = "The name of the package" |
||
298 | * }, |
||
299 | * "version" = { |
||
300 | * "dataType" = "string", |
||
301 | * "description" = "The version of the package" |
||
302 | * }, |
||
303 | * "version_normalized" = { |
||
304 | * "dataType" = "string", |
||
305 | * "description" = "The normalized version of the package" |
||
306 | * }, |
||
307 | * "reference" = { |
||
308 | * "dataType" = "string", |
||
309 | * "description" = "The optional reference" |
||
310 | * } |
||
311 | * } |
||
312 | * } |
||
313 | * } |
||
314 | * ) |
||
315 | */ |
||
316 | public function getProjectVersionsAction($vendor, $project) |
||
358 | |||
359 | /** |
||
360 | * Check if installation is new, partial or complete. |
||
361 | * |
||
362 | * @return JsonResponse |
||
363 | * |
||
364 | * @ApiDoc( |
||
365 | * section="install", |
||
366 | * description="This method provides information about the installation.", |
||
367 | * authentication=false, |
||
368 | * statusCodes = { |
||
369 | * 200 = "When everything worked out ok" |
||
370 | * } |
||
371 | * ) |
||
372 | * @ApiDescription( |
||
373 | * response={ |
||
374 | * "state" = { |
||
375 | * "children" = { |
||
376 | * "tenside_configured" = { |
||
377 | * "dataType" = "bool", |
||
378 | * "description" = "Flag if tenside has been completely configured." |
||
379 | * }, |
||
380 | * "project_created" = { |
||
381 | * "dataType" = "bool", |
||
382 | * "description" = "Flag determining if a composer.json is present." |
||
383 | * }, |
||
384 | * "project_installed" = { |
||
385 | * "dataType" = "bool", |
||
386 | * "description" = "Flag determining if the composer project has been installed (vendor present)." |
||
387 | * } |
||
388 | * } |
||
389 | * }, |
||
390 | * "status" = { |
||
391 | * "dataType" = "string", |
||
392 | * "description" = "Either OK or ERROR" |
||
393 | * }, |
||
394 | * "message" = { |
||
395 | * "dataType" = "string", |
||
396 | * "description" = "The API error message if any (only present when status is ERROR)" |
||
397 | * } |
||
398 | * } |
||
399 | * ) |
||
400 | */ |
||
401 | public function getInstallationStateAction() |
||
416 | |||
417 | /** |
||
418 | * Ensure that we are not installed yet. |
||
419 | * |
||
420 | * @return void |
||
421 | * |
||
422 | * @throws NotAcceptableHttpException When the installation is already complete. |
||
423 | */ |
||
424 | private function checkUninstalled() |
||
430 | |||
431 | /** |
||
432 | * Run the given task and return a response when an error occurred or null if it worked out. |
||
433 | * |
||
434 | * @param string $taskId The task id. |
||
435 | * |
||
436 | * @return void |
||
437 | * |
||
438 | * @throws \RuntimeException When the process could not be started. |
||
439 | */ |
||
440 | private function runInstaller($taskId) |
||
449 | } |
||
450 |