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 |
||
7 | class GoogleCalendarManager |
||
8 | { |
||
9 | private $calendar; |
||
10 | |||
11 | public function __construct(GoogleClientFactory $factory) |
||
15 | |||
16 | View Code Duplication | public function createEvent(DtoEvent $dtoEvent, $data = []) |
|
|
|||
17 | { |
||
18 | $description = ['user' => $dtoEvent->getUser(), 'description' => $dtoEvent->getDescription()]; |
||
19 | $event = new \Google_Service_Calendar_Event(); |
||
20 | $event->setSummary($dtoEvent->getSummary()); |
||
21 | $event->setDescription(json_encode($description)); |
||
22 | $event->setLocation($dtoEvent->getLocation()); |
||
23 | $event->setVisibility('public'); |
||
24 | $start = new \Google_Service_Calendar_EventDateTime(); |
||
25 | $start->setDateTime($dtoEvent->getStart()); |
||
26 | $event->setStart($start); |
||
27 | $end = new \Google_Service_Calendar_EventDateTime(); |
||
28 | $end->setDateTime($dtoEvent->getEnd()); |
||
29 | $event->setEnd($end); |
||
30 | |||
31 | return $this->calendar->events->insert('primary', $event, $data); |
||
32 | } |
||
33 | |||
34 | public function getEventList($query = []) |
||
35 | { |
||
36 | $events = $this->calendar |
||
37 | ->events |
||
38 | ->listEvents('primary', $query); |
||
39 | |||
40 | return [ |
||
41 | 'pageToken' => $events->getNextPageToken(), |
||
42 | 'events' => $events->getItems(), |
||
43 | ]; |
||
44 | } |
||
45 | |||
46 | public function getEventById($id) |
||
47 | { |
||
48 | $event = $this->calendar->events->get('primary', $id); |
||
49 | if ($event->status == 'cancelled') { |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | return $event; |
||
54 | } |
||
55 | |||
56 | public function deleteEvent($id) |
||
60 | |||
61 | View Code Duplication | public function editEvent(DtoEvent $dtoEvent, $id, $data = []) |
|
62 | { |
||
63 | $description = ['user' => $dtoEvent->getUser(), 'description' => $dtoEvent->getDescription()]; |
||
64 | $event = $this->getEventById($id); |
||
65 | $event->setSummary($dtoEvent->getSummary()); |
||
66 | $event->setDescription(json_encode($description)); |
||
67 | $event->setLocation($dtoEvent->getLocation()); |
||
68 | $event->setVisibility('public'); |
||
69 | |||
70 | $start = new \Google_Service_Calendar_EventDateTime(); |
||
71 | $start->setDateTime($dtoEvent->getStart()); |
||
72 | $event->setStart($start); |
||
73 | |||
74 | $end = new \Google_Service_Calendar_EventDateTime(); |
||
75 | $end->setDateTime($dtoEvent->getEnd()); |
||
76 | $event->setEnd($end); |
||
77 | |||
78 | return $this->calendar->events->patch('primary', $id, $event, $data); |
||
79 | } |
||
80 | |||
81 | public function clear() |
||
92 | } |
||
93 |
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.