1
|
|
|
<?php |
2
|
|
|
namespace Egulias\EmailValidator\Parser; |
3
|
|
|
|
4
|
|
|
use Egulias\EmailValidator\EmailLexer; |
5
|
|
|
use Egulias\EmailValidator\Warning\CFWSNearAt; |
6
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
7
|
|
|
use Egulias\EmailValidator\Warning\CFWSWithFWS; |
8
|
|
|
use Egulias\EmailValidator\Result\Reason\CRNoLF; |
9
|
|
|
use Egulias\EmailValidator\Result\Reason\AtextAfterCFWS; |
10
|
|
|
use Egulias\EmailValidator\Result\Reason\CRLFAtTheEnd; |
11
|
|
|
use Egulias\EmailValidator\Result\Reason\CRLFX2; |
12
|
|
|
use Egulias\EmailValidator\Result\Reason\ExpectingCTEXT; |
13
|
|
|
use Egulias\EmailValidator\Result\Result; |
14
|
|
|
use Egulias\EmailValidator\Result\ValidEmail; |
15
|
|
|
|
16
|
|
|
class FoldingWhiteSpace extends Parser |
17
|
|
|
{ |
18
|
|
|
public function parse() : Result |
19
|
|
|
{ |
20
|
|
|
if (!$this->isFWS()) { |
21
|
|
|
return new ValidEmail(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$previous = $this->lexer->getPrevious(); |
25
|
|
|
|
26
|
|
|
$this->checkCRLFInFWS(); |
27
|
|
|
|
28
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_CR) { |
29
|
|
|
return new InvalidEmail(new CRNoLF(), $this->lexer->token['value']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
|
|
|
33
|
|
|
return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->token['value']); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
|
|
|
37
|
|
|
return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->token['value']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
41
|
|
|
$this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
42
|
|
|
} else { |
43
|
|
|
$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return new ValidEmail(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return InvalidEmail|ValidEmail|null |
51
|
|
|
*/ |
52
|
|
|
protected function checkCRLFInFWS() |
53
|
|
|
{ |
54
|
|
|
if ($this->lexer->token['type'] !== EmailLexer::CRLF) { |
55
|
|
|
return new ValidEmail(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
|
|
|
59
|
|
|
return new InvalidEmail(new CRLFX2(), $this->lexer->token['value']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
//this has no coverage. Condition is repeated from above one |
63
|
|
View Code Duplication |
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
|
|
|
64
|
|
|
return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->token['value']); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
protected function isFWS() |
72
|
|
|
{ |
73
|
|
|
if ($this->escaped()) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->lexer->token['type'] === EmailLexer::S_SP || |
78
|
|
|
$this->lexer->token['type'] === EmailLexer::S_HTAB || |
79
|
|
|
$this->lexer->token['type'] === EmailLexer::S_CR || |
80
|
|
|
$this->lexer->token['type'] === EmailLexer::S_LF || |
81
|
|
|
$this->lexer->token['type'] === EmailLexer::CRLF; |
82
|
|
|
} |
83
|
|
|
} |
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.