FoldingWhiteSpace::parse()   B
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10.0244

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 7
nop 0
dl 0
loc 32
ccs 15
cts 16
cp 0.9375
crap 10.0244
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
Egulias\EmailValidator\EmailLexer::S_CR 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 ignore-type  annotation

40
        if ($this->lexer->current->isA(/** @scrutinizer ignore-type */ EmailLexer::S_CR)) {
Loading history...
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
Bug introduced by
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 ignore-type  annotation

63
        if (!$this->lexer->current->isA(/** @scrutinizer ignore-type */ EmailLexer::CRLF)) {
Loading history...
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