| Conditions | 12 |
| Paths | 35 |
| Total Lines | 90 |
| Code Lines | 53 |
| 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 |
||
| 75 | public function doChangePassword(array $data) { |
||
| 76 | if($member = Member::currentUser()) { |
||
| 77 | // The user was logged in, check the current password |
||
| 78 | if(empty($data['OldPassword']) || !$member->checkPassword($data['OldPassword'])->valid()) { |
||
| 79 | $this->clearMessage(); |
||
| 80 | $this->sessionMessage( |
||
| 81 | _t('Member.ERRORPASSWORDNOTMATCH', "Your current password does not match, please try again"), |
||
| 82 | "bad" |
||
| 83 | ); |
||
| 84 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 85 | return $this->controller->redirect($this->controller->Link('changepassword')); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | if(!$member) { |
||
| 90 | if(Session::get('AutoLoginHash')) { |
||
| 91 | $member = Member::member_from_autologinhash(Session::get('AutoLoginHash')); |
||
| 92 | } |
||
| 93 | |||
| 94 | // The user is not logged in and no valid auto login hash is available |
||
| 95 | if(!$member) { |
||
| 96 | Session::clear('AutoLoginHash'); |
||
| 97 | return $this->controller->redirect($this->controller->Link('login')); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | // Check the new password |
||
| 102 | if(empty($data['NewPassword1'])) { |
||
| 103 | $this->clearMessage(); |
||
| 104 | $this->sessionMessage( |
||
| 105 | _t('Member.EMPTYNEWPASSWORD', "The new password can't be empty, please try again"), |
||
| 106 | "bad"); |
||
| 107 | |||
| 108 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 109 | return $this->controller->redirect($this->controller->Link('changepassword')); |
||
| 110 | } |
||
| 111 | else if($data['NewPassword1'] == $data['NewPassword2']) { |
||
| 112 | $isValid = $member->changePassword($data['NewPassword1']); |
||
| 113 | if($isValid->valid()) { |
||
| 114 | $member->logIn(); |
||
| 115 | |||
| 116 | // TODO Add confirmation message to login redirect |
||
| 117 | Session::clear('AutoLoginHash'); |
||
| 118 | |||
| 119 | // Clear locked out status |
||
| 120 | $member->LockedOutUntil = null; |
||
| 121 | $member->FailedLoginCount = null; |
||
| 122 | $member->write(); |
||
| 123 | |||
| 124 | if (!empty($_REQUEST['BackURL']) |
||
| 125 | // absolute redirection URLs may cause spoofing |
||
| 126 | && Director::is_site_url($_REQUEST['BackURL']) |
||
| 127 | ) { |
||
| 128 | $url = Director::absoluteURL($_REQUEST['BackURL']); |
||
| 129 | return $this->controller->redirect($url); |
||
| 130 | } |
||
| 131 | else { |
||
| 132 | // Redirect to default location - the login form saying "You are logged in as..." |
||
| 133 | $redirectURL = HTTP::setGetVar( |
||
| 134 | 'BackURL', |
||
| 135 | Director::absoluteBaseURL(), $this->controller->Link('login') |
||
| 136 | ); |
||
| 137 | return $this->controller->redirect($redirectURL); |
||
| 138 | } |
||
| 139 | } else { |
||
| 140 | $this->clearMessage(); |
||
| 141 | $this->sessionMessage( |
||
| 142 | _t( |
||
| 143 | 'Member.INVALIDNEWPASSWORD', |
||
| 144 | "We couldn't accept that password: {password}", |
||
| 145 | array('password' => nl2br("\n".Convert::raw2xml($isValid->starredList()))) |
||
| 146 | ), |
||
| 147 | "bad", |
||
| 148 | false |
||
| 149 | ); |
||
| 150 | |||
| 151 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 152 | return $this->controller->redirect($this->controller->Link('changepassword')); |
||
| 153 | } |
||
| 154 | |||
| 155 | } else { |
||
| 156 | $this->clearMessage(); |
||
| 157 | $this->sessionMessage( |
||
| 158 | _t('Member.ERRORNEWPASSWORD', "You have entered your new password differently, try again"), |
||
| 159 | "bad"); |
||
| 160 | |||
| 161 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 162 | return $this->controller->redirect($this->controller->Link('changepassword')); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 168 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: