From 7771e2bf2c004d76fddb565ec73e7f78689bb8ff Mon Sep 17 00:00:00 2001 From: Bastien Wermeille Date: Sun, 18 Dec 2022 22:43:52 +0100 Subject: [PATCH] Fix types issues --- src/EmailLexer.php | 19 +++--- src/EmailParser.php | 2 +- src/MessageIDParser.php | 2 +- src/Parser.php | 4 +- src/Parser/Comment.php | 16 ++--- src/Parser/CommentStrategy/DomainComment.php | 2 +- src/Parser/CommentStrategy/LocalComment.php | 2 +- src/Parser/DomainLiteral.php | 20 +++--- src/Parser/DomainPart.php | 64 ++++++++++---------- src/Parser/DoubleQuote.php | 18 +++--- src/Parser/FoldingWhiteSpace.php | 18 +++--- src/Parser/IDLeftPart.php | 2 +- src/Parser/IDRightPart.php | 4 +- src/Parser/LocalPart.php | 28 ++++----- src/Parser/PartParser.php | 6 +- src/Result/InvalidEmail.php | 3 +- src/Warning/Warning.php | 5 +- tests/EmailValidator/EmailLexerTest.php | 14 ++--- 18 files changed, 116 insertions(+), 113 deletions(-) diff --git a/src/EmailLexer.php b/src/EmailLexer.php index 0db4d06..a553f5b 100644 --- a/src/EmailLexer.php +++ b/src/EmailLexer.php @@ -135,14 +135,14 @@ class EmailLexer extends AbstractLexer /** * @var Token */ - protected $previous; + protected Token $previous; /** * The last matched/seen token. * - * @var Token|null + * @var Token */ - public Token|null $token; + public Token $current; /** * The next token in the input. @@ -154,7 +154,7 @@ class EmailLexer extends AbstractLexer /** * @var Token */ - private Token|null $nullToken; + private Token $nullToken; /** @var string */ private $accumulator = ''; @@ -168,7 +168,7 @@ class EmailLexer extends AbstractLexer $nullToken = new Token('', self::S_EMPTY, 0); $this->nullToken = $nullToken; - $this->previous = $this->token = $this->nullToken; + $this->current = $this->previous = $this->nullToken; $this->lookahead = null; } @@ -176,7 +176,7 @@ class EmailLexer extends AbstractLexer { $this->hasInvalidTokens = false; parent::reset(); - $this->previous = $this->token = $this->nullToken; + $this->current = $this->previous = $this->nullToken; } /** @@ -205,19 +205,20 @@ class EmailLexer extends AbstractLexer public function moveNext(): bool { if ($this->hasToRecord && $this->previous === $this->nullToken) { - $this->accumulator .= $this->token->value; + $this->accumulator .= $this->current->value; } - $this->previous = $this->token; + $this->previous = $this->current; if ($this->lookahead === null) { $this->lookahead = $this->nullToken; } $hasNext = parent::moveNext(); + $this->current = $this->token ?? $this->nullToken; if ($this->hasToRecord) { - $this->accumulator .= $this->token->value; + $this->accumulator .= $this->current->value; } return $hasNext; diff --git a/src/EmailParser.php b/src/EmailParser.php index acb4d90..788d016 100644 --- a/src/EmailParser.php +++ b/src/EmailParser.php @@ -36,7 +36,7 @@ class EmailParser extends Parser protected function preLeftParsing(): Result { if (!$this->hasAtToken()) { - return new InvalidEmail(new NoLocalPart(), $this->lexer->token?->value); + return new InvalidEmail(new NoLocalPart(), $this->lexer->current->value); } return new ValidEmail(); } diff --git a/src/MessageIDParser.php b/src/MessageIDParser.php index 761667d..9df27db 100644 --- a/src/MessageIDParser.php +++ b/src/MessageIDParser.php @@ -37,7 +37,7 @@ class MessageIDParser extends Parser protected function preLeftParsing(): Result { if (!$this->hasAtToken()) { - return new InvalidEmail(new NoLocalPart(), $this->lexer->token?->value); + return new InvalidEmail(new NoLocalPart(), $this->lexer->current->value); } return new ValidEmail(); } diff --git a/src/Parser.php b/src/Parser.php index 993ac3f..d577e3e 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -37,7 +37,7 @@ abstract class Parser $this->lexer->setInput($str); if ($this->lexer->hasInvalidTokens()) { - return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->token?->value); + return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->current->value); } $preParsingResult = $this->preLeftParsing(); @@ -73,6 +73,6 @@ abstract class Parser $this->lexer->moveNext(); $this->lexer->moveNext(); - return !$this->lexer->token?->isA(EmailLexer::S_AT); + return !$this->lexer->current->isA(EmailLexer::S_AT); } } diff --git a/src/Parser/Comment.php b/src/Parser/Comment.php index 1032ac3..9e4ab21 100644 --- a/src/Parser/Comment.php +++ b/src/Parser/Comment.php @@ -31,15 +31,15 @@ class Comment extends PartParser public function parse(): Result { - if ($this->lexer->token?->isA(EmailLexer::S_OPENPARENTHESIS)) { + if ($this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS)) { $this->openedParenthesis++; if ($this->noClosingParenthesis()) { - return new InvalidEmail(new UnclosedComment(), $this->lexer->token?->value); + return new InvalidEmail(new UnclosedComment(), $this->lexer->current->value); } } - if ($this->lexer->token?->isA(EmailLexer::S_CLOSEPARENTHESIS)) { - return new InvalidEmail(new UnOpenedComment(), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_CLOSEPARENTHESIS)) { + return new InvalidEmail(new UnOpenedComment(), $this->lexer->current->value); } $this->warnings[WarningComment::CODE] = new WarningComment(); @@ -58,10 +58,10 @@ class Comment extends PartParser } if ($this->openedParenthesis >= 1) { - return new InvalidEmail(new UnclosedComment(), $this->lexer->token?->value); + return new InvalidEmail(new UnclosedComment(), $this->lexer->current->value); } if ($this->openedParenthesis < 0) { - return new InvalidEmail(new UnOpenedComment(), $this->lexer->token?->value); + return new InvalidEmail(new UnOpenedComment(), $this->lexer->current->value); } $finalValidations = $this->commentStrategy->endOfLoopValidations($this->lexer); @@ -78,7 +78,7 @@ class Comment extends PartParser private function warnEscaping(): bool { //Backslash found - if (!$this->lexer->token?->isA(EmailLexer::S_BACKSLASH)) { + if (!$this->lexer->current->isA(EmailLexer::S_BACKSLASH)) { return false; } @@ -87,7 +87,7 @@ class Comment extends PartParser } $this->warnings[QuotedPart::CODE] = - new QuotedPart($this->lexer->getPrevious()->type, $this->lexer->token?->type); + new QuotedPart($this->lexer->getPrevious()->type, $this->lexer->current->type); return true; } diff --git a/src/Parser/CommentStrategy/DomainComment.php b/src/Parser/CommentStrategy/DomainComment.php index edbadec..a197c27 100644 --- a/src/Parser/CommentStrategy/DomainComment.php +++ b/src/Parser/CommentStrategy/DomainComment.php @@ -23,7 +23,7 @@ class DomainComment implements CommentStrategy { //test for end of string if (!$lexer->isNextToken(EmailLexer::S_DOT)) { - return new InvalidEmail(new ExpectingATEXT('DOT not found near CLOSEPARENTHESIS'), $lexer->token?->value); + return new InvalidEmail(new ExpectingATEXT('DOT not found near CLOSEPARENTHESIS'), $lexer->current->value); } //add warning //Address is valid within the message but cannot be used unmodified for the envelope diff --git a/src/Parser/CommentStrategy/LocalComment.php b/src/Parser/CommentStrategy/LocalComment.php index 6efc337..5c18b44 100644 --- a/src/Parser/CommentStrategy/LocalComment.php +++ b/src/Parser/CommentStrategy/LocalComment.php @@ -24,7 +24,7 @@ class LocalComment implements CommentStrategy public function endOfLoopValidations(EmailLexer $lexer): Result { if (!$lexer->isNextToken(EmailLexer::S_AT)) { - return new InvalidEmail(new ExpectingATEXT('ATEX is not expected after closing comments'), $lexer->token?->value); + return new InvalidEmail(new ExpectingATEXT('ATEX is not expected after closing comments'), $lexer->current->value); } $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); return new ValidEmail(); diff --git a/src/Parser/DomainLiteral.php b/src/Parser/DomainLiteral.php index 58fed93..e99498d 100644 --- a/src/Parser/DomainLiteral.php +++ b/src/Parser/DomainLiteral.php @@ -40,14 +40,14 @@ class DomainLiteral extends PartParser $addressLiteral = ''; do { - if ($this->lexer->token?->isA(EmailLexer::C_NUL)) { - return new InvalidEmail(new ExpectingDTEXT(), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::C_NUL)) { + return new InvalidEmail(new ExpectingDTEXT(), $this->lexer->current->value); } $this->addObsoleteWarnings(); if ($this->lexer->isNextTokenAny(array(EmailLexer::S_OPENBRACKET, EmailLexer::S_OPENBRACKET))) { - return new InvalidEmail(new ExpectingDTEXT(), $this->lexer->token?->value); + return new InvalidEmail(new ExpectingDTEXT(), $this->lexer->current->value); } if ($this->lexer->isNextTokenAny( @@ -58,21 +58,21 @@ class DomainLiteral extends PartParser } if ($this->lexer->isNextToken(EmailLexer::S_CR)) { - return new InvalidEmail(new CRNoLF(), $this->lexer->token?->value); + return new InvalidEmail(new CRNoLF(), $this->lexer->current->value); } - if ($this->lexer->token?->isA(EmailLexer::S_BACKSLASH)) { - return new InvalidEmail(new UnusualElements($this->lexer->token?->value), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_BACKSLASH)) { + return new InvalidEmail(new UnusualElements($this->lexer->current->value), $this->lexer->current->value); } - if ($this->lexer->token?->isA(EmailLexer::S_IPV6TAG)) { + if ($this->lexer->current->isA(EmailLexer::S_IPV6TAG)) { $IPv6TAG = true; } - if ($this->lexer->token?->isA(EmailLexer::S_CLOSEBRACKET)) { + if ($this->lexer->current->isA(EmailLexer::S_CLOSEBRACKET)) { break; } - $addressLiteral .= $this->lexer->token?->value; + $addressLiteral .= $this->lexer->current->value; } while ($this->lexer->moveNext()); @@ -189,7 +189,7 @@ class DomainLiteral extends PartParser private function addObsoleteWarnings(): void { - if (in_array($this->lexer->token?->type, self::OBSOLETE_WARNINGS)) { + if (in_array($this->lexer->current->type, self::OBSOLETE_WARNINGS)) { $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); } } diff --git a/src/Parser/DomainPart.php b/src/Parser/DomainPart.php index e7d45d9..9947c42 100644 --- a/src/Parser/DomainPart.php +++ b/src/Parser/DomainPart.php @@ -50,8 +50,8 @@ class DomainPart extends PartParser return $domainChecks; } - if ($this->lexer->token?->isA(EmailLexer::S_AT)) { - return new InvalidEmail(new ConsecutiveAt(), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_AT)) { + return new InvalidEmail(new ConsecutiveAt(), $this->lexer->current->value); } $result = $this->doParseDomainPart(); @@ -69,7 +69,7 @@ class DomainPart extends PartParser $length = strlen($this->domainPart); if ($length > self::DOMAIN_MAX_LENGTH) { - return new InvalidEmail(new DomainTooLong(), $this->lexer->token?->value); + return new InvalidEmail(new DomainTooLong(), $this->lexer->current->value); } return new ValidEmail(); @@ -79,13 +79,13 @@ class DomainPart extends PartParser { $prev = $this->lexer->getPrevious(); if ($prev->isA(EmailLexer::S_DOT)) { - return new InvalidEmail(new DotAtEnd(), $this->lexer->token?->value); + return new InvalidEmail(new DotAtEnd(), $this->lexer->current->value); } if ($prev->isA(EmailLexer::S_HYPHEN)) { return new InvalidEmail(new DomainHyphened('Hypen found at the end of the domain'), $prev->value); } - if ($this->lexer->token?->isA(EmailLexer::S_SP)) { + if ($this->lexer->current->isA(EmailLexer::S_SP)) { return new InvalidEmail(new CRLFAtTheEnd(), $prev->value); } return new ValidEmail(); @@ -103,7 +103,7 @@ class DomainPart extends PartParser return $missingDomain; } - if ($this->lexer->token?->isA(EmailLexer::S_OPENPARENTHESIS)) { + if ($this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS)) { $this->warnings[DeprecatedComment::CODE] = new DeprecatedComment(); } return new ValidEmail(); @@ -111,12 +111,12 @@ class DomainPart extends PartParser private function checkEmptyDomain(): Result { - $thereIsNoDomain = $this->lexer->token?->isA(EmailLexer::S_EMPTY) || - ($this->lexer->token?->isA(EmailLexer::S_SP) && + $thereIsNoDomain = $this->lexer->current->isA(EmailLexer::S_EMPTY) || + ($this->lexer->current->isA(EmailLexer::S_SP) && !$this->lexer->isNextToken(EmailLexer::GENERIC)); if ($thereIsNoDomain) { - return new InvalidEmail(new NoDomainPart(), $this->lexer->token?->value); + return new InvalidEmail(new NoDomainPart(), $this->lexer->current->value); } return new ValidEmail(); @@ -124,11 +124,11 @@ class DomainPart extends PartParser private function checkInvalidTokensAfterAT(): Result { - if ($this->lexer->token?->isA(EmailLexer::S_DOT)) { - return new InvalidEmail(new DotAtStart(), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_DOT)) { + return new InvalidEmail(new DotAtStart(), $this->lexer->current->value); } - if ($this->lexer->token?->isA(EmailLexer::S_HYPHEN)) { - return new InvalidEmail(new DomainHyphened('After AT'), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_HYPHEN)) { + return new InvalidEmail(new DomainHyphened('After AT'), $this->lexer->current->value); } return new ValidEmail(); } @@ -150,14 +150,14 @@ class DomainPart extends PartParser do { $prev = $this->lexer->getPrevious(); - $notAllowedChars = $this->checkNotAllowedChars($this->lexer->token); + $notAllowedChars = $this->checkNotAllowedChars($this->lexer->current); if ($notAllowedChars->isInvalid()) { return $notAllowedChars; } if ( - $this->lexer->token?->isA(EmailLexer::S_OPENPARENTHESIS) || - $this->lexer->token?->isA(EmailLexer::S_CLOSEPARENTHESIS) + $this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS) || + $this->lexer->current->isA(EmailLexer::S_CLOSEPARENTHESIS) ) { $hasComments = true; $commentsResult = $this->parseComments(); @@ -173,7 +173,7 @@ class DomainPart extends PartParser return $dotsResult; } - if ($this->lexer->token?->isA(EmailLexer::S_OPENBRACKET)) { + if ($this->lexer->current->isA(EmailLexer::S_OPENBRACKET)) { $literalResult = $this->parseDomainLiteral(); $this->addTLDWarnings($tldMissing); @@ -190,9 +190,9 @@ class DomainPart extends PartParser return $FwsResult; } - $domain .= $this->lexer->token?->value; + $domain .= $this->lexer->current->value; - if ($this->lexer->token?->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::GENERIC)) { + if ($this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::GENERIC)) { $tldMissing = false; } @@ -201,7 +201,7 @@ class DomainPart extends PartParser return $exceptionsResult; } $this->lexer->moveNext(); - } while (!$this->lexer->token?->isA(EmailLexer::S_EMPTY)); + } while (!$this->lexer->current->isA(EmailLexer::S_EMPTY)); $labelCheck = $this->checkLabelLength(true); if ($labelCheck->isInvalid()) { @@ -235,7 +235,7 @@ class DomainPart extends PartParser try { $this->lexer->find(EmailLexer::S_CLOSEBRACKET); } catch (\RuntimeException $e) { - return new InvalidEmail(new ExpectingDomainLiteralClose(), $this->lexer->token?->value); + return new InvalidEmail(new ExpectingDomainLiteralClose(), $this->lexer->current->value); } $domainLiteralParser = new DomainLiteralParser($this->lexer); @@ -252,19 +252,19 @@ class DomainPart extends PartParser */ protected function checkDomainPartExceptions(Token $prev, bool $hasComments): Result { - if ($this->lexer->token?->isA(EmailLexer::S_OPENBRACKET) && $prev->type !== EmailLexer::S_AT) { - return new InvalidEmail(new ExpectingATEXT('OPENBRACKET not after AT'), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_OPENBRACKET) && $prev->type !== EmailLexer::S_AT) { + return new InvalidEmail(new ExpectingATEXT('OPENBRACKET not after AT'), $this->lexer->current->value); } - if ($this->lexer->token?->isA(EmailLexer::S_HYPHEN) && $this->lexer->isNextToken(EmailLexer::S_DOT)) { - return new InvalidEmail(new DomainHyphened('Hypen found near DOT'), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_HYPHEN) && $this->lexer->isNextToken(EmailLexer::S_DOT)) { + return new InvalidEmail(new DomainHyphened('Hypen found near DOT'), $this->lexer->current->value); } if ( - $this->lexer->token?->isA(EmailLexer::S_BACKSLASH) + $this->lexer->current->isA(EmailLexer::S_BACKSLASH) && $this->lexer->isNextToken(EmailLexer::GENERIC) ) { - return new InvalidEmail(new ExpectingATEXT('Escaping following "ATOM"'), $this->lexer->token?->value); + return new InvalidEmail(new ExpectingATEXT('Escaping following "ATOM"'), $this->lexer->current->value); } return $this->validateTokens($hasComments); @@ -283,8 +283,8 @@ class DomainPart extends PartParser $validDomainTokens[EmailLexer::S_CLOSEPARENTHESIS] = true; } - if (!isset($validDomainTokens[$this->lexer->token?->type])) { - return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->token?->value), $this->lexer->token?->value); + if (!isset($validDomainTokens[$this->lexer->current->type])) { + return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->current->value), $this->lexer->current->value); } return new ValidEmail(); @@ -292,13 +292,13 @@ class DomainPart extends PartParser private function checkLabelLength(bool $isEndOfDomain = false): Result { - if ($this->lexer->token?->isA(EmailLexer::S_DOT) || $isEndOfDomain) { + if ($this->lexer->current->isA(EmailLexer::S_DOT) || $isEndOfDomain) { if ($this->isLabelTooLong($this->label)) { - return new InvalidEmail(new LabelTooLong(), $this->lexer->token?->value); + return new InvalidEmail(new LabelTooLong(), $this->lexer->current->value); } $this->label = ''; } - $this->label .= $this->lexer->token?->value; + $this->label .= $this->lexer->current->value; return new ValidEmail(); } diff --git a/src/Parser/DoubleQuote.php b/src/Parser/DoubleQuote.php index c78978c..b96dba5 100644 --- a/src/Parser/DoubleQuote.php +++ b/src/Parser/DoubleQuote.php @@ -36,19 +36,19 @@ class DoubleQuote extends PartParser $this->lexer->moveNext(); - while (!$this->lexer->token?->isA(EmailLexer::S_DQUOTE) && !$this->lexer->token?->isA(EmailLexer::S_EMPTY)) { - if (isset($special[$this->lexer->token?->type]) && $setSpecialsWarning) { + while (!$this->lexer->current->isA(EmailLexer::S_DQUOTE) && !$this->lexer->current->isA(EmailLexer::S_EMPTY)) { + if (isset($special[$this->lexer->current->type]) && $setSpecialsWarning) { $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); $setSpecialsWarning = false; } - if ($this->lexer->token?->isA(EmailLexer::S_BACKSLASH) && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) { + if ($this->lexer->current->isA(EmailLexer::S_BACKSLASH) && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) { $this->lexer->moveNext(); } $this->lexer->moveNext(); - if (!$this->escaped() && isset($invalid[$this->lexer->token?->type])) { - return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->token?->value); + if (!$this->escaped() && isset($invalid[$this->lexer->current->type])) { + return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->current->value); } } @@ -60,7 +60,7 @@ class DoubleQuote extends PartParser } if (!$this->lexer->isNextToken(EmailLexer::S_AT) && !$prev->isA(EmailLexer::S_BACKSLASH)) { - return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->token?->value); + return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->current->value); } return new ValidEmail(); @@ -72,15 +72,15 @@ class DoubleQuote extends PartParser if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous->isA(EmailLexer::GENERIC)) { $description = 'https://tools.ietf.org/html/rfc5322#section-3.2.4 - quoted string should be a unit'; - return new InvalidEmail(new ExpectingATEXT($description), $this->lexer->token?->value); + return new InvalidEmail(new ExpectingATEXT($description), $this->lexer->current->value); } try { $this->lexer->find(EmailLexer::S_DQUOTE); } catch (\Exception $e) { - return new InvalidEmail(new UnclosedQuotedString(), $this->lexer->token?->value); + return new InvalidEmail(new UnclosedQuotedString(), $this->lexer->current->value); } - $this->warnings[QuotedString::CODE] = new QuotedString($previous->value, $this->lexer->token?->value); + $this->warnings[QuotedString::CODE] = new QuotedString($previous->value, $this->lexer->current->value); return new ValidEmail(); } diff --git a/src/Parser/FoldingWhiteSpace.php b/src/Parser/FoldingWhiteSpace.php index df51b8e..348a7af 100644 --- a/src/Parser/FoldingWhiteSpace.php +++ b/src/Parser/FoldingWhiteSpace.php @@ -37,16 +37,16 @@ class FoldingWhiteSpace extends PartParser return $resultCRLF; } - if ($this->lexer->token?->isA(EmailLexer::S_CR)) { - return new InvalidEmail(new CRNoLF(), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_CR)) { + return new InvalidEmail(new CRNoLF(), $this->lexer->current->value); } if ($this->lexer->isNextToken(EmailLexer::GENERIC) && !$previous->isA(EmailLexer::S_AT)) { - return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->token?->value); + return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->current->value); } - if ($this->lexer->token?->isA(EmailLexer::S_LF) || $this->lexer->token?->isA(EmailLexer::C_NUL)) { - return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_LF) || $this->lexer->current->isA(EmailLexer::C_NUL)) { + return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->current->value); } if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous->isA(EmailLexer::S_AT)) { @@ -60,17 +60,17 @@ class FoldingWhiteSpace extends PartParser protected function checkCRLFInFWS(): Result { - if (!$this->lexer->token?->isA(EmailLexer::CRLF)) { + if (!$this->lexer->current->isA(EmailLexer::CRLF)) { return new ValidEmail(); } if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { - return new InvalidEmail(new CRLFX2(), $this->lexer->token?->value); + return new InvalidEmail(new CRLFX2(), $this->lexer->current->value); } //this has no coverage. Condition is repeated from above one if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { - return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->token?->value); + return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->current->value); } return new ValidEmail(); @@ -82,6 +82,6 @@ class FoldingWhiteSpace extends PartParser return false; } - return in_array($this->lexer->token?->type, self::FWS_TYPES); + return in_array($this->lexer->current->type, self::FWS_TYPES); } } diff --git a/src/Parser/IDLeftPart.php b/src/Parser/IDLeftPart.php index 03c1e0d..bedcf7b 100644 --- a/src/Parser/IDLeftPart.php +++ b/src/Parser/IDLeftPart.php @@ -10,6 +10,6 @@ class IDLeftPart extends LocalPart { protected function parseComments(): Result { - return new InvalidEmail(new CommentsInIDRight(), $this->lexer->token?->value); + return new InvalidEmail(new CommentsInIDRight(), $this->lexer->current->value); } } diff --git a/src/Parser/IDRightPart.php b/src/Parser/IDRightPart.php index 6f41c68..d2fc1d7 100644 --- a/src/Parser/IDRightPart.php +++ b/src/Parser/IDRightPart.php @@ -21,8 +21,8 @@ class IDRightPart extends DomainPart EmailLexer::S_LOWERTHAN => true, ]; - if (isset($invalidDomainTokens[$this->lexer->token?->type])) { - return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->token?->value), $this->lexer->token?->value); + if (isset($invalidDomainTokens[$this->lexer->current->type])) { + return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->current->value), $this->lexer->current->value); } return new ValidEmail(); } diff --git a/src/Parser/LocalPart.php b/src/Parser/LocalPart.php index 2cb54d9..a6087e6 100644 --- a/src/Parser/LocalPart.php +++ b/src/Parser/LocalPart.php @@ -36,12 +36,12 @@ class LocalPart extends PartParser { $this->lexer->startRecording(); - while (!$this->lexer->token?->isA(EmailLexer::S_AT) && !$this->lexer->token?->isA(EmailLexer::S_EMPTY)) { + while (!$this->lexer->current->isA(EmailLexer::S_AT) && !$this->lexer->current->isA(EmailLexer::S_EMPTY)) { if ($this->hasDotAtStart()) { - return new InvalidEmail(new DotAtStart(), $this->lexer->token?->value); + return new InvalidEmail(new DotAtStart(), $this->lexer->current->value); } - if ($this->lexer->token?->isA(EmailLexer::S_DQUOTE)) { + if ($this->lexer->current->isA(EmailLexer::S_DQUOTE)) { $dquoteParsingResult = $this->parseDoubleQuote(); //Invalid double quote parsing @@ -51,8 +51,8 @@ class LocalPart extends PartParser } if ( - $this->lexer->token?->isA(EmailLexer::S_OPENPARENTHESIS) || - $this->lexer->token?->isA(EmailLexer::S_CLOSEPARENTHESIS) + $this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS) || + $this->lexer->current->isA(EmailLexer::S_CLOSEPARENTHESIS) ) { $commentsResult = $this->parseComments(); @@ -62,15 +62,15 @@ class LocalPart extends PartParser } } - if ($this->lexer->token?->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) { - return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) { + return new InvalidEmail(new ConsecutiveDot(), $this->lexer->current->value); } if ( - $this->lexer->token?->isA(EmailLexer::S_DOT) && + $this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_AT) ) { - return new InvalidEmail(new DotAtEnd(), $this->lexer->token?->value); + return new InvalidEmail(new DotAtEnd(), $this->lexer->current->value); } $resultEscaping = $this->validateEscaping(); @@ -102,8 +102,8 @@ class LocalPart extends PartParser protected function validateTokens(bool $hasComments): Result { - if (isset(self::INVALID_TOKENS[$this->lexer->token?->type])) { - return new InvalidEmail(new ExpectingATEXT('Invalid token found'), $this->lexer->token?->value); + if (isset(self::INVALID_TOKENS[$this->lexer->current->type])) { + return new InvalidEmail(new ExpectingATEXT('Invalid token found'), $this->lexer->current->value); } return new ValidEmail(); } @@ -125,7 +125,7 @@ class LocalPart extends PartParser private function hasDotAtStart(): bool { - return $this->lexer->token?->isA(EmailLexer::S_DOT) && $this->lexer->getPrevious()->isA(EmailLexer::S_EMPTY); + return $this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->getPrevious()->isA(EmailLexer::S_EMPTY); } private function parseDoubleQuote(): Result @@ -151,12 +151,12 @@ class LocalPart extends PartParser private function validateEscaping(): Result { //Backslash found - if (!$this->lexer->token?->isA(EmailLexer::S_BACKSLASH)) { + if (!$this->lexer->current->isA(EmailLexer::S_BACKSLASH)) { return new ValidEmail(); } if ($this->lexer->isNextToken(EmailLexer::GENERIC)) { - return new InvalidEmail(new ExpectingATEXT('Found ATOM after escaping'), $this->lexer->token?->value); + return new InvalidEmail(new ExpectingATEXT('Found ATOM after escaping'), $this->lexer->current->value); } if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { diff --git a/src/Parser/PartParser.php b/src/Parser/PartParser.php index e672ea5..7034e0b 100644 --- a/src/Parser/PartParser.php +++ b/src/Parser/PartParser.php @@ -45,8 +45,8 @@ abstract class PartParser protected function checkConsecutiveDots(): Result { - if ($this->lexer->token?->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) { - return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token?->value); + if ($this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) { + return new InvalidEmail(new ConsecutiveDot(), $this->lexer->current->value); } return new ValidEmail(); @@ -57,6 +57,6 @@ abstract class PartParser $previous = $this->lexer->getPrevious(); return $previous->isA(EmailLexer::S_BACKSLASH) - && !$this->lexer->token?->isA(EmailLexer::GENERIC); + && !$this->lexer->current->isA(EmailLexer::GENERIC); } } diff --git a/src/Result/InvalidEmail.php b/src/Result/InvalidEmail.php index 180f4d8..4cc7a2e 100644 --- a/src/Result/InvalidEmail.php +++ b/src/Result/InvalidEmail.php @@ -38,9 +38,8 @@ class InvalidEmail implements Result return $this->reason->code(); } - public function reason() : Reason + public function reason(): Reason { return $this->reason; } - } diff --git a/src/Warning/Warning.php b/src/Warning/Warning.php index 8b39d64..c3f62f6 100644 --- a/src/Warning/Warning.php +++ b/src/Warning/Warning.php @@ -40,7 +40,10 @@ abstract class Warning return $this->rfcNumber; } - public function __toString() + /** + * @return string + */ + public function __toString(): string { return $this->message() . " rfc: " . $this->rfcNumber . "internal code: " . static::CODE; } diff --git a/tests/EmailValidator/EmailLexerTest.php b/tests/EmailValidator/EmailLexerTest.php index 4af25e3..c800e93 100644 --- a/tests/EmailValidator/EmailLexerTest.php +++ b/tests/EmailValidator/EmailLexerTest.php @@ -24,7 +24,7 @@ class EmailLexerTest extends TestCase $lexer->setInput($str); $lexer->moveNext(); $lexer->moveNext(); - $this->assertEquals($token, $lexer->token?->type); + $this->assertEquals($token, $lexer->current->type); } public function testLexerParsesMultipleSpaces() @@ -33,9 +33,9 @@ class EmailLexerTest extends TestCase $lexer->setInput(' '); $lexer->moveNext(); $lexer->moveNext(); - $this->assertEquals(EmailLexer::S_SP, $lexer->token?->type); + $this->assertEquals(EmailLexer::S_SP, $lexer->current->type); $lexer->moveNext(); - $this->assertEquals(EmailLexer::S_SP, $lexer->token?->type); + $this->assertEquals(EmailLexer::S_SP, $lexer->current->type); } /** @@ -48,7 +48,7 @@ class EmailLexerTest extends TestCase $lexer->moveNext(); $lexer->moveNext(); - $this->assertEquals(EmailLexer::INVALID, $lexer->token?->type); + $this->assertEquals(EmailLexer::INVALID, $lexer->current->type); } public function invalidUTF8CharsProvider() @@ -101,7 +101,7 @@ class EmailLexerTest extends TestCase $lexer->moveNext(); $lexer->skipUntil(EmailLexer::S_HTAB); $lexer->moveNext(); - $this->assertEquals(EmailLexer::S_HTAB, $lexer->token?->type); + $this->assertEquals(EmailLexer::S_HTAB, $lexer->current->type); } public function testLexerForUTF8() @@ -110,9 +110,9 @@ class EmailLexerTest extends TestCase $lexer->setInput("áÇ@bar.com"); $lexer->moveNext(); $lexer->moveNext(); - $this->assertEquals(EmailLexer::GENERIC, $lexer->token?->type); + $this->assertEquals(EmailLexer::GENERIC, $lexer->current->type); $lexer->moveNext(); - $this->assertEquals(EmailLexer::GENERIC, $lexer->token?->type); + $this->assertEquals(EmailLexer::GENERIC, $lexer->current->type); } public function testLexerSearchToken()