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 |
||
17 | class PerformanceEventsController extends AbstractController |
||
18 | { |
||
19 | const MAX_DAYS_PER_GET = 367; |
||
20 | |||
21 | /** |
||
22 | * @Route("", name="get_performanceevents", methods={"GET"}) |
||
23 | * @SWG\Response( |
||
24 | * response=200, |
||
25 | * description="Returns a collection of theatre performanceEvents", |
||
26 | * @SWG\Schema( |
||
27 | * type="array", |
||
28 | * @SWG\Items(ref=@Model(type=PerformanceEventsResponse::class)) |
||
29 | * ) |
||
30 | * ) |
||
31 | * @SWG\Response( |
||
32 | * response=400, |
||
33 | * description="Returns when date diff more than 1 year", |
||
34 | * ) |
||
35 | * |
||
36 | * @QueryParam(name="fromDate", default="today", requirements="\d{2}-\d{2}-\d{4}|today" , description="Find entries from this date, fromat=dd-mm-yyyy") |
||
37 | * @QueryParam(name="toDate", default="+1 Year", requirements="\d{2}-\d{2}-\d{4}|\+1 Year" , description="Find entries to this date, fromat=dd-mm-yyyy") |
||
38 | * @QueryParam(name="limit", default="all", requirements="\d+|all" , description="Count of entities in collection") |
||
39 | * @QueryParam(name="performance", description="Performance slug") |
||
40 | * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
||
41 | */ |
||
42 | 2 | public function cgetAction(ParamFetcher $paramFetcher) |
|
91 | |||
92 | /** |
||
93 | * @Route("/{id}", name="get_performance_event", methods={"GET"}) |
||
94 | * @SWG\Response( |
||
95 | * response=200, |
||
96 | * description="Returns one PerformanceEvent by Id", |
||
97 | * @Model(type=PerformanceEvent::class) |
||
98 | * ) |
||
99 | * @SWG\Response( |
||
100 | * response=404, |
||
101 | * description="Returns when PerformanceEvent by id was not found id database", |
||
102 | * ) |
||
103 | * @SWG\Get(deprecated=true) |
||
104 | * |
||
105 | * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
||
106 | */ |
||
107 | 1 | public function getAction(ParamFetcher $paramFetcher, $id) |
|
133 | } |
||
134 |
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.