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 EventsService { |
||
38 | |||
39 | |||
40 | /** @var string */ |
||
41 | private $userId; |
||
42 | |||
43 | /** @var IManager */ |
||
44 | private $activityManager; |
||
45 | |||
46 | /** @var IUserManager */ |
||
47 | private $userManager; |
||
48 | |||
49 | /** @var CirclesRequest */ |
||
50 | private $circlesRequest; |
||
51 | |||
52 | /** @var MiscService */ |
||
53 | private $miscService; |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Events constructor. |
||
58 | * |
||
59 | * @param string $userId |
||
60 | * @param IManager $activityManager |
||
61 | * @param IUserManager $userManager |
||
62 | * @param CirclesRequest $circlesRequest |
||
63 | * @param MiscService $miscService |
||
64 | */ |
||
65 | public function __construct( |
||
75 | |||
76 | |||
77 | /** |
||
78 | * onCircleCreation() |
||
79 | * |
||
80 | * Called when a circle is created. |
||
81 | * Broadcast an activity to the cloud |
||
82 | * We won't do anything if the circle is not PUBLIC or PRIVATE |
||
83 | * |
||
84 | * @param Circle $circle |
||
85 | */ |
||
86 | public function onCircleCreation(Circle $circle) { |
||
104 | |||
105 | |||
106 | /** |
||
107 | * onCircleDestruction() |
||
108 | * |
||
109 | * Called when a circle is destroyed. |
||
110 | * Broadcast an activity on its members. |
||
111 | * We won't do anything if the circle is PERSONAL |
||
112 | * |
||
113 | * @param Circle $circle |
||
114 | */ |
||
115 | View Code Duplication | public function onCircleDestruction(Circle $circle) { |
|
|
|||
116 | if ($circle->getType() === Circle::CIRCLES_PERSONAL) { |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | $event = $this->generateEvent('circles_as_member'); |
||
121 | $event->setSubject('circle_delete', ['circle' => json_encode($circle)]); |
||
122 | $this->publishEvent( |
||
123 | $event, $this->circlesRequest->getMembers($circle->getId(), Member::LEVEL_MEMBER) |
||
124 | ); |
||
125 | } |
||
126 | |||
127 | |||
128 | /** |
||
129 | * onMemberNew() |
||
130 | * |
||
131 | * Called when a member is added to a circle. |
||
132 | * Broadcast an activity to the new member and to the moderators of the circle. |
||
133 | * We won't do anything if the circle is PERSONAL |
||
134 | * If the level is still 0, we will redirect to onMemberAlmost and manage the |
||
135 | * invitation/request from there |
||
136 | * If the level is Owner, we ignore the event. |
||
137 | * |
||
138 | * @param Circle $circle |
||
139 | * @param Member $member |
||
140 | */ |
||
141 | public function onMemberNew(Circle $circle, Member $member) { |
||
164 | |||
165 | |||
166 | /** |
||
167 | * onMemberAlmost() |
||
168 | * |
||
169 | * Called when a member is added to a circle with level=0 |
||
170 | * Trigger onMemberInvitation() or onMemberInvitationRequest() based on Member Status |
||
171 | * |
||
172 | * @param Circle $circle |
||
173 | * @param Member $member |
||
174 | */ |
||
175 | private function onMemberAlmost(Circle $circle, Member $member) { |
||
189 | |||
190 | |||
191 | /** |
||
192 | * onMemberInvitation() |
||
193 | * |
||
194 | * Called when a member is invited to a circle. |
||
195 | * Broadcast an activity to the invited member and to the moderators of the circle. |
||
196 | * |
||
197 | * @param Circle $circle |
||
198 | * @param Member $member |
||
199 | */ |
||
200 | View Code Duplication | public function onMemberInvitation(Circle $circle, Member $member) { |
|
217 | |||
218 | |||
219 | /** |
||
220 | * onMemberInvitationRequest() |
||
221 | * |
||
222 | * Called when a member request an invitation to a private circle. |
||
223 | * Broadcast an activity to the requester and to the moderators of the circle. |
||
224 | * |
||
225 | * @param Circle $circle |
||
226 | * @param Member $member |
||
227 | */ |
||
228 | View Code Duplication | public function onMemberInvitationRequest(Circle $circle, Member $member) { |
|
247 | |||
248 | |||
249 | /** |
||
250 | * onCircleMemberLeaving() |
||
251 | * |
||
252 | * Called when a member is removed from a circle. |
||
253 | * Broadcast an activity to the new member and to the moderators of the circle. |
||
254 | * We won't do anything if the circle is PERSONAL |
||
255 | * |
||
256 | * @param Circle $circle |
||
257 | * @param Member $member |
||
258 | */ |
||
259 | public function onMemberLeaving(Circle $circle, Member $member) { |
||
278 | |||
279 | |||
280 | /** |
||
281 | * onMemberNew() |
||
282 | * |
||
283 | * Called when a member have his level changed. |
||
284 | * Broadcast an activity to all moderator of the circle. |
||
285 | * We won't do anything if the circle is PERSONAL |
||
286 | * If the level is Owner, we identify the event as a Coup d'Etat and we broadcast all members. |
||
287 | * |
||
288 | * @param Circle $circle |
||
289 | * @param Member $member |
||
290 | */ |
||
291 | public function onMemberLevel(Circle $circle, Member $member) { |
||
315 | |||
316 | |||
317 | View Code Duplication | private function onMemberOwner(Circle $circle, Member $member) { |
|
330 | |||
331 | /** |
||
332 | * generateEvent() |
||
333 | * Create an Activity Event with the basic settings for the app. |
||
334 | * |
||
335 | * @param $type |
||
336 | * |
||
337 | * @return \OCP\Activity\IEvent |
||
338 | */ |
||
339 | private function generateEvent($type) { |
||
347 | |||
348 | |||
349 | private function publishEvent(IEvent $event, array $users) { |
||
363 | |||
364 | |||
365 | } |
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.