From a6c8d7101b19a451c1707b1b79bbbc56e4bdb7ec Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 19 Jul 2019 22:52:08 +0200 Subject: [PATCH] Fix PHP 7.4 support (#203) Adding support for deprecated PHP behaviours. --- .travis.yml | 1 + EmailValidator/EmailLexer.php | 16 +++++++++++++++- EmailValidator/Parser/DomainPart.php | 2 +- EmailValidator/Parser/LocalPart.php | 6 +++--- .../Validation/MultipleValidationWithAndTest.php | 15 ++++++++------- composer.json | 1 + phpunit.xml.dist | 11 +++++++---- 7 files changed, 36 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 650525c..db38e6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ php: - 7.1 - 7.2 - 7.3 + - 7.4snapshot env: global: diff --git a/EmailValidator/EmailLexer.php b/EmailValidator/EmailLexer.php index 38e4714..e27a4b8 100644 --- a/EmailValidator/EmailLexer.php +++ b/EmailValidator/EmailLexer.php @@ -77,10 +77,22 @@ class EmailLexer extends AbstractLexer protected $previous; + private static $nullToken = [ + 'value' => '', + 'type' => null, + 'position' => 0, + ]; + + public function __construct() + { + $this->previous = $this->token = self::$nullToken; + } + public function reset() { $this->hasInvalidTokens = false; parent::reset(); + $this->previous = $this->token = self::$nullToken; } public function hasInvalidTokens() @@ -122,8 +134,10 @@ class EmailLexer extends AbstractLexer public function moveNext() { $this->previous = $this->token; + $hasNext = parent::moveNext(); + $this->token = $this->token ?: self::$nullToken; - return parent::moveNext(); + return $hasNext; } /** diff --git a/EmailValidator/Parser/DomainPart.php b/EmailValidator/Parser/DomainPart.php index f2fd25a..8ed240b 100644 --- a/EmailValidator/Parser/DomainPart.php +++ b/EmailValidator/Parser/DomainPart.php @@ -184,7 +184,7 @@ class DomainPart extends Parser $domain .= $this->lexer->token['value']; $this->lexer->moveNext(); - } while ($this->lexer->token); + } while (null !== $this->lexer->token['type']); return $domain; } diff --git a/EmailValidator/Parser/LocalPart.php b/EmailValidator/Parser/LocalPart.php index 8ab16ab..fa1d17b 100644 --- a/EmailValidator/Parser/LocalPart.php +++ b/EmailValidator/Parser/LocalPart.php @@ -21,8 +21,8 @@ class LocalPart extends Parser $closingQuote = false; $openedParenthesis = 0; - while ($this->lexer->token['type'] !== EmailLexer::S_AT && $this->lexer->token) { - if ($this->lexer->token['type'] === EmailLexer::S_DOT && !$this->lexer->getPrevious()) { + while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { + if ($this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']) { throw new DotAtStart(); } @@ -86,7 +86,7 @@ class LocalPart extends Parser $this->lexer->moveNext(); - while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && $this->lexer->token) { + while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) { $parseAgain = false; if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) { $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); diff --git a/Tests/EmailValidator/Validation/MultipleValidationWithAndTest.php b/Tests/EmailValidator/Validation/MultipleValidationWithAndTest.php index 5dff28d..740c892 100644 --- a/Tests/EmailValidator/Validation/MultipleValidationWithAndTest.php +++ b/Tests/EmailValidator/Validation/MultipleValidationWithAndTest.php @@ -2,6 +2,7 @@ namespace Egulias\Tests\EmailValidator\Validation; +use Egulias\EmailValidator\EmailLexer; use Egulias\EmailValidator\EmailValidator; use Egulias\EmailValidator\Exception\CommaInDomain; use Egulias\EmailValidator\Exception\NoDomainPart; @@ -17,7 +18,7 @@ class MultipleValidationWithAndTest extends TestCase { public function testUsesAndLogicalOperation() { - $lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); + $lexer = new EmailLexer(); $validationTrue = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); $validationTrue->expects($this->any())->method("isValid")->willReturn(true); $validationTrue->expects($this->any())->method("getWarnings")->willReturn([]); @@ -38,7 +39,7 @@ class MultipleValidationWithAndTest extends TestCase public function testValidationIsValid() { - $lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); + $lexer = new EmailLexer(); $validation = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); $validation->expects($this->any())->method("isValid")->willReturn(true); @@ -59,7 +60,7 @@ class MultipleValidationWithAndTest extends TestCase ]; $expectedResult = array_merge($warnings1, $warnings2); - $lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); + $lexer = new EmailLexer(); $validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); $validation1->expects($this->any())->method("isValid")->willReturn(true); $validation1->expects($this->once())->method("getWarnings")->willReturn($warnings1); @@ -81,7 +82,7 @@ class MultipleValidationWithAndTest extends TestCase $expectedResult = new MultipleErrors([$error1, $error2]); - $lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); + $lexer = new EmailLexer(); $validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); $validation1->expects($this->once())->method("isValid")->willReturn(false); @@ -105,7 +106,7 @@ class MultipleValidationWithAndTest extends TestCase $expectedResult = new MultipleErrors([$error1]); - $lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); + $lexer = new EmailLexer(); $validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); $validation1->expects($this->any())->method("isValid")->willReturn(false); @@ -128,7 +129,7 @@ class MultipleValidationWithAndTest extends TestCase $expectedResult = new MultipleErrors([$error]); - $lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); + $lexer = new EmailLexer(); $validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); $validation1->expects($this->any())->method("isValid")->willReturn(false); @@ -147,7 +148,7 @@ class MultipleValidationWithAndTest extends TestCase public function testBreakoutOnInvalidEmail() { - $lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); + $lexer = new EmailLexer(); $validationNotCalled = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); $validationNotCalled->expects($this->never())->method("isValid"); diff --git a/composer.json b/composer.json index 5423e9f..36bd0ca 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "require-dev" : { "satooshi/php-coveralls": "^1.0.1", "phpunit/phpunit": "^4.8.35||^5.7||^6.0", + "symfony/phpunit-bridge": "^4.4@dev", "dominicsayers/isemail": "dev-master" }, "suggest": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b0812f9..754445a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,6 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" bootstrap="vendor/autoload.php" > @@ -19,8 +18,12 @@ - - ./vendor - + + ./EmailValidator/ + + + + +