Passed
Push — 3.x ( 5cc047...007bfd )
by Eduardo Gulias
03:20
created

FoldingWhiteSpace::checkCRLFInFWS()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 0
dl 0
loc 16
ccs 5
cts 8
cp 0.625
crap 4.8437
rs 10
c 0
b 0
f 0
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
}