From 3d128082680cdd7d59948c910df333ea6a51fcc2 Mon Sep 17 00:00:00 2001 From: Eduardo Gulias Davis Date: Sun, 4 May 2014 18:53:31 +0200 Subject: [PATCH] Improving code quality --- documentation/Ohter.md | 6 ++++ src/Egulias/EmailValidator/EmailParser.php | 37 +++++++++++----------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/documentation/Ohter.md b/documentation/Ohter.md index 04e5eda..9ec4bd9 100644 --- a/documentation/Ohter.md +++ b/documentation/Ohter.md @@ -61,3 +61,9 @@ http://www.rfc-editor.org/errata_search.php?rfc=1123&eid=1353 However, a valid host name can never have the dotted-decimal form #.#.#.#, since this change does not permit the highest-level component label to start with a digit even if it is not all-numeric. + +Comments +-------- +Comments at the start of the domain are deprecated in the text +Comments at the start of a subdomain are obs-domain +(http://tools.ietf.org/html/rfc5322#section-3.4.1) diff --git a/src/Egulias/EmailValidator/EmailParser.php b/src/Egulias/EmailValidator/EmailParser.php index 4c1d498..bfbba9c 100644 --- a/src/Egulias/EmailValidator/EmailParser.php +++ b/src/Egulias/EmailValidator/EmailParser.php @@ -12,6 +12,7 @@ class EmailParser protected $warnings = array(); protected $domainPart = ''; + protected $lexer; public function __construct(EmailLexer $lexer) { @@ -76,9 +77,7 @@ class EmailParser if ($this->lexer->token['type'] === EmailLexer::S_EMPTY) { throw new \InvalidArgumentException('ERR_NODOMAIN'); } - // Comments at the start of the domain are deprecated in the text - // Comments at the start of a subdomain are obs-domain - // (http://tools.ietf.org/html/rfc5322#section-3.4.1) + if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { $this->warnings[] = EmailValidator::DEPREC_COMMENT; $this->parseComments(); @@ -97,9 +96,9 @@ class EmailParser $this->parseComments(); $this->lexer->moveNext(); } - if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) { - throw new \InvalidArgumentException('ERR_CONSECUTIVEDOTS'); - } + + $this->checkConsecutiveDots(); + if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) { throw new \InvalidArgumentException('ERR_DOMAINHYPHENEND'); } @@ -168,20 +167,16 @@ class EmailParser $IPv6TAG = false; $addressLiteral = ''; if ($this->lexer->isNextToken(EmailLexer::S_COLON)) { - // Address starts with a single colon $this->warnings[] = EmailValidator::RFC5322_IPV6_COLONSTRT; } if ($this->lexer->isNextToken(EmailLexer::S_IPV6TAG)) { - try { - $lexer = clone $this->lexer; - $lexer->moveNext(); - if ($lexer->isNextToken(EmailLexer::S_DOUBLECOLON)) { - $this->warnings[] = EmailValidator::RFC5322_IPV6_COLONSTRT; - } - } catch (\Exception $e) { + $lexer = clone $this->lexer; + $lexer->moveNext(); + if ($lexer->isNextToken(EmailLexer::S_DOUBLECOLON)) { + $this->warnings[] = EmailValidator::RFC5322_IPV6_COLONSTRT; } - } + do { if ($this->lexer->token['type'] === EmailLexer::C_NUL) { throw new \InvalidArgumentException('ERR_EXPECTING_DTEXT'); @@ -336,10 +331,7 @@ class EmailParser $this->parseComments(); } - if ($this->lexer->token['type'] === EmailLexer::S_DOT && - $this->lexer->isNextToken(EmailLexer::S_DOT)) { - throw new \InvalidArgumentException('ERR_CONSECUTIVEDOTS'); - } + $this->checkConsecutiveDots(); if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_AT)) { throw new \InvalidArgumentException('ERR_DOT_END'); @@ -448,4 +440,11 @@ class EmailParser $this->warnings[] = EmailValidator::CFWS_FWS; } } + + private function checkConsecutiveDots() + { + if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) { + throw new \InvalidArgumentException('ERR_CONSECUTIVEDOTS'); + } + } }