Conditions | 12 |
Paths | 48 |
Total Lines | 95 |
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 | $oldPassword = isset($data['OldPassword']) ? $data['OldPassword'] : null; |
||
204 | if ($member && !$this->checkPassword($member, $oldPassword)) { |
||
205 | $form->sessionMessage( |
||
206 | _t( |
||
207 | 'SilverStripe\\Security\\Member.ERRORPASSWORDNOTMATCH', |
||
208 | 'Your current password does not match, please try again' |
||
209 | ), |
||
210 | 'bad' |
||
211 | ); |
||
212 | |||
213 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
214 | return $this->redirectBackToForm(); |
||
215 | } |
||
216 | |||
217 | if (!$member) { |
||
218 | if (Session::get('AutoLoginHash')) { |
||
219 | $member = Member::member_from_autologinhash(Session::get('AutoLoginHash')); |
||
220 | } |
||
221 | |||
222 | // The user is not logged in and no valid auto login hash is available |
||
223 | if (!$member) { |
||
224 | Session::clear('AutoLoginHash'); |
||
225 | |||
226 | return $this->redirect($this->addBackURLParam(Security::singleton()->Link('login'))); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | // Check the new password |
||
231 | if (empty($data['NewPassword1'])) { |
||
232 | $form->sessionMessage( |
||
233 | _t( |
||
234 | 'SilverStripe\\Security\\Member.EMPTYNEWPASSWORD', |
||
235 | "The new password can't be empty, please try again" |
||
236 | ), |
||
237 | 'bad' |
||
238 | ); |
||
239 | |||
240 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
241 | return $this->redirectBackToForm(); |
||
242 | } |
||
243 | |||
244 | // Fail if passwords do not match |
||
245 | if ($data['NewPassword1'] !== $data['NewPassword2']) { |
||
246 | $form->sessionMessage( |
||
247 | _t( |
||
248 | 'SilverStripe\\Security\\Member.ERRORNEWPASSWORD', |
||
249 | 'You have entered your new password differently, try again' |
||
250 | ), |
||
251 | 'bad' |
||
252 | ); |
||
253 | |||
254 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
255 | return $this->redirectBackToForm(); |
||
256 | } |
||
257 | |||
258 | // Check if the new password is accepted |
||
259 | $validationResult = $member->changePassword($data['NewPassword1']); |
||
260 | if (!$validationResult->isValid()) { |
||
261 | $form->setSessionValidationResult($validationResult); |
||
262 | |||
263 | return $this->redirectBackToForm(); |
||
264 | } |
||
265 | |||
266 | // Clear locked out status |
||
267 | $member->LockedOutUntil = null; |
||
268 | $member->FailedLoginCount = null; |
||
269 | // Clear the members login hashes |
||
270 | $member->AutoLoginHash = null; |
||
271 | $member->AutoLoginExpired = DBDatetime::create()->now(); |
||
272 | $member->write(); |
||
273 | |||
274 | if ($member->canLogIn()) { |
||
275 | /** @var IdentityStore $identityStore */ |
||
276 | $identityStore = Injector::inst()->get(IdentityStore::class); |
||
277 | $identityStore->logIn($member, false, $this->getRequest()); |
||
278 | } |
||
279 | |||
280 | // TODO Add confirmation message to login redirect |
||
281 | Session::clear('AutoLoginHash'); |
||
282 | |||
283 | // Redirect to backurl |
||
284 | $backURL = $this->getBackURL(); |
||
285 | if ($backURL) { |
||
286 | return $this->redirect($backURL); |
||
287 | } |
||
288 | |||
289 | // Redirect to default location - the login form saying "You are logged in as..." |
||
290 | $url = Security::singleton()->Link('login'); |
||
291 | |||
292 | return $this->redirect($url); |
||
293 | } |
||
294 | |||
330 |
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.