function simplification

This commit is contained in:
Eduardo Gulias Davis
2020-05-23 15:39:27 +02:00
parent ad92070629
commit 3145070028
2 changed files with 19 additions and 44 deletions
+15 -30
View File
@@ -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;
}
}
+4 -14
View File
@@ -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;
}
/**