Skip to content

Commit 27be0e7

Browse files
authored
code refactor (#364)
* 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).
1 parent 97c28cd commit 27be0e7

16 files changed

Lines changed: 56 additions & 97 deletions

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A library for validating emails against several RFC.
88

9-
## Supported RFCs ##
9+
## Supported RFCs
1010

1111
This library aims to support RFCs:
1212

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

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

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

3131

32-
## Requirements ##
32+
## Requirements
3333

3434
* PHP 8.1
3535
* [Composer](https://getcomposer.org) is required for installation
3636
* [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)
3737

3838
**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`
3939

40-
## Installation ##
40+
## Installation
4141

4242
Run the command below to install via Composer
4343

4444
```shell
4545
composer require egulias/email-validator
4646
```
4747

48-
## Getting Started ##
48+
## Getting Started
4949

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

@@ -61,14 +61,14 @@ $validator->isValid("example@example.com", new RFCValidation()); //true
6161
```
6262

6363

64-
### Available validations ###
64+
### Available validations
6565

6666
1. [RFCValidation](/src/Validation/RFCValidation.php): Standard RFC-like email validation.
6767
2. [NoRFCWarningsValidation](/src/Validation/NoRFCWarningsValidation.php): RFC-like validation that will fail when warnings* are found.
6868
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.
69-
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.
70-
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.
71-
7. [Your own validation](#how-to-extend): You can extend the library behaviour by implementing your own validations.
69+
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.
70+
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.
71+
6. [Your own validation](#how-to-extend): You can extend the library behaviour by implementing your own validations.
7272

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

@@ -89,21 +89,21 @@ $multipleValidations = new MultipleValidationWithAnd([
8989
$validator->isValid("example@ietf.org", $multipleValidations); //true
9090
```
9191

92-
#### Additional validations ####
92+
#### Additional validations
9393
Validations not present in the RFCs
9494

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

9797

98-
### How to extend ###
98+
### How to extend
9999

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

102-
## Contributing ##
102+
## Contributing
103103

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

106-
## Other Contributors ##
106+
## Other Contributors
107107

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

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

116-
## License ##
116+
## License
117117

118118
Released under the MIT License attached with this code.

src/EmailParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private function processLocalPart(): Result
5656
$localPartParser = new LocalPart($this->lexer);
5757
$localPartResult = $localPartParser->parse();
5858
$this->localPart = $localPartParser->localPart();
59-
$this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
59+
$this->warnings = [...$localPartParser->getWarnings(), ...$this->warnings];
6060

6161
return $localPartResult;
6262
}
@@ -66,7 +66,7 @@ private function processDomainPart(): Result
6666
$domainPartParser = new DomainPart($this->lexer);
6767
$domainPartResult = $domainPartParser->parse();
6868
$this->domainPart = $domainPartParser->domainPart();
69-
$this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
69+
$this->warnings = [...$domainPartParser->getWarnings(), ...$this->warnings];
7070

7171
return $domainPartResult;
7272
}

src/MessageIDParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function processIDLeft(): Result
5757
$localPartParser = new IDLeftPart($this->lexer);
5858
$localPartResult = $localPartParser->parse();
5959
$this->idLeft = $localPartParser->localPart();
60-
$this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
60+
$this->warnings = [...$localPartParser->getWarnings(), ...$this->warnings];
6161

6262
return $localPartResult;
6363
}
@@ -67,7 +67,7 @@ private function processIDRight(): Result
6767
$domainPartParser = new IDRightPart($this->lexer);
6868
$domainPartResult = $domainPartParser->parse();
6969
$this->idRight = $domainPartParser->domainPart();
70-
$this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
70+
$this->warnings = [...$domainPartParser->getWarnings(), ...$this->warnings];
7171

7272
return $domainPartResult;
7373
}

src/Parser/Comment.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,28 @@ public function parse(): Result
6666

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

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

7171
return $finalValidations;
7272
}
7373

7474

7575
/**
76-
* @return bool
76+
* @return void
7777
*/
78-
private function warnEscaping(): bool
78+
private function warnEscaping(): void
7979
{
8080
//Backslash found
8181
if (!$this->lexer->current->isA(EmailLexer::S_BACKSLASH)) {
82-
return false;
82+
return;
8383
}
8484

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

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

9493
private function noClosingParenthesis(): bool

src/Parser/CommentStrategy/DomainComment.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ class DomainComment implements CommentStrategy
1212
{
1313
public function exitCondition(EmailLexer $lexer, int $openedParenthesis): bool
1414
{
15-
if (($openedParenthesis === 0 && $lexer->isNextToken(EmailLexer::S_DOT))) { // || !$internalLexer->moveNext()) {
16-
return false;
17-
}
18-
19-
return true;
15+
return !($openedParenthesis === 0 && $lexer->isNextToken(EmailLexer::S_DOT));
2016
}
2117

2218
public function endOfLoopValidations(EmailLexer $lexer): Result

src/Parser/DomainLiteral.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ public function parse(): Result
8282

8383
if (!$isAddressLiteralIPv4) {
8484
return new ValidEmail();
85-
} else {
86-
$addressLiteral = $this->convertIPv4ToIPv6($addressLiteral);
8785
}
8886

87+
$addressLiteral = $this->convertIPv4ToIPv6($addressLiteral);
88+
8989
if (!$IPv6TAG) {
9090
$this->warnings[WarningDomainLiteral::CODE] = new WarningDomainLiteral();
9191
return new ValidEmail();

src/Parser/DomainPart.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected function parseComments(): Result
137137
{
138138
$commentParser = new Comment($this->lexer, new DomainComment());
139139
$result = $commentParser->parse();
140-
$this->warnings = array_merge($this->warnings, $commentParser->getWarnings());
140+
$this->warnings = [...$this->warnings, ...$commentParser->getWarnings()];
141141

142142
return $result;
143143
}
@@ -213,9 +213,9 @@ protected function doParseDomainPart(): Result
213213
return new ValidEmail();
214214
}
215215

216-
/**
216+
/**
217217
* @param Token<int, string> $token
218-
*
218+
*
219219
* @return Result
220220
*/
221221
private function checkNotAllowedChars(Token $token): Result
@@ -240,14 +240,14 @@ protected function parseDomainLiteral(): Result
240240

241241
$domainLiteralParser = new DomainLiteralParser($this->lexer);
242242
$result = $domainLiteralParser->parse();
243-
$this->warnings = array_merge($this->warnings, $domainLiteralParser->getWarnings());
243+
$this->warnings = [...$this->warnings, ...$domainLiteralParser->getWarnings()];
244244
return $result;
245245
}
246246

247247
/**
248248
* @param Token<int, string> $prev
249249
* @param bool $hasComments
250-
*
250+
*
251251
* @return Result
252252
*/
253253
protected function checkDomainPartExceptions(Token $prev, bool $hasComments): Result
@@ -323,4 +323,4 @@ public function domainPart(): string
323323
{
324324
return $this->domainPart;
325325
}
326-
}
326+
}

src/Parser/DoubleQuote.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public function parse(): Result
1717
{
1818

1919
$validQuotedString = $this->checkDQUOTE();
20-
if ($validQuotedString->isInvalid()) return $validQuotedString;
20+
if ($validQuotedString->isInvalid()) {
21+
return $validQuotedString;
22+
}
2123

2224
$special = [
2325
EmailLexer::S_CR => true,
@@ -56,7 +58,9 @@ public function parse(): Result
5658

5759
if ($prev->isA(EmailLexer::S_BACKSLASH)) {
5860
$validQuotedString = $this->checkDQUOTE();
59-
if ($validQuotedString->isInvalid()) return $validQuotedString;
61+
if ($validQuotedString->isInvalid()) {
62+
return $validQuotedString;
63+
}
6064
}
6165

6266
if (!$this->lexer->isNextToken(EmailLexer::S_AT) && !$prev->isA(EmailLexer::S_BACKSLASH)) {

src/Parser/LocalPart.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private function parseLocalFWS(): Result
118118
$foldingWS = new FoldingWhiteSpace($this->lexer);
119119
$resultFWS = $foldingWS->parse();
120120
if ($resultFWS->isValid()) {
121-
$this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
121+
$this->warnings = [...$this->warnings, ...$foldingWS->getWarnings()];
122122
}
123123
return $resultFWS;
124124
}
@@ -132,7 +132,7 @@ private function parseDoubleQuote(): Result
132132
{
133133
$dquoteParser = new DoubleQuote($this->lexer);
134134
$parseAgain = $dquoteParser->parse();
135-
$this->warnings = array_merge($this->warnings, $dquoteParser->getWarnings());
135+
$this->warnings = [...$this->warnings, ...$dquoteParser->getWarnings()];
136136

137137
return $parseAgain;
138138
}
@@ -141,10 +141,8 @@ protected function parseComments(): Result
141141
{
142142
$commentParser = new Comment($this->lexer, new LocalComment());
143143
$result = $commentParser->parse();
144-
$this->warnings = array_merge($this->warnings, $commentParser->getWarnings());
145-
if ($result->isInvalid()) {
146-
return $result;
147-
}
144+
$this->warnings = [...$this->warnings, ...$commentParser->getWarnings()];
145+
148146
return $result;
149147
}
150148

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

162-
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
163-
return new ValidEmail();
164-
}
165-
166160
return new ValidEmail();
167161
}
168162
}

src/Parser/PartParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function parseFWS(): Result
4040
{
4141
$foldingWS = new FoldingWhiteSpace($this->lexer);
4242
$resultFWS = $foldingWS->parse();
43-
$this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
43+
$this->warnings = [...$this->warnings, ...$foldingWS->getWarnings()];
4444
return $resultFWS;
4545
}
4646

0 commit comments

Comments
 (0)