1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
6
|
|
|
use Egulias\EmailValidator\Result\Result; |
7
|
|
|
use Egulias\EmailValidator\Parser\LocalPart; |
8
|
|
|
use Egulias\EmailValidator\Parser\DomainPart; |
9
|
|
|
use Egulias\EmailValidator\Result\ValidEmail; |
10
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
11
|
|
|
use Egulias\EmailValidator\Warning\EmailTooLong; |
12
|
|
|
use Egulias\EmailValidator\Result\Reason\NoLocalPart; |
13
|
|
|
|
14
|
|
View Code Duplication |
class EmailParser extends Parser |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
const EMAIL_MAX_LENGTH = 254; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $domainPart = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $localPart = ''; |
27
|
|
|
|
28
|
181 |
|
public function parse(string $str) : Result |
29
|
|
|
{ |
30
|
181 |
|
$result = parent::parse($str); |
31
|
|
|
|
32
|
181 |
|
$this->addLongEmailWarning($this->localPart, $this->domainPart); |
33
|
|
|
|
34
|
181 |
|
return $result; |
35
|
|
|
} |
36
|
|
|
|
37
|
177 |
|
protected function preLeftParsing(): Result |
38
|
|
|
{ |
39
|
177 |
|
if (!$this->hasAtToken()) { |
40
|
1 |
|
return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]); |
41
|
|
|
} |
42
|
176 |
|
return new ValidEmail(); |
43
|
|
|
} |
44
|
|
|
|
45
|
176 |
|
protected function parseLeftFromAt(): Result |
46
|
|
|
{ |
47
|
176 |
|
return $this->processLocalPart(); |
48
|
|
|
} |
49
|
|
|
|
50
|
140 |
|
protected function parseRightFromAt(): Result |
51
|
|
|
{ |
52
|
140 |
|
return $this->processDomainPart(); |
53
|
|
|
} |
54
|
|
|
|
55
|
176 |
|
private function processLocalPart() : Result |
56
|
|
|
{ |
57
|
176 |
|
$localPartParser = new LocalPart($this->lexer); |
58
|
176 |
|
$localPartResult = $localPartParser->parse(); |
59
|
176 |
|
$this->localPart = $localPartParser->localPart(); |
60
|
176 |
|
$this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings); |
61
|
|
|
|
62
|
176 |
|
return $localPartResult; |
63
|
|
|
} |
64
|
|
|
|
65
|
140 |
|
private function processDomainPart() : Result |
66
|
|
|
{ |
67
|
140 |
|
$domainPartParser = new DomainPart($this->lexer); |
68
|
140 |
|
$domainPartResult = $domainPartParser->parse(); |
69
|
140 |
|
$this->domainPart = $domainPartParser->domainPart(); |
70
|
140 |
|
$this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings); |
71
|
|
|
|
72
|
140 |
|
return $domainPartResult; |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
public function getDomainPart() : string |
76
|
|
|
{ |
77
|
4 |
|
return $this->domainPart; |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
public function getLocalPart() : string |
81
|
|
|
{ |
82
|
4 |
|
return $this->localPart; |
83
|
|
|
} |
84
|
|
|
|
85
|
181 |
|
private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void |
86
|
|
|
{ |
87
|
181 |
|
if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAIL_MAX_LENGTH) { |
88
|
|
|
$this->warnings[EmailTooLong::CODE] = new EmailTooLong(); |
89
|
|
|
} |
90
|
181 |
|
} |
91
|
|
|
} |
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.