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 |
||
10 | class GoogleCalendar |
||
11 | { |
||
12 | /** @var \Google_Service_Calendar */ |
||
13 | protected $calendarService; |
||
14 | |||
15 | /** @var string */ |
||
16 | protected $calendarId; |
||
17 | |||
18 | public function __construct(Google_Service_Calendar $calendarService, $calendarId) |
||
19 | { |
||
20 | $this->calendarService = $calendarService; |
||
21 | |||
22 | $this->calendarId = $calendarId; |
||
23 | } |
||
24 | |||
25 | public function getCalendarId(): string |
||
26 | { |
||
27 | return $this->calendarId; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param \Carbon\Carbon $startDateTime |
||
32 | * @param \Carbon\Carbon $endDateTime |
||
33 | * @param array $queryParameters |
||
34 | * |
||
35 | * @link https://developers.google.com/google-apps/calendar/v3/reference/events/list |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | public function listEvents( |
||
40 | Carbon $startDateTime = null, |
||
41 | Carbon $endDateTime = null, |
||
42 | array $queryParameters = [] |
||
43 | ): array |
||
44 | { |
||
45 | $parameters = ['singleEvents' => true]; |
||
46 | |||
47 | if (is_null($startDateTime)) { |
||
48 | $startDateTime = Carbon::now()->startOfDay(); |
||
49 | } |
||
50 | |||
51 | $parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339); |
||
52 | |||
53 | if (is_null($endDateTime)) { |
||
54 | $endDateTime = Carbon::now()->addYear()->endOfDay(); |
||
55 | } |
||
56 | $parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339); |
||
57 | |||
58 | $parameters = array_merge($parameters, $queryParameters); |
||
59 | |||
60 | return $this |
||
61 | ->calendarService |
||
62 | ->events |
||
63 | ->listEvents($this->calendarId, $parameters) |
||
64 | ->getItems(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Get a single event. |
||
69 | * |
||
70 | * @param string $eventId |
||
71 | * |
||
72 | * @return \Google_Service_Calendar_Event |
||
73 | */ |
||
74 | public function getEvent(string $eventId): Google_Service_Calendar_Event |
||
75 | { |
||
76 | return $this->calendarService->events->get($this->calendarId, $eventId); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Insert an event. |
||
81 | * |
||
82 | * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event |
||
83 | * |
||
84 | * @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert |
||
85 | * |
||
86 | * @return \Google_Service_Calendar_Event |
||
87 | */ |
||
88 | View Code Duplication | public function insertEvent($event): Google_Service_Calendar_Event |
|
|
|||
89 | { |
||
90 | if ($event instanceof Event) { |
||
91 | $event = $event->googleEvent; |
||
92 | } |
||
93 | |||
94 | return $this->calendarService->events->insert($this->calendarId, $event); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event |
||
99 | * |
||
100 | * @return \Google_Service_Calendar_Event |
||
101 | */ |
||
102 | View Code Duplication | public function updateEvent($event): Google_Service_Calendar_Event |
|
103 | { |
||
104 | if ($event instanceof Event) { |
||
105 | $event = $event->googleEvent; |
||
106 | } |
||
107 | |||
108 | return $this->calendarService->events->update($this->calendarId, $event->id, $event); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string|\Spatie\GoogleCalendar\Event $eventId |
||
113 | */ |
||
114 | public function deleteEvent($eventId) |
||
115 | { |
||
116 | if ($eventId instanceof Event) { |
||
117 | $eventId = $eventId->id; |
||
118 | } |
||
119 | |||
120 | $this->calendarService->events->delete($this->calendarId, $eventId); |
||
121 | } |
||
122 | |||
123 | public function getService(): Google_Service_Calendar |
||
127 | } |
||
128 |
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.