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 |
||
34 | class EventController extends RestController |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Find events by name part |
||
39 | * @Route("s/like/{searchString}/{limit}/{offset}", name="events_find_like") |
||
40 | * @Method("GET") |
||
41 | * @ApiDoc( |
||
42 | * section="Event", |
||
43 | * statusCodes={ |
||
44 | * 200="OK", |
||
45 | * } |
||
46 | * ) |
||
47 | * @param string $searchString Search string |
||
48 | * @param int $limit Limit results. Default is 50 |
||
49 | 4 | * @param int $offset Starting serial number of result collection. Default is 0 |
|
50 | */ |
||
51 | 4 | public function findLikeAction($searchString = null, $limit = null, $offset = null) |
|
74 | |||
75 | /** |
||
76 | * List all events |
||
77 | * @Route("s/{limit}/{offset}", name="events_list") |
||
78 | * @Method("GET") |
||
79 | * @ApiDoc( |
||
80 | * section="Event", |
||
81 | * statusCodes={ |
||
82 | * 200="OK", |
||
83 | * } |
||
84 | * ) |
||
85 | * @param int $limit Limit results. Default is 50 |
||
86 | 1 | * @param int $offset Starting serial number of result collection. Default is 0 |
|
87 | */ |
||
88 | 1 | public function listAction($limit = null, $offset = null): Response |
|
98 | |||
99 | /** |
||
100 | * View event by id |
||
101 | * @Route("/{eventId}", name="event_view") |
||
102 | * @Method("GET") |
||
103 | * @ApiDoc( |
||
104 | * section="Event", |
||
105 | * statusCodes={ |
||
106 | * 200="Event was found", |
||
107 | * 404="Event with given id was not found", |
||
108 | * } |
||
109 | * ) |
||
110 | 3 | * @param string $eventId event id |
|
111 | */ |
||
112 | View Code Duplication | public function viewAction(string $eventId): Response |
|
126 | |||
127 | /** |
||
128 | * Create new event |
||
129 | * @Route("") |
||
130 | * @Method("POST") |
||
131 | * @Security("has_role('ROLE_USER')") |
||
132 | * @ApiDoc( |
||
133 | * section="Event", |
||
134 | * requirements={ |
||
135 | * { |
||
136 | * "name"="name", |
||
137 | * "dataType"="string", |
||
138 | * "requirement"="true", |
||
139 | * "description"="event name" |
||
140 | * }, |
||
141 | * { |
||
142 | * "name"="date", |
||
143 | * "dataType"="date (dd-MM-yyyy HH:mm)", |
||
144 | * "requirement"="true", |
||
145 | * "description"="event date" |
||
146 | * }, |
||
147 | * { |
||
148 | * "name"="description", |
||
149 | * "dataType"="text", |
||
150 | * "requirement"="true", |
||
151 | * "description"="event description" |
||
152 | * }, |
||
153 | * }, |
||
154 | * statusCodes={ |
||
155 | * 201="New event was created. Link to new resource in header 'Location'", |
||
156 | * 400="Validation error", |
||
157 | * } |
||
158 | 2 | * ) |
|
159 | */ |
||
160 | 2 | public function createAction(Request $request): Response |
|
186 | |||
187 | /** |
||
188 | * Edit event |
||
189 | * @Route("/{eventId}", name="event_edit") |
||
190 | * @Method("PUT") |
||
191 | * @Security("has_role('ROLE_USER')") |
||
192 | * @ApiDoc( |
||
193 | * section="Event", |
||
194 | * requirements={ |
||
195 | * { |
||
196 | * "name"="name", |
||
197 | * "dataType"="string", |
||
198 | * "requirement"="true", |
||
199 | * "description"="event name" |
||
200 | * }, |
||
201 | * { |
||
202 | * "name"="date", |
||
203 | * "dataType"="date (dd-MM-yyyy HH:mm)", |
||
204 | * "requirement"="true", |
||
205 | * "description"="event date" |
||
206 | * }, |
||
207 | * { |
||
208 | * "name"="description", |
||
209 | * "dataType"="string", |
||
210 | * "requirement"="true", |
||
211 | * "description"="event description" |
||
212 | * }, |
||
213 | * }, |
||
214 | * statusCodes={ |
||
215 | * 204="Event was edited with new data", |
||
216 | * 400="Validation error", |
||
217 | * 404="Event with given id was not found", |
||
218 | * } |
||
219 | * ) |
||
220 | 2 | * @param string $eventId event id |
|
221 | */ |
||
222 | 2 | public function editAction(Request $request, string $eventId): Response |
|
259 | |||
260 | /** |
||
261 | * Delete event |
||
262 | * @Route("/{eventId}", name="event_delete") |
||
263 | * @Method("DELETE") |
||
264 | * @Security("has_role('ROLE_USER')") |
||
265 | * @ApiDoc( |
||
266 | * section="Event", |
||
267 | * statusCodes={ |
||
268 | * 204="Event was deleted", |
||
269 | * 404="Event with given id was not found", |
||
270 | * } |
||
271 | * ) |
||
272 | 1 | * @param string $eventId event id |
|
273 | */ |
||
274 | View Code Duplication | public function deleteEvent(string $eventId): Response |
|
291 | 1 | ||
292 | /** |
||
293 | 1 | * Add image to event |
|
294 | 1 | * @Route("/{eventId}/image", name="event_image_add") |
|
295 | 1 | * @Method("POST") |
|
296 | * @Security("has_role('ROLE_USER')") |
||
297 | * @ApiDoc( |
||
298 | * section="Event", |
||
299 | 4 | * statusCodes={ |
|
300 | * 404="Event with given id was not found", |
||
301 | * } |
||
302 | 4 | * ) |
|
303 | 4 | * @param string $eventId event id |
|
304 | 4 | */ |
|
305 | 4 | public function addImageAction(Request $request, string $eventId): Response |
|
353 | |||
354 | /** |
||
355 | * Get event image |
||
356 | * @Route("/{eventId}/image/{imageName}", name="event_image_view") |
||
357 | * @Method("GET") |
||
358 | * @ApiDoc( |
||
359 | * section="Event", |
||
360 | * statusCodes={ |
||
361 | * 404="Event with given id was not found", |
||
362 | * 404="Image with given name was not found", |
||
363 | * } |
||
364 | * ) |
||
365 | * @param string $eventId event id |
||
366 | * @param string $imageName image name |
||
367 | */ |
||
368 | public function viewImageAction(string $eventId, string $imageName): Response |
||
389 | |||
390 | private function createEventNotFoundErrorResult(string $eventId): ApiError |
||
397 | |||
398 | private function createEventCreationForm(): FormInterface |
||
415 | |||
416 | private function createLocationById(string $eventId): string |
||
420 | |||
421 | private function createEventByForm(FormInterface $form): Event |
||
429 | } |
||
430 |
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.