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 |
||
23 | class Gists extends AbstractPackage |
||
24 | { |
||
25 | /** |
||
26 | * Create a gist. |
||
27 | * |
||
28 | * @param mixed $files Either an array of file paths or a single file path as a string. |
||
29 | * @param boolean $public True if the gist should be public. |
||
30 | * @param string $description The optional description of the gist. |
||
31 | * |
||
32 | * @return object |
||
33 | * |
||
34 | * @since 1.0 |
||
35 | * @throws \DomainException |
||
36 | */ |
||
37 | View Code Duplication | public function create($files, $public = false, $description = null) |
|
54 | |||
55 | /** |
||
56 | * Delete a gist. |
||
57 | * |
||
58 | * @param integer $gistId The gist number. |
||
59 | * |
||
60 | * @return void |
||
61 | * |
||
62 | * @since 1.0 |
||
63 | * @throws \DomainException |
||
64 | */ |
||
65 | public function delete($gistId) |
||
73 | |||
74 | /** |
||
75 | * Edit a gist. |
||
76 | * |
||
77 | * @param integer $gistId The gist number. |
||
78 | * @param mixed $files Either an array of file paths or a single file path as a string. |
||
79 | * @param boolean $public True if the gist should be public. |
||
80 | * @param string $description The description of the gist. |
||
81 | * |
||
82 | * @return object |
||
83 | * |
||
84 | * @since 1.0 |
||
85 | * @throws \DomainException |
||
86 | */ |
||
87 | public function edit($gistId, $files = null, $public = null, $description = null) |
||
119 | |||
120 | /** |
||
121 | * Fork a gist. |
||
122 | * |
||
123 | * @param integer $gistId The gist number. |
||
124 | * |
||
125 | * @return object |
||
126 | * |
||
127 | * @since 1.0 |
||
128 | * @throws \DomainException |
||
129 | */ |
||
130 | public function fork($gistId) |
||
138 | |||
139 | /** |
||
140 | * Get a single gist. |
||
141 | * |
||
142 | * @param integer $gistId The gist number. |
||
143 | * |
||
144 | * @return object |
||
145 | * |
||
146 | * @since 1.0 |
||
147 | * @throws \DomainException |
||
148 | */ |
||
149 | public function get($gistId) |
||
157 | |||
158 | /** |
||
159 | * List gist commits. |
||
160 | * |
||
161 | * @param integer $gistId The gist number. |
||
162 | * @param integer $page The page number from which to get items. |
||
163 | * @param integer $limit The number of items on a page. |
||
164 | * |
||
165 | * @return array |
||
166 | * |
||
167 | * @since 1.4.0 |
||
168 | * @throws \DomainException |
||
169 | */ |
||
170 | public function getCommitList($gistId, $page = 0, $limit = 0) |
||
178 | |||
179 | /** |
||
180 | * List gist forks. |
||
181 | * |
||
182 | * @param integer $gistId The gist number. |
||
183 | * @param integer $page The page number from which to get items. |
||
184 | * @param integer $limit The number of items on a page. |
||
185 | * |
||
186 | * @return array |
||
187 | * |
||
188 | * @since 1.4.0 |
||
189 | * @throws \DomainException |
||
190 | */ |
||
191 | public function getForkList($gistId, $page = 0, $limit = 0) |
||
199 | |||
200 | /** |
||
201 | * List gists. |
||
202 | * |
||
203 | * If a user is authenticated it will return the user's gists, otherwise |
||
204 | * it will return all public gists. |
||
205 | * |
||
206 | * @param integer $page The page number from which to get items. |
||
207 | * @param integer $limit The number of items on a page. |
||
208 | * |
||
209 | * @return array |
||
210 | * |
||
211 | * @since 1.0 |
||
212 | * @throws \DomainException |
||
213 | */ |
||
214 | public function getList($page = 0, $limit = 0) |
||
222 | |||
223 | /** |
||
224 | * List a user’s gists. |
||
225 | * |
||
226 | * @param string $user The name of the GitHub user from which to list gists. |
||
227 | * @param integer $page The page number from which to get items. |
||
228 | * @param integer $limit The number of items on a page. |
||
229 | * @param \DateTime $since Only gists updated at or after this time are returned. |
||
230 | * |
||
231 | * @return array |
||
232 | * |
||
233 | * @since 1.0 |
||
234 | * @throws \DomainException |
||
235 | */ |
||
236 | View Code Duplication | public function getListByUser($user, $page = 0, $limit = 0, \DateTime $since = null) |
|
245 | |||
246 | /** |
||
247 | * List all public gists. |
||
248 | * |
||
249 | * @param integer $page The page number from which to get items. |
||
250 | * @param integer $limit The number of items on a page. |
||
251 | * @param \DateTime $since Only gists updated at or after this time are returned. |
||
252 | * |
||
253 | * @return array |
||
254 | * |
||
255 | * @since 1.0 |
||
256 | * @throws \DomainException |
||
257 | */ |
||
258 | View Code Duplication | public function getListPublic($page = 0, $limit = 0, \DateTime $since = null) |
|
267 | |||
268 | /** |
||
269 | * List starred gists. |
||
270 | * |
||
271 | * @param integer $page The page number from which to get items. |
||
272 | * @param integer $limit The number of items on a page. |
||
273 | * @param \DateTime $since Only gists updated at or after this time are returned. |
||
274 | * |
||
275 | * @return array |
||
276 | * |
||
277 | * @since 1.0 |
||
278 | * @throws \DomainException |
||
279 | */ |
||
280 | View Code Duplication | public function getListStarred($page = 0, $limit = 0, \DateTime $since = null) |
|
289 | |||
290 | /** |
||
291 | * Get a specific revision of a gist. |
||
292 | * |
||
293 | * @param integer $gistId The gist number. |
||
294 | * @param string $sha The SHA for the revision to get. |
||
295 | * |
||
296 | * @return object |
||
297 | * |
||
298 | * @since 1.4.0 |
||
299 | * @throws \DomainException |
||
300 | */ |
||
301 | public function getRevision($gistId, $sha) |
||
309 | |||
310 | /** |
||
311 | * Check if a gist is starred. |
||
312 | * |
||
313 | * @param integer $gistId The gist number. |
||
314 | * |
||
315 | * @return boolean True if gist is starred |
||
316 | * |
||
317 | * @since 1.0 |
||
318 | * @throws UnexpectedResponseException |
||
319 | */ |
||
320 | public function isStarred($gistId) |
||
344 | |||
345 | /** |
||
346 | * Star a gist. |
||
347 | * |
||
348 | * @param integer $gistId The gist number. |
||
349 | * |
||
350 | * @return void |
||
351 | * |
||
352 | * @since 1.0 |
||
353 | * @throws \DomainException |
||
354 | */ |
||
355 | public function star($gistId) |
||
363 | |||
364 | /** |
||
365 | * Unstar a gist. |
||
366 | * |
||
367 | * @param integer $gistId The gist number. |
||
368 | * |
||
369 | * @return void |
||
370 | * |
||
371 | * @since 1.0 |
||
372 | * @throws \DomainException |
||
373 | */ |
||
374 | public function unstar($gistId) |
||
382 | |||
383 | /** |
||
384 | * Method to fetch a data array for transmitting to the GitHub API for a list of files based on |
||
385 | * an input array of file paths or filename and content pairs. |
||
386 | * |
||
387 | * @param array $files The list of file paths or filenames and content. |
||
388 | * |
||
389 | * @return array |
||
390 | * |
||
391 | * @since 1.0 |
||
392 | * @throws \InvalidArgumentException |
||
393 | */ |
||
394 | protected function buildFileData(array $files) |
||
419 | } |
||
420 |
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.