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 |
||
71 | class SyncService { |
||
72 | |||
73 | |||
74 | use TStringTools; |
||
75 | |||
76 | |||
77 | /** @var IUserManager */ |
||
78 | private $userManager; |
||
79 | |||
80 | /** @var IGroupManager */ |
||
81 | private $groupManager; |
||
82 | |||
83 | /** @var CircleRequest */ |
||
84 | private $circleRequest; |
||
85 | |||
86 | /** @var MemberRequest */ |
||
87 | private $memberRequest; |
||
88 | |||
89 | /** @var FederatedUserService */ |
||
90 | private $federatedUserService; |
||
91 | |||
92 | /** @var federatedEventService */ |
||
93 | private $federatedEventService; |
||
94 | |||
95 | /** @var MemberService */ |
||
96 | private $memberService; |
||
97 | |||
98 | /** @var MembershipService */ |
||
99 | private $membershipService; |
||
100 | |||
101 | /** @var ConfigService */ |
||
102 | private $configService; |
||
103 | |||
104 | |||
105 | /** |
||
106 | * SyncService constructor. |
||
107 | * |
||
108 | * @param IUserManager $userManager |
||
109 | * @param IGroupManager $groupManager |
||
110 | * @param CircleRequest $circleRequest |
||
111 | * @param MemberRequest $memberRequest |
||
112 | * @param FederatedUserService $federatedUserService |
||
113 | * @param federatedEventService $federatedEventService |
||
114 | * @param MemberService $memberService |
||
115 | * @param MembershipService $membershipService |
||
116 | * @param ConfigService $configService |
||
117 | */ |
||
118 | View Code Duplication | public function __construct( |
|
139 | |||
140 | |||
141 | /** |
||
142 | * @return bool |
||
143 | * @throws MigrationTo22Exception |
||
144 | */ |
||
145 | public function migration(): bool { |
||
156 | |||
157 | /** |
||
158 | * @return void |
||
159 | * @throws MigrationTo22Exception |
||
160 | */ |
||
161 | private function migrationTo22(): void { |
||
164 | |||
165 | |||
166 | /** |
||
167 | * @return void |
||
168 | */ |
||
169 | public function syncAll(): void { |
||
177 | |||
178 | |||
179 | /** |
||
180 | * @throws FederatedUserException |
||
181 | * @throws InvalidIdException |
||
182 | * @throws RequestBuilderException |
||
183 | * @throws SingleCircleNotFoundException |
||
184 | */ |
||
185 | public function syncOcc(): void { |
||
188 | |||
189 | |||
190 | /** |
||
191 | * @return void |
||
192 | */ |
||
193 | public function syncNextcloudUsers(): void { |
||
201 | |||
202 | /** |
||
203 | * @param string $userId |
||
204 | * |
||
205 | * @return FederatedUser |
||
206 | * @throws FederatedUserException |
||
207 | * @throws FederatedUserNotFoundException |
||
208 | * @throws InvalidIdException |
||
209 | * @throws SingleCircleNotFoundException |
||
210 | * @throws RequestBuilderException |
||
211 | */ |
||
212 | public function syncNextcloudUser(string $userId): FederatedUser { |
||
215 | |||
216 | |||
217 | /** |
||
218 | * @return void |
||
219 | */ |
||
220 | public function syncNextcloudGroups(): void { |
||
228 | |||
229 | /** |
||
230 | * @param string $groupId |
||
231 | * |
||
232 | * @return Circle |
||
233 | * @throws FederatedUserException |
||
234 | * @throws FederatedUserNotFoundException |
||
235 | * @throws GroupNotFoundException |
||
236 | * @throws InvalidIdException |
||
237 | * @throws SingleCircleNotFoundException |
||
238 | * @throws FederatedEventException |
||
239 | * @throws FederatedItemException |
||
240 | * @throws InitiatorNotConfirmedException |
||
241 | * @throws OwnerNotFoundException |
||
242 | * @throws RemoteInstanceException |
||
243 | * @throws RemoteNotFoundException |
||
244 | * @throws RemoteResourceNotFoundException |
||
245 | * @throws UnknownRemoteException |
||
246 | * @throws RequestBuilderException |
||
247 | */ |
||
248 | public function syncNextcloudGroup(string $groupId): Circle { |
||
265 | |||
266 | |||
267 | /** |
||
268 | * @param string $userId |
||
269 | * |
||
270 | * @throws FederatedUserException |
||
271 | * @throws FederatedUserNotFoundException |
||
272 | * @throws InvalidIdException |
||
273 | * @throws RequestBuilderException |
||
274 | * @throws SingleCircleNotFoundException |
||
275 | */ |
||
276 | View Code Duplication | public function userDeleted(string $userId): void { |
|
284 | |||
285 | |||
286 | /** |
||
287 | * @param string $groupId |
||
288 | * |
||
289 | * @throws FederatedUserException |
||
290 | * @throws InvalidIdException |
||
291 | * @throws RequestBuilderException |
||
292 | * @throws SingleCircleNotFoundException |
||
293 | */ |
||
294 | public function groupDeleted(string $groupId): void { |
||
319 | |||
320 | |||
321 | /** |
||
322 | * @param Circle $circle |
||
323 | * @param string $userId |
||
324 | * |
||
325 | * @return Member |
||
326 | * @throws FederatedUserException |
||
327 | * @throws FederatedUserNotFoundException |
||
328 | * @throws InvalidIdException |
||
329 | * @throws RequestBuilderException |
||
330 | * @throws SingleCircleNotFoundException |
||
331 | */ |
||
332 | private function generateGroupMember(Circle $circle, string $userId): Member { |
||
344 | |||
345 | |||
346 | /** |
||
347 | * @param string $groupId |
||
348 | * @param string $userId |
||
349 | * |
||
350 | * @return Member |
||
351 | * @throws FederatedEventException |
||
352 | * @throws FederatedItemException |
||
353 | * @throws FederatedUserException |
||
354 | * @throws FederatedUserNotFoundException |
||
355 | * @throws GroupNotFoundException |
||
356 | * @throws InitiatorNotConfirmedException |
||
357 | * @throws InvalidIdException |
||
358 | * @throws OwnerNotFoundException |
||
359 | * @throws RemoteInstanceException |
||
360 | * @throws RemoteNotFoundException |
||
361 | * @throws RemoteResourceNotFoundException |
||
362 | * @throws RequestBuilderException |
||
363 | * @throws SingleCircleNotFoundException |
||
364 | * @throws UnknownRemoteException |
||
365 | */ |
||
366 | public function groupMemberAdded(string $groupId, string $userId): void { |
||
379 | |||
380 | |||
381 | /** |
||
382 | * @param string $groupId |
||
383 | * @param string $userId |
||
384 | * |
||
385 | * @throws FederatedEventException |
||
386 | * @throws FederatedItemException |
||
387 | * @throws FederatedUserException |
||
388 | * @throws FederatedUserNotFoundException |
||
389 | * @throws GroupNotFoundException |
||
390 | * @throws InitiatorNotConfirmedException |
||
391 | * @throws InvalidIdException |
||
392 | * @throws OwnerNotFoundException |
||
393 | * @throws RemoteInstanceException |
||
394 | * @throws RemoteNotFoundException |
||
395 | * @throws RemoteResourceNotFoundException |
||
396 | * @throws RequestBuilderException |
||
397 | * @throws SingleCircleNotFoundException |
||
398 | * @throws UnknownRemoteException |
||
399 | */ |
||
400 | View Code Duplication | public function groupMemberRemoved(string $groupId, string $userId): void { |
|
407 | |||
408 | |||
409 | /** |
||
410 | * @return void |
||
411 | */ |
||
412 | public function syncContacts(): void { |
||
414 | |||
415 | |||
416 | /** |
||
417 | * @return void |
||
418 | */ |
||
419 | public function syncGlobalScale(): void { |
||
421 | |||
422 | |||
423 | /** |
||
424 | * @return void |
||
425 | */ |
||
426 | public function syncRemote(): void { |
||
428 | |||
429 | |||
430 | /** |
||
431 | * @param string $circleId |
||
432 | * |
||
433 | * @return void |
||
434 | */ |
||
435 | public function syncRemoteCircle(string $circleId): void { |
||
437 | |||
438 | |||
439 | } |
||
440 | |||
441 |
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.