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 |
||
| 33 | class LabelService { |
||
| 34 | |||
| 35 | /** @var LabelMapper */ |
||
| 36 | private $labelMapper; |
||
| 37 | /** @var PermissionService */ |
||
| 38 | private $permissionService; |
||
| 39 | /** @var BoardService */ |
||
| 40 | private $boardService; |
||
| 41 | |||
| 42 | public function __construct(LabelMapper $labelMapper, PermissionService $permissionService, BoardService $boardService) { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param $labelId |
||
| 50 | * @return \OCP\AppFramework\Db\Entity |
||
| 51 | * @throws \OCA\Deck\NoPermissionException |
||
| 52 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
| 53 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 54 | * @throws BadRequestException |
||
| 55 | */ |
||
| 56 | public function find($labelId) { |
||
| 57 | if (is_numeric($labelId) === false) { |
||
| 58 | throw new BadRequestException('label id must be a number'); |
||
| 59 | } |
||
| 60 | $this->permissionService->checkPermission($this->labelMapper, $labelId, Acl::PERMISSION_READ); |
||
| 61 | return $this->labelMapper->find($labelId); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param $title |
||
| 66 | * @param $color |
||
| 67 | * @param $boardId |
||
| 68 | * @return \OCP\AppFramework\Db\Entity |
||
| 69 | * @throws StatusException |
||
| 70 | * @throws \OCA\Deck\NoPermissionException |
||
| 71 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
| 72 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 73 | * @throws BadRequestException |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function create($title, $color, $boardId) { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param $id |
||
| 102 | * @return \OCP\AppFramework\Db\Entity |
||
| 103 | * @throws StatusException |
||
| 104 | * @throws \OCA\Deck\NoPermissionException |
||
| 105 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
| 106 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 107 | * @throws BadRequestException |
||
| 108 | */ |
||
| 109 | public function delete($id) { |
||
| 110 | |||
| 111 | if (is_numeric($id) === false) { |
||
| 112 | throw new BadRequestException('label id must be a number'); |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->permissionService->checkPermission($this->labelMapper, $id, Acl::PERMISSION_MANAGE); |
||
| 116 | if ($this->boardService->isArchived($this->labelMapper, $id)) { |
||
| 117 | throw new StatusException('Operation not allowed. This board is archived.'); |
||
| 118 | } |
||
| 119 | return $this->labelMapper->delete($this->find($id)); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param $id |
||
| 124 | * @param $title |
||
| 125 | * @param $color |
||
| 126 | * @return \OCP\AppFramework\Db\Entity |
||
| 127 | * @throws StatusException |
||
| 128 | * @throws \OCA\Deck\NoPermissionException |
||
| 129 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
| 130 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
| 131 | * @throws BadRequestException |
||
| 132 | */ |
||
| 133 | public function update($id, $title, $color) { |
||
| 156 | |||
| 157 | } |
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.