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 |
||
| 12 | class Followers extends AbstractUsers |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * List followers of a user |
||
| 17 | * |
||
| 18 | * @link https://developer.github.com/v3/users/followers/#list-followers-of-a-user |
||
| 19 | * |
||
| 20 | * @param null|string $username |
||
| 21 | * |
||
| 22 | * @return array |
||
| 23 | * @throws \Exception |
||
| 24 | */ |
||
| 25 | View Code Duplication | public function listFollowers(string $username = null): array |
|
|
|
|||
| 26 | { |
||
| 27 | $url = '/user/followers'; |
||
| 28 | if (null !== $username) { |
||
| 29 | $url = $this->getApi()->sprintf('/users/:username/followers', $username); |
||
| 30 | } |
||
| 31 | |||
| 32 | return $this->getApi()->request($url); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * List users followed by another user |
||
| 37 | * |
||
| 38 | * @link https://developer.github.com/v3/users/followers/#list-users-followed-by-another-user |
||
| 39 | * |
||
| 40 | * @param null|string $username |
||
| 41 | * |
||
| 42 | * @return array |
||
| 43 | * @throws \Exception |
||
| 44 | */ |
||
| 45 | View Code Duplication | public function listUsersFollowedBy(string $username = null): array |
|
| 46 | { |
||
| 47 | $url = '/user/following'; |
||
| 48 | if (null !== $username) { |
||
| 49 | $url = $this->getApi()->sprintf('/users/:username/following', $username); |
||
| 50 | } |
||
| 51 | |||
| 52 | return $this->getApi()->request($url); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Check if you are following a user |
||
| 57 | * |
||
| 58 | * @link https://developer.github.com/v3/users/followers/#check-if-you-are-following-a-user |
||
| 59 | * |
||
| 60 | * @param string $username |
||
| 61 | * |
||
| 62 | * @return bool |
||
| 63 | * @throws \Exception |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function checkFollowingUser(string $username): bool |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Check if one user follows another |
||
| 78 | * |
||
| 79 | * @link https://developer.github.com/v3/users/followers/#check-if-one-user-follows-another |
||
| 80 | * |
||
| 81 | * @param string $username |
||
| 82 | * @param string $targetUser |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | * @throws \Exception |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function checkUserFollowsAnother(string $username, string $targetUser): bool |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Follow a user |
||
| 101 | * |
||
| 102 | * @link https://developer.github.com/v3/users/followers/#follow-a-user |
||
| 103 | * |
||
| 104 | * @param string $username |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | * @throws \Exception |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function followUser(string $username): bool |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Unfollow a user |
||
| 122 | * |
||
| 123 | * @link https://developer.github.com/v3/users/followers/#unfollow-a-user |
||
| 124 | * |
||
| 125 | * @param string $username |
||
| 126 | * |
||
| 127 | * @return bool |
||
| 128 | * @throws \Exception |
||
| 129 | */ |
||
| 130 | View Code Duplication | public function unfollowUser(string $username): bool |
|
| 141 | } |
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.