Refactoring: reduce email validation complexity
rise readibility by using early returns
remove condition depth
MODIFY
src/EmailAddress.php
src/EmailAddress.php
@@ -335,32 +335,30 @@ class EmailAddress extends AbstractValidator
protected function validateLocalPart()
{
// First try to match the local part on the common dot-atom format
- $result = false;
// Dot-atom characters are: 1*atext *("." 1*atext)
// atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*",
// "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"
$atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $this->localPart)) {
- $result = true;
- } elseif ($this->validateInternationalizedLocalPart($this->localPart)) {
- $result = true;
- } else {
+ return true;
+ }
+ if ($this->validateInternationalizedLocalPart($this->localPart)) {
+ return true;
+ }
+
// Try quoted string format (RFC 5321 Chapter 4.1.2)
// Quoted-string characters are: DQUOTE *(qtext/quoted-pair) DQUOTE
$qtext = '\x20-\x21\x23-\x5b\x5d-\x7e'; // %d32-33 / %d35-91 / %d93-126
$quotedPair = '\x20-\x7e'; // %d92 %d32-126
if (preg_match('/^"(['. $qtext .']|\x5c[' . $quotedPair . '])*"$/', $this->localPart)) {
- $result = true;
- } else {
+ return true;
+ }
$this->error(self::DOT_ATOM);
$this->error(self::QUOTED_STRING);
$this->error(self::INVALID_LOCAL_PART);
- }
- }
-
- return $result;
+ return false;
}
/**

Refactoring: reduce email validation complexity

rise readibility by using early returns
remove condition depth