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:
Complex classes like Member often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Member, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class Member extends BaseMember { |
||
42 | |||
43 | |||
44 | public function inviteToCircle($circleType) { |
||
56 | |||
57 | |||
58 | /** |
||
59 | * @param int $circleType |
||
60 | * |
||
61 | * @throws MemberCantJoinCircleException |
||
62 | */ |
||
63 | public function joinCircle($circleType) { |
||
64 | |||
65 | switch ($circleType) { |
||
66 | case Circle::CIRCLES_PERSONAL: |
||
67 | case Circle::CIRCLES_SECRET: |
||
68 | case Circle::CIRCLES_PUBLIC: |
||
69 | return $this->addMemberToCircle(); |
||
70 | |||
71 | case Circle::CIRCLES_CLOSED: |
||
72 | return $this->joinClosedCircle(); |
||
73 | } |
||
74 | |||
75 | throw new MemberCantJoinCircleException($this->l10n->t('You cannot join this circle')); |
||
76 | } |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Update status of member like he joined a public circle. |
||
81 | */ |
||
82 | public function addMemberToCircle() { |
||
83 | |||
84 | if ($this->getStatus() === Member::STATUS_NONMEMBER |
||
85 | || $this->getStatus() === Member::STATUS_KICKED |
||
86 | ) { |
||
87 | $this->setAsAMember(Member::LEVEL_MEMBER); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Update status of member like he joined a closed circle |
||
94 | * (invite/request) |
||
95 | */ |
||
96 | View Code Duplication | private function joinClosedCircle() { |
|
|
|||
97 | |||
98 | switch ($this->getStatus()) { |
||
99 | case Member::STATUS_NONMEMBER: |
||
100 | case Member::STATUS_KICKED: |
||
101 | $this->setStatus(Member::STATUS_REQUEST); |
||
102 | break; |
||
103 | |||
104 | case Member::STATUS_INVITED: |
||
105 | $this->setAsAMember(Member::LEVEL_MEMBER); |
||
106 | break; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | |||
111 | View Code Duplication | private function inviteIntoClosedCircle() { |
|
112 | switch ($this->getStatus()) { |
||
113 | case Member::STATUS_NONMEMBER: |
||
114 | case Member::STATUS_KICKED: |
||
115 | $this->setStatus(Member::STATUS_INVITED); |
||
116 | break; |
||
117 | |||
118 | case Member::STATUS_REQUEST: |
||
119 | $this->setAsAMember(Member::LEVEL_MEMBER); |
||
120 | break; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | |||
125 | /** |
||
126 | * @throws MemberIsNotModeratorException |
||
127 | */ |
||
128 | public function hasToBeModerator() { |
||
135 | |||
136 | |||
137 | /** |
||
138 | * @param $level |
||
139 | * |
||
140 | * @throws ModeratorIsNotHighEnoughException |
||
141 | */ |
||
142 | public function hasToBeHigherLevel($level) { |
||
150 | |||
151 | |||
152 | /** |
||
153 | * @throws MemberDoesNotExistException |
||
154 | */ |
||
155 | public function hasToBeMember() { |
||
162 | |||
163 | |||
164 | /** |
||
165 | * @throws MemberDoesNotExistException |
||
166 | */ |
||
167 | public function hasToBeMemberOrAlmost() { |
||
174 | |||
175 | |||
176 | /** |
||
177 | * @throws MemberIsOwnerException |
||
178 | */ |
||
179 | public function cantBeOwner() { |
||
186 | |||
187 | |||
188 | /** |
||
189 | * return if member already exists |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function alreadyExistOrJoining() { |
||
199 | |||
200 | |||
201 | /** |
||
202 | * @param bool $able |
||
203 | */ |
||
204 | public function broadcasting($able) { |
||
207 | |||
208 | |||
209 | /** |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function isBroadcasting() { |
||
215 | |||
216 | |||
217 | /** |
||
218 | * @throws MemberTypeCantEditLevelException |
||
219 | */ |
||
220 | public function levelHasToBeEditable() { |
||
227 | |||
228 | |||
229 | /** |
||
230 | * @throws MemberAlreadyExistsException |
||
231 | * @throws MemberIsBlockedException |
||
232 | */ |
||
233 | View Code Duplication | public function hasToBeAbleToJoinTheCircle() { |
|
247 | |||
248 | |||
249 | /** |
||
250 | * @throws MemberAlreadyExistsException |
||
251 | */ |
||
252 | View Code Duplication | public function hasToBeInviteAble() { |
|
266 | |||
267 | } |
||
268 |
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.