Adjusting duplicated methods

This commit is contained in:
Eduardo Gulias Davis
2020-09-12 16:25:24 +02:00
parent bad6b501d5
commit 0ba6f87f8e
5 changed files with 47 additions and 71 deletions
+22
View File
@@ -5,6 +5,7 @@ namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Warning\CFWSNearAt;
use Egulias\EmailValidator\Warning\QuotedPart;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Parser\CommentStrategy;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
@@ -65,6 +66,27 @@ class Comment extends Parser
return $finalValidations;
}
/**
* @return bool
*/
private function warnEscaping() : bool
{
//Backslash found
if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
return false;
}
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
return false;
}
$this->warnings[QuotedPart::CODE] =
new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
return true;
}
private function noClosingParenthesis() : bool
{
try {
-13
View File
@@ -76,17 +76,4 @@ class FoldingWhiteSpace extends Parser
$this->lexer->token['type'] === EmailLexer::S_LF ||
$this->lexer->token['type'] === EmailLexer::CRLF;
}
/**
* @return bool
*/
protected function escaped()
{
$previous = $this->lexer->getPrevious();
return $previous && $previous['type'] === EmailLexer::S_BACKSLASH
&&
$this->lexer->token['type'] !== EmailLexer::GENERIC;
}
}
+22 -4
View File
@@ -91,7 +91,7 @@ class LocalPart extends Parser
return new ValidEmail();
}
protected function parseLocalFWS() : Result
private function parseLocalFWS() : Result
{
//use $this->parseFWS()
$foldingWS = new FoldingWhiteSpace($this->lexer);
@@ -102,12 +102,12 @@ class LocalPart extends Parser
return $resultFWS;
}
protected function hasDotAtStart() : bool
private function hasDotAtStart() : bool
{
return $this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type'];
}
protected function parseDoubleQuote() : Result
private function parseDoubleQuote() : Result
{
$dquoteParser = new DoubleQuote($this->lexer);
$parseAgain = $dquoteParser->parse("remove useless arg");
@@ -116,7 +116,7 @@ class LocalPart extends Parser
return $parseAgain;
}
protected function parseComments()
private function parseComments()
{
$commentParser = new Comment($this->lexer, new LocalComment());
$result = $commentParser->parse('remove');
@@ -126,4 +126,22 @@ class LocalPart extends Parser
}
return $result;
}
private function validateEscaping() : Result
{
//Backslash found
if ($this->lexer->token['type'] !== 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']);
}
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
return new ValidEmail();
}
return new ValidEmail();
}
}
+1 -52
View File
@@ -3,15 +3,10 @@
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\CRLFAtTheEnd;
use Egulias\EmailValidator\Exception\CRLFX2;
use Egulias\EmailValidator\Exception\ExpectingATEXT;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT as ReasonExpectingATEXT;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Warning\QuotedPart;
abstract class Parser
{
@@ -63,7 +58,7 @@ abstract class Parser
/**
* @return bool
*/
protected function escaped()
protected function escaped() : bool
{
$previous = $this->lexer->getPrevious();
@@ -71,50 +66,4 @@ abstract class Parser
&&
$this->lexer->token['type'] !== EmailLexer::GENERIC;
}
/**
* @return bool
*/
protected function warnEscaping() : bool
{
//Backslash found
if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
return false;
}
if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
throw new ExpectingATEXT();
}
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
return false;
}
$this->warnings[QuotedPart::CODE] =
new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
return true;
}
protected function validateEscaping() : Result
{
//Backslash found
if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
return new ValidEmail();
}
if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
return new InvalidEmail(new ReasonExpectingATEXT('Found ATOM after escaping'), $this->lexer->token['value']);
}
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
return new ValidEmail();
}
$this->warnings[QuotedPart::CODE] =
new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
return new ValidEmail();
}
}
@@ -3,7 +3,7 @@
namespace Egulias\EmailValidator\Tests\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\Reason\NoDomainPart as ReasonNoDomainPart;
use Egulias\EmailValidator\Result\Reason\NoDomainPart;
use Egulias\EmailValidator\Result\Reason\RFCWarnings;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
use PHPUnit\Framework\TestCase;
@@ -15,7 +15,7 @@ class NoRFCWarningsValidationTest extends TestCase
$validation = new NoRFCWarningsValidation();
$this->assertFalse($validation->isValid('non-email-string', new EmailLexer()));
$this->assertInstanceOf(ReasonNoDomainPart::class, $validation->getError()->reason());
$this->assertInstanceOf(NoDomainPart::class, $validation->getError()->reason());
}
public function testEmailWithWarningsIsInvalid()