| Conditions | 11 |
| Paths | 15 |
| Total Lines | 32 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 47 | public function validate($value, Constraint $constraint) |
||
| 48 | { |
||
| 49 | if (!$constraint instanceof ValidGoogleAuthCode) { |
||
| 50 | throw new UnexpectedTypeException($constraint, ValidGoogleAuthCode::class); |
||
| 51 | } |
||
| 52 | |||
| 53 | if (null === $value || '' === $value) { |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!\is_string($value)) { |
||
| 58 | throw new UnexpectedValueException($value, 'string'); |
||
| 59 | } |
||
| 60 | |||
| 61 | if(!ctype_digit($value)) { |
||
| 62 | $this->context->addViolation('validator.google_code.only_digits_allowed'); |
||
| 63 | } |
||
| 64 | |||
| 65 | //Number must have 6 digits |
||
| 66 | if(strlen($value) !== 6) { |
||
| 67 | $this->context->addViolation('validator.google_code.wrong_digit_count'); |
||
| 68 | } |
||
| 69 | |||
| 70 | //Try to retrieve the user we want to check |
||
| 71 | if($this->context->getObject() instanceof FormInterface && |
||
| 72 | $this->context->getObject()->getParent() instanceof FormInterface |
||
| 73 | && $this->context->getObject()->getParent()->getData() instanceof User) { |
||
| 74 | $user = $this->context->getObject()->getParent()->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 | } |