Fix Tests + Psalm issues

This commit is contained in:
Bastien Wermeille
2022-12-15 00:04:10 +01:00
parent d80748070a
commit 3d25492123
10 changed files with 60 additions and 40 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
},
"require": {
"php": ">=7.2",
"doctrine/lexer": "^2.0",
"doctrine/lexer": "^2.1",
"symfony/polyfill-intl-idn": "^1.15"
},
"require-dev": {
Generated
+13 -13
View File
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "fa788fd0799305dbc0cb247a6f981084",
"content-hash": "e4db7645a86d3dd51c48de4de7474017",
"packages": [
{
"name": "doctrine/deprecations",
@@ -51,16 +51,16 @@
},
{
"name": "doctrine/lexer",
"version": "2.0.0",
"version": "2.1.0",
"source": {
"type": "git",
"url": "https://github.com/doctrine/lexer.git",
"reference": "3cf140b81e55d5d640f73367d829db7e3023ef69"
"reference": "39ab8fcf5a51ce4b85ca97c7a7d033eb12831124"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/lexer/zipball/3cf140b81e55d5d640f73367d829db7e3023ef69",
"reference": "3cf140b81e55d5d640f73367d829db7e3023ef69",
"url": "https://api.github.com/repos/doctrine/lexer/zipball/39ab8fcf5a51ce4b85ca97c7a7d033eb12831124",
"reference": "39ab8fcf5a51ce4b85ca97c7a7d033eb12831124",
"shasum": ""
},
"require": {
@@ -109,7 +109,7 @@
],
"support": {
"issues": "https://github.com/doctrine/lexer/issues",
"source": "https://github.com/doctrine/lexer/tree/2.0.0"
"source": "https://github.com/doctrine/lexer/tree/2.1.0"
},
"funding": [
{
@@ -125,7 +125,7 @@
"type": "tidelift"
}
],
"time": "2022-12-11T10:51:23+00:00"
"time": "2022-12-14T08:49:07+00:00"
},
{
"name": "symfony/polyfill-intl-idn",
@@ -1947,16 +1947,16 @@
},
{
"name": "phpunit/php-code-coverage",
"version": "9.2.20",
"version": "9.2.21",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/php-code-coverage.git",
"reference": "af7463c955007de36db0c5e26d03e2f933c2e980"
"reference": "3f893e19712bb0c8bc86665d1562e9fd509c4ef0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/af7463c955007de36db0c5e26d03e2f933c2e980",
"reference": "af7463c955007de36db0c5e26d03e2f933c2e980",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/3f893e19712bb0c8bc86665d1562e9fd509c4ef0",
"reference": "3f893e19712bb0c8bc86665d1562e9fd509c4ef0",
"shasum": ""
},
"require": {
@@ -2012,7 +2012,7 @@
],
"support": {
"issues": "https://github.com/sebastianbergmann/php-code-coverage/issues",
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.20"
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.21"
},
"funding": [
{
@@ -2020,7 +2020,7 @@
"type": "github"
}
],
"time": "2022-12-13T07:49:28+00:00"
"time": "2022-12-14T13:26:54+00:00"
},
{
"name": "phpunit/php-file-iterator",
+18 -9
View File
@@ -5,10 +5,11 @@ namespace Egulias\EmailValidator;
use Doctrine\Common\Lexer\AbstractLexer;
use Doctrine\Common\Lexer\Token;
/** @extends AbstractLexer<int, string> */
class EmailLexer extends AbstractLexer
{
//ASCII values
public const S_EMPTY = null;
public const S_EMPTY = -1;
public const C_NUL = 0;
public const S_HTAB = 9;
public const S_LF = 10;
@@ -132,25 +133,30 @@ class EmailLexer extends AbstractLexer
protected $hasInvalidTokens = false;
/**
* @var Token
* @var Token<int, string>
*/
protected $previous = null;
protected $previous;
/**
* The last matched/seen token.
*
* @var Token
* @var Token<int, string>
*
* @psalm-suppress NonInvariantDocblockPropertyType
*/
public $token;
/**
* The next token in the input.
*
* @var Token|null
* @var Token<int, string>|null
*/
public $lookahead;
private $nullToken = null;
/**
* @var Token<int, string>
*/
private $nullToken;
/** @var string */
private $accumulator = '';
@@ -160,7 +166,10 @@ class EmailLexer extends AbstractLexer
public function __construct()
{
$this->nullToken = new Token('', null, 0);
/** @var Token<int, string> $nullToken */
$nullToken = new Token('', self::S_EMPTY, 0);
$this->nullToken = $nullToken;
$this->previous = $this->token = $this->nullToken;
$this->lookahead = null;
}
@@ -223,7 +232,7 @@ class EmailLexer extends AbstractLexer
* @throws \InvalidArgumentException
* @return integer
*/
protected function getType(&$value)
protected function getType(&$value): int
{
$encoded = $value;
@@ -276,7 +285,7 @@ class EmailLexer extends AbstractLexer
/**
* getPrevious
*
* @return Token
* @return Token<int, string>
*/
public function getPrevious(): Token
{
+12 -1
View File
@@ -201,7 +201,7 @@ class DomainPart extends PartParser
return $exceptionsResult;
}
$this->lexer->moveNext();
} while (null !== $this->lexer->token->type);
} while (!$this->lexer->token->isA(EmailLexer::S_EMPTY));
$labelCheck = $this->checkLabelLength(true);
if ($labelCheck->isInvalid()) {
@@ -213,6 +213,11 @@ class DomainPart extends PartParser
return new ValidEmail();
}
/**
* @param Token<int, string> $token
*
* @return Result
*/
private function checkNotAllowedChars(Token $token): Result
{
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH => true];
@@ -239,6 +244,12 @@ class DomainPart extends PartParser
return $result;
}
/**
* @param Token<int, string> $prev
* @param bool $hasComments
*
* @return Result
*/
protected function checkDomainPartExceptions(Token $prev, bool $hasComments): Result
{
if ($this->lexer->token->isA(EmailLexer::S_OPENBRACKET) && $prev->type !== EmailLexer::S_AT) {
+1 -1
View File
@@ -36,7 +36,7 @@ class DoubleQuote extends PartParser
$this->lexer->moveNext();
while (!$this->lexer->token->isA(EmailLexer::S_DQUOTE) && null !== $this->lexer->token->type) {
while (!$this->lexer->token->isA(EmailLexer::S_DQUOTE) && !$this->lexer->token->isA(EmailLexer::S_EMPTY)) {
if (isset($special[$this->lexer->token->type]) && $setSpecialsWarning) {
$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
$setSpecialsWarning = false;
+2 -2
View File
@@ -36,7 +36,7 @@ class LocalPart extends PartParser
{
$this->lexer->startRecording();
while (!$this->lexer->token->isA(EmailLexer::S_AT) && null !== $this->lexer->token->type) {
while (!$this->lexer->token->isA(EmailLexer::S_AT) && !$this->lexer->token->isA(EmailLexer::S_EMPTY)) {
if ($this->hasDotAtStart()) {
return new InvalidEmail(new DotAtStart(), $this->lexer->token->value);
}
@@ -125,7 +125,7 @@ class LocalPart extends PartParser
private function hasDotAtStart(): bool
{
return $this->lexer->token->isA(EmailLexer::S_DOT) && null === $this->lexer->getPrevious()->type;
return $this->lexer->token->isA(EmailLexer::S_DOT) && $this->lexer->getPrevious()->isA(EmailLexer::S_EMPTY);
}
private function parseDoubleQuote(): Result
+1 -1
View File
@@ -56,7 +56,7 @@ abstract class PartParser
{
$previous = $this->lexer->getPrevious();
return $previous && $previous->isA(EmailLexer::S_BACKSLASH)
return $previous->isA(EmailLexer::S_BACKSLASH)
&& !$this->lexer->token->isA(EmailLexer::GENERIC);
}
}
+4 -4
View File
@@ -163,23 +163,23 @@ class DNSCheckValidation implements EmailValidation
/**
* Validate an MX record
*
* @param object $dnsRecord Given DNS record.
* @param array $dnsRecord Given DNS record.
*
* @return bool True if valid.
*/
private function validateMxRecord($dnsRecord): bool
{
if (!isset($dnsRecord->type)) {
if (!isset($dnsRecord['type'])) {
$this->error = new InvalidEmail(new ReasonNoDNSRecord(), '');
return false;
}
if ($dnsRecord->type !== 'MX') {
if ($dnsRecord['type'] !== 'MX') {
return true;
}
// "Null MX" record indicates the domain accepts no mail (https://tools.ietf.org/html/rfc7505)
if (empty($dnsRecord->target) || $dnsRecord->target === '.') {
if (empty($dnsRecord['target']) || $dnsRecord['target'] === '.') {
$this->error = new InvalidEmail(new DomainAcceptsNoMail(), "");
return false;
}
+6 -6
View File
@@ -58,7 +58,7 @@ class MultipleValidationWithAnd implements EmailValidation
/**
* {@inheritdoc}
*/
public function isValid(string $email, EmailLexer $emailLexer) : bool
public function isValid(string $email, EmailLexer $emailLexer): bool
{
$result = true;
foreach ($this->validations as $validation) {
@@ -78,14 +78,14 @@ class MultipleValidationWithAnd implements EmailValidation
return $result;
}
private function initErrorStorage() : void
private function initErrorStorage(): void
{
if (null === $this->error) {
$this->error = new MultipleErrors();
}
}
private function processError(EmailValidation $validation) : void
private function processError(EmailValidation $validation): void
{
if (null !== $validation->getError()) {
$this->initErrorStorage();
@@ -94,7 +94,7 @@ class MultipleValidationWithAnd implements EmailValidation
}
}
private function shouldStop(bool $result) : bool
private function shouldStop(bool $result): bool
{
return !$result && $this->mode === self::STOP_ON_ERROR;
}
@@ -102,7 +102,7 @@ class MultipleValidationWithAnd implements EmailValidation
/**
* Returns the validation errors.
*/
public function getError() : ?InvalidEmail
public function getError(): ?InvalidEmail
{
return $this->error;
}
@@ -110,7 +110,7 @@ class MultipleValidationWithAnd implements EmailValidation
/**
* {@inheritdoc}
*/
public function getWarnings() : array
public function getWarnings(): array
{
return $this->warnings;
}
+2 -2
View File
@@ -7,8 +7,8 @@ class QuotedPart extends Warning
public const CODE = 36;
/**
* @param scalar $prevToken
* @param scalar $postToken
* @param scalar|null $prevToken
* @param scalar|null $postToken
*/
public function __construct($prevToken, $postToken)
{