| Conditions | 15 |
| Paths | 105 |
| Total Lines | 118 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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:
| 1 | <?php |
||
| 65 | public function saveAction() |
||
| 66 | { |
||
| 67 | $this->checkSecurityToken($this->previousSk, $this->request); |
||
| 68 | |||
| 69 | $mastePassService = $this->dic->get(MasterPassService::class); |
||
| 70 | |||
| 71 | $currentMasterPass = $this->request->analyzeEncrypted('current_masterpass'); |
||
| 72 | $newMasterPass = $this->request->analyzeEncrypted('new_masterpass'); |
||
| 73 | $newMasterPassR = $this->request->analyzeEncrypted('new_masterpass_repeat'); |
||
| 74 | $confirmPassChange = $this->request->analyzeBool('confirm_masterpass_change', false); |
||
| 75 | $noAccountPassChange = $this->request->analyzeBool('no_account_change', false); |
||
| 76 | $taskId = $this->request->analyzeString('taskId'); |
||
| 77 | |||
| 78 | if (!$mastePassService->checkUserUpdateMPass($this->session->getUserData()->getLastUpdateMPass())) { |
||
| 79 | return $this->returnJsonResponse( |
||
| 80 | JsonResponse::JSON_SUCCESS_STICKY, |
||
| 81 | __u('Master password updated'), |
||
| 82 | [__u('Please, restart the session for update it')] |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | |||
| 86 | if (empty($newMasterPass) || empty($currentMasterPass)) { |
||
| 87 | return $this->returnJsonResponse( |
||
| 88 | JsonResponse::JSON_ERROR, |
||
| 89 | __u('Master password not entered') |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($confirmPassChange === false) { |
||
| 94 | return $this->returnJsonResponse( |
||
| 95 | JsonResponse::JSON_ERROR, |
||
| 96 | __u('The password update must be confirmed') |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($newMasterPass === $currentMasterPass) { |
||
| 101 | return $this->returnJsonResponse( |
||
| 102 | JsonResponse::JSON_ERROR, |
||
| 103 | __u('Passwords are the same') |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ($newMasterPass !== $newMasterPassR) { |
||
| 108 | return $this->returnJsonResponse( |
||
| 109 | JsonResponse::JSON_ERROR, |
||
| 110 | __u('Master passwords do not match') |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (!$mastePassService->checkMasterPassword($currentMasterPass)) { |
||
| 115 | return $this->returnJsonResponse( |
||
| 116 | JsonResponse::JSON_ERROR, |
||
| 117 | __u('The current master password does not match') |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | if (!$this->config->getConfigData()->isMaintenance()) { |
||
| 122 | return $this->returnJsonResponse( |
||
| 123 | JsonResponse::JSON_WARNING, |
||
| 124 | __u('Maintenance mode not enabled'), |
||
| 125 | [__u('Please, enable it to avoid unwanted behavior from other sessions')] |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($this->config->getConfigData()->isDemoEnabled()) { |
||
| 130 | return $this->returnJsonResponse( |
||
| 131 | JsonResponse::JSON_WARNING, |
||
| 132 | __u('Ey, this is a DEMO!!') |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | $configService = $this->dic->get(ConfigService::class); |
||
| 137 | |||
| 138 | if (!$noAccountPassChange) { |
||
| 139 | try { |
||
| 140 | $task = $taskId !== null ? TaskFactory::create(__FUNCTION__, $taskId) : null; |
||
| 141 | |||
| 142 | $request = new UpdateMasterPassRequest( |
||
| 143 | $currentMasterPass, |
||
| 144 | $newMasterPass, |
||
| 145 | $configService->getByParam(MasterPassService::PARAM_MASTER_PASS_HASH), |
||
| 146 | $task |
||
| 147 | ); |
||
| 148 | |||
| 149 | $this->eventDispatcher->notifyEvent('update.masterPassword.start', new Event($this)); |
||
| 150 | |||
| 151 | $mastePassService->changeMasterPassword($request); |
||
| 152 | |||
| 153 | $this->eventDispatcher->notifyEvent('update.masterPassword.end', new Event($this)); |
||
| 154 | } catch (Exception $e) { |
||
| 155 | processException($e); |
||
| 156 | |||
| 157 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 158 | |||
| 159 | return $this->returnJsonResponseException($e); |
||
| 160 | } finally { |
||
| 161 | if (isset($task)) { |
||
| 162 | TaskFactory::end($task); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } else { |
||
| 166 | try { |
||
| 167 | $this->eventDispatcher->notifyEvent('update.masterPassword.hash', new Event($this)); |
||
| 168 | |||
| 169 | $mastePassService->updateConfig(Hash::hashKey($newMasterPass)); |
||
| 170 | } catch (Exception $e) { |
||
| 171 | processException($e); |
||
| 172 | |||
| 173 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 174 | |||
| 175 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('Error while saving the Master Password\'s hash')); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return $this->returnJsonResponse( |
||
| 180 | JsonResponse::JSON_SUCCESS_STICKY, |
||
| 181 | __u('Master password updated'), |
||
| 182 | [__u('Please, restart the session for update it')] |
||
| 183 | ); |
||
| 283 | } |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: