Passed
Push — message-id-validator ( 1cb5b2...07464f )
by Eduardo Gulias
02:11
created

FoldingWhiteSpace::parse()   B

Complexity

Conditions 10
Paths 7

Size

Total Lines 33

Duplication

Lines 6
Ratio 18.18 %

Code Coverage

Tests 15
CRAP Score 10.1626

Importance

Changes 0
Metric Value
dl 6
loc 33
ccs 15
cts 17
cp 0.8824
rs 7.6666
c 0
b 0
f 0
cc 10
nc 7
nop 0
crap 10.1626

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
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 161
    public function parse() : Result
19
    {
20 161
        if (!$this->isFWS()) {
21 161
            return new ValidEmail();
22
        }
23
24 20
        $previous = $this->lexer->getPrevious();
25
26 20
        $resultCRLF = $this->checkCRLFInFWS();
27 20
        if ($resultCRLF->isInvalid()) {
28
            return $resultCRLF;
29
        }
30
31 20
        if ($this->lexer->token['type'] === EmailLexer::S_CR) {
32 3
            return new InvalidEmail(new CRNoLF(), $this->lexer->token['value']);
33
        }
34
35 17 View Code Duplication
        if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type']  !== EmailLexer::S_AT) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
36 7
            return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->token['value']);
37
        }
38
39 12 View Code Duplication
        if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
40
            return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->token['value']);
41
        }
42
43 12
        if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type']  === EmailLexer::S_AT) {
44 3
            $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
45
        } else {
46 9
            $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
47
        }
48
49 12
        return new ValidEmail();
50
    }
51
52 20
    protected function checkCRLFInFWS() : Result
53
    {
54 20
        if ($this->lexer->token['type'] !== EmailLexer::CRLF) {
55 20
            return new ValidEmail();
56
        }
57
58 View Code Duplication
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
64
            return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->token['value']);
65
        }
66
67
        return new ValidEmail();
68
    }
69
     
70 161
    protected function isFWS() : bool
71
    {
72 161
        if ($this->escaped()) {
73 1
            return false;
74
        }
75
76 161
        return $this->lexer->token['type'] === EmailLexer::S_SP ||
77 161
            $this->lexer->token['type'] === EmailLexer::S_HTAB ||
78 161
            $this->lexer->token['type'] === EmailLexer::S_CR ||
79 161
            $this->lexer->token['type'] === EmailLexer::S_LF ||
80 161
            $this->lexer->token['type'] === EmailLexer::CRLF;
81
    }
82
}