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 Releases extends AbstractRepositories |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * List releases for a repository |
||
| 19 | * |
||
| 20 | * @link https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository |
||
| 21 | * @return array |
||
| 22 | */ |
||
| 23 | public function listReleases(): array |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get a single release |
||
| 31 | * |
||
| 32 | * @link https://developer.github.com/v3/repos/releases/#get-a-single-release |
||
| 33 | * |
||
| 34 | * @param string $id |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public function getSingleRelease(string $id): array |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get the latest release |
||
| 46 | * |
||
| 47 | * @link https://developer.github.com/v3/repos/releases/#get-the-latest-release |
||
| 48 | * @return array |
||
| 49 | * @throws \Exception |
||
| 50 | */ |
||
| 51 | public function getLatestRelease(): array |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get a release by tag name |
||
| 59 | * |
||
| 60 | * @link https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name |
||
| 61 | * |
||
| 62 | * @param string $tag |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | * @throws \Exception |
||
| 66 | */ |
||
| 67 | public function getReleaseByTagName(string $tag): array |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Create a release |
||
| 75 | * |
||
| 76 | * @link https://developer.github.com/v3/repos/releases/#create-a-release |
||
| 77 | * |
||
| 78 | * @param string $tagName |
||
| 79 | * @param string $targetCommitish |
||
| 80 | * @param string $name |
||
| 81 | * @param string $body |
||
| 82 | * @param bool $draft |
||
| 83 | * @param bool $preRelease |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function createRelease(string $tagName, string $targetCommitish = AbstractApi::BRANCH_MASTER, |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Edit a release |
||
| 104 | * |
||
| 105 | * @link https://developer.github.com/v3/repos/releases/#edit-a-release |
||
| 106 | * |
||
| 107 | * @param string $id |
||
| 108 | * @param string $tagName |
||
| 109 | * @param string $targetCommitish |
||
| 110 | * @param string $name |
||
| 111 | * @param string $body |
||
| 112 | * @param bool $draft |
||
| 113 | * @param bool $preRelease |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function editRelease(string $id, string $tagName, string $targetCommitish = AbstractApi::BRANCH_MASTER, |
||
| 118 | string $name, string $body, bool $draft = false, bool $preRelease = false): array |
||
| 119 | { |
||
| 120 | return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/:id', |
||
| 121 | $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $id), Request::METHOD_PATCH, [ |
||
| 122 | 'tag_name' => $tagName, |
||
| 123 | 'target_commitish' => $targetCommitish, |
||
| 124 | 'name' => $name, |
||
| 125 | 'body' => $body, |
||
| 126 | 'draft' => $draft, |
||
| 127 | 'prerelease' => $preRelease |
||
| 128 | ]); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Delete a release |
||
| 133 | * |
||
| 134 | * @link https://developer.github.com/v3/repos/releases/#delete-a-release |
||
| 135 | * |
||
| 136 | * @param string $id |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function deleteRelease(string $id): bool |
||
| 151 | |||
| 152 | /** |
||
| 153 | * List assets for a release |
||
| 154 | * |
||
| 155 | * @link https://developer.github.com/v3/repos/releases/#list-assets-for-a-release |
||
| 156 | * |
||
| 157 | * @param string $id |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | public function getReleaseAssets(string $id): array |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Upload a release asset |
||
| 169 | * |
||
| 170 | * @link https://developer.github.com/v3/repos/releases/#upload-a-release-asset |
||
| 171 | * |
||
| 172 | * @param string $id |
||
| 173 | * @param string $contentType |
||
| 174 | * @param string $name |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function uploadReleaseAsset(string $id, string $contentType, string $name): array |
||
| 179 | { |
||
| 180 | |||
| 181 | return $this->getApi()->setApiUrl(AbstractApi::API_UPLOADS)->request($this->getApi() |
||
| 182 | ->sprintf('/repos/:owner/:repo/releases/:id/assets?:arg', |
||
| 183 | $this->getRepositories() |
||
| 184 | ->getOwner(), |
||
| 185 | $this->getRepositories() |
||
| 186 | ->getRepo(), $id, |
||
| 187 | http_build_query(['name' => $name])), |
||
| 188 | Request::METHOD_POST, [ |
||
| 189 | 'Content-Type' => $contentType |
||
| 190 | ]); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get a single release asset |
||
| 195 | * |
||
| 196 | * @link https://developer.github.com/v3/repos/releases/#get-a-single-release-asset |
||
| 197 | * |
||
| 198 | * @param string $id |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function getSingleReleaseAsset(string $id): array |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Edit a release asset |
||
| 210 | * |
||
| 211 | * @link https://developer.github.com/v3/repos/releases/#edit-a-release-asset |
||
| 212 | * |
||
| 213 | * @param string $id |
||
| 214 | * @param string $name |
||
| 215 | * @param string $label |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | public function editReleaseAsset(string $id, string $name, string $label = ''): array |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Delete a release asset |
||
| 230 | * |
||
| 231 | * @link https://developer.github.com/v3/repos/releases/#delete-a-release-asset |
||
| 232 | * |
||
| 233 | * @param string $id |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function deleteReleaseAsset(string $id): bool |
||
| 248 | } |
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.