Test Failed
Pull Request — 3.x (#306)
by
unknown
02:04
created

FoldingWhiteSpace   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 69
ccs 25
cts 29
cp 0.8621
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkCRLFInFWS() 0 16 4
B parse() 0 32 10
A isFWS() 0 7 2
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 PartParser
17
{
18
    const FWS_TYPES = [
19
        EmailLexer::S_SP,
20
        EmailLexer::S_HTAB,
21
        EmailLexer::S_CR,
22
        EmailLexer::S_LF,
23
        EmailLexer::CRLF
24
    ];
25
26 167
    public function parse() : Result
27
    {
28 167
        if (!$this->isFWS()) {
29 166
            return new ValidEmail();
30
        }
31
32 22
        $previous = $this->lexer->getPrevious();
33
34 22
        $resultCRLF = $this->checkCRLFInFWS();
35 22
        if ($resultCRLF->isInvalid()) {
36 1
            return $resultCRLF;
37
        }
38
39 21
        if ($this->lexer->token['type'] === EmailLexer::S_CR) {
40 3
            return new InvalidEmail(new CRNoLF(), $this->lexer->token['value']);
41
        }
42
43 18
        if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type']  !== EmailLexer::S_AT) {
44 8
            return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->token['value']);
45
        }
46
47 12
        if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) {
48
            return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->token['value']);
49
        }
50
51 12
        if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type']  === EmailLexer::S_AT) {
52 3
            $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
53
        } else {
54 9
            $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
55
        }
56
57 12
        return new ValidEmail();
58
    }
59
60 22
    protected function checkCRLFInFWS() : Result
61
    {
62 22
        if ($this->lexer->token['type'] !== EmailLexer::CRLF) {
63 21
            return new ValidEmail();
64
        }
65
66 1
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
67 1
            return new InvalidEmail(new CRLFX2(), $this->lexer->token['value']);
68
        }
69
70
        //this has no coverage. Condition is repeated from above one
71
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
72
            return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->token['value']);
73
        }
74
75
        return new ValidEmail();
76
    }
77
     
78 167
    protected function isFWS() : bool
79
    {
80 167
        if ($this->escaped()) {
81 1
            return false;
82
        }
83
84 167
        return in_array($this->lexer->token['type'], self::FWS_TYPES);
85
    }
86
}