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 StackService 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 StackService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class StackService { |
||
| 38 | |||
| 39 | private $stackMapper; |
||
| 40 | private $cardMapper; |
||
| 41 | private $boardMapper; |
||
| 42 | private $labelMapper; |
||
| 43 | private $permissionService; |
||
| 44 | private $boardService; |
||
| 45 | private $cardService; |
||
| 46 | private $assignedUsersMapper; |
||
| 47 | private $attachmentService; |
||
| 48 | |||
| 49 | View Code Duplication | public function __construct( |
|
| 70 | |||
| 71 | private function enrichStackWithCards($stack) { |
||
| 84 | |||
| 85 | private function enrichStacksWithCards($stacks) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param $stackId |
||
| 93 | * @return \OCP\AppFramework\Db\Entity |
||
| 94 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
| 95 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 96 | * @throws BadRequestException |
||
| 97 | */ |
||
| 98 | public function find($stackId) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param $boardId |
||
| 116 | * @return array |
||
| 117 | * @throws \OCA\Deck\NoPermissionException |
||
| 118 | * @throws BadRequestException |
||
| 119 | */ |
||
| 120 | public function findAll($boardId) { |
||
| 130 | |||
| 131 | public function fetchDeleted($boardId) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $boardId |
||
| 140 | * @return array |
||
| 141 | * @throws \OCA\Deck\NoPermissionException |
||
| 142 | * @throws BadRequestException |
||
| 143 | */ |
||
| 144 | public function findAllArchived($boardId) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param $title |
||
| 167 | * @param $boardId |
||
| 168 | * @param integer $order |
||
| 169 | * @return \OCP\AppFramework\Db\Entity |
||
| 170 | * @throws StatusException |
||
| 171 | * @throws \OCA\Deck\NoPermissionException |
||
| 172 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
| 173 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 174 | * @throws BadRequestException |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function create($title, $boardId, $order) { |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param $id |
||
| 204 | * @return \OCP\AppFramework\Db\Entity |
||
| 205 | * @throws \OCA\Deck\NoPermissionException |
||
| 206 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
| 207 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 208 | * @throws BadRequestException |
||
| 209 | */ |
||
| 210 | View Code Duplication | public function delete($id) { |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param $id |
||
| 229 | * @param $title |
||
| 230 | * @param $boardId |
||
| 231 | * @param $order |
||
| 232 | * @param $deletedAt |
||
| 233 | * @return \OCP\AppFramework\Db\Entity |
||
| 234 | * @throws StatusException |
||
| 235 | * @throws \OCA\Deck\NoPermissionException |
||
| 236 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
| 237 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 238 | * @throws BadRequestException |
||
| 239 | */ |
||
| 240 | public function update($id, $title, $boardId, $order, $deletedAt) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param $id |
||
| 272 | * @param $order |
||
| 273 | * @return array |
||
| 274 | * @throws \OCA\Deck\NoPermissionException |
||
| 275 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
| 276 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 277 | * @throws BadRequestException |
||
| 278 | */ |
||
| 279 | public function reorder($id, $order) { |
||
| 312 | } |
||
| 313 |
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.