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 |
||
23 | class VersionController extends RestController |
||
24 | { |
||
25 | /** |
||
26 | * @var CoreUtils |
||
27 | */ |
||
28 | private $coreUtils; |
||
29 | |||
30 | /** |
||
31 | * Build core utils |
||
32 | * @param CoreUtils $coreUtils coreUtils |
||
33 | * @return void |
||
34 | */ |
||
35 | public function setCoreUtils(CoreUtils $coreUtils) |
||
39 | |||
40 | /** |
||
41 | * Returns all version numbers |
||
42 | * |
||
43 | * @return \Symfony\Component\HttpFoundation\Response $response Response with result or error |
||
44 | */ |
||
45 | View Code Duplication | public function versionsAction() |
|
|
|||
46 | { |
||
47 | $response = $this->getResponse() |
||
48 | ->setStatusCode(Response::HTTP_OK); |
||
49 | $response->headers->set('Content-Type', 'application/json'); |
||
50 | $versions = array(); |
||
51 | $versions['versions'] = $this->coreUtils->getVersion(); |
||
52 | return $this->render( |
||
53 | 'GravitonRestBundle:Main:index.json.twig', |
||
54 | ['response' => json_encode($versions)], |
||
55 | $response |
||
56 | ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Returns schema |
||
61 | * |
||
62 | * @return \Symfony\Component\HttpFoundation\Response $response Response with result or error |
||
63 | */ |
||
64 | View Code Duplication | public function versionsSchemaAction() |
|
76 | } |
||
77 |
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.