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 |
||
13 | class Gists extends AbstractReceiver |
||
14 | { |
||
15 | |||
16 | /** Available sub-Receiver */ |
||
17 | const COMMENTS = 'Comments'; |
||
18 | |||
19 | /** |
||
20 | * List a user's gists |
||
21 | * |
||
22 | * @link https://developer.github.com/v3/gists/#list-a-users-gists |
||
23 | * |
||
24 | * @param string $username |
||
25 | * @param string $since |
||
26 | * |
||
27 | * @return array |
||
28 | */ |
||
29 | public function listGists(string $username = null, string $since = '1970-01-01'): array |
||
39 | |||
40 | /** |
||
41 | * List all public gists: |
||
42 | * |
||
43 | * @link https://developer.github.com/v3/gists/#list-all-public-gists |
||
44 | * |
||
45 | * @param string $since |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | public function listPublicGists(string $since = '1970-01-01'): array |
||
54 | |||
55 | /** |
||
56 | * List the authenticated user’s starred gists |
||
57 | * |
||
58 | * @link https://developer.github.com/v3/gists/#list-starred-gists |
||
59 | * |
||
60 | * @param string $since |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | public function listUsersStarredGists(string $since = '1970-01-01'): array |
||
69 | |||
70 | /** |
||
71 | * Get a single gist |
||
72 | * |
||
73 | * @link https://developer.github.com/v3/gists/#get-a-single-gist |
||
74 | * |
||
75 | * @param string $id |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getGist(string $id): array |
||
83 | |||
84 | /** |
||
85 | * Get a specific revision of a gist |
||
86 | * |
||
87 | * @link https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist |
||
88 | * |
||
89 | * @param string $id |
||
90 | * @param string $sha |
||
91 | * |
||
92 | * @return array |
||
93 | * @throws \Exception |
||
94 | */ |
||
95 | public function getGistRevision(string $id, string $sha): array |
||
99 | |||
100 | /** |
||
101 | * Create a gist |
||
102 | * |
||
103 | * @link https://developer.github.com/v3/gists/#create-a-gist |
||
104 | * |
||
105 | * @param array $files |
||
106 | * @param string $description |
||
107 | * @param bool $public |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public function createGist(array $files, string $description = null, bool $public = false): array |
||
119 | |||
120 | /** |
||
121 | * Edit a gist |
||
122 | * |
||
123 | * @link https://developer.github.com/v3/gists/#edit-a-gist |
||
124 | * |
||
125 | * @param string $id |
||
126 | * @param string $description |
||
127 | * @param array $files |
||
128 | * @param string $content |
||
129 | * @param string $filename |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | public function editGist(string $id, string $description = '', array $files = [], string $content = '', |
||
143 | |||
144 | /** |
||
145 | * List gist commits |
||
146 | * |
||
147 | * @link https://developer.github.com/v3/gists/#list-gist-commits |
||
148 | * |
||
149 | * @param string $id |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | public function listGistsCommits(string $id): array |
||
157 | |||
158 | /** |
||
159 | * Star a gist |
||
160 | * |
||
161 | * @link https://developer.github.com/v3/gists/#star-a-gist |
||
162 | * |
||
163 | * @param string $id |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | View Code Duplication | public function starGist(string $id): bool |
|
177 | |||
178 | /** |
||
179 | * Unstar a gist |
||
180 | * |
||
181 | * @link https://developer.github.com/v3/gists/#unstar-a-gist |
||
182 | * |
||
183 | * @param string $id |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | View Code Duplication | public function unStarGist(string $id): bool |
|
197 | |||
198 | /** |
||
199 | * Check if a gist is starred |
||
200 | * |
||
201 | * @link https://developer.github.com/v3/gists/#check-if-a-gist-is-starred |
||
202 | * |
||
203 | * @param string $id |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | View Code Duplication | public function checkGistIsStarred(string $id): bool |
|
217 | |||
218 | /** |
||
219 | * Fork a gist |
||
220 | * |
||
221 | * @link https://developer.github.com/v3/gists/#fork-a-gist |
||
222 | * |
||
223 | * @param string $id |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | public function forkGist(string $id): array |
||
231 | |||
232 | /** |
||
233 | * List gist forks |
||
234 | * |
||
235 | * @link https://developer.github.com/v3/gists/#list-gist-forks |
||
236 | * |
||
237 | * @param string $id |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | public function listGistForks(string $id): array |
||
245 | |||
246 | /** |
||
247 | * Delete a gist |
||
248 | * |
||
249 | * @link https://developer.github.com/v3/gists/#delete-a-gist |
||
250 | * |
||
251 | * @param string $id |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | View Code Duplication | public function deleteGist(string $id): bool |
|
265 | } |
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.