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 |
||
| 41 | class CirclesRequest extends CirclesRequestBuilder { |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * @param int $circleId |
||
| 46 | * @param string $userId |
||
| 47 | * |
||
| 48 | * @return Circle |
||
|
|
|||
| 49 | * @throws CircleDoesNotExistException |
||
| 50 | */ |
||
| 51 | public function getCircleFromId($circleId, $userId = '') { |
||
| 52 | $qb = $this->getCirclesSelectSql(); |
||
| 53 | |||
| 54 | $this->limitToId($qb, $circleId); |
||
| 55 | if ($userId !== '') { |
||
| 56 | $this->leftJoinUserIdAsMember($qb, $userId); |
||
| 57 | } |
||
| 58 | |||
| 59 | |||
| 60 | // $this->leftjoinOwner($qb); |
||
| 61 | // $this->buildWithMemberLevel($qb, 'u.level', $level); |
||
| 62 | // $this->buildWithCircleId($qb, 'c.id', $circleId); |
||
| 63 | // $this->buildWithOrXTypes($qb, $userId, $type, $name, $circleId); |
||
| 64 | |||
| 65 | $cursor = $qb->execute(); |
||
| 66 | $data = $cursor->fetch(); |
||
| 67 | if ($data === false || $data === null) { |
||
| 68 | throw new CircleDoesNotExistException( |
||
| 69 | $this->l10n->t('Circle not found') |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | $entry = $this->parseCirclesSelectSql($data); |
||
| 74 | |||
| 75 | return $entry; |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * saveFrame() |
||
| 81 | * |
||
| 82 | * Insert a new entry in the database to save the SharingFrame. |
||
| 83 | * |
||
| 84 | * @param SharingFrame $frame |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function saveFrame(SharingFrame $frame) { |
|
| 87 | |||
| 88 | $qb = $this->getSharesInsertSql(); |
||
| 89 | $qb->setValue('circle_id', $qb->createNamedParameter($frame->getCircleId())) |
||
| 90 | ->setValue('source', $qb->createNamedParameter($frame->getSource())) |
||
| 91 | ->setValue('type', $qb->createNamedParameter($frame->getType())) |
||
| 92 | ->setValue('headers', $qb->createNamedParameter($frame->getHeaders(true))) |
||
| 93 | ->setValue('author', $qb->createNamedParameter($frame->getAuthor())) |
||
| 94 | ->setValue('cloud_id', $qb->createNamedParameter($frame->getCloudId())) |
||
| 95 | ->setValue('unique_id', $qb->createNamedParameter($frame->getUniqueId())) |
||
| 96 | ->setValue('payload', $qb->createNamedParameter($frame->getPayload(true))); |
||
| 97 | |||
| 98 | $qb->execute(); |
||
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | View Code Duplication | public function updateFrame(SharingFrame $frame) { |
|
| 103 | $qb = $this->getSharesUpdateSql($frame->getUniqueId()); |
||
| 104 | $qb->set('circle_id', $qb->createNamedParameter($frame->getCircleId())) |
||
| 105 | ->set('source', $qb->createNamedParameter($frame->getSource())) |
||
| 106 | ->set('type', $qb->createNamedParameter($frame->getType())) |
||
| 107 | ->set('headers', $qb->createNamedParameter($frame->getHeaders(true))) |
||
| 108 | ->set('author', $qb->createNamedParameter($frame->getAuthor())) |
||
| 109 | ->set('cloud_id', $qb->createNamedParameter($frame->getCloudId())) |
||
| 110 | ->set('unique_id', $qb->createNamedParameter($frame->getUniqueId())) |
||
| 111 | ->set('payload', $qb->createNamedParameter($frame->getPayload(true))); |
||
| 112 | |||
| 113 | $qb->execute(); |
||
| 114 | } |
||
| 115 | |||
| 116 | |||
| 117 | public function updateCircle(Circle $circle) { |
||
| 118 | $qb = $this->getCirclesUpdateSql($circle->getId()); |
||
| 119 | $qb->set('name', $qb->createNamedParameter($circle->getName())) |
||
| 120 | ->set('description', $qb->createNamedParameter($circle->getDescription())) |
||
| 121 | ->set('settings', $qb->createNamedParameter($circle->getSettings(true))); |
||
| 122 | |||
| 123 | $qb->execute(); |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string $uniqueId |
||
| 129 | * |
||
| 130 | * @return Circle |
||
| 131 | * @throws CircleDoesNotExistException |
||
| 132 | */ |
||
| 133 | public function getCircleFromUniqueId($uniqueId) { |
||
| 134 | $qb = $this->getCirclesSelectSql(); |
||
| 135 | $this->limitToUniqueId($qb, (string)$uniqueId); |
||
| 136 | |||
| 137 | $cursor = $qb->execute(); |
||
| 138 | $data = $cursor->fetch(); |
||
| 139 | |||
| 140 | if ($data === false) { |
||
| 141 | throw new CircleDoesNotExistException( |
||
| 142 | $this->l10n->t('Circle not found') |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | $entry = $this->parseCirclesSelectSql($data); |
||
| 146 | $cursor->closeCursor(); |
||
| 147 | |||
| 148 | return $entry; |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * @param int $circleId |
||
| 154 | * @param string $uniqueId |
||
| 155 | * |
||
| 156 | * @return SharingFrame |
||
| 157 | */ |
||
| 158 | public function getFrame($circleId, $uniqueId) { |
||
| 159 | $qb = $this->getSharesSelectSql(); |
||
| 160 | $this->limitToUniqueId($qb, (string)$uniqueId); |
||
| 161 | $this->limitToCircleId($qb, (int)$circleId); |
||
| 162 | |||
| 163 | $cursor = $qb->execute(); |
||
| 164 | $data = $cursor->fetch(); |
||
| 165 | $entry = $this->parseSharesSelectSql($data); |
||
| 166 | |||
| 167 | return $entry; |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * return the FederatedLink identified by a remote Circle UniqueId and the Token of the link |
||
| 173 | * |
||
| 174 | * @param string $token |
||
| 175 | * @param string $uniqueId |
||
| 176 | * |
||
| 177 | * @return FederatedLink |
||
| 178 | * @throws FederatedLinkDoesNotExistException |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function getLinkFromToken($token, $uniqueId) { |
|
| 181 | $qb = $this->getLinksSelectSql(); |
||
| 182 | $this->limitToUniqueId($qb, (string)$uniqueId); |
||
| 183 | $this->limitToToken($qb, (string)$token); |
||
| 184 | |||
| 185 | $cursor = $qb->execute(); |
||
| 186 | $data = $cursor->fetch(); |
||
| 187 | |||
| 188 | if ($data === false || $data === null) { |
||
| 189 | throw new FederatedLinkDoesNotExistException( |
||
| 190 | $this->l10n->t('Federated Link not found') |
||
| 191 | ); |
||
| 192 | } |
||
| 193 | |||
| 194 | $entry = $this->parseLinksSelectSql($data); |
||
| 195 | $cursor->closeCursor(); |
||
| 196 | |||
| 197 | return $entry; |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * return the FederatedLink identified by a its Id |
||
| 203 | * |
||
| 204 | * @param int $linkId |
||
| 205 | * |
||
| 206 | * @return FederatedLink |
||
| 207 | * @throws FederatedLinkDoesNotExistException |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function getLinkFromId($linkId) { |
|
| 210 | $qb = $this->getLinksSelectSql(); |
||
| 211 | $this->limitToId($qb, (string)$linkId); |
||
| 212 | |||
| 213 | $cursor = $qb->execute(); |
||
| 214 | $data = $cursor->fetch(); |
||
| 215 | $cursor->closeCursor(); |
||
| 216 | |||
| 217 | if ($data === false || $data === null) { |
||
| 218 | throw new FederatedLinkDoesNotExistException( |
||
| 219 | $this->l10n->t('Federated Link not found') |
||
| 220 | ); |
||
| 221 | } |
||
| 222 | |||
| 223 | $entry = $this->parseLinksSelectSql($data); |
||
| 224 | |||
| 225 | return $entry; |
||
| 226 | } |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * returns all FederatedLink from a circle |
||
| 231 | * |
||
| 232 | * @param int $circleId |
||
| 233 | * |
||
| 234 | * @return FederatedLink[] |
||
| 235 | */ |
||
| 236 | public function getLinksFromCircle($circleId) { |
||
| 237 | $qb = $this->getLinksSelectSql(); |
||
| 238 | $this->limitToCircleId($qb, $circleId); |
||
| 239 | |||
| 240 | $links = []; |
||
| 241 | $cursor = $qb->execute(); |
||
| 242 | while ($data = $cursor->fetch()) { |
||
| 243 | $link = $this->parseLinksSelectSql($data); |
||
| 244 | if ($link !== null) { |
||
| 245 | $links[] = $link; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | $cursor->closeCursor(); |
||
| 249 | |||
| 250 | return $links; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param integer $circleId |
||
| 255 | * @param int $level |
||
| 256 | * |
||
| 257 | * @return Member[] |
||
| 258 | */ |
||
| 259 | public function getMembers($circleId, $level = Member::LEVEL_MEMBER) { |
||
| 280 | |||
| 281 | |||
| 282 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.