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 |
||
| 39 | class CirclesService { |
||
| 40 | |||
| 41 | /** @var string */ |
||
| 42 | private $userId; |
||
| 43 | |||
| 44 | /** @var IL10N */ |
||
| 45 | private $l10n; |
||
| 46 | |||
| 47 | /** @var ConfigService */ |
||
| 48 | private $configService; |
||
| 49 | |||
| 50 | /** @var CirclesMapper */ |
||
| 51 | private $dbCircles; |
||
| 52 | |||
| 53 | /** @var MembersMapper */ |
||
| 54 | private $dbMembers; |
||
| 55 | |||
| 56 | /** @var MiscService */ |
||
| 57 | private $miscService; |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * CirclesService constructor. |
||
| 62 | * |
||
| 63 | * @param $userId |
||
| 64 | * @param IL10N $l10n |
||
| 65 | * @param ConfigService $configService |
||
| 66 | * @param DatabaseService $databaseService |
||
| 67 | * @param MiscService $miscService |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function __construct( |
|
|
|
|||
| 70 | $userId, |
||
| 71 | IL10N $l10n, |
||
| 72 | ConfigService $configService, |
||
| 73 | DatabaseService $databaseService, |
||
| 74 | MiscService $miscService |
||
| 75 | ) { |
||
| 76 | $this->userId = $userId; |
||
| 77 | $this->l10n = $l10n; |
||
| 78 | $this->configService = $configService; |
||
| 79 | $this->miscService = $miscService; |
||
| 80 | |||
| 81 | $this->dbCircles = $databaseService->getCirclesMapper(); |
||
| 82 | $this->dbMembers = $databaseService->getMembersMapper(); |
||
| 83 | } |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Create circle using this->userId as owner |
||
| 88 | * |
||
| 89 | * @param int $type |
||
| 90 | * @param string $name |
||
| 91 | * |
||
| 92 | * @return Circle |
||
| 93 | * @throws CircleTypeDisabledException |
||
| 94 | * @throws \Exception |
||
| 95 | */ |
||
| 96 | public function createCircle($type, $name) { |
||
| 97 | self::convertTypeStringToBitValue($type); |
||
| 98 | |||
| 99 | if ($type === "") { |
||
| 100 | throw new CircleTypeDisabledException( |
||
| 101 | $this->l10n->t('You need a specify a type of circle') |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (!$this->configService->isCircleAllowed($type)) { |
||
| 106 | throw new CircleTypeDisabledException( |
||
| 107 | $this->l10n->t('You cannot create this type of circle') |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | $owner = new Member($this->l10n, $this->userId); |
||
| 112 | $owner->setStatus(Member::STATUS_MEMBER); |
||
| 113 | $circle = new Circle($this->l10n, $type, $name); |
||
| 114 | $circle->setMembers([$owner]); |
||
| 115 | |||
| 116 | try { |
||
| 117 | $this->dbCircles->create($circle, $owner); |
||
| 118 | $this->dbMembers->add($owner); |
||
| 119 | } catch (\Exception $e) { |
||
| 120 | $this->dbCircles->destroy($circle->getId()); |
||
| 121 | throw $e; |
||
| 122 | } |
||
| 123 | |||
| 124 | return $circle; |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 130 | * |
||
| 131 | * @param $type |
||
| 132 | * @param string $name |
||
| 133 | * @param int $level |
||
| 134 | * |
||
| 135 | * @return Circle[] |
||
| 136 | * @throws CircleTypeDisabledException |
||
| 137 | */ |
||
| 138 | public function listCircles($type, $name = '', $level = 0) { |
||
| 139 | self::convertTypeStringToBitValue($type); |
||
| 140 | |||
| 141 | if (!$this->configService->isCircleAllowed((int)$type)) { |
||
| 142 | throw new CircleTypeDisabledException( |
||
| 143 | $this->l10n->t('You cannot display this type of circle') |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | $data = []; |
||
| 148 | $result = $this->dbCircles->findCirclesByUser($this->userId, $type, $name, $level); |
||
| 149 | foreach ($result as $item) { |
||
| 150 | $data[] = $item; |
||
| 151 | } |
||
| 152 | |||
| 153 | return $data; |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * returns details on circle and its members if this->userId is a member itself. |
||
| 159 | * |
||
| 160 | * @param $circleId |
||
| 161 | * |
||
| 162 | * @return Circle |
||
| 163 | * @throws \Exception |
||
| 164 | * @internal param $circleId |
||
| 165 | * @internal param string $iError |
||
| 166 | */ |
||
| 167 | public function detailsCircle($circleId) { |
||
| 168 | |||
| 169 | try { |
||
| 170 | $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); |
||
| 171 | if ($circle->getUser() |
||
| 172 | ->isLevel(Member::LEVEL_MEMBER) |
||
| 173 | ) { |
||
| 174 | $members = $this->dbMembers->getMembersFromCircle( |
||
| 175 | $circleId, $circle->getUser() |
||
| 176 | ); |
||
| 177 | $circle->setMembers($members); |
||
| 178 | } |
||
| 179 | } catch (\Exception $e) { |
||
| 180 | throw $e; |
||
| 181 | } |
||
| 182 | |||
| 183 | return $circle; |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Join a circle. |
||
| 189 | * |
||
| 190 | * @param $circleId |
||
| 191 | * |
||
| 192 | * @return null|Member |
||
| 193 | * @throws \Exception |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function joinCircle($circleId) { |
|
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Leave a circle. |
||
| 221 | * |
||
| 222 | * @param $circleId |
||
| 223 | * |
||
| 224 | * @return null|Member |
||
| 225 | * @throws \Exception |
||
| 226 | */ |
||
| 227 | public function leaveCircle($circleId) { |
||
| 228 | |||
| 229 | try { |
||
| 230 | $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); |
||
| 231 | $member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId, false); |
||
| 232 | |||
| 233 | if (!$member->isAlmostMember()) { |
||
| 234 | $member->hasToBeMember(); |
||
| 235 | } |
||
| 236 | |||
| 237 | $member->cantBeOwner(); |
||
| 238 | $member->setStatus(Member::STATUS_NONMEMBER); |
||
| 239 | $member->setLevel(Member::LEVEL_NONE); |
||
| 240 | $this->dbMembers->editMember($member); |
||
| 241 | |||
| 242 | } catch (\Exception $e) { |
||
| 243 | throw $e; |
||
| 244 | } |
||
| 245 | |||
| 246 | return $member; |
||
| 247 | } |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * destroy a circle. |
||
| 252 | * |
||
| 253 | * @param int $circleId |
||
| 254 | * |
||
| 255 | * @throws MemberIsNotOwnerException |
||
| 256 | */ |
||
| 257 | public function removeCircle($circleId) { |
||
| 258 | |||
| 259 | try { |
||
| 260 | $member = $this->dbMembers->getMemberFromCircle($circleId, $this->userId, false); |
||
| 261 | $member->hasToBeOwner(); |
||
| 262 | |||
| 263 | $this->dbMembers->removeAllFromCircle($circleId); |
||
| 264 | $this->dbCircles->destroy($circleId); |
||
| 265 | |||
| 266 | } catch (MemberIsNotOwnerException $e) { |
||
| 267 | throw $e; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @param $circleName |
||
| 274 | * |
||
| 275 | * @return Circle|null |
||
| 276 | */ |
||
| 277 | public function infoCircleByName($circleName) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Convert a Type in String to its Bit Value |
||
| 283 | * |
||
| 284 | * @param $type |
||
| 285 | * |
||
| 286 | * @return int |
||
| 287 | */ |
||
| 288 | public static function convertTypeStringToBitValue(& $type) { |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * getCircleIcon() |
||
| 311 | * |
||
| 312 | * Return the right imagePath for a type of circle. |
||
| 313 | * |
||
| 314 | * @param string $type |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public static function getCircleIcon($type) { |
||
| 333 | |||
| 334 | } |
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.