Skip to content

Commit

Permalink
code refactor (#364)
Browse files Browse the repository at this point in the history
* use spread syntax instead of array_merge()

* use type cast instead of function cast

* removed redundant returns

* removed redundant phpdoc - type is already inferred

* removed invalid phpdoc - type is already inferred

The PHPDoc return type hint was incomplete, it should have been `InvalidEmail|null`, however, it can be removed altogether as the return type is already inferred from the code.

* changed warnEscaping()'s return value from bool to void

This is not a breaking change as the method is private,  and it's only used at one place, where the return value was not used anyway.

* made private class property local

`private $parser` was used in only one place, hence it can be local, there is no reason to put it into the class' scope.

* removed unnecessary type casting

Concatenation already casts `static::CODE` from `int` to `string`, no reason to do it explicitly.

* removed redundant initializers - constructor overwrites them immediately

* removed redundant else block

* simplified if-else statement

* wrapped if body in brackets to comply with PSR12

* fixed README formatting

- fixed numbering at the `Available validations` section
- fixed overall formatting

* Revert "removed redundant phpdoc - type is already inferred"

This reverts commit 68a9ae2.

* don't wrap long lines

* make properties typed

Also using constructor property promotion, see more info about it [here](https://php.watch/versions/8.0/constructor-property-promotion).
  • Loading branch information
xHeaven authored Jun 7, 2023
1 parent 97c28cd commit 27be0e7
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 97 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A library for validating emails against several RFC.

## Supported RFCs ##
## Supported RFCs

This library aims to support RFCs:

Expand All @@ -21,31 +21,31 @@ This library aims to support RFCs:

**Current major version with full support is v3**

| Version | Released | EOL | Only critical bug fixes | Full |
| :-----: | :--------: | :---: | :---------------------: | :---: |
| v4.x | 2023/01/07 | - | X | X |
| v3.x | 2020/12/29 | - | X | |
| v2.1.x | 2016/05/16 | YES | | |
| v1.2 | 2013/19/05 | YES | | |
| Version | Released | EOL | Only critical bug fixes | Full |
|:-------:|:----------:|:---:|:-----------------------:|:----:|
| v4.x | 2023/01/07 | - | X | X |
| v3.x | 2020/12/29 | - | X | |
| v2.1.x | 2016/05/16 | YES | | |
| v1.2 | 2013/19/05 | YES | | |


## Requirements ##
## Requirements

* PHP 8.1
* [Composer](https://getcomposer.org) is required for installation
* [Spoofchecking](/src/Validation/Extra/SpoofCheckValidation.php) and [DNSCheckValidation](/src/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)

**Note**: `PHP version upgrades will happen to accomodate to the pace of major frameworks. Minor versions bumps will go via minor versions of this library (i.e: PHP7.3 -> v3.x+1). Major versions will go with major versions of the library`

## Installation ##
## Installation

Run the command below to install via Composer

```shell
composer require egulias/email-validator
```

## Getting Started ##
## Getting Started

`EmailValidator` requires you to decide which (or combination of them) validation/s strategy/ies you'd like to follow for each [validation](#available-validations).

Expand All @@ -61,14 +61,14 @@ $validator->isValid("[email protected]", new RFCValidation()); //true
```


### Available validations ###
### Available validations

1. [RFCValidation](/src/Validation/RFCValidation.php): Standard RFC-like email validation.
2. [NoRFCWarningsValidation](/src/Validation/NoRFCWarningsValidation.php): RFC-like validation that will fail when warnings* are found.
3. [DNSCheckValidation](/src/Validation/DNSCheckValidation.php): Will check if there are DNS records that signal that the server accepts emails. This does not entail that the email exists.
5. [MultipleValidationWithAnd](/src/Validation/MultipleValidationWithAnd.php): It is a validation that operates over other validations performing a logical and (&&) over the result of each validation.
6. [MessageIDValidation](/src/Validation/MessageIDValidation.php): Follows [RFC2822 for message-id](https://tools.ietf.org/html/rfc2822#section-3.6.4) to validate that field, that has some differences in the domain part.
7. [Your own validation](#how-to-extend): You can extend the library behaviour by implementing your own validations.
4. [MultipleValidationWithAnd](/src/Validation/MultipleValidationWithAnd.php): It is a validation that operates over other validations performing a logical and (&&) over the result of each validation.
5. [MessageIDValidation](/src/Validation/MessageIDValidation.php): Follows [RFC2822 for message-id](https://tools.ietf.org/html/rfc2822#section-3.6.4) to validate that field, that has some differences in the domain part.
6. [Your own validation](#how-to-extend): You can extend the library behaviour by implementing your own validations.

*warnings: Warnings are deviations from the RFC that in a broader interpretation are accepted.

Expand All @@ -89,21 +89,21 @@ $multipleValidations = new MultipleValidationWithAnd([
$validator->isValid("[email protected]", $multipleValidations); //true
```

#### Additional validations ####
#### Additional validations
Validations not present in the RFCs

1. [SpoofCheckValidation](/src/Validation/Extra/SpoofCheckValidation.php): Will check for multi-utf-8 chars that can signal an erroneous email name.


### How to extend ###
### How to extend

It's easy! You just need to implement [EmailValidation](/src/Validation/EmailValidation.php) and you can use your own validation.

## Contributing ##
## Contributing

Please follow the [Contribution guide](CONTRIBUTING.md). Is short and simple and will help a lot.

## Other Contributors ##
## Other Contributors

(You can find current contributors [here](https://github.com/egulias/EmailValidator/graphs/contributors))

Expand All @@ -113,6 +113,6 @@ As this is a port from another library and work, here are other people related t
* Josepf Bielawski [@stloyd](https://github.com/stloyd): For its first re-work of Dominic's lib
* Dominic Sayers [@dominicsayers](https://github.com/dominicsayers): The original isemail function

## License ##
## License

Released under the MIT License attached with this code.
4 changes: 2 additions & 2 deletions src/EmailParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private function processLocalPart(): Result
$localPartParser = new LocalPart($this->lexer);
$localPartResult = $localPartParser->parse();
$this->localPart = $localPartParser->localPart();
$this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
$this->warnings = [...$localPartParser->getWarnings(), ...$this->warnings];

return $localPartResult;
}
Expand All @@ -66,7 +66,7 @@ private function processDomainPart(): Result
$domainPartParser = new DomainPart($this->lexer);
$domainPartResult = $domainPartParser->parse();
$this->domainPart = $domainPartParser->domainPart();
$this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
$this->warnings = [...$domainPartParser->getWarnings(), ...$this->warnings];

return $domainPartResult;
}
Expand Down
4 changes: 2 additions & 2 deletions src/MessageIDParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function processIDLeft(): Result
$localPartParser = new IDLeftPart($this->lexer);
$localPartResult = $localPartParser->parse();
$this->idLeft = $localPartParser->localPart();
$this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
$this->warnings = [...$localPartParser->getWarnings(), ...$this->warnings];

return $localPartResult;
}
Expand All @@ -67,7 +67,7 @@ private function processIDRight(): Result
$domainPartParser = new IDRightPart($this->lexer);
$domainPartResult = $domainPartParser->parse();
$this->idRight = $domainPartParser->domainPart();
$this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
$this->warnings = [...$domainPartParser->getWarnings(), ...$this->warnings];

return $domainPartResult;
}
Expand Down
11 changes: 5 additions & 6 deletions src/Parser/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,28 @@ public function parse(): Result

$finalValidations = $this->commentStrategy->endOfLoopValidations($this->lexer);

$this->warnings = array_merge($this->warnings, $this->commentStrategy->getWarnings());
$this->warnings = [...$this->warnings, ...$this->commentStrategy->getWarnings()];

return $finalValidations;
}


/**
* @return bool
* @return void
*/
private function warnEscaping(): bool
private function warnEscaping(): void
{
//Backslash found
if (!$this->lexer->current->isA(EmailLexer::S_BACKSLASH)) {
return false;
return;
}

if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
return false;
return;
}

$this->warnings[QuotedPart::CODE] =
new QuotedPart($this->lexer->getPrevious()->type, $this->lexer->current->type);
return true;
}

private function noClosingParenthesis(): bool
Expand Down
6 changes: 1 addition & 5 deletions src/Parser/CommentStrategy/DomainComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ class DomainComment implements CommentStrategy
{
public function exitCondition(EmailLexer $lexer, int $openedParenthesis): bool
{
if (($openedParenthesis === 0 && $lexer->isNextToken(EmailLexer::S_DOT))) { // || !$internalLexer->moveNext()) {
return false;
}

return true;
return !($openedParenthesis === 0 && $lexer->isNextToken(EmailLexer::S_DOT));
}

public function endOfLoopValidations(EmailLexer $lexer): Result
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/DomainLiteral.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public function parse(): Result

if (!$isAddressLiteralIPv4) {
return new ValidEmail();
} else {
$addressLiteral = $this->convertIPv4ToIPv6($addressLiteral);
}

$addressLiteral = $this->convertIPv4ToIPv6($addressLiteral);

if (!$IPv6TAG) {
$this->warnings[WarningDomainLiteral::CODE] = new WarningDomainLiteral();
return new ValidEmail();
Expand Down
12 changes: 6 additions & 6 deletions src/Parser/DomainPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected function parseComments(): Result
{
$commentParser = new Comment($this->lexer, new DomainComment());
$result = $commentParser->parse();
$this->warnings = array_merge($this->warnings, $commentParser->getWarnings());
$this->warnings = [...$this->warnings, ...$commentParser->getWarnings()];

return $result;
}
Expand Down Expand Up @@ -213,9 +213,9 @@ protected function doParseDomainPart(): Result
return new ValidEmail();
}

/**
/**
* @param Token<int, string> $token
*
*
* @return Result
*/
private function checkNotAllowedChars(Token $token): Result
Expand All @@ -240,14 +240,14 @@ protected function parseDomainLiteral(): Result

$domainLiteralParser = new DomainLiteralParser($this->lexer);
$result = $domainLiteralParser->parse();
$this->warnings = array_merge($this->warnings, $domainLiteralParser->getWarnings());
$this->warnings = [...$this->warnings, ...$domainLiteralParser->getWarnings()];
return $result;
}

/**
* @param Token<int, string> $prev
* @param bool $hasComments
*
*
* @return Result
*/
protected function checkDomainPartExceptions(Token $prev, bool $hasComments): Result
Expand Down Expand Up @@ -323,4 +323,4 @@ public function domainPart(): string
{
return $this->domainPart;
}
}
}
8 changes: 6 additions & 2 deletions src/Parser/DoubleQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function parse(): Result
{

$validQuotedString = $this->checkDQUOTE();
if ($validQuotedString->isInvalid()) return $validQuotedString;
if ($validQuotedString->isInvalid()) {
return $validQuotedString;
}

$special = [
EmailLexer::S_CR => true,
Expand Down Expand Up @@ -56,7 +58,9 @@ public function parse(): Result

if ($prev->isA(EmailLexer::S_BACKSLASH)) {
$validQuotedString = $this->checkDQUOTE();
if ($validQuotedString->isInvalid()) return $validQuotedString;
if ($validQuotedString->isInvalid()) {
return $validQuotedString;
}
}

if (!$this->lexer->isNextToken(EmailLexer::S_AT) && !$prev->isA(EmailLexer::S_BACKSLASH)) {
Expand Down
14 changes: 4 additions & 10 deletions src/Parser/LocalPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private function parseLocalFWS(): Result
$foldingWS = new FoldingWhiteSpace($this->lexer);
$resultFWS = $foldingWS->parse();
if ($resultFWS->isValid()) {
$this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
$this->warnings = [...$this->warnings, ...$foldingWS->getWarnings()];
}
return $resultFWS;
}
Expand All @@ -132,7 +132,7 @@ private function parseDoubleQuote(): Result
{
$dquoteParser = new DoubleQuote($this->lexer);
$parseAgain = $dquoteParser->parse();
$this->warnings = array_merge($this->warnings, $dquoteParser->getWarnings());
$this->warnings = [...$this->warnings, ...$dquoteParser->getWarnings()];

return $parseAgain;
}
Expand All @@ -141,10 +141,8 @@ protected function parseComments(): Result
{
$commentParser = new Comment($this->lexer, new LocalComment());
$result = $commentParser->parse();
$this->warnings = array_merge($this->warnings, $commentParser->getWarnings());
if ($result->isInvalid()) {
return $result;
}
$this->warnings = [...$this->warnings, ...$commentParser->getWarnings()];

return $result;
}

Expand All @@ -159,10 +157,6 @@ private function validateEscaping(): Result
return new InvalidEmail(new ExpectingATEXT('Found ATOM after escaping'), $this->lexer->current->value);
}

if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
return new ValidEmail();
}

return new ValidEmail();
}
}
2 changes: 1 addition & 1 deletion src/Parser/PartParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function parseFWS(): Result
{
$foldingWS = new FoldingWhiteSpace($this->lexer);
$resultFWS = $foldingWS->parse();
$this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
$this->warnings = [...$this->warnings, ...$foldingWS->getWarnings()];
return $resultFWS;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Result/Reason/UnusualElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class UnusualElements implements Reason
/**
* @var string $element
*/
private $element = '';
private $element;

public function __construct(string $element)
{
Expand Down
15 changes: 1 addition & 14 deletions src/Validation/DNSRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@

class DNSRecords
{

/**
* @var array $records
*/
private $records = [];

/**
* @var bool $error
*/
private $error = false;

/**
* @param array $records
* @param bool $error
*/
public function __construct(array $records, bool $error = false)
public function __construct(private readonly array $records, private readonly bool $error = false)
{
$this->records = $records;
$this->error = $error;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Validation/Extra/SpoofCheckValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public function isValid(string $email, EmailLexer $emailLexer) : bool
return $this->error === null;
}

/**
* @return InvalidEmail
*/
public function getError() : ?InvalidEmail
{
return $this->error;
Expand Down
Loading

0 comments on commit 27be0e7

Please sign in to comment.