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 | public function __construct(IDBConnection $db, Utils $utils) { |
||
36 | |||
37 | |||
38 | /** |
||
39 | * Obtains the credentials by vault id (not guid) |
||
40 | * @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
||
41 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
||
42 | * @return Vault[] |
||
43 | */ |
||
44 | 1 | public function getCredentialsByVaultId($vault_id, $user_id) { |
|
49 | |||
50 | /** |
||
51 | * Get a random credentail from a vault |
||
52 | * @param $vault_id |
||
53 | * @param $user_id |
||
54 | * @return Credential |
||
55 | */ |
||
56 | 1 | public function getRandomCredentialByVaultId($vault_id, $user_id) { |
|
64 | |||
65 | /** |
||
66 | * Get expired credentials |
||
67 | * @param $timestamp |
||
68 | * @return Credential[] |
||
69 | */ |
||
70 | 1 | public function getExpiredCredentials($timestamp){ |
|
75 | |||
76 | /** |
||
77 | * Get an credential by id. |
||
78 | * Optional user id |
||
79 | * @param $credential_id |
||
80 | * @param null $user_id |
||
81 | * @return Credential |
||
82 | */ |
||
83 | 1 | View Code Duplication | public function getCredentialById($credential_id, $user_id = null){ |
84 | $sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
||
85 | 1 | 'WHERE `id` = ?'; |
|
86 | // If we want to check the owner, add it to the query |
||
87 | 1 | $params = [$credential_id]; |
|
88 | 1 | if ($user_id !== null){ |
|
89 | 1 | $sql .= ' and `user_id` = ? '; |
|
90 | 1 | array_push($params, $user_id); |
|
91 | } |
||
92 | 1 | return $this->findEntity($sql,$params); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get credential label by id |
||
97 | * @param $credential_id |
||
98 | * @return Credential |
||
99 | */ |
||
100 | 1 | public function getCredentialLabelById($credential_id){ |
|
105 | |||
106 | /** |
||
107 | * Save credential to the database. |
||
108 | * @param $raw_credential |
||
109 | * @return Credential |
||
110 | */ |
||
111 | 1 | public function create($raw_credential){ |
|
112 | 1 | $credential = new Credential(); |
|
113 | |||
114 | 1 | $credential->setGuid($this->utils->GUID()); |
|
115 | 1 | $credential->setVaultId($raw_credential['vault_id']); |
|
116 | 1 | $credential->setUserId($raw_credential['user_id']); |
|
117 | 1 | $credential->setLabel($raw_credential['label']); |
|
118 | 1 | $credential->setDescription($raw_credential['description']); |
|
119 | 1 | $credential->setCreated($this->utils->getTime()); |
|
120 | 1 | $credential->setChanged($this->utils->getTime()); |
|
121 | 1 | $credential->setTags($raw_credential['tags']); |
|
122 | 1 | $credential->setEmail($raw_credential['email']); |
|
123 | 1 | $credential->setUsername($raw_credential['username']); |
|
124 | 1 | $credential->setPassword($raw_credential['password']); |
|
125 | 1 | $credential->setUrl($raw_credential['url']); |
|
126 | 1 | $credential->setFavicon($raw_credential['favicon']); |
|
127 | 1 | $credential->setRenewInterval($raw_credential['renew_interval']); |
|
128 | 1 | $credential->setExpireTime($raw_credential['expire_time']); |
|
129 | 1 | $credential->setDeleteTime($raw_credential['delete_time']); |
|
130 | 1 | $credential->setFiles($raw_credential['files']); |
|
131 | 1 | $credential->setCustomFields($raw_credential['custom_fields']); |
|
132 | 1 | $credential->setOtp($raw_credential['otp']); |
|
133 | 1 | $credential->setHidden($raw_credential['hidden']); |
|
134 | 1 | if(isset($raw_credential['shared_key'])) { |
|
135 | $credential->setSharedKey($raw_credential['shared_key']); |
||
136 | } |
||
137 | 1 | return parent::insert($credential); |
|
|
|||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Update a credential |
||
142 | * @param $raw_credential array An array containing all the credential fields |
||
143 | * @return Credential The updated credential |
||
144 | */ |
||
145 | 1 | public function updateCredential($raw_credential){ |
|
172 | |||
173 | 1 | public function deleteCredential(Credential $credential){ |
|
176 | |||
177 | 1 | public function upd(Credential $credential){ |
|
180 | |||
181 | /** |
||
182 | * Finds a credential by the given guid |
||
183 | * @param $credential_guid |
||
184 | * @return Credential |
||
185 | */ |
||
186 | 1 | View Code Duplication | public function getCredentialByGUID($credential_guid, $user_id = null){ |
195 | } |
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 theSon
calls the wrong method in the parent class.