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:
Complex classes like LocalController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LocalController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
63 | class LocalController extends OcsController { |
||
64 | |||
65 | |||
66 | use TNC22Deserialize; |
||
67 | use TNC22Logger; |
||
68 | |||
69 | |||
70 | /** @var IUserSession */ |
||
71 | private $userSession; |
||
72 | |||
73 | /** @var FederatedUserService */ |
||
74 | private $federatedUserService; |
||
75 | |||
76 | /** @var CircleService */ |
||
77 | private $circleService; |
||
78 | |||
79 | /** @var MemberService */ |
||
80 | private $memberService; |
||
81 | |||
82 | /** @var MembershipService */ |
||
83 | private $membershipService; |
||
84 | |||
85 | /** @var SearchService */ |
||
86 | private $searchService; |
||
87 | |||
88 | /** @var ConfigService */ |
||
89 | protected $configService; |
||
90 | |||
91 | |||
92 | /** |
||
93 | * LocalController constructor. |
||
94 | * |
||
95 | * @param string $appName |
||
96 | * @param IRequest $request |
||
97 | * @param IUserSession $userSession |
||
98 | * @param FederatedUserService $federatedUserService |
||
99 | * @param CircleService $circleService |
||
100 | * @param MemberService $memberService |
||
101 | * @param MembershipService $membershipService |
||
102 | * @param SearchService $searchService |
||
103 | * @param ConfigService $configService |
||
104 | */ |
||
105 | public function __construct( |
||
127 | |||
128 | |||
129 | /** |
||
130 | * @NoAdminRequired |
||
131 | * |
||
132 | * @param string $name |
||
133 | * @param bool $personal |
||
134 | * @param bool $local |
||
135 | * |
||
136 | * @return DataResponse |
||
137 | * @throws OCSException |
||
138 | */ |
||
139 | public function create(string $name, bool $personal = false, bool $local = false): DataResponse { |
||
149 | |||
150 | |||
151 | /** |
||
152 | * @NoAdminRequired |
||
153 | * |
||
154 | * @param string $circleId |
||
155 | * |
||
156 | * @return DataResponse |
||
157 | * @throws OCSException |
||
158 | */ |
||
159 | View Code Duplication | public function destroy(string $circleId): DataResponse { |
|
169 | |||
170 | |||
171 | /** |
||
172 | * @NoAdminRequired |
||
173 | * |
||
174 | * @param string $term |
||
175 | * |
||
176 | * @return DataResponse |
||
177 | * @throws OCSException |
||
178 | */ |
||
179 | View Code Duplication | public function search(string $term): DataResponse { |
|
188 | |||
189 | |||
190 | /** |
||
191 | * @NoAdminRequired |
||
192 | * |
||
193 | * @param string $circleId |
||
194 | * |
||
195 | * @return DataResponse |
||
196 | * @throws OCSException |
||
197 | */ |
||
198 | View Code Duplication | public function circleDetails(string $circleId): DataResponse { |
|
207 | |||
208 | |||
209 | /** |
||
210 | * @NoAdminRequired |
||
211 | * |
||
212 | * @param string $circleId |
||
213 | * @param string $userId |
||
214 | * @param int $type |
||
215 | * |
||
216 | * @return DataResponse |
||
217 | * @throws OCSException |
||
218 | */ |
||
219 | public function memberAdd(string $circleId, string $userId, int $type): DataResponse { |
||
241 | |||
242 | |||
243 | /** |
||
244 | * @NoAdminRequired |
||
245 | * |
||
246 | * @param string $circleId |
||
247 | * @param array $members |
||
248 | * |
||
249 | * @return DataResponse |
||
250 | * @throws OCSException |
||
251 | */ |
||
252 | public function membersAdd(string $circleId, array $members): DataResponse { |
||
273 | |||
274 | |||
275 | /** |
||
276 | * @NoAdminRequired |
||
277 | * |
||
278 | * @param string $circleId |
||
279 | * |
||
280 | * @return DataResponse |
||
281 | * @throws OCSException |
||
282 | */ |
||
283 | View Code Duplication | public function circleJoin(string $circleId): DataResponse { |
|
293 | |||
294 | |||
295 | /** |
||
296 | * @NoAdminRequired |
||
297 | * |
||
298 | * @param string $circleId |
||
299 | * |
||
300 | * @return DataResponse |
||
301 | * @throws OCSException |
||
302 | */ |
||
303 | View Code Duplication | public function circleLeave(string $circleId): DataResponse { |
|
313 | |||
314 | |||
315 | /** |
||
316 | * @NoAdminRequired |
||
317 | * |
||
318 | * @param string $circleId |
||
319 | * @param string $memberId |
||
320 | * @param string|int $level |
||
321 | * |
||
322 | * @return DataResponse |
||
323 | * @throws OCSException |
||
324 | */ |
||
325 | public function memberLevel(string $circleId, string $memberId, $level): DataResponse { |
||
342 | |||
343 | |||
344 | /** |
||
345 | * @NoAdminRequired |
||
346 | * |
||
347 | * @param string $circleId |
||
348 | * @param string $memberId |
||
349 | * |
||
350 | * @return DataResponse |
||
351 | * @throws OCSException |
||
352 | */ |
||
353 | public function memberConfirm(string $circleId, string $memberId): DataResponse { |
||
368 | |||
369 | |||
370 | /** |
||
371 | * @NoAdminRequired |
||
372 | * |
||
373 | * @param string $circleId |
||
374 | * @param string $memberId |
||
375 | * |
||
376 | * @return DataResponse |
||
377 | * @throws OCSException |
||
378 | */ |
||
379 | View Code Duplication | public function memberRemove(string $circleId, string $memberId): DataResponse { |
|
391 | |||
392 | |||
393 | /** |
||
394 | * @NoAdminRequired |
||
395 | * |
||
396 | * @return DataResponse |
||
397 | * @throws OCSException |
||
398 | */ |
||
399 | public function circles(): DataResponse { |
||
408 | |||
409 | |||
410 | /** |
||
411 | * @NoAdminRequired |
||
412 | * |
||
413 | * @param string $circleId |
||
414 | * |
||
415 | * @return DataResponse |
||
416 | * @throws OCSException |
||
417 | */ |
||
418 | View Code Duplication | public function members(string $circleId): DataResponse { |
|
427 | |||
428 | |||
429 | /** |
||
430 | * @NoAdminRequired |
||
431 | * |
||
432 | * @param string $circleId |
||
433 | * @param string $value |
||
434 | * |
||
435 | * @return DataResponse |
||
436 | * @throws OCSException |
||
437 | */ |
||
438 | public function editName(string $circleId, string $value): DataResponse { |
||
449 | |||
450 | |||
451 | /** |
||
452 | * @NoAdminRequired |
||
453 | * |
||
454 | * @param string $circleId |
||
455 | * @param string $value |
||
456 | * |
||
457 | * @return DataResponse |
||
458 | * @throws OCSException |
||
459 | */ |
||
460 | View Code Duplication | public function editDescription(string $circleId, string $value): DataResponse { |
|
471 | |||
472 | |||
473 | /** |
||
474 | * @NoAdminRequired |
||
475 | * |
||
476 | * @param string $circleId |
||
477 | * @param array $value |
||
478 | * |
||
479 | * @return DataResponse |
||
480 | * @throws OCSException |
||
481 | */ |
||
482 | View Code Duplication | public function editSettings(string $circleId, array $value): DataResponse { |
|
493 | |||
494 | |||
495 | /** |
||
496 | * @NoAdminRequired |
||
497 | * |
||
498 | * @param string $circleId |
||
499 | * @param int $value |
||
500 | * |
||
501 | * @return DataResponse |
||
502 | * @throws OCSException |
||
503 | */ |
||
504 | View Code Duplication | public function editConfig(string $circleId, int $value): DataResponse { |
|
515 | |||
516 | |||
517 | /** |
||
518 | * @NoAdminRequired |
||
519 | * |
||
520 | * @param string $circleId |
||
521 | * @param string $singleId |
||
522 | * |
||
523 | * @return DataResponse |
||
524 | * @throws OCSException |
||
525 | */ |
||
526 | public function link(string $circleId, string $singleId): DataResponse { |
||
536 | |||
537 | |||
538 | /** |
||
539 | * @throws FederatedUserNotFoundException |
||
540 | * @throws InvalidIdException |
||
541 | * @throws FederatedUserException |
||
542 | * @throws SingleCircleNotFoundException |
||
543 | * @throws RequestBuilderException |
||
544 | */ |
||
545 | private function setCurrentFederatedUser() { |
||
549 | |||
550 | } |
||
551 | |||
552 |
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.