| Conditions | 12 |
| Paths | 24 |
| Total Lines | 96 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 199 | public function doChangePassword(array $data, $form) |
||
| 200 | { |
||
| 201 | $member = Security::getCurrentUser(); |
||
| 202 | // The user was logged in, check the current password |
||
| 203 | if ($member && ( |
||
| 204 | empty($data['OldPassword']) || |
||
| 205 | !$member->checkPassword($data['OldPassword'])->isValid() |
||
| 206 | ) |
||
| 207 | ) { |
||
| 208 | $form->sessionMessage( |
||
| 209 | _t( |
||
| 210 | 'SilverStripe\\Security\\Member.ERRORPASSWORDNOTMATCH', |
||
| 211 | 'Your current password does not match, please try again' |
||
| 212 | ), |
||
| 213 | 'bad' |
||
| 214 | ); |
||
| 215 | |||
| 216 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 217 | return $this->redirectBackToForm(); |
||
| 218 | } |
||
| 219 | |||
| 220 | if (!$member) { |
||
| 221 | if (Session::get('AutoLoginHash')) { |
||
| 222 | $member = Member::member_from_autologinhash(Session::get('AutoLoginHash')); |
||
| 223 | } |
||
| 224 | |||
| 225 | // The user is not logged in and no valid auto login hash is available |
||
| 226 | if (!$member) { |
||
| 227 | Session::clear('AutoLoginHash'); |
||
| 228 | |||
| 229 | return $this->redirect($this->addBackURLParam(Security::singleton()->Link('login'))); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | // Check the new password |
||
| 234 | if (empty($data['NewPassword1'])) { |
||
| 235 | $form->sessionMessage( |
||
| 236 | _t( |
||
| 237 | 'SilverStripe\\Security\\Member.EMPTYNEWPASSWORD', |
||
| 238 | "The new password can't be empty, please try again" |
||
| 239 | ), |
||
| 240 | 'bad' |
||
| 241 | ); |
||
| 242 | |||
| 243 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 244 | return $this->redirectBackToForm(); |
||
| 245 | } |
||
| 246 | |||
| 247 | // Fail if passwords do not match |
||
| 248 | if ($data['NewPassword1'] !== $data['NewPassword2']) { |
||
| 249 | $form->sessionMessage( |
||
| 250 | _t( |
||
| 251 | 'SilverStripe\\Security\\Member.ERRORNEWPASSWORD', |
||
| 252 | 'You have entered your new password differently, try again' |
||
| 253 | ), |
||
| 254 | 'bad' |
||
| 255 | ); |
||
| 256 | |||
| 257 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 258 | return $this->redirectBackToForm(); |
||
| 259 | } |
||
| 260 | |||
| 261 | // Check if the new password is accepted |
||
| 262 | $validationResult = $member->changePassword($data['NewPassword1']); |
||
| 263 | if (!$validationResult->isValid()) { |
||
| 264 | $form->setSessionValidationResult($validationResult); |
||
| 265 | |||
| 266 | return $this->redirectBackToForm(); |
||
| 267 | } |
||
| 268 | |||
| 269 | // Clear locked out status |
||
| 270 | $member->LockedOutUntil = null; |
||
| 271 | $member->FailedLoginCount = null; |
||
| 272 | // Clear the members login hashes |
||
| 273 | $member->AutoLoginHash = null; |
||
| 274 | $member->AutoLoginExpired = DBDatetime::create()->now(); |
||
| 275 | $member->write(); |
||
| 276 | |||
| 277 | if ($member->canLogIn()->isValid()) { |
||
| 278 | Injector::inst()->get(IdentityStore::class)->logIn($member, false, $this->getRequest()); |
||
| 279 | } |
||
| 280 | |||
| 281 | // TODO Add confirmation message to login redirect |
||
| 282 | Session::clear('AutoLoginHash'); |
||
| 283 | |||
| 284 | // Redirect to backurl |
||
| 285 | $backURL = $this->getBackURL(); |
||
| 286 | if ($backURL) { |
||
| 287 | return $this->redirect($backURL); |
||
| 288 | } |
||
| 289 | |||
| 290 | // Redirect to default location - the login form saying "You are logged in as..." |
||
| 291 | $url = Security::singleton()->Link('login'); |
||
| 292 | |||
| 293 | return $this->redirect($url); |
||
| 294 | } |
||
| 295 | |||
| 309 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.