Compare commits

..

1 Commits

Author SHA1 Message Date
Alexander M. Turek 5f35e41eba Allow doctrine/lexer 2 (#345) 2022-12-30 15:09:25 +01:00
68 changed files with 620 additions and 435 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ imports:
build:
environment:
php: '7.3'
php: '7.4'
tests:
override:
-
-9
View File
@@ -2,10 +2,6 @@ language: php
matrix:
include:
- php: 7.3
env:
- psalm=yes
dist: bionic
- php: 7.4
dist: bionic
env:
@@ -15,11 +11,6 @@ matrix:
- psalm=yes
dist: bionic
#ppc64le support code
- php: 7.3
arch: ppc64le
env:
- psalm=yes
dist: bionic
- php: 7.4
arch: ppc64le
env:
+1 -1
View File
@@ -5,7 +5,7 @@
* Access to local part and domain part from EmailParser
* Validations outside of the scope of the RFC will be considered "extra" validations, thus opening the door for adding new; will live in their own folder "extra" (as requested in #248, #195, #183).
## Breacking changes
## Breaking changes
* PHP version upgraded to match Symfony's (as of 12/2020).
* DNSCheckValidation now fails for missing MX records. While the RFC argues that the existence of only A records to be valid, starting in v3 they will be considered invalid.
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright (c) 2013-2021 Eduardo Gulias Davis
Copyright (c) 2013-2022 Eduardo Gulias Davis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
+1 -1
View File
@@ -14,7 +14,7 @@
},
"require": {
"php": ">=7.2",
"doctrine/lexer": "^1.2",
"doctrine/lexer": "^1.2|^2",
"symfony/polyfill-intl-idn": "^1.15"
},
"require-dev": {
Generated
+347 -248
View File
File diff suppressed because it is too large Load Diff
+5 -16
View File
@@ -1,19 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.8.3@389af1bfc739bfdff3f9e3dc7bd6499aee51a831">
<file src="src/EmailLexer.php">
<DocblockTypeContradiction occurrences="1">
<code>self::$nullToken</code>
</DocblockTypeContradiction>
</file>
<file src="src/Parser/Parser.php">
<MissingReturnType occurrences="1">
<code>parse</code>
</MissingReturnType>
</file>
<file src="src/Validation/SpoofCheckValidation.php">
<UndefinedClass occurrences="2">
<code>Spoofchecker</code>
<code>Spoofchecker</code>
</UndefinedClass>
<files psalm-version="5.4.0@62db5d4f6a7ae0a20f7cc5a4952d730272fc0863">
<file src="src/Parser/DomainPart.php">
<RedundantConditionGivenDocblockType occurrences="1">
<code>null !== $this-&gt;lexer-&gt;token['type']</code>
</RedundantConditionGivenDocblockType>
</file>
</files>
+7 -2
View File
@@ -1,10 +1,9 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config ./vendor/vimeo/psalm/config.xsd"
errorBaseline="./psalm.baseline.xml"
errorBaseline="psalm.baseline.xml"
>
<projectFiles>
<directory name="src" />
@@ -14,5 +13,11 @@
</projectFiles>
<issueHandlers>
<DeprecatedMethod>
<errorLevel type="suppress">
<!-- This issue needs to be resolved before upgrading to Lexer 3 -->
<referencedMethod name="Doctrine\Common\Lexer\Token::offsetGet"/>
</errorLevel>
</DeprecatedMethod>
</issueHandlers>
</psalm>
+67 -59
View File
@@ -3,58 +3,62 @@
namespace Egulias\EmailValidator;
use Doctrine\Common\Lexer\AbstractLexer;
use Doctrine\Common\Lexer\Token;
/**
* @extends AbstractLexer<int, string>
*/
class EmailLexer extends AbstractLexer
{
//ASCII values
const S_EMPTY = null;
const C_NUL = 0;
const S_HTAB = 9;
const S_LF = 10;
const S_CR = 13;
const S_SP = 32;
const EXCLAMATION = 33;
const S_DQUOTE = 34;
const NUMBER_SIGN = 35;
const DOLLAR = 36;
const PERCENTAGE = 37;
const AMPERSAND = 38;
const S_SQUOTE = 39;
const S_OPENPARENTHESIS = 40;
const S_CLOSEPARENTHESIS = 41;
const ASTERISK = 42;
const S_PLUS = 43;
const S_COMMA = 44;
const S_HYPHEN = 45;
const S_DOT = 46;
const S_SLASH = 47;
const S_COLON = 58;
const S_SEMICOLON = 59;
const S_LOWERTHAN = 60;
const S_EQUAL = 61;
const S_GREATERTHAN = 62;
const QUESTIONMARK = 63;
const S_AT = 64;
const S_OPENBRACKET = 91;
const S_BACKSLASH = 92;
const S_CLOSEBRACKET = 93;
const CARET = 94;
const S_UNDERSCORE = 95;
const S_BACKTICK = 96;
const S_OPENCURLYBRACES = 123;
const S_PIPE = 124;
const S_CLOSECURLYBRACES = 125;
const S_TILDE = 126;
const C_DEL = 127;
const INVERT_QUESTIONMARK= 168;
const INVERT_EXCLAMATION = 173;
const GENERIC = 300;
const S_IPV6TAG = 301;
const INVALID = 302;
const CRLF = 1310;
const S_DOUBLECOLON = 5858;
const ASCII_INVALID_FROM = 127;
const ASCII_INVALID_TO = 199;
public const S_EMPTY = null;
public const C_NUL = 0;
public const S_HTAB = 9;
public const S_LF = 10;
public const S_CR = 13;
public const S_SP = 32;
public const EXCLAMATION = 33;
public const S_DQUOTE = 34;
public const NUMBER_SIGN = 35;
public const DOLLAR = 36;
public const PERCENTAGE = 37;
public const AMPERSAND = 38;
public const S_SQUOTE = 39;
public const S_OPENPARENTHESIS = 40;
public const S_CLOSEPARENTHESIS = 41;
public const ASTERISK = 42;
public const S_PLUS = 43;
public const S_COMMA = 44;
public const S_HYPHEN = 45;
public const S_DOT = 46;
public const S_SLASH = 47;
public const S_COLON = 58;
public const S_SEMICOLON = 59;
public const S_LOWERTHAN = 60;
public const S_EQUAL = 61;
public const S_GREATERTHAN = 62;
public const QUESTIONMARK = 63;
public const S_AT = 64;
public const S_OPENBRACKET = 91;
public const S_BACKSLASH = 92;
public const S_CLOSEBRACKET = 93;
public const CARET = 94;
public const S_UNDERSCORE = 95;
public const S_BACKTICK = 96;
public const S_OPENCURLYBRACES = 123;
public const S_PIPE = 124;
public const S_CLOSECURLYBRACES = 125;
public const S_TILDE = 126;
public const C_DEL = 127;
public const INVERT_QUESTIONMARK= 168;
public const INVERT_EXCLAMATION = 173;
public const GENERIC = 300;
public const S_IPV6TAG = 301;
public const INVALID = 302;
public const CRLF = 1310;
public const S_DOUBLECOLON = 5858;
public const ASCII_INVALID_FROM = 127;
public const ASCII_INVALID_TO = 199;
/**
* US-ASCII visible characters not valid for atext (@link http://tools.ietf.org/html/rfc5322#section-3.2.3)
@@ -107,11 +111,11 @@ class EmailLexer extends AbstractLexer
'¡' => self::INVERT_EXCLAMATION,
];
const INVALID_CHARS_REGEX = "/[^\p{S}\p{C}\p{Cc}]+/iu";
public const INVALID_CHARS_REGEX = "/[^\p{S}\p{C}\p{Cc}]+/iu";
const VALID_UTF8_REGEX = '/\p{Cc}+/u';
public const VALID_UTF8_REGEX = '/\p{Cc}+/u';
const CATCHABLE_PATTERNS = [
public const CATCHABLE_PATTERNS = [
'[a-zA-Z]+[46]?', //ASCII and domain literal
'[^\x00-\x7F]', //UTF-8
'[0-9]+',
@@ -121,11 +125,11 @@ class EmailLexer extends AbstractLexer
'.',
];
const NON_CATCHABLE_PATTERNS = [
public const NON_CATCHABLE_PATTERNS = [
'[\xA0-\xff]+',
];
const MODIFIERS = 'iu';
public const MODIFIERS = 'iu';
/** @var bool */
protected $hasInvalidTokens = false;
@@ -140,18 +144,20 @@ class EmailLexer extends AbstractLexer
/**
* The last matched/seen token.
*
* @var array
* @var array|Token
*
* @psalm-suppress NonInvariantDocblockPropertyType
* @psalm-var array{value:string, type:null|int, position:int}
* @psalm-suppress NonInvariantDocblockPropertyType
* @psalm-var array{value:string, type:null|int, position:int}|Token<int, string>
*/
public $token;
/**
* The next token in the input.
*
* @var array|null
* @var array|Token|null
*
* @psalm-suppress NonInvariantDocblockPropertyType
* @psalm-var array{position: int, type: int|null|string, value: int|string}|Token<int, string>|null
*/
public $lookahead;
@@ -210,7 +216,9 @@ class EmailLexer extends AbstractLexer
$this->accumulator .= $this->token['value'];
}
$this->previous = $this->token;
$this->previous = $this->token instanceof Token
? ['value' => $this->token->value, 'type' => $this->token->type, 'position' => $this->token->position]
: $this->token;
if($this->lookahead === null) {
$this->lookahead = self::$nullToken;
@@ -237,7 +245,7 @@ class EmailLexer extends AbstractLexer
$encoded = $value;
if (mb_detect_encoding($value, 'auto', true) !== 'UTF-8') {
$encoded = utf8_encode($value);
$encoded = mb_convert_encoding($value, 'UTF-8', 'Windows-1252');
}
if ($this->isValid($encoded)) {
+1 -2
View File
@@ -2,7 +2,6 @@
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Parser\LocalPart;
use Egulias\EmailValidator\Parser\DomainPart;
@@ -13,7 +12,7 @@ use Egulias\EmailValidator\Result\Reason\NoLocalPart;
class EmailParser extends Parser
{
const EMAIL_MAX_LENGTH = 254;
public const EMAIL_MAX_LENGTH = 254;
/**
* @var string
+2 -3
View File
@@ -2,7 +2,6 @@
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Parser\IDLeftPart;
use Egulias\EmailValidator\Parser\IDRightPart;
@@ -14,7 +13,7 @@ use Egulias\EmailValidator\Result\Reason\NoLocalPart;
class MessageIDParser extends Parser
{
const EMAILID_MAX_LENGTH = 254;
public const EMAILID_MAX_LENGTH = 254;
/**
* @var string
@@ -89,4 +88,4 @@ class MessageIDParser extends Parser
$this->warnings[EmailTooLong::CODE] = new EmailTooLong();
}
}
}
}
+3 -2
View File
@@ -59,7 +59,8 @@ class Comment extends PartParser
if($this->openedParenthesis >= 1) {
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
} else if ($this->openedParenthesis < 0) {
}
if ($this->openedParenthesis < 0) {
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
}
@@ -100,4 +101,4 @@ class Comment extends PartParser
return true;
}
}
}
}
@@ -15,4 +15,4 @@ interface CommentStrategy
public function endOfLoopValidations(EmailLexer $lexer) : Result;
public function getWarnings() : array;
}
}
+1 -1
View File
@@ -34,4 +34,4 @@ class DomainComment implements CommentStrategy
{
return [];
}
}
}
+1 -1
View File
@@ -34,4 +34,4 @@ class LocalComment implements CommentStrategy
{
return $this->warnings;
}
}
}
+4 -4
View File
@@ -22,9 +22,9 @@ use Egulias\EmailValidator\Warning\DomainLiteral as WarningDomainLiteral;
class DomainLiteral extends PartParser
{
const IPV4_REGEX = '/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
public const IPV4_REGEX = '/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
const OBSOLETE_WARNINGS = [
public const OBSOLETE_WARNINGS = [
EmailLexer::INVALID,
EmailLexer::C_DEL,
EmailLexer::S_LF,
@@ -50,7 +50,7 @@ class DomainLiteral extends PartParser
}
if ($this->lexer->isNextTokenAny(
array(EmailLexer::S_HTAB, EmailLexer::S_SP, $this->lexer->token['type'] === EmailLexer::CRLF)
array(EmailLexer::S_HTAB, EmailLexer::S_SP, EmailLexer::CRLF)
)) {
$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
$this->parseFWS();
@@ -208,4 +208,4 @@ class DomainLiteral extends PartParser
}
}
}
}
+9 -5
View File
@@ -2,6 +2,7 @@
namespace Egulias\EmailValidator\Parser;
use Doctrine\Common\Lexer\Token;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Warning\TLD;
use Egulias\EmailValidator\Result\Result;
@@ -24,8 +25,8 @@ use Egulias\EmailValidator\Parser\DomainLiteral as DomainLiteralParser;
class DomainPart extends PartParser
{
const DOMAIN_MAX_LENGTH = 253;
const LABEL_MAX_LENGTH = 63;
public const DOMAIN_MAX_LENGTH = 253;
public const LABEL_MAX_LENGTH = 63;
/**
* @var string
@@ -212,7 +213,10 @@ class DomainPart extends PartParser
return new ValidEmail();
}
private function checkNotAllowedChars(array $token) : Result
/**
* @psalm-param array|Token<int, string> $token
*/
private function checkNotAllowedChars($token) : Result
{
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
if (isset($notAllowed[$token['type']])) {
@@ -292,7 +296,7 @@ class DomainPart extends PartParser
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);
idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG);
}
return strlen($label) > self::LABEL_MAX_LENGTH;
@@ -309,4 +313,4 @@ class DomainPart extends PartParser
{
return $this->domainPart;
}
}
}
+1 -2
View File
@@ -2,7 +2,6 @@
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Parser\Parser;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\CFWSWithFWS;
@@ -85,4 +84,4 @@ class DoubleQuote extends PartParser
return new ValidEmail();
}
}
}
+2 -2
View File
@@ -15,7 +15,7 @@ use Egulias\EmailValidator\Result\ValidEmail;
class FoldingWhiteSpace extends PartParser
{
const FWS_TYPES = [
public const FWS_TYPES = [
EmailLexer::S_SP,
EmailLexer::S_HTAB,
EmailLexer::S_CR,
@@ -83,4 +83,4 @@ class FoldingWhiteSpace extends PartParser
return in_array($this->lexer->token['type'], self::FWS_TYPES);
}
}
}
+1 -2
View File
@@ -3,7 +3,6 @@
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Parser\LocalPart;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\CommentsInIDRight;
@@ -13,4 +12,4 @@ class IDLeftPart extends LocalPart
{
return new InvalidEmail(new CommentsInIDRight(), $this->lexer->token['value']);
}
}
}
+1 -1
View File
@@ -26,4 +26,4 @@ class IDRightPart extends DomainPart
}
return new ValidEmail();
}
}
}
+2 -2
View File
@@ -15,7 +15,7 @@ use Egulias\EmailValidator\Parser\CommentStrategy\LocalComment;
class LocalPart extends PartParser
{
const INVALID_TOKENS = [
public const INVALID_TOKENS = [
EmailLexer::S_COMMA => EmailLexer::S_COMMA,
EmailLexer::S_CLOSEBRACKET => EmailLexer::S_CLOSEBRACKET,
EmailLexer::S_OPENBRACKET => EmailLexer::S_OPENBRACKET,
@@ -162,4 +162,4 @@ class LocalPart extends PartParser
return new ValidEmail();
}
}
}
+1 -1
View File
@@ -60,4 +60,4 @@ abstract class PartParser
&&
$this->lexer->token['type'] !== EmailLexer::GENERIC;
}
}
}
+2 -2
View File
@@ -6,7 +6,7 @@ use Egulias\EmailValidator\Result\Reason\Reason;
class InvalidEmail implements Result
{
private $token;
private $token;
/**
* @var Reason
*/
@@ -43,4 +43,4 @@ class InvalidEmail implements Result
return $this->reason;
}
}
}
+1 -1
View File
@@ -13,4 +13,4 @@ class AtextAfterCFWS implements Reason
{
return 'ATEXT found after CFWS';
}
}
}
+2 -2
View File
@@ -4,8 +4,8 @@ namespace Egulias\EmailValidator\Result\Reason;
class CRLFAtTheEnd implements Reason
{
const CODE = 149;
const REASON = "CRLF at the end";
public const CODE = 149;
public const REASON = "CRLF at the end";
public function code() : int
{
+1 -1
View File
@@ -13,4 +13,4 @@ class CharNotAllowed implements Reason
{
return "Character not allowed";
}
}
}
+1 -1
View File
@@ -13,4 +13,4 @@ class CommaInDomain implements Reason
{
return "Comma ',' is not allowed in domain part";
}
}
}
+1 -1
View File
@@ -13,4 +13,4 @@ class CommentsInIDRight implements Reason
{
return 'Comments are not allowed in IDRight for message-id';
}
}
}
+1 -1
View File
@@ -10,4 +10,4 @@ abstract class DetailedReason implements Reason
{
$this->detailedDescription = $details;
}
}
}
+1 -1
View File
@@ -13,4 +13,4 @@ class DomainAcceptsNoMail implements Reason
{
return 'Domain accepts no mail (Null MX, RFC7505)';
}
}
}
+1 -1
View File
@@ -23,4 +23,4 @@ class ExceptionFound implements Reason
{
return $this->exception->getMessage();
}
}
}
@@ -13,4 +13,4 @@ class ExpectingDomainLiteralClose implements Reason
{
return "Closing bracket ']' for domain literal not found";
}
}
}
+1 -1
View File
@@ -13,4 +13,4 @@ class LocalOrReservedDomain implements Reason
{
return 'Local, mDNS or reserved domain (RFC2606, RFC6762)';
}
}
}
+1 -1
View File
@@ -13,4 +13,4 @@ class NoDNSRecord implements Reason
{
return 'No MX or A DSN record was found for this email';
}
}
}
+1 -1
View File
@@ -13,4 +13,4 @@ interface Reason
* Short description of the result, human readable.
*/
public function description() : string;
}
}
+1 -1
View File
@@ -13,4 +13,4 @@ class UnOpenedComment implements Reason
{
return 'Missing opening comment parentheses - https://tools.ietf.org/html/rfc5322#section-3.2.2';
}
}
}
+1 -1
View File
@@ -23,4 +23,4 @@ class UnusualElements implements Reason
{
return 'Unusual element found, wourld render invalid in majority of cases. Element found: ' . $this->element;
}
}
}
+1 -1
View File
@@ -24,4 +24,4 @@ interface Result
* Code for user land to act upon.
*/
public function code() : int;
}
}
+1 -2
View File
@@ -1,7 +1,6 @@
<?php
namespace Egulias\EmailValidator\Result;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\SpoofEmail as ReasonSpoofEmail;
class SpoofEmail extends InvalidEmail
@@ -11,4 +10,4 @@ class SpoofEmail extends InvalidEmail
$this->reason = new ReasonSpoofEmail();
parent::__construct($this->reason, '');
}
}
}
+1 -1
View File
@@ -24,4 +24,4 @@ class ValidEmail implements Result
return 0;
}
}
}
+24 -18
View File
@@ -21,7 +21,7 @@ class DNSCheckValidation implements EmailValidation
* Reserved Top Level DNS Names (https://tools.ietf.org/html/rfc2606#section-2),
* mDNS and private DNS Namespaces (https://tools.ietf.org/html/rfc6762#appendix-G)
*/
const RESERVED_DNS_TOP_LEVEL_NAMES = [
public const RESERVED_DNS_TOP_LEVEL_NAMES = [
// Reserved Top Level DNS Names
'test',
'example',
@@ -55,11 +55,22 @@ class DNSCheckValidation implements EmailValidation
*/
private $mxRecords = [];
public function __construct()
/**
* @var DNSGetRecordWrapper
*/
private $dnsGetRecord;
public function __construct(?DNSGetRecordWrapper $dnsGetRecord = null)
{
if (!function_exists('idn_to_ascii')) {
throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__));
}
if ($dnsGetRecord == null) {
$dnsGetRecord = new DNSGetRecordWrapper();
}
$this->dnsGetRecord = $dnsGetRecord;
}
public function isValid(string $email, EmailLexer $emailLexer) : bool
@@ -121,27 +132,17 @@ class DNSCheckValidation implements EmailValidation
*/
private function validateDnsRecords($host) : bool
{
// A workaround to fix https://bugs.php.net/bug.php?id=73149
/** @psalm-suppress InvalidArgument */
set_error_handler(
static function (int $errorLevel, string $errorMessage): ?bool {
throw new \RuntimeException("Unable to get DNS record for the host: $errorMessage");
}
);
$dnsRecordsResult = $this->dnsGetRecord->getRecords($host, static::DNS_RECORD_TYPES_TO_CHECK);
try {
// Get all MX, A and AAAA DNS records for host
$dnsRecords = dns_get_record($host, static::DNS_RECORD_TYPES_TO_CHECK);
} catch (\RuntimeException $exception) {
if ($dnsRecordsResult->withError()) {
$this->error = new InvalidEmail(new UnableToGetDNSRecord(), '');
return false;
} finally {
restore_error_handler();
}
$dnsRecords = $dnsRecordsResult->getRecords();
// No MX, A or AAAA DNS records
if ($dnsRecords === [] || $dnsRecords === false) {
if ($dnsRecords === []) {
$this->error = new InvalidEmail(new ReasonNoDNSRecord(), '');
return false;
}
@@ -168,6 +169,11 @@ class DNSCheckValidation implements EmailValidation
*/
private function validateMxRecord($dnsRecord) : bool
{
if (!isset($dnsRecord['type'])) {
$this->error = new InvalidEmail(new ReasonNoDNSRecord(), '');
return false;
}
if ($dnsRecord['type'] !== 'MX') {
return true;
}
@@ -182,4 +188,4 @@ class DNSCheckValidation implements EmailValidation
return true;
}
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace Egulias\EmailValidator\Validation;
class DNSGetRecordWrapper
{
/**
* @param string $host
* @param int $type
*/
public function getRecords(string $host, int $type) : DNSRecords
{
// A workaround to fix https://bugs.php.net/bug.php?id=73149
/** @psalm-suppress InvalidArgument */
set_error_handler(
static function (int $errorLevel, string $errorMessage): ?bool {
throw new \RuntimeException("Unable to get DNS record for the host: $errorMessage");
}
);
try {
// Get all MX, A and AAAA DNS records for host
return new DNSRecords(dns_get_record($host, $type));
} catch (\RuntimeException $exception) {
return new DNSRecords([], true);
} finally {
restore_error_handler();
}
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace Egulias\EmailValidator\Validation;
class DNSRecords
{
/**
* @var array $records
*/
private $records = [];
/**
* @var bool $error
*/
private $error = false;
public function __construct(array $records, bool $error = false)
{
$this->records = $records;
$this->error = $error;
}
public function getRecords() : array
{
return $this->records;
}
public function withError() : bool
{
return $this->error;
}
}
@@ -9,7 +9,7 @@ class EmptyValidationList extends \InvalidArgumentException
/**
* @param int $code
*/
public function __construct($code = 0, Exception $previous = null)
public function __construct($code = 0, ?Exception $previous = null)
{
parent::__construct("Empty validation list is not allowed", $code, $previous);
}
+2 -2
View File
@@ -13,13 +13,13 @@ class MultipleValidationWithAnd implements EmailValidation
* If one of validations fails, the remaining validations will be skipped.
* This means MultipleErrors will only contain a single error, the first found.
*/
const STOP_ON_ERROR = 0;
public const STOP_ON_ERROR = 0;
/**
* All of validations will be invoked even if one of them got failure.
* So MultipleErrors will contain all causes.
*/
const ALLOW_ALL_ERRORS = 1;
public const ALLOW_ALL_ERRORS = 1;
/**
* @var EmailValidation[]
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class AddressLiteral extends Warning
{
const CODE = 12;
public const CODE = 12;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class CFWSNearAt extends Warning
{
const CODE = 49;
public const CODE = 49;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class CFWSWithFWS extends Warning
{
const CODE = 18;
public const CODE = 18;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class Comment extends Warning
{
const CODE = 17;
public const CODE = 17;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class DeprecatedComment extends Warning
{
const CODE = 37;
public const CODE = 37;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class DomainLiteral extends Warning
{
const CODE = 70;
public const CODE = 70;
public function __construct()
{
+1 -1
View File
@@ -6,7 +6,7 @@ use Egulias\EmailValidator\EmailParser;
class EmailTooLong extends Warning
{
const CODE = 66;
public const CODE = 66;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class IPV6BadChar extends Warning
{
const CODE = 74;
public const CODE = 74;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class IPV6ColonEnd extends Warning
{
const CODE = 77;
public const CODE = 77;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class IPV6ColonStart extends Warning
{
const CODE = 76;
public const CODE = 76;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class IPV6Deprecated extends Warning
{
const CODE = 13;
public const CODE = 13;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class IPV6DoubleColon extends Warning
{
const CODE = 73;
public const CODE = 73;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class IPV6GroupCount extends Warning
{
const CODE = 72;
public const CODE = 72;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class IPV6MaxGroups extends Warning
{
const CODE = 75;
public const CODE = 75;
public function __construct()
{
+2 -2
View File
@@ -4,8 +4,8 @@ namespace Egulias\EmailValidator\Warning;
class LocalTooLong extends Warning
{
const CODE = 64;
const LOCAL_PART_LENGTH = 64;
public const CODE = 64;
public const LOCAL_PART_LENGTH = 64;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class NoDNSMXRecord extends Warning
{
const CODE = 6;
public const CODE = 6;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class ObsoleteDTEXT extends Warning
{
const CODE = 71;
public const CODE = 71;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class QuotedPart extends Warning
{
const CODE = 36;
public const CODE = 36;
/**
* @param scalar $prevToken
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class QuotedString extends Warning
{
const CODE = 11;
public const CODE = 11;
/**
* @param scalar $prevToken
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
class TLD extends Warning
{
const CODE = 9;
public const CODE = 9;
public function __construct()
{
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Egulias\EmailValidator\Warning;
abstract class Warning
{
const CODE = 0;
public const CODE = 0;
/**
* @var string
@@ -9,6 +9,8 @@ use Egulias\EmailValidator\Result\Reason\LocalOrReservedDomain;
use Egulias\EmailValidator\Result\Reason\NoDNSRecord;
use Egulias\EmailValidator\Result\Reason\UnableToGetDNSRecord;
use Egulias\EmailValidator\Validation\DNSCheckValidation;
use Egulias\EmailValidator\Validation\DNSGetRecordWrapper;
use Egulias\EmailValidator\Validation\DNSRecords;
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
use PHPUnit\Framework\TestCase;
@@ -116,13 +118,35 @@ class DNSCheckValidationTest extends TestCase
error_reporting(\E_ALL);
// UnableToGetDNSRecord raises on network errors (e.g. timeout) that we cant emulate in tests (for sure),
// but we can try to get timeout error by trying to fetch all DNS records
$validation = new class extends DNSCheckValidation {
protected const DNS_RECORD_TYPES_TO_CHECK = \DNS_ALL;
// but we can simulate with the wrapper helper
$wrapper = new class extends DNSGetRecordWrapper {
public function getRecords(string $host, int $type) : DNSRecords
{
return new DNSRecords([], true);
}
};
$validation = new DNSCheckValidation($wrapper);
$expectedError = new InvalidEmail(new UnableToGetDNSRecord(), '');
$validation->isValid('example@invalid.example.com', new EmailLexer());
$this->assertEquals($expectedError, $validation->getError());
}
}
public function testMissingTypeKey()
{
$wrapper = new class extends DNSGetRecordWrapper {
public function getRecords(string $host, int $type): DNSRecords
{
return new DNSRecords(['host' => 'test']);
}
};
$validation = new DNSCheckValidation($wrapper);
$expectedError = new InvalidEmail(new NoDNSRecord(), '');
$validation->isValid('example@invalid.example.com', new EmailLexer());
$this->assertEquals($expectedError, $validation->getError());
}
}