Conditions | 24 |
Paths | 4162 |
Total Lines | 140 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 600 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
123 | public function updateCredential($changed, $created, |
||
124 | $credential_id, $custom_fields, $delete_time, $credential_guid, |
||
125 | $description, $email, $expire_time, $icon, $files, $guid, |
||
126 | $hidden, $label, $otp, $password, $renew_interval, |
||
127 | $tags, $url, $username, $vault_id, $revision_created, $shared_key, $acl, $unshare_action, $set_share_key, $skip_revision) { |
||
128 | |||
129 | |||
130 | $storedCredential = $this->credentialService->getCredentialByGUID($credential_guid); |
||
131 | |||
132 | $credential = array( |
||
133 | 'credential_id' => $credential_id, |
||
134 | 'guid' => $guid, |
||
135 | 'label' => $label, |
||
136 | 'description' => $description, |
||
137 | 'created' => $created, |
||
138 | 'changed' => $changed, |
||
139 | 'vault_id' => $vault_id, |
||
140 | 'tags' => $tags, |
||
141 | 'email' => $email, |
||
142 | 'username' => $username, |
||
143 | 'password' => $password, |
||
144 | 'url' => $url, |
||
145 | 'icon' => json_encode($icon), |
||
146 | 'renew_interval' => $renew_interval, |
||
147 | 'expire_time' => $expire_time, |
||
148 | 'files' => $files, |
||
149 | 'custom_fields' => $custom_fields, |
||
150 | 'delete_time' => $delete_time, |
||
151 | 'hidden' => $hidden, |
||
152 | 'otp' => $otp, |
||
153 | 'user_id' => $storedCredential->getUserId() |
||
154 | ); |
||
155 | |||
156 | |||
157 | if (!hash_equals($storedCredential->getUserId(), $this->userId)) { |
||
158 | $acl = $this->sharingService->getCredentialAclForUser($this->userId, $storedCredential->getGuid()); |
||
159 | if ($acl->hasPermission(SharingACL::WRITE)) { |
||
160 | $credential['shared_key'] = $storedCredential->getSharedKey(); |
||
161 | } else { |
||
162 | return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED); |
||
163 | } |
||
164 | if (!$this->settings->isEnabled('user_sharing_enabled')) { |
||
165 | return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | |||
170 | $link = ''; // @TODO create direct link to credential |
||
171 | if ($revision_created) { |
||
172 | $activity = 'item_apply_revision'; |
||
173 | $this->activityService->add( |
||
174 | $activity . '_self', array($label, $this->userId, $revision_created), |
||
175 | '', array(), |
||
176 | $link, $this->userId, Activity::TYPE_ITEM_ACTION); |
||
177 | } else if (($storedCredential->getDeleteTime() === 0) && (int)$delete_time > 0) { |
||
178 | $activity = 'item_deleted'; |
||
179 | $this->activityService->add( |
||
180 | $activity . '_self', array($label, $this->userId), |
||
181 | '', array(), |
||
182 | $link, $this->userId, Activity::TYPE_ITEM_ACTION); |
||
183 | } else if (($storedCredential->getDeleteTime() > 0) && (int)$delete_time === 0) { |
||
184 | $activity = 'item_recovered'; |
||
185 | $this->activityService->add( |
||
186 | $activity . '_self', array($label, $this->userId), |
||
187 | '', array(), |
||
188 | $link, $this->userId, Activity::TYPE_ITEM_ACTION); |
||
189 | } else if ($label !== $storedCredential->getLabel()) { |
||
190 | $activity = 'item_renamed'; |
||
191 | $this->activityService->add( |
||
192 | $activity . '_self', array($storedCredential->getLabel(), $label, $this->userId), |
||
193 | '', array(), |
||
194 | $link, $this->userId, Activity::TYPE_ITEM_RENAMED); |
||
195 | } else { |
||
196 | $activity = 'item_edited'; |
||
197 | $this->activityService->add( |
||
198 | $activity . '_self', array($label, $this->userId), |
||
199 | '', array(), |
||
200 | $link, $this->userId, Activity::TYPE_ITEM_ACTION); |
||
201 | } |
||
202 | $acl_list = null; |
||
203 | |||
204 | try { |
||
205 | $acl_list = $this->sharingService->getCredentialAclList($storedCredential->getGuid()); |
||
206 | } catch (\Exception $exception) { |
||
207 | // Just check if we have an acl list |
||
208 | } |
||
209 | if (!empty($acl_list)) { |
||
210 | $params = array(); |
||
211 | switch ($activity) { |
||
212 | case 'item_recovered': |
||
213 | case 'item_deleted': |
||
214 | case 'item_edited': |
||
215 | $params = array($credential['label'], $this->userId); |
||
216 | break; |
||
217 | case 'item_apply_revision': |
||
218 | $params = array($credential['label'], $this->userId, $revision_created); |
||
219 | break; |
||
220 | case 'item_renamed': |
||
221 | $params = array($storedCredential->getLabel(), $label, $this->userId); |
||
222 | break; |
||
223 | } |
||
224 | |||
225 | foreach ($acl_list as $sharingACL) { |
||
226 | $target_user = $sharingACL->getUserId(); |
||
227 | if ($target_user === $this->userId) { |
||
228 | continue; |
||
229 | } |
||
230 | $this->activityService->add( |
||
231 | $activity, $params, |
||
232 | '', array(), |
||
233 | $link, $target_user, Activity::TYPE_ITEM_ACTION); |
||
234 | } |
||
235 | if (!hash_equals($this->userId, $storedCredential->getUserId())) { |
||
236 | $this->activityService->add( |
||
237 | $activity, $params, |
||
238 | '', array(), |
||
239 | $link, $storedCredential->getUserId(), Activity::TYPE_ITEM_ACTION); |
||
240 | } |
||
241 | } |
||
242 | if ($set_share_key === true) { |
||
243 | $storedCredential->setSharedKey($shared_key); |
||
244 | $credential['shared_key'] = $shared_key; |
||
245 | } |
||
246 | if ($unshare_action === true) { |
||
247 | $storedCredential->setSharedKey(''); |
||
248 | $credential['shared_key'] = ''; |
||
249 | } |
||
250 | |||
251 | if (!isset($credential['shared_key'])) { |
||
252 | $credential['shared_key'] = $storedCredential->getSharedKey(); |
||
253 | } |
||
254 | |||
255 | if (!$skip_revision) { |
||
256 | $this->credentialRevisionService->createRevision($storedCredential, $storedCredential->getUserId(), $credential_id, $this->userId); |
||
257 | } |
||
258 | |||
259 | $credential = $this->credentialService->updateCredential($credential); |
||
260 | |||
261 | return new JSONResponse($credential); |
||
262 | } |
||
263 | |||
353 | } |
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.