| Conditions | 17 |
| Paths | 97 |
| Total Lines | 64 |
| Code Lines | 37 |
| 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 |
||
| 18 | public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
||
| 19 | { |
||
| 20 | if (!$rule instanceof Email) { |
||
| 21 | throw new UnexpectedRuleException(Email::class, $rule); |
||
| 22 | } |
||
| 23 | |||
| 24 | $originalValue = $value; |
||
| 25 | $result = new Result(); |
||
| 26 | |||
| 27 | if (!is_string($value)) { |
||
| 28 | $valid = false; |
||
| 29 | } elseif (!preg_match( |
||
| 30 | '/^(?P<name>(?:"?([^"]*)"?\s)?)(?:\s+)?((?P<open><?)((?P<local>.+)@(?P<domain>[^>]+))(?P<close>>?))$/i', |
||
| 31 | $value, |
||
| 32 | $matches |
||
| 33 | )) { |
||
| 34 | $valid = false; |
||
| 35 | } else { |
||
| 36 | /** @psalm-var array{name:string,local:string,open:string,domain:string,close:string} $matches */ |
||
| 37 | if ($rule->enableIDN) { |
||
| 38 | $matches['local'] = $this->idnToAscii($matches['local']); |
||
| 39 | $matches['domain'] = $this->idnToAscii($matches['domain']); |
||
| 40 | $value = implode([ |
||
| 41 | $matches['name'], |
||
| 42 | $matches['open'], |
||
| 43 | $matches['local'], |
||
| 44 | '@', |
||
| 45 | $matches['domain'], |
||
| 46 | $matches['close'], |
||
| 47 | ]); |
||
| 48 | } |
||
| 49 | |||
| 50 | if (is_string($matches['local']) && strlen($matches['local']) > 64) { |
||
| 51 | // The maximum total length of a user name or other local-part is 64 octets. RFC 5322 section 4.5.3.1.1 |
||
| 52 | // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1 |
||
| 53 | $valid = false; |
||
| 54 | } elseif (is_string($matches['local']) && strlen($matches['local'] . '@' . $matches['domain']) > 254) { |
||
| 55 | // There is a restriction in RFC 2821 on the length of an address in MAIL and RCPT commands |
||
| 56 | // of 254 characters. Since addresses that do not fit in those fields are not normally useful, the |
||
| 57 | // upper limit on address lengths should normally be considered to be 254. |
||
| 58 | // |
||
| 59 | // Dominic Sayers, RFC 3696 erratum 1690 |
||
| 60 | // http://www.rfc-editor.org/errata_search.php?eid=1690 |
||
| 61 | $valid = false; |
||
| 62 | } else { |
||
| 63 | $valid = preg_match($rule->pattern, $value) || ($rule->allowName && preg_match( |
||
| 64 | $rule->fullPattern, |
||
| 65 | $value |
||
| 66 | )); |
||
| 67 | if ($valid && $rule->checkDNS) { |
||
| 68 | $valid = checkdnsrr($matches['domain'] . '.', 'MX') || checkdnsrr($matches['domain'] . '.', 'A'); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | if ($rule->enableIDN && $valid === false) { |
||
| 74 | $valid = (bool) preg_match($rule->idnEmailPattern, $originalValue); |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($valid === false) { |
||
| 78 | $result->addError($rule->message); |
||
| 79 | } |
||
| 80 | |||
| 81 | return $result; |
||
| 82 | } |
||
| 89 |