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 |
||
| 42 | class TokensRequest extends TokensRequestBuilder { |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string $token |
||
| 47 | * |
||
| 48 | * @return SharesToken |
||
| 49 | * @throws TokenDoesNotExistException |
||
| 50 | */ |
||
| 51 | public function getByToken(string $token) { |
||
| 52 | $qb = $this->getTokensSelectSql(); |
||
| 53 | $this->limitToToken($qb, $token); |
||
| 54 | |||
| 55 | $cursor = $qb->execute(); |
||
| 56 | $data = $cursor->fetch(); |
||
| 57 | $cursor->closeCursor(); |
||
| 58 | if ($data === false) { |
||
| 59 | throw new TokenDoesNotExistException('Unknown share token'); |
||
| 60 | } |
||
| 61 | |||
| 62 | return $this->parseTokensSelectSql($data); |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $shareId |
||
| 68 | * @param string $circleId |
||
| 69 | * @param string $email |
||
| 70 | * |
||
| 71 | * @return SharesToken |
||
| 72 | * @throws TokenDoesNotExistException |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function getTokenFromMember(string $shareId, string $circleId, string $email) { |
|
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @param Member $member |
||
| 93 | * |
||
| 94 | * @return SharesToken[] |
||
| 95 | */ |
||
| 96 | public function getTokensFromMember(Member $member) { |
||
| 97 | $qb = $this->getTokensSelectSql(); |
||
| 98 | $this->limitToUserId($qb, $member->getUserId()); |
||
| 99 | $this->limitToCircleId($qb, $member->getCircleId()); |
||
| 100 | |||
| 101 | $shares = []; |
||
| 102 | $cursor = $qb->execute(); |
||
| 103 | while ($data = $cursor->fetch()) { |
||
| 104 | $shares[] = $this->parseTokensSelectSql($data); |
||
| 105 | } |
||
| 106 | $cursor->closeCursor(); |
||
| 107 | |||
| 108 | return $shares; |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * @param Member $member |
||
| 114 | * @param int $shareId |
||
| 115 | * |
||
| 116 | * @param string $password |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function generateTokenForMember(Member $member, int $shareId, string $password = '') { |
||
| 121 | try { |
||
| 122 | $token = $this->miscService->token(15); |
||
| 123 | |||
| 124 | $hasher = \OC::$server->getHasher(); |
||
| 125 | $password = ($password !== '') ? $hasher->hash($password) : ''; |
||
| 126 | |||
| 127 | $qb = $this->getTokensInsertSql(); |
||
| 128 | $qb->setValue('circle_id', $qb->createNamedParameter($member->getCircleId())) |
||
| 129 | ->setValue('user_id', $qb->createNamedParameter($member->getUserId())) |
||
| 130 | ->setValue('share_id', $qb->createNamedParameter($shareId)) |
||
| 131 | ->setValue('token', $qb->createNamedParameter($token)) |
||
| 132 | ->setValue('password', $qb->createNamedParameter($password)); |
||
| 133 | |||
| 134 | $qb->execute(); |
||
| 135 | |||
| 136 | return $token; |
||
| 137 | } catch (Exception $e) { |
||
| 138 | $this->miscService->log('exception while generateTokenForMember: ' . $e->getMessage()); |
||
| 139 | |||
| 140 | return ''; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * @param int $shareId |
||
| 147 | */ |
||
| 148 | public function removeTokenByShareId(int $shareId) { |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * @param Member $member |
||
| 158 | */ |
||
| 159 | public function removeTokensFromMember(Member $member) { |
||
| 166 | |||
| 167 | } |
||
| 168 | |||
| 169 |
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.