| Conditions | 10 |
| Paths | 10 |
| Total Lines | 65 |
| Code Lines | 36 |
| 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 |
||
| 28 | public function validate($email_address) |
||
| 29 | { |
||
| 30 | if (! preg_match('/^.+\@\S+$/', $email_address)) { |
||
| 31 | // email not in correct {string}@{string} format |
||
| 32 | throw new EmailValidationException( |
||
| 33 | esc_html__('Email does not have the required @ sign.', 'event_espresso') |
||
| 34 | ); |
||
| 35 | } |
||
| 36 | $atIndex = $this->getAtIndex($email_address); |
||
| 37 | $local = $this->getLocalPartOfEmail($email_address, $atIndex); |
||
| 38 | $localLen = strlen($local); |
||
| 39 | if ($localLen < 1) { |
||
| 40 | //no local part |
||
| 41 | throw new EmailValidationException( |
||
| 42 | esc_html__('Email local-part (before the @) is required.', 'event_espresso') |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | if ($localLen > 64) { |
||
| 46 | // local part length exceeded |
||
| 47 | throw new EmailValidationException( |
||
| 48 | esc_html__('Email local-part (before the @) is too long.', 'event_espresso') |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | if ($local[0] === '.') { |
||
| 52 | // local part starts with '.' |
||
| 53 | throw new EmailValidationException( |
||
| 54 | esc_html__('Email local-part (before the @) must not begin with a period.', 'event_espresso') |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | if ($local[$localLen - 1] === '.') { |
||
| 58 | // local part starts or ends with '.' |
||
| 59 | throw new EmailValidationException( |
||
| 60 | esc_html__('Email local-part (before the @) must not end with a period.', 'event_espresso') |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | if (preg_match('/\\.\\./', $local)) { |
||
| 64 | // local part has two consecutive dots |
||
| 65 | throw new EmailValidationException( |
||
| 66 | esc_html__( |
||
| 67 | 'Email local-part (before the @) must not have two consecutive periods.', |
||
| 68 | 'event_espresso' |
||
| 69 | ) |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | $domain = $this->getDomainPartOfEmail($email_address, $atIndex); |
||
| 73 | $domainLen = strlen($domain); |
||
| 74 | if ($domainLen < 1) { |
||
| 75 | throw new EmailValidationException( |
||
| 76 | esc_html__('Email domain (after the @) is required.', 'event_espresso') |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | if ($domainLen > 255) { |
||
| 80 | // domain part length exceeded |
||
| 81 | throw new EmailValidationException( |
||
| 82 | esc_html__('Email domain (after the @) is too long.', 'event_espresso') |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | if (preg_match('/\\.\\./', $domain)) { |
||
| 86 | // domain part has two consecutive dots |
||
| 87 | throw new EmailValidationException( |
||
| 88 | esc_html__('Email domain (after the @) must not have two consecutive periods.', 'event_espresso') |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | return true; |
||
| 92 | } |
||
| 93 | |||
| 137 |