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 |
||
| 42 | class MembersService { |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | private $userId; |
||
| 46 | |||
| 47 | /** @var IL10N */ |
||
| 48 | private $l10n; |
||
| 49 | |||
| 50 | /** @var IUserManager */ |
||
| 51 | private $userManager; |
||
| 52 | |||
| 53 | /** @var ConfigService */ |
||
| 54 | private $configService; |
||
| 55 | |||
| 56 | /** @var CirclesRequest */ |
||
| 57 | private $circlesRequest; |
||
| 58 | |||
| 59 | /** @var MembersRequest */ |
||
| 60 | private $membersRequest; |
||
| 61 | |||
| 62 | /** @var EventsService */ |
||
| 63 | private $eventsService; |
||
| 64 | |||
| 65 | /** @var MiscService */ |
||
| 66 | private $miscService; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * MembersService constructor. |
||
| 70 | * |
||
| 71 | * @param $userId |
||
| 72 | * @param IL10N $l10n |
||
| 73 | * @param IUserManager $userManager |
||
| 74 | * @param ConfigService $configService |
||
| 75 | * @param CirclesRequest $circlesRequest |
||
| 76 | * @param MembersRequest $membersRequest |
||
| 77 | * @param EventsService $eventsService |
||
| 78 | * @param MiscService $miscService |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function __construct( |
|
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $circleUniqueId |
||
| 103 | * @param string $name |
||
| 104 | * |
||
| 105 | * @return array |
||
| 106 | * @throws \Exception |
||
| 107 | */ |
||
| 108 | public function addMember($circleUniqueId, $name) { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $circleUniqueId |
||
| 137 | * @param string $groupId |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | * @throws \Exception |
||
| 141 | */ |
||
| 142 | public function importMembersFromGroup($circleUniqueId, $groupId) { |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * getMember(); |
||
| 180 | * |
||
| 181 | * Will return any data of a user related to a circle (as a Member). User can be a 'non-member' |
||
| 182 | * Viewer needs to be at least Member of the Circle |
||
| 183 | * |
||
| 184 | * @param $circleId |
||
| 185 | * @param $userId |
||
| 186 | * |
||
| 187 | * @return Member |
||
| 188 | * @throws \Exception |
||
| 189 | */ |
||
| 190 | public function getMember($circleId, $userId) { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * Check if a fresh member can be generated (by addMember) |
||
| 209 | * |
||
| 210 | * @param string $circleUniqueId |
||
| 211 | * @param string $name |
||
| 212 | * |
||
| 213 | * @return null|Member |
||
| 214 | * @throws MemberAlreadyExistsException |
||
| 215 | * @throws \Exception |
||
| 216 | */ |
||
| 217 | private function getFreshNewMember($circleUniqueId, $name) { |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * return the real userId, with its real case |
||
| 245 | * |
||
| 246 | * @param $userId |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | * @throws NoUserException |
||
| 250 | */ |
||
| 251 | private function getRealUserId($userId) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * return if member already exists |
||
| 262 | * |
||
| 263 | * @param Member $member |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | private function memberAlreadyExist($member) { |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $circleUniqueId |
||
| 277 | * @param string $name |
||
| 278 | * @param int $level |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | * @throws \Exception |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function levelMember($circleUniqueId, $name, $level) { |
|
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * @param Circle $circle |
||
| 317 | * @param Member $member |
||
| 318 | * @param $level |
||
| 319 | * |
||
| 320 | * @throws \Exception |
||
| 321 | */ |
||
| 322 | private function editMemberLevel(Circle $circle, Member &$member, $level) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param Circle $circle |
||
| 342 | * @param Member $member |
||
| 343 | * |
||
| 344 | * @throws \Exception |
||
| 345 | */ |
||
| 346 | private function switchOwner(Circle $circle, Member &$member) { |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $circleUniqueId |
||
| 366 | * @param string $name |
||
| 367 | * |
||
| 368 | * @return array |
||
| 369 | * @throws \Exception |
||
| 370 | */ |
||
| 371 | public function removeMember($circleUniqueId, $name) { |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * When a user is removed, remove him from all Circles |
||
| 402 | * |
||
| 403 | * @param $userId |
||
| 404 | */ |
||
| 405 | public function onUserRemoved($userId) { |
||
| 408 | |||
| 409 | |||
| 410 | } |
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.