| Conditions | 13 |
| Paths | 102 |
| Total Lines | 55 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 182 |
| 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:
| 1 | <?php |
||
| 124 | public function edit($id) : Response |
||
| 125 | { |
||
| 126 | //none admin users can only edit themselves |
||
| 127 | if (!$this->userData->hasRole('Default.Admins')) { |
||
|
1 ignored issue
–
show
|
|||
| 128 | $id = $this->userData->getId(); |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($user = $this->model->findFirst($id)) { |
||
| 132 | $request = $this->request->getPut(); |
||
| 133 | |||
| 134 | if (empty($request)) { |
||
| 135 | $request = $this->request->getJsonRawBody(true); |
||
| 136 | } |
||
| 137 | |||
| 138 | //update password |
||
| 139 | if (array_key_exists('new_password', $request) && (!empty($request['new_password']) && !empty($request['password']))) { |
||
| 140 | //Ok let validate user password |
||
| 141 | $validation = new Validation(); |
||
| 142 | $validation->add('new_password', new PresenceOf(['message' => 'The new_password is required.'])); |
||
| 143 | $validation->add('current_password', new PresenceOf(['message' => 'The current_password is required.'])); |
||
| 144 | $validation->add('confirm_new_password', new PresenceOf(['message' => 'The confirm_new_password is required.'])); |
||
| 145 | $messages = $validation->validate($request); |
||
| 146 | |||
| 147 | if (count($messages)) { |
||
| 148 | foreach ($messages as $message) { |
||
| 149 | throw new BadRequestHttpException((string)$message); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $user->updatePassword($request['current_password'], $request['new_password'], $request['confirm_new_password']); |
||
| 154 | } else { |
||
| 155 | //remove on any actino that doesnt involve password |
||
| 156 | unset($request['password']); |
||
| 157 | } |
||
| 158 | |||
| 159 | //change my default company |
||
| 160 | if (array_key_exists('default_company', $request)) { |
||
| 161 | if ($company = Companies::findFirst($request['default_company'])) { |
||
| 162 | if ($company->userAssociatedToCompany($this->userData)) { |
||
| 163 | $user->default_company = $company->getId(); |
||
| 164 | unset($request['default_company']); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | //update |
||
| 170 | if ($user->update($request, $this->updateFields)) { |
||
| 171 | $user->password = null; |
||
| 172 | return $this->response($user); |
||
| 173 | } else { |
||
| 174 | //didnt work |
||
| 175 | throw new ModelException((string)current($user->getMessages())); |
||
| 176 | } |
||
| 177 | } else { |
||
| 178 | throw new NotFoundHttpException('Record not found'); |
||
| 179 | } |
||
| 252 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.