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 |
||
| 41 | class CirclesService { |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | private $userId; |
||
| 45 | |||
| 46 | /** @var IL10N */ |
||
| 47 | private $l10n; |
||
| 48 | |||
| 49 | /** @var ConfigService */ |
||
| 50 | private $configService; |
||
| 51 | |||
| 52 | /** @var CirclesRequest */ |
||
| 53 | private $circlesRequest; |
||
| 54 | |||
| 55 | /** @var MembersRequest */ |
||
| 56 | private $membersRequest; |
||
| 57 | |||
| 58 | /** @var EventsService */ |
||
| 59 | private $eventsService; |
||
| 60 | |||
| 61 | /** @var MiscService */ |
||
| 62 | private $miscService; |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * CirclesService constructor. |
||
| 67 | * |
||
| 68 | * @param $userId |
||
| 69 | * @param IL10N $l10n |
||
| 70 | * @param ConfigService $configService |
||
| 71 | * @param CirclesRequest $circlesRequest |
||
| 72 | * @param MembersRequest $membersRequest |
||
| 73 | * @param EventsService $eventsService |
||
| 74 | * @param MiscService $miscService |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function __construct( |
|
|
1 ignored issue
–
show
|
|||
| 77 | $userId, |
||
| 78 | IL10N $l10n, |
||
| 79 | ConfigService $configService, |
||
| 80 | CirclesRequest $circlesRequest, |
||
| 81 | MembersRequest $membersRequest, |
||
| 82 | EventsService $eventsService, |
||
| 83 | MiscService $miscService |
||
| 84 | ) { |
||
| 85 | $this->userId = $userId; |
||
| 86 | $this->l10n = $l10n; |
||
| 87 | $this->configService = $configService; |
||
| 88 | $this->circlesRequest = $circlesRequest; |
||
| 89 | $this->membersRequest = $membersRequest; |
||
| 90 | $this->eventsService = $eventsService; |
||
| 91 | $this->miscService = $miscService; |
||
| 92 | } |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Create circle using this->userId as owner |
||
| 97 | * |
||
| 98 | * @param int|string $type |
||
| 99 | * @param string $name |
||
| 100 | * |
||
| 101 | * @return Circle |
||
| 102 | * @throws CircleTypeDisabledException |
||
| 103 | * @throws \Exception |
||
| 104 | */ |
||
| 105 | public function createCircle($type, $name) { |
||
| 106 | self::convertTypeStringToBitValue($type); |
||
| 107 | $type = (int)$type; |
||
| 108 | |||
| 109 | if ($type === '') { |
||
| 110 | throw new CircleTypeDisabledException( |
||
| 111 | $this->l10n->t('You need a specify a type of circle') |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | if (!$this->configService->isCircleAllowed($type)) { |
||
| 116 | throw new CircleTypeDisabledException( |
||
| 117 | $this->l10n->t('You cannot create this type of circle') |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | $circle = new Circle($this->l10n, $type, $name); |
||
| 122 | |||
| 123 | try { |
||
| 124 | $this->circlesRequest->createCircle($circle, $this->userId); |
||
| 125 | $this->membersRequest->createMember($circle->getOwner()); |
||
| 126 | } catch (CircleAlreadyExistsException $e) { |
||
| 127 | throw $e; |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->eventsService->onCircleCreation($circle); |
||
| 131 | |||
| 132 | return $circle; |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 138 | * |
||
| 139 | * @param mixed $type |
||
| 140 | * @param string $name |
||
| 141 | * @param int $level |
||
| 142 | * |
||
| 143 | * @return Circle[] |
||
| 144 | * @throws CircleTypeDisabledException |
||
| 145 | */ |
||
| 146 | public function listCircles($type, $name = '', $level = 0) { |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * returns details on circle and its members if this->userId is a member itself. |
||
| 167 | * |
||
| 168 | * @param string $circleUniqueId |
||
| 169 | * |
||
| 170 | * @return Circle |
||
| 171 | * @throws \Exception |
||
| 172 | ] */ |
||
| 173 | public function detailsCircle($circleUniqueId) { |
||
| 174 | |||
| 175 | try { |
||
| 176 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
| 177 | if ($circle->getHigherViewer() |
||
| 178 | ->isLevel(Member::LEVEL_MEMBER) |
||
| 179 | ) { |
||
| 180 | $this->detailsCircleMembers($circle); |
||
| 181 | $this->detailsCircleLinkedGroups($circle); |
||
| 182 | $this->detailsCircleFederatedCircles($circle); |
||
| 183 | } |
||
| 184 | } catch (\Exception $e) { |
||
| 185 | throw $e; |
||
| 186 | } |
||
| 187 | |||
| 188 | return $circle; |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * get the Members list and add the result to the Circle. |
||
| 194 | * |
||
| 195 | * @param Circle $circle |
||
| 196 | */ |
||
| 197 | private function detailsCircleMembers(Circle &$circle) { |
||
| 198 | $members = |
||
| 199 | $this->membersRequest->getMembers($circle->getUniqueId(), $circle->getHigherViewer()); |
||
| 200 | |||
| 201 | $circle->setMembers($members); |
||
| 202 | } |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * get the Linked Group list and add the result to the Circle. |
||
| 207 | * |
||
| 208 | * @param Circle $circle |
||
| 209 | */ |
||
| 210 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 211 | $groups = []; |
||
| 212 | if ($this->configService->isLinkedGroupsAllowed()) { |
||
| 213 | $groups = |
||
| 214 | $this->membersRequest->getGroupsFromCircle( |
||
| 215 | $circle->getUniqueId(), $circle->getHigherViewer() |
||
| 216 | ); |
||
| 217 | } |
||
| 218 | |||
| 219 | $circle->setGroups($groups); |
||
| 220 | } |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * get the Federated Circles list and add the result to the Circle. |
||
| 225 | * |
||
| 226 | * @param Circle $circle |
||
| 227 | */ |
||
| 228 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 229 | $links = []; |
||
| 230 | |||
| 231 | try { |
||
| 232 | if ($this->configService->isFederatedCirclesAllowed()) { |
||
| 233 | $circle->hasToBeFederated(); |
||
| 234 | $links = $this->circlesRequest->getLinksFromCircle($circle->getUniqueId()); |
||
| 235 | } |
||
| 236 | } catch (FederatedCircleNotAllowedException $e) { |
||
| 237 | } |
||
| 238 | |||
| 239 | $circle->setLinks($links); |
||
| 240 | } |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * save new settings if current user is admin. |
||
| 245 | * |
||
| 246 | * @param string $circleUniqueId |
||
| 247 | * @param array $settings |
||
| 248 | * |
||
| 249 | * @return Circle |
||
| 250 | * @throws \Exception |
||
| 251 | */ |
||
| 252 | public function settingsCircle($circleUniqueId, $settings) { |
||
| 253 | |||
| 254 | try { |
||
| 255 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
| 256 | $circle->getHigherViewer() |
||
| 257 | ->hasToBeOwner(); |
||
| 258 | |||
| 259 | $ak = array_keys($settings); |
||
| 260 | foreach ($ak AS $k) { |
||
| 261 | $circle->setSetting($k, $settings[$k]); |
||
| 262 | } |
||
| 263 | |||
| 264 | $this->circlesRequest->updateCircle($circle); |
||
| 265 | } catch (\Exception $e) { |
||
| 266 | throw $e; |
||
| 267 | } |
||
| 268 | |||
| 269 | return $circle; |
||
| 270 | } |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * Join a circle. |
||
| 275 | * |
||
| 276 | * @param string $circleUniqueId |
||
| 277 | * |
||
| 278 | * @return null|Member |
||
| 279 | * @throws \Exception |
||
| 280 | */ |
||
| 281 | View Code Duplication | public function joinCircle($circleUniqueId) { |
|
| 282 | |||
| 283 | try { |
||
| 284 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
| 285 | |||
| 286 | $member = $this->membersRequest->getFreshNewMember($circleUniqueId, $this->userId, Member::TYPE_USER); |
||
| 287 | $member->hasToBeAbleToJoinTheCircle(); |
||
| 288 | $member->joinCircle($circle->getType()); |
||
| 289 | $this->membersRequest->updateMember($member); |
||
| 290 | |||
| 291 | $this->eventsService->onMemberNew($circle, $member); |
||
| 292 | } catch (\Exception $e) { |
||
| 293 | throw $e; |
||
| 294 | } |
||
| 295 | |||
| 296 | return $member; |
||
| 297 | } |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * Leave a circle. |
||
| 302 | * |
||
| 303 | * @param string $circleUniqueId |
||
| 304 | * |
||
| 305 | * @return null|Member |
||
| 306 | * @throws \Exception |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function leaveCircle($circleUniqueId) { |
|
| 309 | |||
| 310 | try { |
||
| 311 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
| 312 | $member = $circle->getViewer(); |
||
| 313 | |||
| 314 | $member->hasToBeMemberOrAlmost(); |
||
| 315 | $member->cantBeOwner(); |
||
| 316 | |||
| 317 | $this->eventsService->onMemberLeaving($circle, $member); |
||
| 318 | |||
| 319 | $member->setStatus(Member::STATUS_NONMEMBER); |
||
| 320 | $member->setLevel(Member::LEVEL_NONE); |
||
| 321 | $this->membersRequest->updateMember($member); |
||
| 322 | } catch (\Exception $e) { |
||
| 323 | throw $e; |
||
| 324 | } |
||
| 325 | |||
| 326 | return $member; |
||
| 327 | } |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * destroy a circle. |
||
| 332 | * |
||
| 333 | * @param string $circleUniqueId |
||
| 334 | * |
||
| 335 | * @throws MemberIsNotOwnerException |
||
| 336 | */ |
||
| 337 | public function removeCircle($circleUniqueId) { |
||
| 338 | |||
| 339 | try { |
||
| 340 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
| 341 | $circle->getHigherViewer() |
||
| 342 | ->hasToBeOwner(); |
||
| 343 | |||
| 344 | $this->eventsService->onCircleDestruction($circle); |
||
| 345 | |||
| 346 | $this->membersRequest->removeAllFromCircle($circleUniqueId); |
||
| 347 | $this->circlesRequest->destroyCircle($circleUniqueId); |
||
| 348 | |||
| 349 | } catch (MemberIsNotOwnerException $e) { |
||
| 350 | throw $e; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * @param $circleName |
||
| 357 | * |
||
| 358 | * @return Circle|null |
||
| 359 | */ |
||
| 360 | public function infoCircleByName($circleName) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Convert a Type in String to its Bit Value |
||
| 366 | * |
||
| 367 | * @param string $type |
||
| 368 | */ |
||
| 369 | public static function convertTypeStringToBitValue(&$type) { |
||
| 370 | if (strtolower($type) === 'personal') { |
||
| 371 | $type = Circle::CIRCLES_PERSONAL; |
||
| 372 | } |
||
| 373 | if (strtolower($type) === 'secret') { |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * getCircleIcon() |
||
| 390 | * |
||
| 391 | * Return the right imagePath for a type of circle. |
||
| 392 | * |
||
| 393 | * @param string $type |
||
| 394 | * @param bool $png |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public static function getCircleIcon($type, $png = false) { |
||
| 427 | |||
| 428 | } |
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.