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 |
||
| 36 | class EventsService { |
||
| 37 | |||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | private $userId; |
||
| 41 | |||
| 42 | /** @var IManager */ |
||
| 43 | private $activityManager; |
||
| 44 | |||
| 45 | /** @var IUserManager */ |
||
| 46 | private $userManager; |
||
| 47 | |||
| 48 | /** @var CirclesRequest */ |
||
| 49 | private $circlesRequest; |
||
| 50 | |||
| 51 | /** @var MiscService */ |
||
| 52 | private $miscService; |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * Events constructor. |
||
| 57 | * |
||
| 58 | * @param string $userId |
||
| 59 | * @param IManager $activityManager |
||
| 60 | * @param IUserManager $userManager |
||
| 61 | * @param CirclesRequest $circlesRequest |
||
| 62 | * @param MiscService $miscService |
||
| 63 | */ |
||
| 64 | public function __construct( |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * onCircleCreation() |
||
| 78 | * |
||
| 79 | * Called when a circle is created. |
||
| 80 | * Broadcast an activity to the cloud |
||
| 81 | * We won't do anything if the circle is not PUBLIC or PRIVATE |
||
| 82 | * |
||
| 83 | * @param Circle $circle |
||
| 84 | */ |
||
| 85 | public function onCircleCreation(Circle $circle) { |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * onCircleDestruction() |
||
| 109 | * |
||
| 110 | * Called when a circle is destroyed. |
||
| 111 | * Broadcast an activity on its members. |
||
| 112 | * We won't do anything if the circle is PERSONAL |
||
| 113 | * |
||
| 114 | * @param Circle $circle |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function onCircleDestruction(Circle $circle) { |
|
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * onCircleInvitation() |
||
| 135 | * |
||
| 136 | * Called when a member is invited to a circle. |
||
| 137 | * Broadcast an activity to the invited member and to the moderators of the circle. |
||
| 138 | * |
||
| 139 | * @param Circle $circle |
||
| 140 | * @param Member $member |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function onCircleInvitation(Circle $circle, Member $member) { |
|
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * onCircleRequestInvitation() |
||
| 166 | * |
||
| 167 | * Called when a member request an invitation to a private circle. |
||
| 168 | * Broadcast an activity to the requester and to the moderators of the circle. |
||
| 169 | * |
||
| 170 | * @param Circle $circle |
||
| 171 | * @param Member $member |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function onCircleRequestInvitation(Circle $circle, Member $member) { |
|
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * onCircleNewMember() |
||
| 196 | * |
||
| 197 | * Called when a member is added to a circle. |
||
| 198 | * Broadcast an activity to the new member and to the moderators of the circle. |
||
| 199 | * We won't do anything if the circle is PERSONAL |
||
| 200 | * |
||
| 201 | * @param Circle $circle |
||
| 202 | * @param Member $member |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function onCircleNewMember(Circle $circle, Member $member) { |
|
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * onCircleMemberLeaving() |
||
| 224 | * |
||
| 225 | * Called when a member is removed from a circle. |
||
| 226 | * Broadcast an activity to the new member and to the moderators of the circle. |
||
| 227 | * We won't do anything if the circle is PERSONAL |
||
| 228 | * |
||
| 229 | * @param Circle $circle |
||
| 230 | * @param Member $member |
||
| 231 | */ |
||
| 232 | View Code Duplication | public function onCircleMemberLeaving(Circle $circle, Member $member) { |
|
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * generateEvent() |
||
| 252 | * Create an Activity Event with the basic settings for the app. |
||
| 253 | * |
||
| 254 | * @param $type |
||
| 255 | * |
||
| 256 | * @return \OCP\Activity\IEvent |
||
| 257 | */ |
||
| 258 | private function generateEvent($type) { |
||
| 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.