diff --git a/src/EmailLexer.php b/src/EmailLexer.php index 28d631f..0db4d06 100644 --- a/src/EmailLexer.php +++ b/src/EmailLexer.php @@ -140,9 +140,7 @@ class EmailLexer extends AbstractLexer /** * The last matched/seen token. * - * @var Token - * - * @psalm-suppress NonInvariantDocblockPropertyType + * @var Token|null */ public Token|null $token; diff --git a/src/EmailParser.php b/src/EmailParser.php index b72a731..acb4d90 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->token?->value); } return new ValidEmail(); } diff --git a/src/MessageIDParser.php b/src/MessageIDParser.php index a80b01a..761667d 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->token?->value); } return new ValidEmail(); } diff --git a/src/Parser.php b/src/Parser.php index c007c49..993ac3f 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->token?->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->token?->isA(EmailLexer::S_AT); } } diff --git a/src/Parser/Comment.php b/src/Parser/Comment.php index 1f66577..1032ac3 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->token?->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->token?->value); } } - if ($this->lexer->token->isA(EmailLexer::S_CLOSEPARENTHESIS)) { - return new InvalidEmail(new UnOpenedComment(), $this->lexer->token->value); + if ($this->lexer->token?->isA(EmailLexer::S_CLOSEPARENTHESIS)) { + return new InvalidEmail(new UnOpenedComment(), $this->lexer->token?->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->token?->value); } if ($this->openedParenthesis < 0) { - return new InvalidEmail(new UnOpenedComment(), $this->lexer->token->value); + return new InvalidEmail(new UnOpenedComment(), $this->lexer->token?->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->token?->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->token?->type); return true; } diff --git a/src/Parser/CommentStrategy/DomainComment.php b/src/Parser/CommentStrategy/DomainComment.php index 339f06b..edbadec 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->token?->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 75cfa68..6efc337 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->token?->value); } $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); return new ValidEmail(); diff --git a/src/Parser/DomainLiteral.php b/src/Parser/DomainLiteral.php index 962b9fd..58fed93 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->token?->isA(EmailLexer::C_NUL)) { + return new InvalidEmail(new ExpectingDTEXT(), $this->lexer->token?->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->token?->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->token?->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->token?->isA(EmailLexer::S_BACKSLASH)) { + return new InvalidEmail(new UnusualElements($this->lexer->token?->value), $this->lexer->token?->value); } - if ($this->lexer->token->isA(EmailLexer::S_IPV6TAG)) { + if ($this->lexer->token?->isA(EmailLexer::S_IPV6TAG)) { $IPv6TAG = true; } - if ($this->lexer->token->isA(EmailLexer::S_CLOSEBRACKET)) { + if ($this->lexer->token?->isA(EmailLexer::S_CLOSEBRACKET)) { break; } - $addressLiteral .= $this->lexer->token->value; + $addressLiteral .= $this->lexer->token?->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->token?->type, self::OBSOLETE_WARNINGS)) { $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); } } diff --git a/src/Parser/DomainPart.php b/src/Parser/DomainPart.php index ac6fd35..e7d45d9 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->token?->isA(EmailLexer::S_AT)) { + return new InvalidEmail(new ConsecutiveAt(), $this->lexer->token?->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->token?->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->token?->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->token?->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->token?->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->token?->isA(EmailLexer::S_EMPTY) || + ($this->lexer->token?->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->token?->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->token?->isA(EmailLexer::S_DOT)) { + return new InvalidEmail(new DotAtStart(), $this->lexer->token?->value); } - if ($this->lexer->token->isA(EmailLexer::S_HYPHEN)) { - return new InvalidEmail(new DomainHyphened('After AT'), $this->lexer->token->value); + if ($this->lexer->token?->isA(EmailLexer::S_HYPHEN)) { + return new InvalidEmail(new DomainHyphened('After AT'), $this->lexer->token?->value); } return new ValidEmail(); } @@ -156,8 +156,8 @@ class DomainPart extends PartParser } if ( - $this->lexer->token->isA(EmailLexer::S_OPENPARENTHESIS) || - $this->lexer->token->isA(EmailLexer::S_CLOSEPARENTHESIS) + $this->lexer->token?->isA(EmailLexer::S_OPENPARENTHESIS) || + $this->lexer->token?->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->token?->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->token?->value; - if ($this->lexer->token->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::GENERIC)) { + if ($this->lexer->token?->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->token?->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->token?->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->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->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->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->token->isA(EmailLexer::S_BACKSLASH) + $this->lexer->token?->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->token?->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->token?->type])) { + return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->token?->value), $this->lexer->token?->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->token?->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->token?->value); } $this->label = ''; } - $this->label .= $this->lexer->token->value; + $this->label .= $this->lexer->token?->value; return new ValidEmail(); } diff --git a/src/Parser/DoubleQuote.php b/src/Parser/DoubleQuote.php index 9295066..c78978c 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->token?->isA(EmailLexer::S_DQUOTE) && !$this->lexer->token?->isA(EmailLexer::S_EMPTY)) { + if (isset($special[$this->lexer->token?->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->token?->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->token?->type])) { + return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->token?->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->token?->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->token?->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->token?->value); } - $this->warnings[QuotedString::CODE] = new QuotedString($previous->value, $this->lexer->token->value); + $this->warnings[QuotedString::CODE] = new QuotedString($previous->value, $this->lexer->token?->value); return new ValidEmail(); } diff --git a/src/Parser/FoldingWhiteSpace.php b/src/Parser/FoldingWhiteSpace.php index b6ded56..df51b8e 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->token?->isA(EmailLexer::S_CR)) { + return new InvalidEmail(new CRNoLF(), $this->lexer->token?->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->token?->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->token?->isA(EmailLexer::S_LF) || $this->lexer->token?->isA(EmailLexer::C_NUL)) { + return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->token?->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->token?->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->token?->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->token?->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->token?->type, self::FWS_TYPES); } } diff --git a/src/Parser/IDLeftPart.php b/src/Parser/IDLeftPart.php index 20707cf..03c1e0d 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->token?->value); } } diff --git a/src/Parser/IDRightPart.php b/src/Parser/IDRightPart.php index 47e5284..6f41c68 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->token?->type])) { + return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->token?->value), $this->lexer->token?->value); } return new ValidEmail(); } diff --git a/src/Parser/LocalPart.php b/src/Parser/LocalPart.php index d42873f..2cb54d9 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->token?->isA(EmailLexer::S_AT) && !$this->lexer->token?->isA(EmailLexer::S_EMPTY)) { if ($this->hasDotAtStart()) { - return new InvalidEmail(new DotAtStart(), $this->lexer->token->value); + return new InvalidEmail(new DotAtStart(), $this->lexer->token?->value); } - if ($this->lexer->token->isA(EmailLexer::S_DQUOTE)) { + if ($this->lexer->token?->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->token?->isA(EmailLexer::S_OPENPARENTHESIS) || + $this->lexer->token?->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->token?->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) { + return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token?->value); } if ( - $this->lexer->token->isA(EmailLexer::S_DOT) && + $this->lexer->token?->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->token?->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->token?->type])) { + return new InvalidEmail(new ExpectingATEXT('Invalid token found'), $this->lexer->token?->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->token?->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->token?->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->token?->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 35418de..e672ea5 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->token?->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) { + return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token?->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->token?->isA(EmailLexer::GENERIC); } } diff --git a/tests/EmailValidator/EmailLexerTest.php b/tests/EmailValidator/EmailLexerTest.php index ad0b07c..4af25e3 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->token?->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->token?->type); $lexer->moveNext(); - $this->assertEquals(EmailLexer::S_SP, $lexer->token->type); + $this->assertEquals(EmailLexer::S_SP, $lexer->token?->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->token?->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->token?->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->token?->type); $lexer->moveNext(); - $this->assertEquals(EmailLexer::GENERIC, $lexer->token->type); + $this->assertEquals(EmailLexer::GENERIC, $lexer->token?->type); } public function testLexerSearchToken()