1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator\Parser; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
6
|
|
|
use Egulias\EmailValidator\Result\Result; |
7
|
|
|
use Egulias\EmailValidator\Result\ValidEmail; |
8
|
|
|
use Egulias\EmailValidator\Exception\DotAtEnd; |
9
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
10
|
|
|
use Egulias\EmailValidator\Warning\LocalTooLong; |
11
|
|
|
use Egulias\EmailValidator\Exception\ExpectingATEXT; |
12
|
|
|
use Egulias\EmailValidator\Result\Reason\DotAtStart; |
13
|
|
|
use Egulias\EmailValidator\Exception\UnopenedComment; |
14
|
|
|
|
15
|
|
|
class LocalPart extends Parser |
16
|
|
|
{ |
17
|
|
|
public function parse($localPart) : Result |
18
|
|
|
{ |
19
|
|
|
$parseDQuote = true; |
|
|
|
|
20
|
|
|
$closingQuote = false; |
21
|
|
|
$openedParenthesis = 0; |
22
|
|
|
$totalLength = 0; |
23
|
|
|
|
24
|
|
|
while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { |
25
|
|
|
if ($this->hasDotAtStart()) { |
26
|
|
|
return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
//if ($parseDQuote) { |
30
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_DQUOTE) { |
31
|
|
|
$dquoteParsingResult = $this->parseDoubleQuote(); |
32
|
|
|
$parseDQuote = !$dquoteParsingResult->isValid(); |
33
|
|
|
|
34
|
|
|
//Invalid double quote parsing |
35
|
|
|
if($parseDQuote) { |
36
|
|
|
return $dquoteParsingResult; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
41
|
|
|
$this->parseComments(); |
42
|
|
|
$openedParenthesis += $this->getOpenedParenthesis(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
|
|
|
46
|
|
|
if ($openedParenthesis === 0) { |
47
|
|
|
throw new UnopenedComment(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$openedParenthesis--; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->checkConsecutiveDots(); |
54
|
|
|
|
55
|
|
View Code Duplication |
if ($this->lexer->token['type'] === EmailLexer::S_DOT && |
|
|
|
|
56
|
|
|
$this->lexer->isNextToken(EmailLexer::S_AT) |
57
|
|
|
) { |
58
|
|
|
throw new DotAtEnd(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->warnEscaping(); |
62
|
|
|
$this->isInvalidToken($this->lexer->token, $closingQuote); |
63
|
|
|
|
64
|
|
|
if ($this->isFWS()) { |
65
|
|
|
$this->parseFWS(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$totalLength += strlen($this->lexer->token['value']); |
69
|
|
|
$this->lexer->moveNext(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) { |
73
|
|
|
$this->warnings[LocalTooLong::CODE] = new LocalTooLong(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new ValidEmail(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function hasDotAtStart() : bool |
80
|
|
|
{ |
81
|
|
|
return $this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function parseDoubleQuote() : Result |
85
|
|
|
{ |
86
|
|
|
$dquoteParser = new DoubleQuote($this->lexer); |
87
|
|
|
$parseAgain = $dquoteParser->parse("remove useless arg"); |
88
|
|
|
$warns = $dquoteParser->getWarnings(); |
89
|
|
|
foreach ($warns as $code => $dWarning) { |
90
|
|
|
$this->warnings[$code] = $dWarning; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $parseAgain; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param bool $closingQuote |
98
|
|
|
*/ |
99
|
|
|
protected function isInvalidToken(array $token, $closingQuote) |
100
|
|
|
{ |
101
|
|
|
$forbidden = array( |
102
|
|
|
EmailLexer::S_COMMA, |
103
|
|
|
EmailLexer::S_CLOSEBRACKET, |
104
|
|
|
EmailLexer::S_OPENBRACKET, |
105
|
|
|
EmailLexer::S_GREATERTHAN, |
106
|
|
|
EmailLexer::S_LOWERTHAN, |
107
|
|
|
EmailLexer::S_COLON, |
108
|
|
|
EmailLexer::S_SEMICOLON, |
109
|
|
|
EmailLexer::INVALID |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
if (in_array($token['type'], $forbidden) && !$closingQuote) { |
113
|
|
|
throw new ExpectingATEXT(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.