From 31450700284119ebf4605daeb6f83f8e944e2faa Mon Sep 17 00:00:00 2001 From: Eduardo Gulias Davis Date: Sat, 23 May 2020 15:39:27 +0200 Subject: [PATCH] function simplification --- EmailValidator/Parser/LocalPart.php | 45 ++++++++++------------------- EmailValidator/Parser/Parser.php | 18 +++--------- 2 files changed, 19 insertions(+), 44 deletions(-) diff --git a/EmailValidator/Parser/LocalPart.php b/EmailValidator/Parser/LocalPart.php index 4febf86..c617cde 100644 --- a/EmailValidator/Parser/LocalPart.php +++ b/EmailValidator/Parser/LocalPart.php @@ -7,7 +7,6 @@ use Egulias\EmailValidator\Result\Result; use Egulias\EmailValidator\Result\ValidEmail; use Egulias\EmailValidator\Result\InvalidEmail; use Egulias\EmailValidator\Warning\LocalTooLong; -use Egulias\EmailValidator\Exception\ExpectingATEXT; use Egulias\EmailValidator\Result\Reason\ConsecutiveDot; use Egulias\EmailValidator\Result\Reason\DotAtEnd; use Egulias\EmailValidator\Result\Reason\DotAtStart; @@ -16,9 +15,6 @@ use Egulias\EmailValidator\Result\Reason\ExpectingATEXT as ReasonExpectingATEXT; class LocalPart extends Parser { - /* - @property array - */ private $invalidTokens = array( EmailLexer::S_COMMA => EmailLexer::S_COMMA, EmailLexer::S_CLOSEBRACKET => EmailLexer::S_CLOSEBRACKET, @@ -32,9 +28,7 @@ class LocalPart extends Parser public function parse($localPart) : Result { - $closingQuote = false; $totalLength = 0; - $commentParser = new Comment($this->lexer); while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { if ($this->hasDotAtStart()) { @@ -52,13 +46,11 @@ class LocalPart extends Parser if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS || $this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS ) { - $result = $commentParser->parse('remove'); - if($result->isInvalid()) { - return $result; - } - $warns = $commentParser->getWarnings(); - foreach ($warns as $code => $dWarning) { - $this->warnings[$code] = $dWarning; + $commentsResult = $this->parseComments(); + + //Invalid comment parsing + if($commentsResult->isInvalid()) { + return $commentsResult; } } @@ -113,24 +105,17 @@ class LocalPart extends Parser return $parseAgain; } - /** - * @param bool $closingQuote - */ - protected function isInvalidToken(array $token, $closingQuote) + protected function parseComments() { - $forbidden = array( - EmailLexer::S_COMMA, - EmailLexer::S_CLOSEBRACKET, - EmailLexer::S_OPENBRACKET, - EmailLexer::S_GREATERTHAN, - EmailLexer::S_LOWERTHAN, - EmailLexer::S_COLON, - EmailLexer::S_SEMICOLON, - EmailLexer::INVALID - ); - - if (in_array($token['type'], $forbidden) && !$closingQuote) { - throw new ExpectingATEXT(); + $commentParser = new Comment($this->lexer); + $result = $commentParser->parse('remove'); + if($result->isInvalid()) { + return $result; } + $warns = $commentParser->getWarnings(); + foreach ($warns as $code => $dWarning) { + $this->warnings[$code] = $dWarning; + } + return $result; } } \ No newline at end of file diff --git a/EmailValidator/Parser/Parser.php b/EmailValidator/Parser/Parser.php index d01edee..5469586 100644 --- a/EmailValidator/Parser/Parser.php +++ b/EmailValidator/Parser/Parser.php @@ -153,16 +153,11 @@ abstract class Parser return false; } - if ($this->lexer->token['type'] === EmailLexer::S_SP || + return $this->lexer->token['type'] === EmailLexer::S_SP || $this->lexer->token['type'] === EmailLexer::S_HTAB || $this->lexer->token['type'] === EmailLexer::S_CR || $this->lexer->token['type'] === EmailLexer::S_LF || - $this->lexer->token['type'] === EmailLexer::CRLF - ) { - return true; - } - - return false; + $this->lexer->token['type'] === EmailLexer::CRLF; } /** @@ -172,14 +167,9 @@ abstract class Parser { $previous = $this->lexer->getPrevious(); - if ($previous && $previous['type'] === EmailLexer::S_BACKSLASH + return $previous && $previous['type'] === EmailLexer::S_BACKSLASH && - $this->lexer->token['type'] !== EmailLexer::GENERIC - ) { - return true; - } - - return false; + $this->lexer->token['type'] !== EmailLexer::GENERIC; } /**