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 BoardService 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 BoardService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class BoardService { |
||
| 49 | |||
| 50 | private $boardMapper; |
||
| 51 | private $stackMapper; |
||
| 52 | private $labelMapper; |
||
| 53 | private $aclMapper; |
||
| 54 | private $l10n; |
||
| 55 | private $permissionService; |
||
| 56 | private $notificationHelper; |
||
| 57 | private $assignedUsersMapper; |
||
| 58 | private $userManager; |
||
| 59 | private $groupManager; |
||
| 60 | private $userId; |
||
| 61 | private $activityManager; |
||
| 62 | private $changeHelper; |
||
| 63 | |||
| 64 | View Code Duplication | public function __construct( |
|
|
|
|||
| 65 | BoardMapper $boardMapper, |
||
| 66 | StackMapper $stackMapper, |
||
| 67 | IL10N $l10n, |
||
| 68 | LabelMapper $labelMapper, |
||
| 69 | AclMapper $aclMapper, |
||
| 70 | PermissionService $permissionService, |
||
| 71 | NotificationHelper $notificationHelper, |
||
| 72 | AssignedUsersMapper $assignedUsersMapper, |
||
| 73 | IUserManager $userManager, |
||
| 74 | IGroupManager $groupManager, |
||
| 75 | ActivityManager $activityManager, |
||
| 76 | ChangeHelper $changeHelper, |
||
| 77 | $userId |
||
| 78 | ) { |
||
| 79 | $this->boardMapper = $boardMapper; |
||
| 80 | $this->stackMapper = $stackMapper; |
||
| 81 | $this->labelMapper = $labelMapper; |
||
| 82 | $this->aclMapper = $aclMapper; |
||
| 83 | $this->l10n = $l10n; |
||
| 84 | $this->permissionService = $permissionService; |
||
| 85 | $this->notificationHelper = $notificationHelper; |
||
| 86 | $this->assignedUsersMapper = $assignedUsersMapper; |
||
| 87 | $this->userManager = $userManager; |
||
| 88 | $this->groupManager = $groupManager; |
||
| 89 | $this->activityManager = $activityManager; |
||
| 90 | $this->changeHelper = $changeHelper; |
||
| 91 | $this->userId = $userId; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | public function findAll($since = 0) { |
||
| 98 | $userInfo = $this->getBoardPrerequisites(); |
||
| 99 | $userBoards = $this->boardMapper->findAllByUser($userInfo['user'], null, null, $since); |
||
| 100 | $groupBoards = $this->boardMapper->findAllByGroups($userInfo['user'], $userInfo['groups'],null, null, $since); |
||
| 101 | $complete = array_merge($userBoards, $groupBoards); |
||
| 102 | $result = []; |
||
| 103 | /** @var Board $item */ |
||
| 104 | foreach ($complete as &$item) { |
||
| 105 | if (!array_key_exists($item->getId(), $result)) { |
||
| 106 | $this->boardMapper->mapOwner($item); |
||
| 107 | if ($item->getAcl() !== null) { |
||
| 108 | foreach ($item->getAcl() as &$acl) { |
||
| 109 | $this->boardMapper->mapAcl($acl); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $this->enrichWithStacks($item); |
||
| 113 | $permissions = $this->permissionService->matchPermissions($item); |
||
| 114 | $item->setPermissions([ |
||
| 115 | 'PERMISSION_READ' => $permissions[Acl::PERMISSION_READ], |
||
| 116 | 'PERMISSION_EDIT' => $permissions[Acl::PERMISSION_EDIT], |
||
| 117 | 'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE], |
||
| 118 | 'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE] |
||
| 119 | ]); |
||
| 120 | $result[$item->getId()] = $item; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | return array_values($result); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param $boardId |
||
| 128 | * @return Board |
||
| 129 | * @throws DoesNotExistException |
||
| 130 | * @throws \OCA\Deck\NoPermissionException |
||
| 131 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 132 | * @throws BadRequestException |
||
| 133 | */ |
||
| 134 | public function find($boardId) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | private function getBoardPrerequisites() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param $mapper |
||
| 176 | * @param $id |
||
| 177 | * @return bool |
||
| 178 | * @throws DoesNotExistException |
||
| 179 | * @throws \OCA\Deck\NoPermissionException |
||
| 180 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 181 | * @throws BadRequestException |
||
| 182 | */ |
||
| 183 | public function isArchived($mapper, $id) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param $mapper |
||
| 206 | * @param $id |
||
| 207 | * @return bool |
||
| 208 | * @throws DoesNotExistException |
||
| 209 | * @throws \OCA\Deck\NoPermissionException |
||
| 210 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 211 | * @throws BadRequestException |
||
| 212 | */ |
||
| 213 | public function isDeleted($mapper, $id) { |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * @param $title |
||
| 241 | * @param $userId |
||
| 242 | * @param $color |
||
| 243 | * @return \OCP\AppFramework\Db\Entity |
||
| 244 | * @throws BadRequestException |
||
| 245 | */ |
||
| 246 | public function create($title, $userId, $color) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param $id |
||
| 302 | * @return Board |
||
| 303 | * @throws DoesNotExistException |
||
| 304 | * @throws \OCA\Deck\NoPermissionException |
||
| 305 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 306 | * @throws BadRequestException |
||
| 307 | */ |
||
| 308 | public function delete($id) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param $id |
||
| 328 | * @return \OCP\AppFramework\Db\Entity |
||
| 329 | * @throws DoesNotExistException |
||
| 330 | * @throws \OCA\Deck\NoPermissionException |
||
| 331 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 332 | */ |
||
| 333 | View Code Duplication | public function deleteUndo($id) { |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param $id |
||
| 350 | * @return \OCP\AppFramework\Db\Entity |
||
| 351 | * @throws DoesNotExistException |
||
| 352 | * @throws \OCA\Deck\NoPermissionException |
||
| 353 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 354 | * @throws BadRequestException |
||
| 355 | */ |
||
| 356 | View Code Duplication | public function deleteForce($id) { |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param $id |
||
| 368 | * @param $title |
||
| 369 | * @param $color |
||
| 370 | * @param $archived |
||
| 371 | * @return \OCP\AppFramework\Db\Entity |
||
| 372 | * @throws DoesNotExistException |
||
| 373 | * @throws \OCA\Deck\NoPermissionException |
||
| 374 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 375 | * @throws BadRequestException |
||
| 376 | */ |
||
| 377 | public function update($id, $title, $color, $archived) { |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * @param $boardId |
||
| 412 | * @param $type |
||
| 413 | * @param $participant |
||
| 414 | * @param $edit |
||
| 415 | * @param $share |
||
| 416 | * @param $manage |
||
| 417 | * @return \OCP\AppFramework\Db\Entity |
||
| 418 | * @throws BadRequestException |
||
| 419 | * @throws \OCA\Deck\NoPermissionException |
||
| 420 | */ |
||
| 421 | public function addAcl($boardId, $type, $participant, $edit, $share, $manage) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param $id |
||
| 475 | * @param $edit |
||
| 476 | * @param $share |
||
| 477 | * @param $manage |
||
| 478 | * @return \OCP\AppFramework\Db\Entity |
||
| 479 | * @throws DoesNotExistException |
||
| 480 | * @throws \OCA\Deck\NoPermissionException |
||
| 481 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 482 | * @throws BadRequestException |
||
| 483 | */ |
||
| 484 | public function updateAcl($id, $edit, $share, $manage) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param $id |
||
| 516 | * @return \OCP\AppFramework\Db\Entity |
||
| 517 | * @throws DoesNotExistException |
||
| 518 | * @throws \OCA\Deck\NoPermissionException |
||
| 519 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 520 | * @throws BadRequestException |
||
| 521 | */ |
||
| 522 | public function deleteAcl($id) { |
||
| 549 | |||
| 550 | private function enrichWithStacks($board, $since = -1) { |
||
| 559 | |||
| 560 | } |
||
| 561 |
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.