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 Contents extends AbstractPackage |
||
24 | { |
||
25 | /** |
||
26 | * Get the README. |
||
27 | * |
||
28 | * This method returns the preferred README for a repository. |
||
29 | * |
||
30 | * @param string $owner The name of the owner of the GitHub repository. |
||
31 | * @param string $repo The name of the GitHub repository. |
||
32 | * @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. |
||
33 | * |
||
34 | * @since 1.0 |
||
35 | * |
||
36 | * @return object |
||
37 | */ |
||
38 | View Code Duplication | public function getReadme($owner, $repo, $ref = '') |
|
53 | |||
54 | /** |
||
55 | * Get contents. |
||
56 | * |
||
57 | * This method returns the contents of any file or directory in a repository. |
||
58 | * |
||
59 | * @param string $owner The name of the owner of the GitHub repository. |
||
60 | * @param string $repo The name of the GitHub repository. |
||
61 | * @param string $path The content path. |
||
62 | * @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. |
||
63 | * |
||
64 | * @since 1.0 |
||
65 | * |
||
66 | * @return object |
||
67 | */ |
||
68 | public function get($owner, $repo, $path, $ref = '') |
||
83 | |||
84 | /** |
||
85 | * Get archive link. |
||
86 | * |
||
87 | * This method will return a 302 to a URL to download a tarball or zipball archive for a repository. |
||
88 | * Please make sure your HTTP framework is configured to follow redirects or you will need to use the |
||
89 | * Location header to make a second GET request. |
||
90 | * |
||
91 | * Note: For private repositories, these links are temporary and expire quickly. |
||
92 | * |
||
93 | * To follow redirects with curl, use the -L switch: |
||
94 | * curl -L https://api.github.com/repos/pengwynn/octokit/tarball > octokit.tar.gz |
||
95 | * |
||
96 | * @param string $owner The name of the owner of the GitHub repository. |
||
97 | * @param string $repo The name of the GitHub repository. |
||
98 | * @param string $archive_format Either tarball or zipball. |
||
99 | * @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. |
||
100 | * |
||
101 | * @throws \UnexpectedValueException |
||
102 | * @since 1.0 |
||
103 | * |
||
104 | * @return object |
||
105 | */ |
||
106 | public function getArchiveLink($owner, $repo, $archive_format = 'zipball', $ref = '') |
||
127 | |||
128 | /** |
||
129 | * Create a file. |
||
130 | * |
||
131 | * This method creates a new file in a repository. |
||
132 | * |
||
133 | * Optional Parameters |
||
134 | * The author section is optional and is filled in with the committer information if omitted. |
||
135 | * If the committer information is omitted, the authenticated user’s information is used. |
||
136 | * |
||
137 | * You must provide values for both name and email, whether you choose to use author or committer. |
||
138 | * Otherwise, you’ll receive a 500 status code. |
||
139 | * |
||
140 | * @param string $owner The owner of the repository. |
||
141 | * @param string $repo The repository name. |
||
142 | * @param string $path The content path. |
||
143 | * @param string $message The commit message. |
||
144 | * @param string $content The new file content, Base64 encoded. |
||
145 | * @param string $branch The branch name. If not provided, uses the repository’s |
||
146 | * default branch (usually master). |
||
147 | * @param string $authorName The name of the author of the commit |
||
148 | * @param string $authorEmail The email of the author of the commit |
||
149 | * @param string $committerName The name of the committer of the commit |
||
150 | * @param string $committerEmail The email of the committer of the commit |
||
151 | * |
||
152 | * @throws \UnexpectedValueException |
||
153 | * |
||
154 | * @return object |
||
155 | */ |
||
156 | View Code Duplication | public function create($owner, $repo, $path, $message, $content, $branch = 'master', |
|
196 | |||
197 | /** |
||
198 | * Update a file. |
||
199 | * |
||
200 | * This method updates a file in a repository. |
||
201 | * |
||
202 | * Optional Parameters |
||
203 | * The author section is optional and is filled in with the committer information if omitted. |
||
204 | * If the committer information is omitted, the authenticated user’s information is used. |
||
205 | * |
||
206 | * You must provide values for both name and email, whether you choose to use author or committer. |
||
207 | * Otherwise, you’ll receive a 500 status code. |
||
208 | * |
||
209 | * @param string $owner The owner of the repository. |
||
210 | * @param string $repo The repository name. |
||
211 | * @param string $path The content path. |
||
212 | * @param string $message The commit message. |
||
213 | * @param string $content The new file content, Base64 encoded. |
||
214 | * @param string $sha The blob SHA of the file being replaced. |
||
215 | * @param string $branch The branch name. If not provided, uses the repository’s |
||
216 | * default branch (usually master). |
||
217 | * @param string $authorName The name of the author of the commit |
||
218 | * @param string $authorEmail The email of the author of the commit |
||
219 | * @param string $committerName The name of the committer of the commit |
||
220 | * @param string $committerEmail The email of the committer of the commit |
||
221 | * |
||
222 | * @throws \UnexpectedValueException |
||
223 | * |
||
224 | * @return object |
||
225 | */ |
||
226 | View Code Duplication | public function update($owner, $repo, $path, $message, $content, $sha, $branch = 'master', |
|
267 | |||
268 | /** |
||
269 | * Delete a file. |
||
270 | * |
||
271 | * This method deletes a file in a repository. |
||
272 | * |
||
273 | * @param string $owner The owner of the repository. |
||
274 | * @param string $repo The repository name. |
||
275 | * @param string $path The content path. |
||
276 | * @param string $message The commit message. |
||
277 | * @param string $sha The blob SHA of the file being replaced. |
||
278 | * @param string $branch The branch name. If not provided, uses the repository’s |
||
279 | * default branch (usually master). |
||
280 | * @param string $authorName The name of the author of the commit |
||
281 | * @param string $authorEmail The email of the author of the commit |
||
282 | * @param string $committerName The name of the committer of the commit |
||
283 | * @param string $committerEmail The email of the committer of the commit |
||
284 | * |
||
285 | * @throws \UnexpectedValueException |
||
286 | * |
||
287 | * @return object |
||
288 | */ |
||
289 | View Code Duplication | public function delete($owner, $repo, $path, $message, $sha, $branch = 'master', |
|
334 | } |
||
335 |
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.