| Conditions | 10 |
| Paths | 24 |
| Total Lines | 65 |
| Code Lines | 42 |
| 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 |
||
| 144 | public function validate($password, $member) |
||
| 145 | { |
||
| 146 | $valid = ValidationResult::create(); |
||
| 147 | |||
| 148 | $minLength = $this->getMinLength(); |
||
| 149 | if ($minLength && strlen($password) < $minLength) { |
||
| 150 | $error = _t( |
||
| 151 | 'SilverStripe\\Security\\PasswordValidator.TOOSHORT', |
||
| 152 | 'Password is too short, it must be {minimum} or more characters long', |
||
| 153 | ['minimum' => $this->minLength] |
||
| 154 | ); |
||
| 155 | |||
| 156 | $valid->addError($error, 'bad', 'TOO_SHORT'); |
||
| 157 | } |
||
| 158 | |||
| 159 | $minTestScore = $this->getMinTestScore(); |
||
| 160 | if ($minTestScore) { |
||
| 161 | $missedTests = []; |
||
| 162 | $testNames = $this->getTestNames(); |
||
| 163 | $tests = $this->getTests(); |
||
| 164 | |||
| 165 | foreach ($testNames as $name) { |
||
| 166 | if (preg_match($tests[$name], $password)) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | $missedTests[] = _t( |
||
| 170 | 'SilverStripe\\Security\\PasswordValidator.STRENGTHTEST' . strtoupper($name), |
||
| 171 | $name, |
||
| 172 | 'The user needs to add this to their password for more complexity' |
||
| 173 | ); |
||
| 174 | } |
||
| 175 | |||
| 176 | $score = count($this->testNames) - count($missedTests); |
||
| 177 | if ($score < $minTestScore) { |
||
| 178 | $error = _t( |
||
| 179 | 'SilverStripe\\Security\\PasswordValidator.LOWCHARSTRENGTH', |
||
| 180 | 'Please increase password strength by adding some of the following characters: {chars}', |
||
| 181 | ['chars' => implode(', ', $missedTests)] |
||
| 182 | ); |
||
| 183 | $valid->addError($error, 'bad', 'LOW_CHARACTER_STRENGTH'); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | $historicCount = $this->getHistoricCount(); |
||
| 188 | if ($historicCount) { |
||
| 189 | $previousPasswords = MemberPassword::get() |
||
| 190 | ->where(array('"MemberPassword"."MemberID"' => $member->ID)) |
||
| 191 | ->sort('"Created" DESC, "ID" DESC') |
||
| 192 | ->limit($historicCount); |
||
| 193 | /** @var MemberPassword $previousPassword */ |
||
| 194 | foreach ($previousPasswords as $previousPassword) { |
||
| 195 | if ($previousPassword->checkPassword($password)) { |
||
| 196 | $error = _t( |
||
| 197 | 'SilverStripe\\Security\\PasswordValidator.PREVPASSWORD', |
||
| 198 | 'You\'ve already used that password in the past, please choose a new password' |
||
| 199 | ); |
||
| 200 | $valid->addError($error, 'bad', 'PREVIOUS_PASSWORD'); |
||
| 201 | break; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | $this->extend('updateValidatePassword', $password, $member, $valid); |
||
| 207 | |||
| 208 | return $valid; |
||
| 209 | } |
||
| 211 |