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 |
||
66 | class AdminController extends OcsController { |
||
67 | |||
68 | |||
69 | use TNC22Deserialize; |
||
70 | use TNC22Logger; |
||
71 | |||
72 | |||
73 | /** @var IUserSession */ |
||
74 | private $userSession; |
||
75 | |||
76 | /** @var FederatedUserService */ |
||
77 | private $federatedUserService; |
||
78 | |||
79 | /** @var CircleService */ |
||
80 | private $circleService; |
||
81 | |||
82 | /** @var MemberService */ |
||
83 | private $memberService; |
||
84 | |||
85 | /** @var MembershipService */ |
||
86 | private $membershipService; |
||
87 | |||
88 | /** @var SearchService */ |
||
89 | private $searchService; |
||
90 | |||
91 | /** @var ConfigService */ |
||
92 | protected $configService; |
||
93 | |||
94 | |||
95 | /** |
||
96 | * LocalController constructor. |
||
97 | * |
||
98 | * @param string $appName |
||
99 | * @param IRequest $request |
||
100 | * @param IUserSession $userSession |
||
101 | * @param FederatedUserService $federatedUserService |
||
102 | * @param CircleService $circleService |
||
103 | * @param MemberService $memberService |
||
104 | * @param MembershipService $membershipService |
||
105 | * @param SearchService $searchService |
||
106 | * @param ConfigService $configService |
||
107 | */ |
||
108 | View Code Duplication | public function __construct( |
|
130 | |||
131 | |||
132 | /** |
||
133 | * @param string $emulated |
||
134 | * @param string $name |
||
135 | * @param bool $personal |
||
136 | * @param bool $local |
||
137 | * |
||
138 | * @return DataResponse |
||
139 | * @throws OCSException |
||
140 | */ |
||
141 | View Code Duplication | public function create( |
|
156 | |||
157 | |||
158 | /** |
||
159 | * @param string $emulated |
||
160 | * @param string $circleId |
||
161 | * |
||
162 | * @return DataResponse |
||
163 | * @throws OCSException |
||
164 | */ |
||
165 | View Code Duplication | public function destroy(string $emulated, string $circleId): DataResponse { |
|
175 | |||
176 | |||
177 | /** |
||
178 | * @param string $emulated |
||
179 | * @param string $circleId |
||
180 | * @param string $userId |
||
181 | * @param int $type |
||
182 | * |
||
183 | * @return DataResponse |
||
184 | * @throws OCSException |
||
185 | */ |
||
186 | View Code Duplication | public function memberAdd(string $emulated, string $circleId, string $userId, int $type): DataResponse { |
|
208 | |||
209 | |||
210 | /** |
||
211 | * @param string $emulated |
||
212 | * @param string $circleId |
||
213 | * @param string $memberId |
||
214 | * @param string|int $level |
||
215 | * |
||
216 | * @return DataResponse |
||
217 | * @throws OCSException |
||
218 | */ |
||
219 | View Code Duplication | public function memberLevel(string $emulated, string $circleId, string $memberId, $level): DataResponse { |
|
236 | |||
237 | |||
238 | /** |
||
239 | * @param string $emulated |
||
240 | * |
||
241 | * @return DataResponse |
||
242 | * @throws OCSException |
||
243 | */ |
||
244 | View Code Duplication | public function circles(string $emulated): DataResponse { |
|
253 | |||
254 | |||
255 | /** |
||
256 | * @param string $emulated |
||
257 | * @param string $circleId |
||
258 | * |
||
259 | * @return DataResponse |
||
260 | * @throws OCSException |
||
261 | */ |
||
262 | View Code Duplication | public function circleDetails(string $emulated, string $circleId): DataResponse { |
|
271 | |||
272 | |||
273 | /** |
||
274 | * @param string $emulated |
||
275 | * @param string $circleId |
||
276 | * |
||
277 | * @return DataResponse |
||
278 | * @throws OCSException |
||
279 | */ |
||
280 | View Code Duplication | public function circleJoin(string $emulated, string $circleId): DataResponse { |
|
290 | |||
291 | |||
292 | /** |
||
293 | * @param string $emulated |
||
294 | * @param string $circleId |
||
295 | * |
||
296 | * @return DataResponse |
||
297 | * @throws OCSException |
||
298 | */ |
||
299 | View Code Duplication | public function circleLeave(string $emulated, string $circleId): DataResponse { |
|
309 | |||
310 | |||
311 | /** |
||
312 | * @param string $emulated |
||
313 | * @param string $circleId |
||
314 | * @param string $memberId |
||
315 | * |
||
316 | * @return DataResponse |
||
317 | * @throws OCSException |
||
318 | */ |
||
319 | View Code Duplication | public function memberConfirm(string $emulated, string $circleId, string $memberId): DataResponse { |
|
334 | |||
335 | |||
336 | /** |
||
337 | * @param string $emulated |
||
338 | * @param string $circleId |
||
339 | * @param string $memberId |
||
340 | * |
||
341 | * @return DataResponse |
||
342 | * @throws OCSException |
||
343 | */ |
||
344 | View Code Duplication | public function memberRemove(string $emulated, string $circleId, string $memberId): DataResponse { |
|
356 | |||
357 | |||
358 | /** |
||
359 | * @param string $emulated |
||
360 | * @param string $circleId |
||
361 | * |
||
362 | * @return DataResponse |
||
363 | * @throws OCSException |
||
364 | */ |
||
365 | View Code Duplication | public function members(string $emulated, string $circleId): DataResponse { |
|
374 | |||
375 | |||
376 | /** |
||
377 | * @param string $emulated |
||
378 | * @param string $circleId |
||
379 | * @param string $value |
||
380 | * |
||
381 | * @return DataResponse |
||
382 | * @throws OCSException |
||
383 | */ |
||
384 | View Code Duplication | public function editName(string $emulated, string $circleId, string $value): DataResponse { |
|
395 | |||
396 | |||
397 | /** |
||
398 | * @param string $emulated |
||
399 | * @param string $circleId |
||
400 | * @param string $value |
||
401 | * |
||
402 | * @return DataResponse |
||
403 | * @throws OCSException |
||
404 | */ |
||
405 | View Code Duplication | public function editDescription(string $emulated, string $circleId, string $value): DataResponse { |
|
416 | |||
417 | |||
418 | /** |
||
419 | * @param string $emulated |
||
420 | * @param string $circleId |
||
421 | * @param array $value |
||
422 | * |
||
423 | * @return DataResponse |
||
424 | * @throws OCSException |
||
425 | */ |
||
426 | View Code Duplication | public function editSettings(string $emulated, string $circleId, array $value): DataResponse { |
|
437 | |||
438 | |||
439 | /** |
||
440 | * @param string $emulated |
||
441 | * @param string $circleId |
||
442 | * @param int $value |
||
443 | * |
||
444 | * @return DataResponse |
||
445 | * @throws OCSException |
||
446 | */ |
||
447 | View Code Duplication | public function editConfig(string $emulated, string $circleId, int $value): DataResponse { |
|
458 | |||
459 | |||
460 | /** |
||
461 | * @param string $emulated |
||
462 | * @param string $circleId |
||
463 | * @param string $singleId |
||
464 | * |
||
465 | * @return DataResponse |
||
466 | * @throws OCSException |
||
467 | */ |
||
468 | View Code Duplication | public function link(string $emulated, string $circleId, string $singleId): DataResponse { |
|
478 | |||
479 | |||
480 | /** |
||
481 | * @param string $emulated |
||
482 | * |
||
483 | * @throws FederatedUserException |
||
484 | * @throws FederatedUserNotFoundException |
||
485 | * @throws InvalidIdException |
||
486 | * @throws RequestBuilderException |
||
487 | * @throws SingleCircleNotFoundException |
||
488 | * @throws ContactAddressBookNotFoundException |
||
489 | * @throws ContactFormatException |
||
490 | * @throws ContactNotFoundException |
||
491 | */ |
||
492 | private function setLocalFederatedUser(string $emulated): void { |
||
497 | |||
498 | } |
||
499 | |||
500 |
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.