Compare commits

...

12 Commits

Author SHA1 Message Date
Eduardo Gulias Davis 128cc721d7 fix(201) - empty domain 2019-06-23 12:14:27 +02:00
Viktor Szépe c26463ff92 Fix class name and parameter types (#200)
* Fix class name and paramtere types

* HHVM is not a thing anymore
2019-05-17 00:02:54 +02:00
Victor Bocharsky a09af2de87 Minor: Use SVG format for some bad looking badges in README (#197) 2019-05-07 22:08:52 +02:00
Victor Bocharsky c802f106b3 Simplify installation command in README (#198)
Composer will automatically use BC constraint, like `^2.1.7`
2019-05-07 22:08:00 +02:00
Joël Harkes 709f21f927 Update MultipleValidationWithAnd.php (#179)
* Update MultipleValidationWithAnd.php

Fix all validations actually get executed when mode is ALLOW_ALL_ERRORS (1)

* this should validate not breake out.

* Actually fix the unit tests

* add real invalid email test
2018-12-04 23:38:24 +01:00
Eduardo Gulias Davis e68c26e065 Update supported RFCs (#186)
solves #186
2018-12-03 11:33:36 +01:00
Andrey Bolonin 8036f2bd05 Update .travis.yml (#182) 2018-11-03 22:52:10 +01:00
nikitos569 0043eab0c5 Update README.md (#180) 2018-10-19 00:14:56 +02:00
SAMUEL NELA 0578b32b30 Added autoload-dev (#178) 2018-09-25 22:47:26 +02:00
Tom Sommer 54859fabea INTL_IDNA_VARIANT_UTS46 is not supported on all systems (#173)
* INTL_IDNA_VARIANT_UTS46 is not supported on all systems

Fall back to INTL_IDNA_VARIANT_2003 if INTL_IDNA_VARIANT_UTS46 is not found.

* Fix syntax error

* Update DNSCheckValidation.php
2018-08-16 22:49:45 +02:00
Tom Sommer 135444003f Correctly check for intl extension in SpoofCheck (#174) 2018-08-13 14:43:59 +02:00
Tom Sommer 9dde0df1e3 DNSCheckValidation requires the Intl extension (#175)
* DNSCheckValidation requires the Intl extension as well

* Check for Intl extension in DNSCheckValidation
2018-08-13 14:43:24 +02:00
15 changed files with 117 additions and 41 deletions
+4 -4
View File
@@ -8,7 +8,7 @@ php:
- 7.0 - 7.0
- 7.1 - 7.1
- 7.2 - 7.2
- hhvm - 7.3
env: env:
global: global:
@@ -28,8 +28,8 @@ install:
- if [ "$deps" = "high" ]; then composer update; fi - if [ "$deps" = "high" ]; then composer update; fi
script: script:
- mkdir -p build/logs - mkdir -p build/logs
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml - vendor/bin/phpunit --coverage-clover build/logs/clover.xml
after_script: after_script:
- php vendor/bin/coveralls - php vendor/bin/coveralls
+3 -3
View File
@@ -89,7 +89,7 @@ class EmailLexer extends AbstractLexer
} }
/** /**
* @param $type * @param string $type
* @throws \UnexpectedValueException * @throws \UnexpectedValueException
* @return boolean * @return boolean
*/ */
@@ -189,7 +189,7 @@ class EmailLexer extends AbstractLexer
} }
/** /**
* @param $value * @param string $value
* @return bool * @return bool
*/ */
protected function isNullType($value) protected function isNullType($value)
@@ -202,7 +202,7 @@ class EmailLexer extends AbstractLexer
} }
/** /**
* @param $value * @param string $value
* @return bool * @return bool
*/ */
protected function isUTF8Invalid($value) protected function isUTF8Invalid($value)
+1 -1
View File
@@ -33,7 +33,7 @@ class EmailParser
} }
/** /**
* @param $str * @param string $str
* @return array * @return array
*/ */
public function parse($str) public function parse($str)
+1 -1
View File
@@ -28,7 +28,7 @@ class EmailValidator
} }
/** /**
* @param $email * @param string $email
* @param EmailValidation $emailValidation * @param EmailValidation $emailValidation
* @return bool * @return bool
*/ */
+1 -1
View File
@@ -2,7 +2,7 @@
namespace Egulias\EmailValidator\Exception; namespace Egulias\EmailValidator\Exception;
class ExpectedQPair extends InvalidEmail class ExpectingQPair extends InvalidEmail
{ {
const CODE = 136; const CODE = 136;
const REASON = "Expecting QPAIR"; const REASON = "Expecting QPAIR";
-2
View File
@@ -2,8 +2,6 @@
namespace Egulias\EmailValidator\Exception; namespace Egulias\EmailValidator\Exception;
use Egulias\EmailValidator\Exception\InvalidEmail;
class NoDNSRecord extends InvalidEmail class NoDNSRecord extends InvalidEmail
{ {
const CODE = 5; const CODE = 5;
+33 -15
View File
@@ -41,21 +41,7 @@ class DomainPart extends Parser
{ {
$this->lexer->moveNext(); $this->lexer->moveNext();
if ($this->lexer->token['type'] === EmailLexer::S_DOT) { $this->performDomainStartChecks();
throw new DotAtStart();
}
if ($this->lexer->token['type'] === EmailLexer::S_EMPTY) {
throw new NoDomainPart();
}
if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) {
throw new DomainHyphened();
}
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
$this->warnings[DeprecatedComment::CODE] = new DeprecatedComment();
$this->parseDomainComments();
}
$domain = $this->doParseDomainPart(); $domain = $this->doParseDomainPart();
@@ -77,6 +63,38 @@ class DomainPart extends Parser
$this->domainPart = $domain; $this->domainPart = $domain;
} }
private function performDomainStartChecks()
{
$this->checkInvalidTokensAfterAT();
$this->checkEmptyDomain();
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
$this->warnings[DeprecatedComment::CODE] = new DeprecatedComment();
$this->parseDomainComments();
}
}
private function checkEmptyDomain()
{
$thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY ||
($this->lexer->token['type'] === EmailLexer::S_SP &&
!$this->lexer->isNextToken(EmailLexer::GENERIC));
if ($thereIsNoDomain) {
throw new NoDomainPart();
}
}
private function checkInvalidTokensAfterAT()
{
if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
throw new DotAtStart();
}
if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) {
throw new DomainHyphened();
}
}
public function getDomainPart() public function getDomainPart()
{ {
return $this->domainPart; return $this->domainPart;
+2 -2
View File
@@ -8,7 +8,7 @@ use Egulias\EmailValidator\Exception\ConsecutiveDot;
use Egulias\EmailValidator\Exception\CRLFAtTheEnd; use Egulias\EmailValidator\Exception\CRLFAtTheEnd;
use Egulias\EmailValidator\Exception\CRLFX2; use Egulias\EmailValidator\Exception\CRLFX2;
use Egulias\EmailValidator\Exception\CRNoLF; use Egulias\EmailValidator\Exception\CRNoLF;
use Egulias\EmailValidator\Exception\ExpectedQPair; use Egulias\EmailValidator\Exception\ExpectingQPair;
use Egulias\EmailValidator\Exception\ExpectingATEXT; use Egulias\EmailValidator\Exception\ExpectingATEXT;
use Egulias\EmailValidator\Exception\ExpectingCTEXT; use Egulias\EmailValidator\Exception\ExpectingCTEXT;
use Egulias\EmailValidator\Exception\UnclosedComment; use Egulias\EmailValidator\Exception\UnclosedComment;
@@ -50,7 +50,7 @@ abstract class Parser
{ {
if (!($this->lexer->token['type'] === EmailLexer::INVALID if (!($this->lexer->token['type'] === EmailLexer::INVALID
|| $this->lexer->token['type'] === EmailLexer::C_DEL)) { || $this->lexer->token['type'] === EmailLexer::C_DEL)) {
throw new ExpectedQPair(); throw new ExpectingQPair();
} }
$this->warnings[QuotedPart::CODE] = $this->warnings[QuotedPart::CODE] =
@@ -18,6 +18,13 @@ class DNSCheckValidation implements EmailValidation
* @var InvalidEmail * @var InvalidEmail
*/ */
private $error; private $error;
public function __construct()
{
if (!extension_loaded('intl')) {
throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__));
}
}
public function isValid($email, EmailLexer $emailLexer) public function isValid($email, EmailLexer $emailLexer)
{ {
@@ -44,7 +51,11 @@ class DNSCheckValidation implements EmailValidation
protected function checkDNS($host) protected function checkDNS($host)
{ {
$host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46), '.') . '.'; $variant = INTL_IDNA_VARIANT_2003;
if ( defined('INTL_IDNA_VARIANT_UTS46') ) {
$variant = INTL_IDNA_VARIANT_UTS46;
}
$host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.';
$Aresult = true; $Aresult = true;
$MXresult = checkdnsrr($host, 'MX'); $MXresult = checkdnsrr($host, 'MX');
@@ -62,7 +62,8 @@ class MultipleValidationWithAnd implements EmailValidation
$errors = []; $errors = [];
foreach ($this->validations as $validation) { foreach ($this->validations as $validation) {
$emailLexer->reset(); $emailLexer->reset();
$result = $result && $validation->isValid($email, $emailLexer); $validationResult = $validation->isValid($email, $emailLexer);
$result = $result && $validationResult;
$this->warnings = array_merge($this->warnings, $validation->getWarnings()); $this->warnings = array_merge($this->warnings, $validation->getWarnings());
$errors = $this->addNewError($validation->getError(), $errors); $errors = $this->addNewError($validation->getError(), $errors);
@@ -16,7 +16,7 @@ class SpoofCheckValidation implements EmailValidation
public function __construct() public function __construct()
{ {
if (!class_exists(Spoofchecker::class)) { if (!extension_loaded('intl')) {
throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__)); throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__));
} }
} }
+7 -4
View File
@@ -1,19 +1,22 @@
# EmailValidator # EmailValidator
[![Build Status](https://travis-ci.org/egulias/EmailValidator.png?branch=master)](https://travis-ci.org/egulias/EmailValidator) [![Coverage Status](https://coveralls.io/repos/egulias/EmailValidator/badge.png?branch=master)](https://coveralls.io/r/egulias/EmailValidator?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/egulias/EmailValidator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/egulias/EmailValidator/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6/small.png)](https://insight.sensiolabs.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6) [![Build Status](https://travis-ci.org/egulias/EmailValidator.svg?branch=master)](https://travis-ci.org/egulias/EmailValidator) [![Coverage Status](https://coveralls.io/repos/egulias/EmailValidator/badge.svg?branch=master)](https://coveralls.io/r/egulias/EmailValidator?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/egulias/EmailValidator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/egulias/EmailValidator/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6/small.png)](https://insight.sensiolabs.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6)
============================= =============================
With the help of [PHPStorm](https://www.jetbrains.com/phpstorm/) ## Suported RFCs ##
This library aims to support:
RFC 5321, 5322, 6530, 6531, 6532.
## Requirements ## ## Requirements ##
* [Composer](https://getcomposer.org) is required for installation * [Composer](https://getcomposer.org) is required for installation
* [Spoofchecking](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/SpoofCheckValidation.php) validation requires that your PHP system have the [PHP Internationalization Libraries](https://php.net/manual/en/book.intl.php) (also known as PHP Intl) * [Spoofchecking](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/SpoofCheckValidation.php) and [DNSCheckValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/DNSCheckValidation.php) validation requires that your PHP system has the [PHP Internationalization Libraries](https://php.net/manual/en/book.intl.php) (also known as PHP Intl)
## Installation ## ## Installation ##
Run the command below to install via Composer Run the command below to install via Composer
```shell ```shell
composer require egulias/email-validator "~2.1" composer require egulias/email-validator
``` ```
## Getting Started ## ## Getting Started ##
@@ -2,15 +2,18 @@
namespace Egulias\Tests\EmailValidator\Validation; namespace Egulias\Tests\EmailValidator\Validation;
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Exception\CommaInDomain; use Egulias\EmailValidator\Exception\CommaInDomain;
use Egulias\EmailValidator\Exception\NoDomainPart; use Egulias\EmailValidator\Exception\NoDomainPart;
use Egulias\EmailValidator\Validation\MultipleErrors; use Egulias\EmailValidator\Validation\MultipleErrors;
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd; use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
use Egulias\EmailValidator\Validation\RFCValidation;
use Egulias\EmailValidator\Warning\AddressLiteral; use Egulias\EmailValidator\Warning\AddressLiteral;
use Egulias\EmailValidator\Warning\DomainLiteral; use Egulias\EmailValidator\Warning\DomainLiteral;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class MultipleValidationWitAndTest extends TestCase class MultipleValidationWithAndTest extends TestCase
{ {
public function testUsesAndLogicalOperation() public function testUsesAndLogicalOperation()
{ {
@@ -81,16 +84,40 @@ class MultipleValidationWitAndTest extends TestCase
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); $lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); $validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
$validation1->expects($this->any())->method("isValid")->willReturn(true); $validation1->expects($this->once())->method("isValid")->willReturn(false);
$validation1->expects($this->once())->method("getWarnings")->willReturn([]);
$validation1->expects($this->once())->method("getError")->willReturn($error1);
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
$validation2->expects($this->once())->method("isValid")->willReturn(false);
$validation2->expects($this->once())->method("getWarnings")->willReturn([]);
$validation2->expects($this->once())->method("getError")->willReturn($error2);
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]);
$multipleValidation->isValid("example@example.com", $lexer);
$this->assertEquals($expectedResult, $multipleValidation->getError());
}
public function testStopsAfterFirstError()
{
$error1 = new CommaInDomain();
$error2 = new NoDomainPart();
$expectedResult = new MultipleErrors([$error1]);
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
$validation1->expects($this->any())->method("isValid")->willReturn(false);
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); $validation1->expects($this->once())->method("getWarnings")->willReturn([]);
$validation1->expects($this->once())->method("getError")->willReturn($error1); $validation1->expects($this->once())->method("getError")->willReturn($error1);
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); $validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
$validation2->expects($this->any())->method("isValid")->willReturn(false); $validation2->expects($this->any())->method("isValid")->willReturn(false);
$validation2->expects($this->once())->method("getWarnings")->willReturn([]); $validation2->expects($this->never())->method("getWarnings")->willReturn([]);
$validation2->expects($this->once())->method("getError")->willReturn($error2); $validation2->expects($this->never())->method("getError")->willReturn($error2);
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); $multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2], MultipleValidationWithAnd::STOP_ON_ERROR);
$multipleValidation->isValid("example@example.com", $lexer); $multipleValidation->isValid("example@example.com", $lexer);
$this->assertEquals($expectedResult, $multipleValidation->getError()); $this->assertEquals($expectedResult, $multipleValidation->getError());
} }
@@ -117,4 +144,16 @@ class MultipleValidationWitAndTest extends TestCase
$multipleValidation->isValid("example@example.com", $lexer); $multipleValidation->isValid("example@example.com", $lexer);
$this->assertEquals($expectedResult, $multipleValidation->getError()); $this->assertEquals($expectedResult, $multipleValidation->getError());
} }
public function testBreakoutOnInvalidEmail()
{
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
$validationNotCalled = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
$validationNotCalled->expects($this->never())->method("isValid");
$validationNotCalled->expects($this->never())->method("getWarnings");
$validationNotCalled->expects($this->never())->method("getError");
$multipleValidation = new MultipleValidationWithAnd([new RFCValidation(), $validationNotCalled], MultipleValidationWithAnd::STOP_ON_ERROR);
$this->assertFalse($multipleValidation->isValid("invalid-email", $lexer));
}
} }
@@ -166,6 +166,7 @@ class RFCValidationTest extends TestCase
['test@email>'], ['test@email>'],
['test@email<'], ['test@email<'],
['test@email{'], ['test@email{'],
['test@ '],
]; ];
} }
+5
View File
@@ -35,5 +35,10 @@
"psr-4": { "psr-4": {
"Egulias\\EmailValidator\\": "EmailValidator" "Egulias\\EmailValidator\\": "EmailValidator"
} }
},
"autoload-dev": {
"psr-4": {
"Egulias\\Tests\\": "test"
}
} }
} }