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 |
||
| 36 | class MembersRequest extends MembersRequestBuilder { |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Returns information about a member. |
||
| 41 | * |
||
| 42 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
| 43 | * In case of interaction with users, Please use MembersService->getMember() instead. |
||
| 44 | * |
||
| 45 | * @param $circleId |
||
| 46 | * @param $userId |
||
| 47 | * |
||
| 48 | * @return Member |
||
| 49 | * @throws MemberDoesNotExistException |
||
| 50 | */ |
||
| 51 | View Code Duplication | public function forceGetMember($circleId, $userId) { |
|
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns members list of a circle, based on their level. |
||
| 73 | * |
||
| 74 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
| 75 | * In case of interaction with users, Please use getMembers() instead. |
||
| 76 | * |
||
| 77 | * @param int $circleId |
||
| 78 | * @param int $level |
||
| 79 | * @param bool $includeGroupMembers |
||
| 80 | * |
||
| 81 | * @return Member[] |
||
| 82 | */ |
||
| 83 | public function forceGetMembers( |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * @param int $circleId |
||
| 109 | * @param Member $viewer |
||
| 110 | * |
||
| 111 | * @return Member[] |
||
| 112 | * @throws \Exception |
||
| 113 | */ |
||
| 114 | public function getMembers($circleId, Member $viewer) { |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * forceGetGroup(); |
||
| 136 | * |
||
| 137 | * returns group information as a member within a Circle. |
||
| 138 | * |
||
| 139 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
| 140 | * In case of interaction with users, Please use getGroup() instead. |
||
| 141 | * |
||
| 142 | * @param int $circleId |
||
| 143 | * @param string $groupId |
||
| 144 | * |
||
| 145 | * @return Member |
||
| 146 | * @throws MemberDoesNotExistException |
||
| 147 | */ |
||
| 148 | View Code Duplication | public function forceGetGroup($circleId, $groupId) { |
|
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * includeGroupMembers(); |
||
| 169 | * |
||
| 170 | * This function will get members of a circle throw NCGroups and fill the result an existing |
||
| 171 | * Members List. In case of duplicate, higher level will be kept. |
||
| 172 | * |
||
| 173 | * @param Member[] $members |
||
| 174 | * @param int $circleId |
||
| 175 | * @param int $level |
||
| 176 | */ |
||
| 177 | private function includeGroupMembers(array &$members, $circleId, $level) { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * returns the index of a specific UserID in a Members List |
||
| 193 | * |
||
| 194 | * @param array $members |
||
| 195 | * @param $userId |
||
| 196 | * |
||
| 197 | * @return int |
||
| 198 | */ |
||
| 199 | private function indexOfMember(array $members, $userId) { |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * Returns members list of a Group Members of a Circle. The Level of the linked group will be |
||
| 213 | * assigned to each entry |
||
| 214 | * |
||
| 215 | * NOTE: Can contains duplicate. |
||
| 216 | * |
||
| 217 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
| 218 | * Do not use in case of direct interaction with users. |
||
| 219 | * |
||
| 220 | * @param int $circleId |
||
| 221 | * @param int $level |
||
| 222 | * |
||
| 223 | * @return Member[] |
||
| 224 | */ |
||
| 225 | public function forceGetGroupMembers($circleId, $level = Member::LEVEL_MEMBER) { |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * return the higher level group linked to a circle, that include the userId. |
||
| 245 | * |
||
| 246 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
| 247 | * In case of direct interaction with users, Please don't use this. |
||
| 248 | * |
||
| 249 | * @param int $circleId |
||
| 250 | * @param string $userId |
||
| 251 | * |
||
| 252 | * @return Member |
||
| 253 | */ |
||
| 254 | public function forceGetHigherLevelGroupFromUser($circleId, $userId) { |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Insert Member into database. |
||
| 278 | * |
||
| 279 | * @param Member $member |
||
| 280 | * |
||
| 281 | * @throws MemberAlreadyExistsException |
||
| 282 | */ |
||
| 283 | public function createMember(Member $member) { |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * @param int $circleId |
||
| 304 | * @param Member $viewer |
||
| 305 | * |
||
| 306 | * @return Member[] |
||
| 307 | * @throws MemberDoesNotExistException |
||
| 308 | */ |
||
| 309 | public function getGroups($circleId, Member $viewer) { |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * Insert Member into database. |
||
| 335 | * |
||
| 336 | * @param Member $member |
||
| 337 | * |
||
| 338 | * @throws MemberAlreadyExistsException |
||
| 339 | */ |
||
| 340 | public function insertGroup(Member $member) { |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * update database entry for a specific Member. |
||
| 359 | * |
||
| 360 | * @param Member $member |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public function updateMember(Member $member) { |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * removeAllFromCircle(); |
||
| 375 | * |
||
| 376 | * Remove All members from a Circle. Used when deleting a Circle. |
||
| 377 | * |
||
| 378 | * @param $circleId |
||
| 379 | */ |
||
| 380 | public function removeAllFromCircle($circleId) { |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * removeAllFromUser(); |
||
| 392 | * |
||
| 393 | * remove All membership from a User. Used when removing a User from the Cloud. |
||
| 394 | * |
||
| 395 | * @param $userId |
||
| 396 | */ |
||
| 397 | public function removeAllFromUser($userId) { |
||
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * update database entry for a specific Group. |
||
| 409 | * |
||
| 410 | * @param Member $member |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | public function updateGroup(Member $member) { |
||
| 422 | |||
| 423 | |||
| 424 | public function unlinkAllFromGroup($groupId) { |
||
| 428 | |||
| 429 | } |
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.