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 Contents extends AbstractRepositories |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Get the README |
||
| 18 | * |
||
| 19 | * @link https://developer.github.com/v3/repos/contents/#get-the-readme |
||
| 20 | * @return array |
||
| 21 | */ |
||
| 22 | public function getReadme(): array |
||
| 27 | |||
| 28 | /** |
||
| 29 | * This method returns the contents of a file or directory in a repository. |
||
| 30 | * |
||
| 31 | * @link https://developer.github.com/v3/repos/contents/#get-contents |
||
| 32 | * |
||
| 33 | * @param string $path |
||
| 34 | * @param string $ref |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public function getContents(string $path = '', string $ref = AbstractApi::BRANCH_MASTER): array |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Create a file |
||
| 47 | * |
||
| 48 | * @link https://developer.github.com/v3/repos/contents/#create-a-file |
||
| 49 | * |
||
| 50 | * @param string $path |
||
| 51 | * @param string $message |
||
| 52 | * @param string $content |
||
| 53 | * @param string $branch |
||
| 54 | * |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | public function createFile(string $path, string $message, string $content, |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Update a file |
||
| 68 | * |
||
| 69 | * @link https://developer.github.com/v3/repos/contents/#update-a-file |
||
| 70 | * |
||
| 71 | * @param string $path |
||
| 72 | * @param string $message |
||
| 73 | * @param string $content |
||
| 74 | * @param string $sha |
||
| 75 | * @param string $branch |
||
| 76 | * |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function updateFile(string $path, string $message, string $content, string $sha, |
|
|
|
|||
| 80 | string $branch = AbstractApi::BRANCH_MASTER): array |
||
| 81 | { |
||
| 82 | return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/contents/:path?:args', |
||
| 83 | $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $path, |
||
| 84 | http_build_query(['message' => $message, 'content' => $content, 'sha' => $sha, 'branch' => $branch])), |
||
| 85 | Request::METHOD_PUT); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Delete a file |
||
| 90 | * |
||
| 91 | * @link https://developer.github.com/v3/repos/contents/#delete-a-file |
||
| 92 | * |
||
| 93 | * @param string $path |
||
| 94 | * @param string $message |
||
| 95 | * @param string $sha |
||
| 96 | * @param string $branch |
||
| 97 | * |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function deleteFile(string $path, string $message, string $sha, |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get archive link |
||
| 113 | * |
||
| 114 | * @link https://developer.github.com/v3/repos/contents/#get-archive-link |
||
| 115 | * |
||
| 116 | * @param string $archiveFormat |
||
| 117 | * @param string $ref |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function getArchiveLink(string $archiveFormat = AbstractApi::ARCHIVE_TARBALL, |
||
| 127 | } |
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.