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 |
||
| 10 | class IssueService extends JiraClient |
||
| 11 | { |
||
| 12 | private $uri = '/issue'; |
||
| 13 | private $searchUri = '/search'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Get issue |
||
| 17 | * |
||
| 18 | * @param $issueIdOrKey Issue id or key |
||
| 19 | * |
||
| 20 | * @return mixed |
||
| 21 | */ |
||
| 22 | View Code Duplication | public function get($issueIdOrKey) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Search issues. |
||
| 35 | * |
||
| 36 | * @param $jql |
||
| 37 | * @param int $startAt |
||
| 38 | * @param int $maxResults |
||
| 39 | * @param array $fields |
||
| 40 | * |
||
| 41 | * @return mixed |
||
| 42 | */ |
||
| 43 | public function search($jql, $startAt = 0, $maxResults = 15, $fields = []) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Create new issue. |
||
| 64 | * |
||
| 65 | * @param IssueField $issueField |
||
| 66 | * |
||
| 67 | * @return mixed |
||
| 68 | */ |
||
| 69 | public function create(IssueField $issueField) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Update issue. |
||
| 86 | * |
||
| 87 | * @param $issueIdOrKey Issue id or key |
||
| 88 | * @param IssueField $issueField |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function update($issueIdOrKey, IssueField $issueField) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add one or more file to an issue. |
||
| 109 | * |
||
| 110 | * @param $issueIdOrKey Issue id or key |
||
| 111 | * @param array $filePathArray attachment file path. |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function addAttachments($issueIdOrKey, array $filePathArray) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param $uri |
||
| 135 | * @param null $toResource |
||
| 136 | * |
||
| 137 | * @return mixed |
||
| 138 | */ |
||
| 139 | public function getAttachmentStream($uri, $toResource = null) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Adds a new comment to an issue. |
||
| 146 | * |
||
| 147 | * @param $issueIdOrKey Issue id or key |
||
| 148 | * @param Comment $comment |
||
| 149 | * |
||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | public function addComment($issueIdOrKey, Comment $comment) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Update comment to an issue. |
||
| 166 | * |
||
| 167 | * @param $issueIdOrKey Issue id or key |
||
| 168 | * @param Comment $comment |
||
| 169 | * |
||
| 170 | * @return mixed |
||
| 171 | * @throws JiraException |
||
| 172 | */ |
||
| 173 | public function updateComment($issueIdOrKey, Comment $comment) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get a list of the transitions possible for this |
||
| 195 | * issue by the current user, along with fields that |
||
| 196 | * are required and their types. |
||
| 197 | * |
||
| 198 | * @param $issueIdOrKey |
||
| 199 | * |
||
| 200 | * @return mixed|string |
||
| 201 | */ |
||
| 202 | public function getTransition($issueIdOrKey) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Find transition id by transition's to field name(aka 'Resolved'). |
||
| 216 | * |
||
| 217 | * @param $issueIdOrKey |
||
| 218 | * @param $transitionToName |
||
| 219 | * |
||
| 220 | * @return mixed |
||
| 221 | * @throws JiraException |
||
| 222 | */ |
||
| 223 | public function findTransitionId($issueIdOrKey, $transitionToName) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Perform a transition on an issue. |
||
| 241 | * |
||
| 242 | * @param $issueIdOrKey Issue id or key |
||
| 243 | * @param Transition $transition |
||
| 244 | * |
||
| 245 | * @return mixed - if transition was successful return http 204(no contents) |
||
| 246 | * @throws JiraException |
||
| 247 | */ |
||
| 248 | public function doTransition($issueIdOrKey, Transition $transition) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get TimeTracking info. |
||
| 265 | * |
||
| 266 | * @param $issueIdOrKey |
||
| 267 | * |
||
| 268 | * @return bool|TimeTracking |
||
| 269 | */ |
||
| 270 | public function getTimeTracking($issueIdOrKey) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * TimeTracking issues. |
||
| 283 | * |
||
| 284 | * @param $issueIdOrKey |
||
| 285 | * @param $timeTracking |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function setTimeTracking($issueIdOrKey, TimeTracking $timeTracking) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get getWorklog. |
||
| 303 | * |
||
| 304 | * @param mixed $issueIdOrKey |
||
| 305 | * |
||
| 306 | * @return Worklog Return Worklog object |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function getWorklog($issueIdOrKey) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param $issueIdOrKey |
||
| 321 | * @param $label |
||
| 322 | * |
||
| 323 | * @return mixed |
||
| 324 | */ |
||
| 325 | View Code Duplication | public function setLabel($issueIdOrKey, $label) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @param $issueIdOrKey |
||
| 339 | * @param $label |
||
| 340 | * |
||
| 341 | * @return mixed |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function removeLabel($issueIdOrKey, $label) |
|
| 354 | } |
||
| 355 |
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.