mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-08-31 12:40:28 +00:00
clean up
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Parser;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Result\Result;
|
||||
use Egulias\EmailValidator\Result\ValidEmail;
|
||||
use Egulias\EmailValidator\Warning\CFWSNearAt;
|
||||
use Egulias\EmailValidator\Result\InvalidEmail;
|
||||
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
|
||||
use Egulias\EmailValidator\Result\Reason\UnclosedComment;
|
||||
use Egulias\EmailValidator\Result\Reason\UnOpenedComment;
|
||||
use Egulias\EmailValidator\Warning\Comment as WarningComment;
|
||||
|
||||
|
||||
class Comment extends Parser
|
||||
{
|
||||
private $MopenedParenthesis = 0;
|
||||
|
||||
public function parse($str)
|
||||
{
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
||||
$this->MopenedParenthesis++;
|
||||
if($this->noClosingParenthesis()) {
|
||||
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
|
||||
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
|
||||
}
|
||||
|
||||
$this->warnings[WarningComment::CODE] = new WarningComment();
|
||||
while (!$this->lexer->isNextToken(EmailLexer::S_AT)) {//!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
|
||||
if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
|
||||
$this->MopenedParenthesis++;
|
||||
}
|
||||
$this->warnEscaping();
|
||||
if($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
|
||||
$this->MopenedParenthesis--;
|
||||
}
|
||||
$this->lexer->moveNext();
|
||||
}
|
||||
|
||||
if($this->MopenedParenthesis >= 1) {
|
||||
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
|
||||
} else if ($this->MopenedParenthesis < 0) {
|
||||
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
|
||||
}
|
||||
|
||||
if (!$this->lexer->isNextToken(EmailLexer::S_AT)) {
|
||||
return new InvalidEmail(new ExpectingATEXT('ATEX is not expected after closing comments'), $this->lexer->token['value']);
|
||||
}
|
||||
|
||||
//You should always end at @
|
||||
$this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
|
||||
return new ValidEmail();
|
||||
}
|
||||
|
||||
private function noClosingParenthesis() : bool
|
||||
{
|
||||
try {
|
||||
$this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
|
||||
return false;
|
||||
} catch (\RuntimeException $e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,9 @@ class LocalPart extends Parser
|
||||
{
|
||||
public function parse($localPart) : Result
|
||||
{
|
||||
$parseDQuote = true;
|
||||
$closingQuote = false;
|
||||
$openedParenthesis = 0;
|
||||
$totalLength = 0;
|
||||
$commentParser = new Comment($this->lexer);
|
||||
|
||||
while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
|
||||
if ($this->hasDotAtStart()) {
|
||||
@@ -28,25 +27,23 @@ class LocalPart extends Parser
|
||||
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_DQUOTE) {
|
||||
$dquoteParsingResult = $this->parseDoubleQuote();
|
||||
$parseDQuote = !$dquoteParsingResult->isValid();
|
||||
|
||||
//Invalid double quote parsing
|
||||
if($parseDQuote) {
|
||||
if(!$dquoteParsingResult->isValid()) {
|
||||
return $dquoteParsingResult;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
||||
$this->parseComments();
|
||||
$openedParenthesis += $this->getOpenedParenthesis();
|
||||
}
|
||||
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
|
||||
if ($openedParenthesis === 0) {
|
||||
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS ||
|
||||
$this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS ) {
|
||||
$result = $commentParser->parse('remove');
|
||||
if(!$result->isValid()) {
|
||||
return $result;
|
||||
}
|
||||
$warns = $commentParser->getWarnings();
|
||||
foreach ($warns as $code => $dWarning) {
|
||||
$this->warnings[$code] = $dWarning;
|
||||
}
|
||||
|
||||
$openedParenthesis--;
|
||||
}
|
||||
|
||||
$this->checkConsecutiveDots();
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Result\Reason;
|
||||
|
||||
class UnclosedComment implements Reason
|
||||
{
|
||||
public function code() : int
|
||||
{
|
||||
return 146;
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'No colosing comment token found';
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ use Egulias\EmailValidator\Result\Reason\NoLocalPart;
|
||||
use Egulias\EmailValidator\Result\Reason\UnOpenedComment;
|
||||
use Egulias\EmailValidator\Result\Reason\DotAtStart as ReasonDotAtStart;
|
||||
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT as ReasonExpectingATEXT;
|
||||
use Egulias\EmailValidator\Result\Reason\UnclosedComment as ReasonUnclosedComment;
|
||||
use Egulias\EmailValidator\Result\Reason\UnclosedQuotedString;
|
||||
|
||||
class RFCValidationTest extends TestCase
|
||||
@@ -199,7 +200,7 @@ class RFCValidationTest extends TestCase
|
||||
[new DotAtStart(), 'example@.localhost'],
|
||||
[new DotAtEnd(), 'example@localhost.'],
|
||||
[new DotAtEnd(), 'example.@example.co.uk'],
|
||||
[new UnclosedComment(), '(example@localhost'],
|
||||
[new InvalidEmail(new ReasonUnclosedComment(), '('), '(example@localhost'],
|
||||
[new InvalidEmail(new UnclosedQuotedString(), '"'), '"example@localhost'],
|
||||
[
|
||||
new InvalidEmail(
|
||||
@@ -207,7 +208,6 @@ class RFCValidationTest extends TestCase
|
||||
'"'),
|
||||
'exa"mple@localhost'
|
||||
],
|
||||
[new UnclosedComment(), '(example@localhost'],
|
||||
[new InvalidEmail(new UnOpenedComment(), ')'), 'comment)example@localhost'],
|
||||
[new InvalidEmail(new UnOpenedComment(), ')'), 'example(comment))@localhost'],
|
||||
[new ExceptionUnopenedComment, 'example@comment)localhost'],
|
||||
|
||||
Reference in New Issue
Block a user