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 |
||
20 | class OrganizerController extends RestController |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * List all registered organizers |
||
25 | * @Route("s/{limit}/{offset}", name="organizers_list") |
||
26 | * @Method("GET") |
||
27 | * @ApiDoc( |
||
28 | * section="Organizer", |
||
29 | * requirements={ |
||
30 | * { |
||
31 | * "name"="limit", |
||
32 | * "dataType"="int", |
||
33 | * "requirement"="false", |
||
34 | * "description"="limit number. Default is 50" |
||
35 | * }, |
||
36 | * { |
||
37 | * "name"="offset", |
||
38 | * "dataType"="int", |
||
39 | * "requirement"="false", |
||
40 | * "description"="offset number. Default is 0" |
||
41 | * }, |
||
42 | * }, |
||
43 | * statusCodes={ |
||
44 | * 200="OK", |
||
45 | * } |
||
46 | * ) |
||
47 | * @param int $limit Limit results. Default is 50 |
||
48 | * @param int $offset Starting serial number of result collection. Default is 0 |
||
49 | */ |
||
50 | 1 | public function listAction($limit = null, $offset = null): Response |
|
54 | |||
55 | /** |
||
56 | * View organizer by name |
||
57 | * @Route("/{id}", name="organizer_view") |
||
58 | * @Method("GET") |
||
59 | * @ApiDoc( |
||
60 | * section="Organizer", |
||
61 | * requirements={ |
||
62 | * { |
||
63 | * "name"="id", |
||
64 | * "dataType"="string", |
||
65 | * "requirement"="true", |
||
66 | * "description"="organizer name" |
||
67 | * }, |
||
68 | * }, |
||
69 | * statusCodes={ |
||
70 | * 200="Organizer was found", |
||
71 | * 404="Organizer with given name was not found", |
||
72 | * } |
||
73 | * ) |
||
74 | * @param string $id organizer id |
||
75 | */ |
||
76 | 3 | public function viewAction(string $id): Response |
|
80 | |||
81 | /** |
||
82 | * Create new organizer |
||
83 | * @Route("", name="organizer_create") |
||
84 | * @Method("POST") |
||
85 | * @Security("has_role('ROLE_USER')") |
||
86 | * @ApiDoc( |
||
87 | * section="Organizer", |
||
88 | * requirements={ |
||
89 | * { |
||
90 | * "name"="name", |
||
91 | * "dataType"="string", |
||
92 | * "requirement"="true", |
||
93 | * "description"="organization name" |
||
94 | * }, |
||
95 | * { |
||
96 | * "name"="description", |
||
97 | * "dataType"="string", |
||
98 | * "requirement"="true", |
||
99 | * "description"="organization description" |
||
100 | * }, |
||
101 | * { |
||
102 | * "name"="members", |
||
103 | * "dataType"="array", |
||
104 | * "requirement"="false", |
||
105 | * "description"="logins and short descriptions of organization members" |
||
106 | * }, |
||
107 | * }, |
||
108 | * statusCodes={ |
||
109 | * 201="New organizer was created. Link to new resource provided in header 'Location'", |
||
110 | * 400="Validation error", |
||
111 | * } |
||
112 | * ) |
||
113 | */ |
||
114 | 2 | View Code Duplication | public function createAction(Request $request): Response |
128 | |||
129 | /** |
||
130 | * Add new member to organization |
||
131 | * @Route("/{id}/members", name="organizer_member_create") |
||
132 | * @Method("POST") |
||
133 | * @Security("has_role('ROLE_USER')") |
||
134 | * @ApiDoc( |
||
135 | * section="Organizer", |
||
136 | * requirements={ |
||
137 | * { |
||
138 | * "name"="login", |
||
139 | * "dataType"="string", |
||
140 | * "requirement"="true", |
||
141 | * "description"="user login" |
||
142 | * }, |
||
143 | * { |
||
144 | * "name"="short_description", |
||
145 | * "dataType"="string", |
||
146 | * "requirement"="true", |
||
147 | * "description"="short description of user role in organization" |
||
148 | * }, |
||
149 | * { |
||
150 | * "name"="description", |
||
151 | * "dataType"="string", |
||
152 | * "requirement"="false", |
||
153 | * "description"="long description of user" |
||
154 | * }, |
||
155 | * }, |
||
156 | * statusCodes={ |
||
157 | * 201="Member was added to organization", |
||
158 | * 400="Validation error", |
||
159 | * } |
||
160 | * ) |
||
161 | * @param string $id organizer id |
||
162 | */ |
||
163 | 1 | public function createMemberAction(string $id): Response |
|
169 | } |
||
170 |
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.