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 |
||
29 | class ArticleController extends FOSRestController |
||
30 | { |
||
31 | /** |
||
32 | * List all articles for current tenant. |
||
33 | * |
||
34 | * @ApiDoc( |
||
35 | * resource=true, |
||
36 | * description="List all articles for current tenant", |
||
37 | * statusCodes={ |
||
38 | * 200="Returned on success.", |
||
39 | * } |
||
40 | * ) |
||
41 | * @Route("/api/{version}/content/articles/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_list_articles") |
||
42 | * @Method("GET") |
||
43 | * |
||
44 | * @Cache(expires="10 minutes", public=true) |
||
45 | */ |
||
46 | public function listAction(Request $request) |
||
58 | |||
59 | /** |
||
60 | * Show single tenant article. |
||
61 | * |
||
62 | * @ApiDoc( |
||
63 | * resource=true, |
||
64 | * description="Show single tenant article", |
||
65 | * statusCodes={ |
||
66 | * 200="Returned on success." |
||
67 | * } |
||
68 | * ) |
||
69 | * @Route("/api/{version}/content/articles/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_show_articles", requirements={"id"=".+"}) |
||
70 | * @Method("GET") |
||
71 | * |
||
72 | * @Cache(expires="10 minutes", public=true) |
||
73 | */ |
||
74 | 6 | View Code Duplication | public function getAction($id) |
84 | |||
85 | /** |
||
86 | * Updates articles. |
||
87 | * |
||
88 | * Possible article statuses are: |
||
89 | * |
||
90 | * * new |
||
91 | * * submitted |
||
92 | * * published |
||
93 | * * unpublished |
||
94 | * |
||
95 | * Changing status from any status to `published` will make article visible for every user. |
||
96 | * |
||
97 | * Changing status from `published` to any other will make article hidden for user who don't have rights to see unpublished articles. |
||
98 | * |
||
99 | * @ApiDoc( |
||
100 | * resource=true, |
||
101 | * description="Updates articles", |
||
102 | * statusCodes={ |
||
103 | * 200="Returned on success.", |
||
104 | * 400="Returned when validation failed.", |
||
105 | * 500="Returned when unexpected error." |
||
106 | * }, |
||
107 | * input="SWP\Bundle\ContentBundle\Form\Type\ArticleType" |
||
108 | * ) |
||
109 | * @Route("/api/{version}/content/articles/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_update_articles", requirements={"id"=".+"}) |
||
110 | * @Method("PATCH") |
||
111 | */ |
||
112 | 9 | public function updateAction(Request $request, $id) |
|
135 | |||
136 | 8 | private function reactOnStatusChange($originalArticleStatus, ArticleInterface $article) |
|
153 | |||
154 | 9 | private function findOr404($id) |
|
162 | } |
||
163 |
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.