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 |
||
| 11 | class GoogleCalendar |
||
| 12 | { |
||
| 13 | /** @var \Google_Service_Calendar */ |
||
| 14 | protected $calendarService; |
||
| 15 | |||
| 16 | /** @var string */ |
||
| 17 | protected $calendarId; |
||
| 18 | |||
| 19 | /** @var iterable */ |
||
| 20 | protected $batchRequests; |
||
| 21 | |||
| 22 | public function __construct(Google_Service_Calendar $calendarService, string $calendarId) |
||
| 30 | |||
| 31 | public function getCalendarId(): string |
||
| 35 | |||
| 36 | /* |
||
| 37 | * @link https://developers.google.com/google-apps/calendar/v3/reference/events/list |
||
| 38 | */ |
||
| 39 | public function listEvents(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = []): array |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $eventId |
||
| 66 | * @param array $optParams |
||
| 67 | * |
||
| 68 | * @return Google_Service_Calendar_Event|RequestInterface |
||
| 69 | */ |
||
| 70 | public function getEvent(string $eventId, $optParams = []) |
||
| 74 | |||
| 75 | View Code Duplication | public function getEvents(iterable $eventIds, $optParams = []) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param $event |
||
| 88 | * @param array $optParams |
||
| 89 | * |
||
| 90 | * @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert |
||
| 91 | * @return Google_Service_Calendar_Event|RequestInterface |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function insertEvent($event, $optParams = []) |
|
| 101 | |||
| 102 | View Code Duplication | public function insertEvents(iterable $events, $optParams = []) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param $event |
||
| 115 | * @param array $optParams |
||
| 116 | * |
||
| 117 | * @return Google_Service_Calendar_Event|RequestInterface |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function updateEvent($event, $optParams = []) |
|
| 127 | |||
| 128 | View Code Duplication | public function updateEvents(iterable $events, $optParams = []) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param $eventId |
||
| 141 | * @param array $optParams |
||
| 142 | * |
||
| 143 | * @return RequestInterface |
||
| 144 | */ |
||
| 145 | public function deleteEvent($eventId, $optParams = []) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param iterable $eventIds |
||
| 156 | * @param array $optParams |
||
| 157 | * |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function deleteEvents(iterable $eventIds, $optParams = []) |
|
| 170 | |||
| 171 | public function getService(): Google_Service_Calendar |
||
| 175 | |||
| 176 | public function enableBatch(bool $bool) |
||
| 182 | |||
| 183 | public function sendBatch():array |
||
| 187 | } |
||
| 188 |
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.