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 |
||
| 36 | class CirclesRequest extends CirclesRequestBuilder { |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * forceGetCircle(); |
||
| 41 | * |
||
| 42 | * returns data of a circle from its Id. |
||
| 43 | * |
||
| 44 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
| 45 | * In case of interaction with users, Please use getCircle() instead. |
||
| 46 | * |
||
| 47 | * @param string $circleUniqueId |
||
| 48 | * |
||
| 49 | * @return Circle |
||
| 50 | * @throws CircleDoesNotExistException |
||
| 51 | */ |
||
| 52 | public function forceGetCircle($circleUniqueId) { |
||
| 53 | $qb = $this->getCirclesSelectSql(); |
||
| 54 | |||
| 55 | $this->limitToShortenUniqueId($qb, $circleUniqueId, Circle::SHORT_UNIQUE_ID_LENGTH); |
||
| 56 | |||
| 57 | $cursor = $qb->execute(); |
||
| 58 | $data = $cursor->fetch(); |
||
| 59 | $cursor->closeCursor(); |
||
| 60 | |||
| 61 | if ($data === false) { |
||
| 62 | throw new CircleDoesNotExistException($this->l10n->t('Circle not found')); |
||
| 63 | } |
||
| 64 | |||
| 65 | $entry = $this->parseCirclesSelectSql($data); |
||
| 66 | |||
| 67 | return $entry; |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * forceGetCircles(); |
||
| 73 | * |
||
| 74 | * returns data of a all circles. |
||
| 75 | * |
||
| 76 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
| 77 | * In case of interaction with users, Please use getCircles() instead. |
||
| 78 | * |
||
| 79 | * @return Circle[] |
||
| 80 | */ |
||
| 81 | public function forceGetCircles() { |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * forceGetCircleByName(); |
||
| 99 | * |
||
| 100 | * returns data of a circle from its Name. |
||
| 101 | * |
||
| 102 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
| 103 | * In case of interaction with users, do not use this method. |
||
| 104 | * |
||
| 105 | * @param $name |
||
| 106 | * |
||
| 107 | * @return null|Circle |
||
| 108 | * @throws CircleDoesNotExistException |
||
| 109 | */ |
||
| 110 | public function forceGetCircleByName($name) { |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $userId |
||
| 132 | * @param int $type |
||
| 133 | * @param string $name |
||
| 134 | * @param int $level |
||
| 135 | * |
||
| 136 | * @return Circle[] |
||
| 137 | */ |
||
| 138 | public function getCircles($userId, $type = 0, $name = '', $level = 0) { |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * |
||
| 168 | * @param string $circleUniqueId |
||
| 169 | * @param string $viewerId |
||
| 170 | * |
||
| 171 | * @return Circle |
||
| 172 | * @throws CircleDoesNotExistException |
||
| 173 | */ |
||
| 174 | public function getCircle($circleUniqueId, $viewerId) { |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * createCircle(); |
||
| 204 | * |
||
| 205 | * Create a circle with $userId as its owner. |
||
| 206 | * Will returns the circle |
||
| 207 | * |
||
| 208 | * @param Circle $circle |
||
| 209 | * @param $userId |
||
| 210 | * |
||
| 211 | * @throws CircleAlreadyExistsException |
||
| 212 | */ |
||
| 213 | public function createCircle(Circle &$circle, $userId) { |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * remove a circle |
||
| 241 | * |
||
| 242 | * @param string $circleUniqueId |
||
| 243 | */ |
||
| 244 | public function destroyCircle($circleUniqueId) { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * returns if the circle is already in database |
||
| 254 | * |
||
| 255 | * @param Circle $circle |
||
| 256 | * @param string $userId |
||
| 257 | * |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | private function isCircleUnique(Circle $circle, $userId) { |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * return if the personal circle is unique |
||
| 284 | * |
||
| 285 | * @param Circle $circle |
||
| 286 | * @param string $userId |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | private function isPersonalCircleUnique(Circle $circle, $userId) { |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * @param Circle $circle |
||
| 310 | * @param string $userId |
||
| 311 | * |
||
| 312 | * @throws CircleAlreadyExistsException |
||
| 313 | */ |
||
| 314 | public function updateCircle(Circle $circle, $userId) { |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $uniqueId |
||
| 333 | * |
||
| 334 | * @return Circle |
||
| 335 | * @throws CircleDoesNotExistException |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function getCircleFromUniqueId($uniqueId) { |
|
| 353 | |||
| 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.