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 |
||
| 40 | class Member extends BaseMember { |
||
| 41 | |||
| 42 | |||
| 43 | public function inviteToCircle($circleType) { |
||
| 44 | |||
| 45 | if ($circleType === 0) { |
||
| 46 | throw new CircleTypeNotValidException('Circle Type is not valid'); |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($circleType === Circle::CIRCLES_CLOSED) { |
||
| 50 | return $this->inviteIntoClosedCircle(); |
||
| 51 | } |
||
| 52 | |||
| 53 | return $this->addMemberToCircle(); |
||
| 54 | } |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * @param int $circleType |
||
| 59 | * |
||
| 60 | * @throws MemberCantJoinCircleException |
||
| 61 | */ |
||
| 62 | public function joinCircle($circleType) { |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Update status of member like he joined a public circle. |
||
| 79 | */ |
||
| 80 | private function addMemberToCircle() { |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * Update status of member like he joined a closed circle |
||
| 92 | * (invite/request) |
||
| 93 | */ |
||
| 94 | View Code Duplication | private function joinClosedCircle() { |
|
|
|
|||
| 95 | |||
| 96 | switch ($this->getStatus()) { |
||
| 97 | case Member::STATUS_NONMEMBER: |
||
| 98 | case Member::STATUS_KICKED: |
||
| 99 | $this->setStatus(Member::STATUS_REQUEST); |
||
| 100 | break; |
||
| 101 | |||
| 102 | case Member::STATUS_INVITED: |
||
| 103 | $this->setAsAMember(Member::LEVEL_MEMBER); |
||
| 104 | break; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | View Code Duplication | private function inviteIntoClosedCircle() { |
|
| 110 | switch ($this->getStatus()) { |
||
| 111 | case Member::STATUS_NONMEMBER: |
||
| 112 | case Member::STATUS_KICKED: |
||
| 113 | $this->setStatus(Member::STATUS_INVITED); |
||
| 114 | break; |
||
| 115 | |||
| 116 | case Member::STATUS_REQUEST: |
||
| 117 | $this->setAsAMember(Member::LEVEL_MEMBER); |
||
| 118 | break; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * @throws MemberIsNotModeratorException |
||
| 125 | */ |
||
| 126 | public function hasToBeModerator() { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @param $level |
||
| 137 | * |
||
| 138 | * @throws ModeratorIsNotHighEnoughException |
||
| 139 | */ |
||
| 140 | public function hasToBeHigherLevel($level) { |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * @throws MemberIsNotModeratorException |
||
| 152 | */ |
||
| 153 | public function hasToBeOwner() { |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @throws MemberIsNotModeratorException |
||
| 164 | */ |
||
| 165 | public function hasToBeAdmin() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @throws MemberDoesNotExistException |
||
| 175 | */ |
||
| 176 | public function hasToBeMember() { |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @throws MemberDoesNotExistException |
||
| 187 | */ |
||
| 188 | public function hasToBeMemberOrAlmost() { |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * @throws MemberIsOwnerException |
||
| 199 | */ |
||
| 200 | public function cantBeOwner() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @throws MemberAlreadyExistsException |
||
| 210 | * @throws MemberIsBlockedException |
||
| 211 | */ |
||
| 212 | public function hasToBeAbleToJoinTheCircle() { |
||
| 226 | |||
| 227 | } |
||
| 228 | |||
| 230 |
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.