| Conditions | 11 |
| Paths | 15 |
| Total Lines | 37 |
| Code Lines | 21 |
| 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 |
||
| 42 | public function validate($value, Constraint $constraint): void |
||
| 43 | { |
||
| 44 | if (!$constraint instanceof ValidGoogleAuthCode) { |
||
| 45 | throw new UnexpectedTypeException($constraint, ValidGoogleAuthCode::class); |
||
| 46 | } |
||
| 47 | |||
| 48 | if (null === $value || '' === $value) { |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | if (!is_string($value)) { |
||
| 53 | throw new UnexpectedValueException($value, 'string'); |
||
| 54 | } |
||
| 55 | |||
| 56 | if (!ctype_digit($value)) { |
||
| 57 | $this->context->addViolation('validator.google_code.only_digits_allowed'); |
||
| 58 | } |
||
| 59 | |||
| 60 | //Number must have 6 digits |
||
| 61 | if (6 !== strlen($value)) { |
||
| 62 | $this->context->addViolation('validator.google_code.wrong_digit_count'); |
||
| 63 | } |
||
| 64 | |||
| 65 | //Try to retrieve the user we want to check |
||
| 66 | if ($this->context->getObject() instanceof FormInterface && |
||
| 67 | $this->context->getObject() |
||
| 68 | ->getParent() instanceof FormInterface |
||
| 69 | && $this->context->getObject() |
||
| 70 | ->getParent() |
||
| 71 | ->getData() instanceof User) { |
||
| 72 | $user = $this->context->getObject() |
||
| 73 | ->getParent() |
||
| 74 | ->getData(); |
||
| 75 | |||
| 76 | //Check if the given code is valid |
||
| 77 | if (!$this->googleAuthenticator->checkCode($user, $value)) { |
||
| 78 | $this->context->addViolation('validator.google_code.wrong_code'); |
||
| 79 | } |
||
| 83 |