From 6c9f1a04502cc8ba891620f17468a4313015616e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 31 Jul 2019 08:33:34 +0200 Subject: [PATCH] Fix PHP 7.4 support for 1.x --- src/Egulias/EmailValidator/EmailLexer.php | 16 +++++++++++++++- src/Egulias/EmailValidator/Parser/DomainPart.php | 2 +- src/Egulias/EmailValidator/Parser/LocalPart.php | 11 +++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/Egulias/EmailValidator/EmailLexer.php b/src/Egulias/EmailValidator/EmailLexer.php index 882c968..70b26de 100644 --- a/src/Egulias/EmailValidator/EmailLexer.php +++ b/src/Egulias/EmailValidator/EmailLexer.php @@ -77,10 +77,22 @@ class EmailLexer extends AbstractLexer protected $previous; + private static $nullToken = array( + 'value' => '', + 'type' => null, + 'position' => 0, + ); + + public function __construct() + { + $this->previous = $this->token = self::$nullToken; + } + public function reset() { $this->hasInvalidTokens = false; parent::reset(); + $this->previous = $this->token = self::$nullToken; } public function hasInvalidTokens() @@ -122,8 +134,10 @@ public function getPrevious() public function moveNext() { $this->previous = $this->token; + $hasNext = parent::moveNext(); + $this->token = $this->token ?: self::$nullToken; - return parent::moveNext(); + return $hasNext; } /** diff --git a/src/Egulias/EmailValidator/Parser/DomainPart.php b/src/Egulias/EmailValidator/Parser/DomainPart.php index 66a51fb..a680039 100644 --- a/src/Egulias/EmailValidator/Parser/DomainPart.php +++ b/src/Egulias/EmailValidator/Parser/DomainPart.php @@ -141,7 +141,7 @@ protected function doParseDomainPart() $domain .= $this->lexer->token['value']; $this->lexer->moveNext(); - } while ($this->lexer->token); + } while (null !== $this->lexer->token['type']); return $domain; } diff --git a/src/Egulias/EmailValidator/Parser/LocalPart.php b/src/Egulias/EmailValidator/Parser/LocalPart.php index 449748f..0cbbcdb 100644 --- a/src/Egulias/EmailValidator/Parser/LocalPart.php +++ b/src/Egulias/EmailValidator/Parser/LocalPart.php @@ -13,9 +13,12 @@ public function parse($localPart) $closingQuote = false; $openedParenthesis = 0; - while ($this->lexer->token['type'] !== EmailLexer::S_AT && $this->lexer->token) { - if ($this->lexer->token['type'] === EmailLexer::S_DOT && !$this->lexer->getPrevious()) { - throw new \InvalidArgumentException('ERR_DOT_START'); + while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { + if ($this->lexer->token['type'] === EmailLexer::S_DOT) { + $previous = $this->lexer->getPrevious(); + if (null === $previous['type']) { + throw new \InvalidArgumentException('ERR_DOT_START'); + } } $closingQuote = $this->checkDQUOTE($closingQuote); @@ -78,7 +81,7 @@ protected function parseDoubleQuote() $this->lexer->moveNext(); - while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && $this->lexer->token) { + while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) { $parseAgain = false; if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) { $this->warnings[] = EmailValidator::CFWS_FWS;