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 |
||
| 20 | class Members extends AbstractPackage |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Members list. |
||
| 24 | * |
||
| 25 | * List all users who are members of an organization. |
||
| 26 | * A member is a user that belongs to at least 1 team in the organization. |
||
| 27 | * If the authenticated user is also a member of this organization then |
||
| 28 | * both concealed and public members will be returned. |
||
| 29 | * If the requester is not a member of the organization the query will be |
||
| 30 | * redirected to the public members list. |
||
| 31 | * |
||
| 32 | * @param string $org The name of the organization. |
||
| 33 | * |
||
| 34 | * @throws \UnexpectedValueException |
||
| 35 | * @since 1.0 |
||
| 36 | * |
||
| 37 | * @return boolean|mixed |
||
| 38 | */ |
||
| 39 | View Code Duplication | public function getList($org) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Check membership. |
||
| 65 | * |
||
| 66 | * Check if a user is, publicly or privately, a member of the organization. |
||
| 67 | * |
||
| 68 | * @param string $org The name of the organization. |
||
| 69 | * @param string $user The name of the user. |
||
| 70 | * |
||
| 71 | * @throws \UnexpectedValueException |
||
| 72 | * @since 1.0 |
||
| 73 | * |
||
| 74 | * @return boolean |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function check($org, $user) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Add a member. |
||
| 109 | * |
||
| 110 | * To add someone as a member to an org, you must add them to a team. |
||
| 111 | */ |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Remove a member. |
||
| 115 | * |
||
| 116 | * Removing a user from this list will remove them from all teams and they will no longer have |
||
| 117 | * any access to the organization’s repositories. |
||
| 118 | * |
||
| 119 | * @param string $org The name of the organization. |
||
| 120 | * @param string $user The name of the user. |
||
| 121 | * |
||
| 122 | * @since 1.0 |
||
| 123 | * |
||
| 124 | * @return object |
||
| 125 | */ |
||
| 126 | public function remove($org, $user) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Public members list. |
||
| 139 | * |
||
| 140 | * Members of an organization can choose to have their membership publicized or not. |
||
| 141 | * |
||
| 142 | * @param string $org The name of the organization. |
||
| 143 | * |
||
| 144 | * @since 1.0 |
||
| 145 | * |
||
| 146 | * @return object |
||
| 147 | */ |
||
| 148 | View Code Duplication | public function getListPublic($org) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Check public membership. |
||
| 160 | * |
||
| 161 | * @param string $org The name of the organization. |
||
| 162 | * @param string $user The name of the user. |
||
| 163 | * |
||
| 164 | * @throws \UnexpectedValueException |
||
| 165 | * @since 1.0 |
||
| 166 | * |
||
| 167 | * @return boolean |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function checkPublic($org, $user) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Publicize a user’s membership. |
||
| 196 | * |
||
| 197 | * @param string $org The name of the organization. |
||
| 198 | * @param string $user The name of the user. |
||
| 199 | * |
||
| 200 | * @since 1.0 |
||
| 201 | * |
||
| 202 | * @return object |
||
| 203 | */ |
||
| 204 | public function publicize($org, $user) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Conceal a user’s membership. |
||
| 217 | * |
||
| 218 | * @param string $org The name of the organization. |
||
| 219 | * @param string $user The name of the user. |
||
| 220 | * |
||
| 221 | * @since 1.0 |
||
| 222 | * |
||
| 223 | * @return object |
||
| 224 | */ |
||
| 225 | public function conceal($org, $user) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get organization membership |
||
| 238 | * |
||
| 239 | * In order to get a user's membership with an organization, the authenticated user must be an organization owner. |
||
| 240 | * |
||
| 241 | * @param string $org The name of the organization. |
||
| 242 | * @param string $user The name of the user. |
||
| 243 | * |
||
| 244 | * @return object |
||
| 245 | * |
||
| 246 | * @since 1.4.0 |
||
| 247 | */ |
||
| 248 | public function getMembership($org, $user) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Add or update organization membership |
||
| 258 | * |
||
| 259 | * In order to create or update a user's membership with an organization, the authenticated user must be an organization owner. |
||
| 260 | * |
||
| 261 | * @param string $org The name of the organization. |
||
| 262 | * @param string $user The name of the user. |
||
| 263 | * @param string $role The role to give the user in the organization. Can be either 'member' or 'admin'. |
||
| 264 | * |
||
| 265 | * @return object |
||
| 266 | * |
||
| 267 | * @since 1.4.0 |
||
| 268 | */ |
||
| 269 | public function updateMembership($org, $user, $role = 'member') |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Remove organization membership |
||
| 290 | * |
||
| 291 | * In order to remove a user's membership with an organization, the authenticated user must be an organization owner. |
||
| 292 | * |
||
| 293 | * @param string $org The name of the organization. |
||
| 294 | * @param string $user The name of the user. |
||
| 295 | * |
||
| 296 | * @return object |
||
| 297 | * |
||
| 298 | * @since 1.4.0 |
||
| 299 | */ |
||
| 300 | public function removeMembership($org, $user) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * List your organization memberships |
||
| 313 | * |
||
| 314 | * @return object |
||
| 315 | * |
||
| 316 | * @since 1.4.0 |
||
| 317 | */ |
||
| 318 | public function listMemberships() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get your organization membership |
||
| 328 | * |
||
| 329 | * @param string $org The name of the organization. |
||
| 330 | * |
||
| 331 | * @return object |
||
| 332 | * |
||
| 333 | * @since 1.4.0 |
||
| 334 | */ |
||
| 335 | public function listOrganizationMembership($org) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Edit your organization membership |
||
| 345 | * |
||
| 346 | * @param string $org The name of the organization. |
||
| 347 | * @param string $state The state that the membership should be in. |
||
| 348 | * |
||
| 349 | * @return object |
||
| 350 | * |
||
| 351 | * @since 1.4.0 |
||
| 352 | */ |
||
| 353 | View Code Duplication | public function editOrganizationMembership($org, $state) |
|
| 366 | } |
||
| 367 |
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.