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 |
||
| 30 | class CredentialMapper extends Mapper { |
||
| 31 | private $utils; |
||
| 32 | |||
| 33 | public function __construct(IDBConnection $db, Utils $utils) { |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Obtains the credentials by vault id (not guid) |
||
| 41 | * |
||
| 42 | * @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
||
| 43 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
||
| 44 | * @return Credential[] |
||
| 45 | */ |
||
| 46 | 1 | public function getCredentialsByVaultId($vault_id, $user_id) { |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Get a random credentail from a vault |
||
| 54 | * |
||
| 55 | * @param $vault_id |
||
| 56 | * @param $user_id |
||
| 57 | * @return Credential |
||
| 58 | */ |
||
| 59 | 1 | public function getRandomCredentialByVaultId($vault_id, $user_id) { |
|
| 60 | $sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
||
| 61 | 1 | 'WHERE `user_id` = ? and vault_id = ? AND shared_key is NULL LIMIT 20'; |
|
| 62 | 1 | $entities = $this->findEntities($sql, [$user_id, $vault_id]); |
|
| 63 | 1 | $count = count($entities) - 1; |
|
| 64 | 1 | $entities = array_splice($entities, rand(0, $count), 1); |
|
| 65 | 1 | return $entities; |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get expired credentials |
||
| 70 | * |
||
| 71 | * @param $timestamp |
||
| 72 | * @return Credential[] |
||
| 73 | */ |
||
| 74 | 1 | public function getExpiredCredentials($timestamp) { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Get an credential by id. |
||
| 82 | * Optional user id |
||
| 83 | * |
||
| 84 | * @param $credential_id |
||
| 85 | * @param null $user_id |
||
| 86 | * @return Credential |
||
| 87 | */ |
||
| 88 | 1 | View Code Duplication | public function getCredentialById($credential_id, $user_id = null) { |
| 89 | $sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
||
| 90 | 1 | 'WHERE `id` = ?'; |
|
| 91 | // If we want to check the owner, add it to the query |
||
| 92 | 1 | $params = [$credential_id]; |
|
| 93 | 1 | if ($user_id !== null) { |
|
| 94 | 1 | $sql .= ' and `user_id` = ? '; |
|
| 95 | 1 | array_push($params, $user_id); |
|
| 96 | } |
||
| 97 | 1 | return $this->findEntity($sql, $params); |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get credential label by id |
||
| 102 | * |
||
| 103 | * @param $credential_id |
||
| 104 | * @return Credential |
||
| 105 | */ |
||
| 106 | 1 | public function getCredentialLabelById($credential_id) { |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Save credential to the database. |
||
| 114 | * |
||
| 115 | * @param $raw_credential |
||
| 116 | * @return Credential |
||
| 117 | */ |
||
| 118 | 1 | public function create($raw_credential) { |
|
| 119 | 1 | $credential = new Credential(); |
|
| 120 | |||
| 121 | 1 | $credential->setGuid($this->utils->GUID()); |
|
| 122 | 1 | $credential->setVaultId($raw_credential['vault_id']); |
|
| 123 | 1 | $credential->setUserId($raw_credential['user_id']); |
|
| 124 | 1 | $credential->setLabel($raw_credential['label']); |
|
| 125 | 1 | $credential->setDescription($raw_credential['description']); |
|
| 126 | 1 | $credential->setCreated($this->utils->getTime()); |
|
| 127 | 1 | $credential->setChanged($this->utils->getTime()); |
|
| 128 | 1 | $credential->setTags($raw_credential['tags']); |
|
| 129 | 1 | $credential->setEmail($raw_credential['email']); |
|
| 130 | 1 | $credential->setUsername($raw_credential['username']); |
|
| 131 | 1 | $credential->setPassword($raw_credential['password']); |
|
| 132 | 1 | $credential->setUrl($raw_credential['url']); |
|
| 133 | 1 | $credential->setFavicon($raw_credential['favicon']); |
|
| 134 | 1 | $credential->setRenewInterval($raw_credential['renew_interval']); |
|
| 135 | 1 | $credential->setExpireTime($raw_credential['expire_time']); |
|
| 136 | 1 | $credential->setDeleteTime($raw_credential['delete_time']); |
|
| 137 | 1 | $credential->setFiles($raw_credential['files']); |
|
| 138 | 1 | $credential->setCustomFields($raw_credential['custom_fields']); |
|
| 139 | 1 | $credential->setOtp($raw_credential['otp']); |
|
| 140 | 1 | $credential->setHidden($raw_credential['hidden']); |
|
| 141 | 1 | if (isset($raw_credential['shared_key'])) { |
|
| 142 | $credential->setSharedKey($raw_credential['shared_key']); |
||
| 143 | } |
||
| 144 | 1 | return parent::insert($credential); |
|
|
|
|||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Update a credential |
||
| 149 | * |
||
| 150 | * @param $raw_credential array An array containing all the credential fields |
||
| 151 | * @return Credential The updated credential |
||
| 152 | */ |
||
| 153 | 1 | public function updateCredential($raw_credential) { |
|
| 154 | 1 | $original = $this->getCredentialByGUID($raw_credential['guid']); |
|
| 155 | 1 | $credential = new Credential(); |
|
| 156 | 1 | $credential->setId($original->getId()); |
|
| 157 | 1 | $credential->setGuid($original->getGuid()); |
|
| 158 | 1 | $credential->setVaultId($original->getVaultId()); |
|
| 159 | 1 | $credential->setUserId($original->getUserId()); |
|
| 160 | 1 | $credential->setLabel($raw_credential['label']); |
|
| 161 | 1 | $credential->setDescription($raw_credential['description']); |
|
| 162 | 1 | $credential->setCreated($original->getCreated()); |
|
| 163 | 1 | $credential->setChanged($this->utils->getTime()); |
|
| 164 | 1 | $credential->setTags($raw_credential['tags']); |
|
| 165 | 1 | $credential->setEmail($raw_credential['email']); |
|
| 166 | 1 | $credential->setUsername($raw_credential['username']); |
|
| 167 | 1 | $credential->setPassword($raw_credential['password']); |
|
| 168 | 1 | $credential->setUrl($raw_credential['url']); |
|
| 169 | 1 | $credential->setFavicon($raw_credential['favicon']); |
|
| 170 | 1 | $credential->setRenewInterval($raw_credential['renew_interval']); |
|
| 171 | 1 | $credential->setExpireTime($raw_credential['expire_time']); |
|
| 172 | 1 | $credential->setFiles($raw_credential['files']); |
|
| 173 | 1 | $credential->setCustomFields($raw_credential['custom_fields']); |
|
| 174 | 1 | $credential->setOtp($raw_credential['otp']); |
|
| 175 | 1 | $credential->setHidden($raw_credential['hidden']); |
|
| 176 | 1 | $credential->setDeleteTime($raw_credential['delete_time']); |
|
| 177 | 1 | if (isset($raw_credential['shared_key'])) { |
|
| 178 | 1 | $credential->setSharedKey($raw_credential['shared_key']); |
|
| 179 | } |
||
| 180 | 1 | return parent::update($credential); |
|
| 181 | } |
||
| 182 | |||
| 183 | 1 | public function deleteCredential(Credential $credential) { |
|
| 186 | |||
| 187 | 1 | public function upd(Credential $credential) { |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Finds a credential by the given guid |
||
| 193 | * |
||
| 194 | * @param $credential_guid |
||
| 195 | * @return Credential |
||
| 196 | */ |
||
| 197 | 1 | View Code Duplication | public function getCredentialByGUID($credential_guid, $user_id = null) { |
| 206 | } |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.