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 |
||
34 | class LabelService { |
||
35 | |||
36 | /** @var LabelMapper */ |
||
37 | private $labelMapper; |
||
38 | /** @var PermissionService */ |
||
39 | private $permissionService; |
||
40 | /** @var BoardService */ |
||
41 | private $boardService; |
||
42 | /** @var ChangeHelper */ |
||
43 | private $changeHelper; |
||
44 | |||
45 | public function __construct(LabelMapper $labelMapper, PermissionService $permissionService, BoardService $boardService, ChangeHelper $changeHelper) { |
||
51 | |||
52 | /** |
||
53 | * @param $labelId |
||
54 | * @return \OCP\AppFramework\Db\Entity |
||
55 | * @throws \OCA\Deck\NoPermissionException |
||
56 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
57 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
58 | * @throws BadRequestException |
||
59 | */ |
||
60 | View Code Duplication | public function find($labelId) { |
|
67 | |||
68 | /** |
||
69 | * @param $title |
||
70 | * @param $color |
||
71 | * @param $boardId |
||
72 | * @return \OCP\AppFramework\Db\Entity |
||
73 | * @throws StatusException |
||
74 | * @throws \OCA\Deck\NoPermissionException |
||
75 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
76 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
77 | * @throws BadRequestException |
||
78 | */ |
||
79 | public function create($title, $color, $boardId) { |
||
80 | |||
81 | if ($title === false || $title === null) { |
||
82 | throw new BadRequestException('title must be provided'); |
||
83 | } |
||
84 | |||
85 | if ($color === false || $color === null) { |
||
86 | throw new BadRequestException('color must be provided'); |
||
87 | } |
||
88 | |||
89 | if (is_numeric($boardId) === false) { |
||
90 | throw new BadRequestException('board id must be a number'); |
||
91 | } |
||
92 | |||
93 | $this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_MANAGE); |
||
94 | |||
95 | $boardLabels = $this->labelMapper->findAll($boardId); |
||
96 | if (\is_array($boardLabels)) { |
||
97 | foreach($boardLabels as $boardLabel) { |
||
98 | if ($boardLabel->getTitle() === $title) { |
||
99 | throw new BadRequestException('title must be unique'); |
||
100 | break; |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | if ($this->boardService->isArchived(null, $boardId)) { |
||
106 | throw new StatusException('Operation not allowed. This board is archived.'); |
||
107 | } |
||
108 | $label = new Label(); |
||
109 | $label->setTitle($title); |
||
110 | $label->setColor($color); |
||
111 | $label->setBoardId($boardId); |
||
112 | $this->changeHelper->boardChanged($boardId); |
||
113 | return $this->labelMapper->insert($label); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param $id |
||
118 | * @return \OCP\AppFramework\Db\Entity |
||
119 | * @throws StatusException |
||
120 | * @throws \OCA\Deck\NoPermissionException |
||
121 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
122 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
123 | * @throws BadRequestException |
||
124 | */ |
||
125 | public function delete($id) { |
||
139 | |||
140 | /** |
||
141 | * @param $id |
||
142 | * @param $title |
||
143 | * @param $color |
||
144 | * @return \OCP\AppFramework\Db\Entity |
||
145 | * @throws StatusException |
||
146 | * @throws \OCA\Deck\NoPermissionException |
||
147 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
148 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
149 | * @throws BadRequestException |
||
150 | */ |
||
151 | public function update($id, $title, $color) { |
||
191 | |||
192 | } |
||
193 |
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.