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 | |||
18 | /** |
||
19 | * @Route("/api/performanceevents") |
||
20 | */ |
||
21 | class PerformanceEventsController extends AbstractController |
||
22 | { |
||
23 | const MAX_DAYS_PER_GET = 367; |
||
24 | |||
25 | private SerializerInterface $serializer; |
||
|
|||
26 | |||
27 | public function __construct(SerializerInterface $serializer) |
||
28 | { |
||
29 | $this->serializer = $serializer; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @Route("", name="get_performanceevents", methods={"GET"}) |
||
34 | * @SWG\Response( |
||
35 | * response=200, |
||
36 | * description="Returns a collection of theatre performanceEvents", |
||
37 | * @SWG\Schema( |
||
38 | * type="array", |
||
39 | * @SWG\Items(ref=@Model(type=PerformanceEventsResponse::class)) |
||
40 | * ) |
||
41 | * ) |
||
42 | * @SWG\Response( |
||
43 | * response=400, |
||
44 | * description="Returns when date diff more than 1 year", |
||
45 | * ) |
||
46 | * |
||
47 | * @QueryParam(name="fromDate", default="today", requirements="\d{2}-\d{2}-\d{4}|today" , description="Find entries from this date, fromat=dd-mm-yyyy") |
||
48 | * @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") |
||
49 | * @QueryParam(name="limit", default="all", requirements="\d+|all" , description="Count of entities in collection") |
||
50 | * @QueryParam(name="performance", description="Performance slug") |
||
51 | * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
||
52 | */ |
||
53 | public function cgetAction(ParamFetcher $paramFetcher) |
||
54 | { |
||
55 | $em = $this->getDoctrine()->getManager(); |
||
56 | |||
57 | $dateDiff = strtotime($paramFetcher->get('toDate')) - strtotime($paramFetcher->get('fromDate')); |
||
58 | |||
59 | if (self::MAX_DAYS_PER_GET < abs(floor($dateDiff/(60*60*24)))) { |
||
60 | throw new BadRequestHttpException(sprintf('You can\'t get more than "%s" days', self::MAX_DAYS_PER_GET)); |
||
61 | } |
||
62 | |||
63 | $limit = 'all' == $paramFetcher->get('limit') |
||
64 | ? null |
||
65 | : (int) $paramFetcher->get('limit'); |
||
66 | |||
67 | $performanceEvents = $em->getRepository('App:PerformanceEvent') |
||
68 | ->findByDateRangeAndSlug( |
||
69 | new \DateTime($paramFetcher->get('fromDate')), |
||
70 | new \DateTime($paramFetcher->get('toDate')), |
||
71 | $paramFetcher->get('performance'), |
||
72 | $limit |
||
73 | ) |
||
74 | ; |
||
75 | |||
76 | $performanceEventsResponse = new PerformanceEventsResponse(); |
||
77 | $performanceEventsResponse->setPerformanceEvents($performanceEvents); |
||
78 | |||
79 | return new Response( |
||
80 | $this->serializer->serialize( |
||
81 | $performanceEventsResponse, |
||
82 | 'json', |
||
83 | SerializationContext::create()->setGroups(array('poster'))) |
||
84 | ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @Route("/{id}", name="get_performance_event", methods={"GET"}) |
||
89 | * @SWG\Response( |
||
90 | * response=200, |
||
91 | * description="Returns one PerformanceEvent by Id", |
||
92 | * @Model(type=PerformanceEvent::class) |
||
93 | * ) |
||
94 | * @SWG\Response( |
||
95 | * response=404, |
||
96 | * description="Returns when PerformanceEvent by id was not found id database", |
||
97 | * ) |
||
98 | * @SWG\Get(deprecated=true) |
||
99 | * |
||
100 | * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
||
101 | */ |
||
102 | public function getAction(ParamFetcher $paramFetcher, $id) |
||
103 | { |
||
104 | $em = $this->getDoctrine()->getManager(); |
||
105 | |||
106 | $performanceEvent = $em->getRepository('App:PerformanceEvent')->findOneById($id); |
||
107 | |||
108 | if (!$performanceEvent) { |
||
109 | throw $this->createNotFoundException('Unable to find '.$id.' entity'); |
||
110 | } |
||
111 | |||
112 | $performanceEvent->setLocale($paramFetcher->get('locale')); |
||
113 | $em->refresh($performanceEvent); |
||
114 | |||
115 | $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale')); |
||
116 | $em->refresh($performanceEvent->getPerformance()); |
||
117 | |||
118 | if ($performanceEvent->getTranslations()) { |
||
119 | $performanceEvent->unsetTranslations(); |
||
120 | } |
||
121 | |||
122 | if ($performanceEvent->getPerformance()->getTranslations()) { |
||
123 | $performanceEvent->getPerformance()->unsetTranslations(); |
||
124 | } |
||
125 | |||
126 | return $performanceEvent; |
||
127 | } |
||
128 | } |
||
129 |