| 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 |
||
| 222 | public function validate($password, $member) |
||
| 223 | { |
||
| 224 | $valid = ValidationResult::create(); |
||
| 225 | |||
| 226 | $minLength = $this->getMinLength(); |
||
| 227 | if ($minLength && strlen($password) < $minLength) { |
||
| 228 | $error = _t( |
||
| 229 | 'SilverStripe\\Security\\PasswordValidator.TOOSHORT', |
||
| 230 | 'Password is too short, it must be {minimum} or more characters long', |
||
| 231 | ['minimum' => $this->minLength] |
||
| 232 | ); |
||
| 233 | |||
| 234 | $valid->addError($error, 'bad', 'TOO_SHORT'); |
||
| 235 | } |
||
| 236 | |||
| 237 | $minTestScore = $this->getMinTestScore(); |
||
| 238 | if ($minTestScore) { |
||
| 239 | $missedTests = []; |
||
| 240 | $testNames = $this->getTestNames(); |
||
| 241 | $tests = $this->getTests(); |
||
| 242 | |||
| 243 | foreach ($testNames as $name) { |
||
| 244 | if (preg_match($tests[$name], $password)) { |
||
| 245 | continue; |
||
| 246 | } |
||
| 247 | $missedTests[] = _t( |
||
| 248 | 'SilverStripe\\Security\\PasswordValidator.STRENGTHTEST' . strtoupper($name), |
||
| 249 | $name, |
||
| 250 | 'The user needs to add this to their password for more complexity' |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | |||
| 254 | $score = count($this->testNames) - count($missedTests); |
||
| 255 | if ($score < $minTestScore) { |
||
| 256 | $error = _t( |
||
| 257 | 'SilverStripe\\Security\\PasswordValidator.LOWCHARSTRENGTH', |
||
| 258 | 'Please increase password strength by adding some of the following characters: {chars}', |
||
| 259 | ['chars' => implode(', ', $missedTests)] |
||
| 260 | ); |
||
| 261 | $valid->addError($error, 'bad', 'LOW_CHARACTER_STRENGTH'); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | $historicCount = $this->getHistoricCount(); |
||
| 266 | if ($historicCount) { |
||
| 267 | $previousPasswords = MemberPassword::get() |
||
| 268 | ->where(array('"MemberPassword"."MemberID"' => $member->ID)) |
||
| 269 | ->sort('"Created" DESC, "ID" DESC') |
||
| 270 | ->limit($historicCount); |
||
| 271 | /** @var MemberPassword $previousPassword */ |
||
| 272 | foreach ($previousPasswords as $previousPassword) { |
||
| 273 | if ($previousPassword->checkPassword($password)) { |
||
| 274 | $error = _t( |
||
| 275 | 'SilverStripe\\Security\\PasswordValidator.PREVPASSWORD', |
||
| 276 | 'You\'ve already used that password in the past, please choose a new password' |
||
| 277 | ); |
||
| 278 | $valid->addError($error, 'bad', 'PREVIOUS_PASSWORD'); |
||
| 279 | break; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | $this->extend('updateValidatePassword', $password, $member, $valid, $this); |
||
| 285 | |||
| 286 | return $valid; |
||
| 287 | } |
||
| 289 |