Skip to content

Commit

Permalink
Port @vajexal #278
Browse files Browse the repository at this point in the history
  • Loading branch information
egulias committed Dec 28, 2020
1 parent 830c69e commit f72a7a1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
60 changes: 42 additions & 18 deletions src/Parser/DomainPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ class DomainPart extends Parser
const DOMAIN_MAX_LENGTH = 253;
const LABEL_MAX_LENGTH = 63;


/**
* @var string
*/
protected $domainPart = '';

/**
* @var string
*/
protected $label = '';

public function parse() : Result
{
$this->lexer->moveNext();
Expand Down Expand Up @@ -174,18 +178,21 @@ protected function doParseDomainPart() : Result
$literalResult = $this->parseDomainLiteral();

$this->addTLDWarnings($tldMissing);
//Invalid literal parsing
//if($literalResult->isInvalid()) {
// return $literalResult;
//}
return $literalResult;
}

$labelCheck = $this->checkLabelLength($prev);
if ($labelCheck->isInvalid()) {
return $labelCheck;
if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
$labelCheck = $this->checkLabelLength($this->label);
$this->label = '';
if ($labelCheck->isInvalid()) {
return $labelCheck;
}
} else {
$this->label .= $this->lexer->token['value'];
}

//$labelCheck = $this->checkLabelLength($prev);

$FwsResult = $this->parseFWS();
if($FwsResult->isInvalid()) {
return $FwsResult;
Expand All @@ -202,12 +209,14 @@ protected function doParseDomainPart() : Result
return $exceptionsResult;
}
$this->lexer->moveNext();
//if ($this->lexer->token['type'] === EmailLexer::S_SP) {
// return new InvalidEmail(new CharNotAllowed(), $this->lexer->token['value']);
//}

} while (null !== $this->lexer->token['type']);

$labelCheck = $this->checkLabelLength($this->label);
$this->label = '';
if ($labelCheck->isInvalid()) {
return $labelCheck;
}
$this->addTLDWarnings($tldMissing);

$this->domainPart = $domain;
Expand Down Expand Up @@ -277,18 +286,33 @@ protected function checkDomainPartExceptions(array $prev, bool $hasComments) : R
return new ValidEmail();
}

protected function checkLabelLength(array $prev) : Result

private function checkLabelLength(string $label) : Result
{
if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
$prev['type'] === EmailLexer::GENERIC &&
strlen($prev['value']) > self::LABEL_MAX_LENGTH
) {
//$this->warnings[LabelTooLong::CODE] = new LabelTooLong();
return new InvalidEmail(new LabelTooLong(), $this->lexer->token['value']);
if ($this->isLabelTooLong($label)) {
return new InvalidEmail(new LabelTooLong(), $this->lexer->token['value']);
}
return new ValidEmail();
}


private function isLabelTooLong(string $label) : bool
{
if (preg_match('/[^\x00-\x7F]/', $label)) {
idn_to_ascii(utf8_decode($label), IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG);
}
return strlen($label) > self::LABEL_MAX_LENGTH;
}
//if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
// $prev['type'] === EmailLexer::GENERIC &&
// strlen($prev['value']) > self::LABEL_MAX_LENGTH
//) {
// return new InvalidEmail(new LabelTooLong(), $this->lexer->token['value']);
//}
//return new ValidEmail();
//}

private function addTLDWarnings(bool $isTLDMissing) : void
{
if ($isTLDMissing) {
Expand Down
14 changes: 13 additions & 1 deletion tests/EmailValidator/Validation/NoRFCWarningsValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,23 @@ public function testEmailWithWarningsIsInvalid()
$this->assertInstanceOf(RFCWarnings::class, $validation->getError()->reason());
}

public function testEmailWithoutWarningsIsValid()
/**
* @dataProvider getValidEmailsWithoutWarnings
*/
public function testEmailWithoutWarningsIsValid($email)
{
$validation = new NoRFCWarningsValidation();

$this->assertTrue($validation->isValid('example@example.com', new EmailLexer()));
$this->assertTrue($validation->isValid($email, new EmailLexer()));
$this->assertNull($validation->getError());
}

public function getValidEmailsWithoutWarnings()
{
return [
['example@example.com',],
[sprintf('example@%s.com', str_repeat('ъ', 40)),],
];
}
}
12 changes: 8 additions & 4 deletions tests/EmailValidator/Validation/RFCValidationDomainPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@
namespace Egulias\EmailValidator\Tests\EmailValidator\Validation;

use PHPUnit\Framework\TestCase;
use Egulias\EmailValidator\Validation\RFCValidation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Warning\TLD;
use Egulias\EmailValidator\Warning\Comment;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\IPV6BadChar;
use Egulias\EmailValidator\Result\Reason\CRNoLF;
use Egulias\EmailValidator\Warning\IPV6ColonEnd;
use Egulias\EmailValidator\Warning\DomainLiteral;
use Egulias\EmailValidator\Warning\IPV6MaxGroups;
use Egulias\EmailValidator\Warning\ObsoleteDTEXT;
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
use Egulias\EmailValidator\Warning\AddressLiteral;
use Egulias\EmailValidator\Warning\IPV6ColonStart;
use Egulias\EmailValidator\Warning\IPV6Deprecated;
use Egulias\EmailValidator\Warning\IPV6GroupCount;
use Egulias\EmailValidator\Warning\IPV6DoubleColon;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\DotAtStart;
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
use Egulias\EmailValidator\Validation\RFCValidation;
use Egulias\EmailValidator\Result\Reason\LabelTooLong;
use Egulias\EmailValidator\Result\Reason\NoDomainPart;
use Egulias\EmailValidator\Result\Reason\ConsecutiveAt;
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
use Egulias\EmailValidator\Result\Reason\DomainHyphened;
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
use Egulias\EmailValidator\Result\Reason\ExpectingDTEXT;
use Egulias\EmailValidator\Result\Reason\UnOpenedComment;
use Egulias\EmailValidator\Result\Reason\CRNoLF;


class RFCValidationDomainPartTest extends TestCase
Expand Down Expand Up @@ -178,6 +179,9 @@ public function getInvalidEmailsWithErrors()
[new InvalidEmail(new CRNoLF(), "["), "example@[\r]"],
[new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ,'), ','), 'example@exam,ple.com'],
[new InvalidEmail(new ExpectingATEXT("Invalid token in domain: '"), "'"), "test@example.com'"],
[new InvalidEmail(new LabelTooLong(), "."), sprintf('example@%s.com', str_repeat('ъ', 64))],
[new InvalidEmail(new LabelTooLong(), "."), sprintf('example@%s.com', str_repeat('a4t', 22))],
[new InvalidEmail(new LabelTooLong(), ""), sprintf('example@%s', str_repeat('a4t', 22))],
];
}

Expand Down
1 change: 1 addition & 0 deletions tests/EmailValidator/Validation/RFCValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function getValidEmails()
['"\""@iana.org'],
['müller@möller.de'],
["1500111@профи-инвест.рф"],
[sprintf('example@%s.com', str_repeat('ъ', 40))],
);
}

Expand Down

0 comments on commit f72a7a1

Please sign in to comment.