1 | <?php |
||||
2 | |||||
3 | namespace Egulias\EmailValidator\Parser; |
||||
4 | |||||
5 | use Egulias\EmailValidator\EmailLexer; |
||||
6 | use Egulias\EmailValidator\Warning\CFWSNearAt; |
||||
7 | use Egulias\EmailValidator\Result\InvalidEmail; |
||||
8 | use Egulias\EmailValidator\Warning\CFWSWithFWS; |
||||
9 | use Egulias\EmailValidator\Result\Reason\CRNoLF; |
||||
10 | use Egulias\EmailValidator\Result\Reason\AtextAfterCFWS; |
||||
11 | use Egulias\EmailValidator\Result\Reason\CRLFAtTheEnd; |
||||
12 | use Egulias\EmailValidator\Result\Reason\CRLFX2; |
||||
13 | use Egulias\EmailValidator\Result\Reason\ExpectingCTEXT; |
||||
14 | use Egulias\EmailValidator\Result\Result; |
||||
15 | use Egulias\EmailValidator\Result\ValidEmail; |
||||
16 | |||||
17 | class FoldingWhiteSpace extends PartParser |
||||
18 | { |
||||
19 | public const FWS_TYPES = [ |
||||
20 | EmailLexer::S_SP, |
||||
21 | EmailLexer::S_HTAB, |
||||
22 | EmailLexer::S_CR, |
||||
23 | EmailLexer::S_LF, |
||||
24 | EmailLexer::CRLF |
||||
25 | ]; |
||||
26 | 167 | ||||
27 | public function parse(): Result |
||||
28 | 167 | { |
|||
29 | 166 | if (!$this->isFWS()) { |
|||
30 | return new ValidEmail(); |
||||
31 | } |
||||
32 | 22 | ||||
33 | $previous = $this->lexer->getPrevious(); |
||||
34 | 22 | ||||
35 | 22 | $resultCRLF = $this->checkCRLFInFWS(); |
|||
36 | 1 | if ($resultCRLF->isInvalid()) { |
|||
37 | return $resultCRLF; |
||||
38 | } |
||||
39 | 21 | ||||
40 | 3 | if ($this->lexer->current->isA(EmailLexer::S_CR)) { |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
41 | return new InvalidEmail(new CRNoLF(), $this->lexer->current->value); |
||||
42 | } |
||||
43 | 18 | ||||
44 | 8 | if ($this->lexer->isNextToken(EmailLexer::GENERIC) && !$previous->isA(EmailLexer::S_AT)) { |
|||
45 | return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->current->value); |
||||
46 | } |
||||
47 | 12 | ||||
48 | if ($this->lexer->current->isA(EmailLexer::S_LF) || $this->lexer->current->isA(EmailLexer::C_NUL)) { |
||||
49 | return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->current->value); |
||||
50 | } |
||||
51 | 12 | ||||
52 | 3 | if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous->isA(EmailLexer::S_AT)) { |
|||
53 | $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
||||
54 | 9 | } else { |
|||
55 | $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
||||
56 | } |
||||
57 | 12 | ||||
58 | return new ValidEmail(); |
||||
59 | } |
||||
60 | 22 | ||||
61 | protected function checkCRLFInFWS(): Result |
||||
62 | 22 | { |
|||
63 | 21 | if (!$this->lexer->current->isA(EmailLexer::CRLF)) { |
|||
0 ignored issues
–
show
Egulias\EmailValidator\EmailLexer::CRLF of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $types of Doctrine\Common\Lexer\Token::isA() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
64 | return new ValidEmail(); |
||||
65 | } |
||||
66 | 1 | ||||
67 | 1 | if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|||
68 | return new InvalidEmail(new CRLFX2(), $this->lexer->current->value); |
||||
69 | } |
||||
70 | |||||
71 | //this has no coverage. Condition is repeated from above one |
||||
72 | if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
||||
73 | return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->current->value); |
||||
74 | } |
||||
75 | |||||
76 | return new ValidEmail(); |
||||
77 | } |
||||
78 | 167 | ||||
79 | protected function isFWS(): bool |
||||
80 | 167 | { |
|||
81 | 1 | if ($this->escaped()) { |
|||
82 | return false; |
||||
83 | } |
||||
84 | 167 | ||||
85 | return in_array($this->lexer->current->type, self::FWS_TYPES); |
||||
86 | } |
||||
87 | } |
||||
88 |