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 |
||
| 37 | class Member extends BaseMember implements \JsonSerializable { |
||
| 38 | |||
| 39 | |||
| 40 | public function inviteToCircle($circleType) { |
||
| 41 | if ($circleType === Circle::CIRCLES_PRIVATE) { |
||
| 42 | return $this->inviteIntoPrivateCircle(); |
||
| 43 | } |
||
| 44 | |||
| 45 | return $this->addMemberToCircle(); |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * @param int $circleType |
||
| 51 | * |
||
| 52 | * @throws MemberCantJoinCircle |
||
| 53 | */ |
||
| 54 | public function joinCircle($circleType) { |
||
| 55 | |||
| 56 | switch ($circleType) { |
||
| 57 | case Circle::CIRCLES_HIDDEN: |
||
| 58 | case Circle::CIRCLES_PUBLIC: |
||
| 59 | return $this->addMemberToCircle(); |
||
| 60 | |||
| 61 | case Circle::CIRCLES_PRIVATE: |
||
| 62 | return $this->joinPrivateCircle(); |
||
| 63 | } |
||
| 64 | |||
| 65 | throw new MemberCantJoinCircle(); |
||
| 66 | } |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Update status of member like he joined a public circle. |
||
| 71 | */ |
||
| 72 | private function addMemberToCircle() { |
||
| 73 | |||
| 74 | if ($this->getStatus() === Member::STATUS_NONMEMBER |
||
| 75 | || $this->getStatus() === Member::STATUS_KICKED |
||
| 76 | ) { |
||
| 77 | $this->setAsAMember(Member::LEVEL_MEMBER); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Update status of member like he joined a private circle |
||
| 84 | * (invite/request) |
||
| 85 | */ |
||
| 86 | View Code Duplication | private function joinPrivateCircle() { |
|
| 87 | |||
| 88 | switch ($this->getStatus()) { |
||
| 89 | case Member::STATUS_NONMEMBER: |
||
| 90 | case Member::STATUS_KICKED: |
||
| 91 | $this->setStatus(Member::STATUS_REQUEST); |
||
| 92 | break; |
||
| 93 | |||
| 94 | case Member::STATUS_INVITED: |
||
| 95 | $this->setAsAMember(Member::LEVEL_MEMBER); |
||
| 96 | break; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | View Code Duplication | private function inviteIntoPrivateCircle() { |
|
| 102 | switch ($this->getStatus()) { |
||
| 103 | case Member::STATUS_NONMEMBER: |
||
| 104 | case Member::STATUS_KICKED: |
||
| 105 | $this->setStatus(Member::STATUS_INVITED); |
||
| 106 | break; |
||
| 107 | |||
| 108 | case Member::STATUS_REQUEST: |
||
| 109 | $this->setAsAMember(Member::LEVEL_MEMBER); |
||
| 110 | break; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * @throws MemberIsNotModeratorException |
||
| 117 | */ |
||
| 118 | public function hasToBeModerator() { |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * @throws MemberIsNotModeratorException |
||
| 127 | */ |
||
| 128 | public function hasToBeOwner() { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @throws MemberDoesNotExistException |
||
| 137 | */ |
||
| 138 | public function hasToBeMember() { |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * @throws MemberIsOwnerException |
||
| 147 | */ |
||
| 148 | public function cantBeOwner() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param $member |
||
| 156 | * |
||
| 157 | * @throws MemberAlreadyExistsException |
||
| 158 | * @throws MemberIsBlockedException |
||
| 159 | */ |
||
| 160 | public function hasToBeAbleToJoinTheCircle() { |
||
| 170 | |||
| 171 | public function jsonSerialize() { |
||
| 182 | |||
| 183 | |||
| 184 | } |
||
| 185 | |||
| 187 |