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 |
||
14 | class Notifications extends AbstractActivity |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * List your notifications |
||
19 | * |
||
20 | * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications |
||
21 | * |
||
22 | * @param bool $all |
||
23 | * @param bool $participating |
||
24 | * @param string $since |
||
25 | * @param string $before |
||
26 | * |
||
27 | * @return array |
||
28 | * @throws \Exception |
||
29 | */ |
||
30 | public function listNotifications(bool $all = false, bool $participating = false, string $since = 'now', |
||
40 | |||
41 | /** |
||
42 | * List your notifications in a repository |
||
43 | * |
||
44 | * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository |
||
45 | * |
||
46 | * @param bool $all |
||
47 | * @param bool $participating |
||
48 | * @param string $since |
||
49 | * @param string $before |
||
50 | * |
||
51 | * @return array |
||
52 | * @throws \Exception |
||
53 | */ |
||
54 | public function listRepositoryNotifications(bool $all = false, bool $participating = false, string $since = 'now', |
||
65 | |||
66 | /** |
||
67 | * Mark as read |
||
68 | * |
||
69 | * @link https://developer.github.com/v3/activity/notifications/#mark-as-read |
||
70 | * |
||
71 | * @param string $lastReadAt |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | public function markAsRead(string $lastReadAt = 'now'): array |
||
81 | |||
82 | /** |
||
83 | * Mark notifications as read in a repository |
||
84 | * |
||
85 | * @link https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository |
||
86 | * |
||
87 | * @param string $lastReadAt |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function markAsReadInRepository(string $lastReadAt = 'now'): array |
||
92 | { |
||
93 | return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args', |
||
94 | $this->getActivity()->getOwner(), $this->getActivity()->getRepo(), |
||
95 | http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])), |
||
96 | Request::METHOD_PUT); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * View a single thread |
||
101 | * |
||
102 | * @link https://developer.github.com/v3/activity/notifications/#view-a-single-thread |
||
103 | * |
||
104 | * @param int $id |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function viewThread(int $id): array |
||
112 | |||
113 | /** |
||
114 | * Mark a thread as read |
||
115 | * |
||
116 | * @link https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read |
||
117 | * |
||
118 | * @param int $id |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | public function markThreadAsRead(int $id): array |
||
127 | |||
128 | /** |
||
129 | * Get a Thread Subscription |
||
130 | * |
||
131 | * @link https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription |
||
132 | * |
||
133 | * @param int $id |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | public function getThreadSubscription(int $id): array |
||
142 | |||
143 | /** |
||
144 | * Set a Thread Subscription |
||
145 | * |
||
146 | * @link https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription |
||
147 | * |
||
148 | * @param int $id |
||
149 | * @param bool $subscribed |
||
150 | * @param bool $ignored |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | public function setThreadSubscription(int $id, bool $subscribed = false, bool $ignored = false): array |
||
159 | |||
160 | /** |
||
161 | * Delete a Thread Subscription |
||
162 | * |
||
163 | * @link https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription |
||
164 | * |
||
165 | * @param int $id |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | View Code Duplication | public function deleteThreadSubscription(int $id): bool |
|
180 | } |
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.