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 |
||
74 | class SyncService { |
||
75 | |||
76 | |||
77 | use TStringTools; |
||
78 | use TNC22Logger; |
||
79 | |||
80 | |||
81 | const SYNC_APPS = 1; |
||
82 | const SYNC_USERS = 2; |
||
83 | const SYNC_GROUPS = 4; |
||
84 | const SYNC_GLOBALSCALE = 8; |
||
85 | const SYNC_REMOTES = 16; |
||
86 | const SYNC_CONTACTS = 32; |
||
87 | const SYNC_ALL = 63; |
||
88 | |||
89 | |||
90 | /** @var IUserManager */ |
||
91 | private $userManager; |
||
92 | |||
93 | /** @var IGroupManager */ |
||
94 | private $groupManager; |
||
95 | |||
96 | |||
97 | /** @var CircleRequest */ |
||
98 | private $circleRequest; |
||
99 | |||
100 | /** @var MemberRequest */ |
||
101 | private $memberRequest; |
||
102 | |||
103 | /** @var FederatedUserService */ |
||
104 | private $federatedUserService; |
||
105 | |||
106 | /** @var federatedEventService */ |
||
107 | private $federatedEventService; |
||
108 | |||
109 | /** @var CircleService */ |
||
110 | private $circleService; |
||
111 | |||
112 | /** @var MembershipService */ |
||
113 | private $membershipService; |
||
114 | |||
115 | /** @var OutputService */ |
||
116 | private $outputService; |
||
117 | |||
118 | /** @var ConfigService */ |
||
119 | private $configService; |
||
120 | |||
121 | |||
122 | /** |
||
123 | * SyncService constructor. |
||
124 | * |
||
125 | * @param IUserManager $userManager |
||
126 | * @param IGroupManager $groupManager |
||
127 | * @param CircleRequest $circleRequest |
||
128 | * @param MemberRequest $memberRequest |
||
129 | * @param FederatedUserService $federatedUserService |
||
130 | * @param federatedEventService $federatedEventService |
||
131 | * @param CircleService $circleService |
||
132 | * @param MembershipService $membershipService |
||
133 | * @param OutputService $outputService |
||
134 | * @param ConfigService $configService |
||
135 | */ |
||
136 | public function __construct( |
||
161 | |||
162 | |||
163 | /** |
||
164 | * @param int $sync |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | public function sync(int $sync = self::SYNC_ALL): void { |
||
193 | |||
194 | |||
195 | /** |
||
196 | * @param int $item |
||
197 | * @param int $all |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | private function shouldSync(int $item, int $all): bool { |
||
204 | |||
205 | |||
206 | /** |
||
207 | */ |
||
208 | public function syncApps(): void { |
||
218 | |||
219 | |||
220 | /** |
||
221 | * @return void |
||
222 | */ |
||
223 | View Code Duplication | public function syncNextcloudUsers(): void { |
|
238 | |||
239 | /** |
||
240 | * @param string $userId |
||
241 | * |
||
242 | * @return FederatedUser |
||
243 | * @throws ContactAddressBookNotFoundException |
||
244 | * @throws ContactFormatException |
||
245 | * @throws ContactNotFoundException |
||
246 | * @throws FederatedUserException |
||
247 | * @throws FederatedUserNotFoundException |
||
248 | * @throws InvalidIdException |
||
249 | * @throws RequestBuilderException |
||
250 | * @throws SingleCircleNotFoundException |
||
251 | */ |
||
252 | public function syncNextcloudUser(string $userId): FederatedUser { |
||
257 | |||
258 | |||
259 | /** |
||
260 | * @return void |
||
261 | */ |
||
262 | View Code Duplication | public function syncNextcloudGroups(): void { |
|
276 | |||
277 | /** |
||
278 | * @param string $groupId |
||
279 | * |
||
280 | * @return Circle |
||
281 | * @throws FederatedUserException |
||
282 | * @throws FederatedUserNotFoundException |
||
283 | * @throws GroupNotFoundException |
||
284 | * @throws InvalidIdException |
||
285 | * @throws SingleCircleNotFoundException |
||
286 | * @throws FederatedEventException |
||
287 | * @throws FederatedItemException |
||
288 | * @throws InitiatorNotConfirmedException |
||
289 | * @throws OwnerNotFoundException |
||
290 | * @throws RemoteInstanceException |
||
291 | * @throws RemoteNotFoundException |
||
292 | * @throws RemoteResourceNotFoundException |
||
293 | * @throws UnknownRemoteException |
||
294 | * @throws RequestBuilderException |
||
295 | */ |
||
296 | public function syncNextcloudGroup(string $groupId): Circle { |
||
315 | |||
316 | |||
317 | /** |
||
318 | * @param string $userId |
||
319 | * |
||
320 | * @throws ContactAddressBookNotFoundException |
||
321 | * @throws ContactFormatException |
||
322 | * @throws ContactNotFoundException |
||
323 | * @throws FederatedUserException |
||
324 | * @throws FederatedUserNotFoundException |
||
325 | * @throws InvalidIdException |
||
326 | * @throws RequestBuilderException |
||
327 | */ |
||
328 | public function userDeleted(string $userId): void { |
||
351 | |||
352 | |||
353 | /** |
||
354 | * @param string $groupId |
||
355 | * |
||
356 | * @throws ContactAddressBookNotFoundException |
||
357 | * @throws ContactFormatException |
||
358 | * @throws ContactNotFoundException |
||
359 | * @throws FederatedUserException |
||
360 | * @throws InvalidIdException |
||
361 | * @throws RequestBuilderException |
||
362 | * @throws SingleCircleNotFoundException |
||
363 | */ |
||
364 | public function groupDeleted(string $groupId): void { |
||
393 | |||
394 | |||
395 | /** |
||
396 | * @param Circle $circle |
||
397 | * @param string $userId |
||
398 | * |
||
399 | * @return Member |
||
400 | * @throws ContactAddressBookNotFoundException |
||
401 | * @throws ContactFormatException |
||
402 | * @throws ContactNotFoundException |
||
403 | * @throws FederatedUserException |
||
404 | * @throws FederatedUserNotFoundException |
||
405 | * @throws InvalidIdException |
||
406 | * @throws RequestBuilderException |
||
407 | * @throws SingleCircleNotFoundException |
||
408 | */ |
||
409 | private function generateGroupMember(Circle $circle, string $userId): Member { |
||
422 | |||
423 | |||
424 | /** |
||
425 | * @param string $groupId |
||
426 | * @param string $userId |
||
427 | * |
||
428 | * @return void |
||
429 | * @throws ContactAddressBookNotFoundException |
||
430 | * @throws ContactFormatException |
||
431 | * @throws ContactNotFoundException |
||
432 | * @throws FederatedEventException |
||
433 | * @throws FederatedItemException |
||
434 | * @throws FederatedUserException |
||
435 | * @throws FederatedUserNotFoundException |
||
436 | * @throws GroupNotFoundException |
||
437 | * @throws InitiatorNotConfirmedException |
||
438 | * @throws InvalidIdException |
||
439 | * @throws OwnerNotFoundException |
||
440 | * @throws RemoteInstanceException |
||
441 | * @throws RemoteNotFoundException |
||
442 | * @throws RemoteResourceNotFoundException |
||
443 | * @throws RequestBuilderException |
||
444 | * @throws SingleCircleNotFoundException |
||
445 | * @throws UnknownRemoteException |
||
446 | */ |
||
447 | public function groupMemberAdded(string $groupId, string $userId): void { |
||
460 | |||
461 | |||
462 | /** |
||
463 | * @param string $groupId |
||
464 | * @param string $userId |
||
465 | * |
||
466 | * @throws FederatedEventException |
||
467 | * @throws FederatedItemException |
||
468 | * @throws FederatedUserException |
||
469 | * @throws FederatedUserNotFoundException |
||
470 | * @throws GroupNotFoundException |
||
471 | * @throws InitiatorNotConfirmedException |
||
472 | * @throws InvalidIdException |
||
473 | * @throws OwnerNotFoundException |
||
474 | * @throws RemoteInstanceException |
||
475 | * @throws RemoteNotFoundException |
||
476 | * @throws RemoteResourceNotFoundException |
||
477 | * @throws RequestBuilderException |
||
478 | * @throws SingleCircleNotFoundException |
||
479 | * @throws UnknownRemoteException |
||
480 | */ |
||
481 | public function groupMemberRemoved(string $groupId, string $userId): void { |
||
488 | |||
489 | |||
490 | /** |
||
491 | * @return void |
||
492 | */ |
||
493 | public function syncContacts(): void { |
||
496 | |||
497 | |||
498 | /** |
||
499 | * @return void |
||
500 | */ |
||
501 | public function syncGlobalScale(): void { |
||
504 | |||
505 | |||
506 | /** |
||
507 | * @return void |
||
508 | */ |
||
509 | public function syncRemote(): void { |
||
512 | |||
513 | |||
514 | /** |
||
515 | * @param string $circleId |
||
516 | * |
||
517 | * @return void |
||
518 | */ |
||
519 | public function syncRemoteCircle(string $circleId): void { |
||
521 | |||
522 | } |
||
523 | |||
524 |
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.