Test Setup Failed
Push — 3.0.0-dev ( 7e0ee1...302b98 )
by Eduardo Gulias
01:56
created

LocalPart::parseLocalFWS()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace Egulias\EmailValidator\Parser;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Result\Result;
7
use Egulias\EmailValidator\Result\ValidEmail;
8
use Egulias\EmailValidator\Result\InvalidEmail;
9
use Egulias\EmailValidator\Warning\LocalTooLong;
10
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
11
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
12
use Egulias\EmailValidator\Result\Reason\DotAtStart;
13
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT as ReasonExpectingATEXT;
14
15
class LocalPart extends Parser
16
{
17
18
    private $invalidTokens = array(
19
            EmailLexer::S_COMMA => EmailLexer::S_COMMA,
20
            EmailLexer::S_CLOSEBRACKET => EmailLexer::S_CLOSEBRACKET,
21
            EmailLexer::S_OPENBRACKET => EmailLexer::S_OPENBRACKET,
22
            EmailLexer::S_GREATERTHAN => EmailLexer::S_GREATERTHAN,
23
            EmailLexer::S_LOWERTHAN => EmailLexer::S_LOWERTHAN,
24
            EmailLexer::S_COLON => EmailLexer::S_COLON,
25
            EmailLexer::S_SEMICOLON => EmailLexer::S_SEMICOLON,
26
            EmailLexer::INVALID => EmailLexer::INVALID
27
        );
28
29
    private $foldingWS;
30
31
    public function parse($localPart) : Result
32
    {
33
        $totalLength = 0;
34
        $this->foldingWS = new FoldingWhiteSpace($this->lexer);
35
36
        while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
37
            if ($this->hasDotAtStart()) {
38
                return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']);
39
            }
40
41
            if ($this->lexer->token['type'] === EmailLexer::S_DQUOTE) {
42
                $dquoteParsingResult = $this->parseDoubleQuote();
43
44
                //Invalid double quote parsing
45
                if($dquoteParsingResult->isInvalid()) {
46
                    return $dquoteParsingResult;
47
                }
48
            }
49
50
            if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS || 
51
                $this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS ) {
52
                $commentsResult = $this->parseComments();
53
54
                //Invalid comment parsing
55
                if($commentsResult->isInvalid()) {
56
                    return $commentsResult;
57
                }
58
            }
59
60 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
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...
61
                return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']);
62
            }
63
64 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
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...
65
                $this->lexer->isNextToken(EmailLexer::S_AT)
66
            ) {
67
                return new InvalidEmail(new DotAtEnd(), $this->lexer->token['value']);
68
            }
69
70
            $resultEscaping = $this->validateEscaping();
71
            if ($resultEscaping->isInvalid()) {
72
                return $resultEscaping;
73
            }
74
75 View Code Duplication
            if (isset($this->invalidTokens[$this->lexer->token['type']])) {
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...
76
                return new InvalidEmail(new ReasonExpectingATEXT('Invalid token found'), $this->lexer->token['value']);
77
            }
78
79
            $resultFWS = $this->parseLocalFWS();
80
            if($resultFWS->isInvalid()) {
81
                return $resultFWS;
82
            }
83
84
            $totalLength += strlen($this->lexer->token['value']);
85
            $this->lexer->moveNext();
86
        }
87
88
        if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) {
89
            $this->warnings[LocalTooLong::CODE] = new LocalTooLong();
90
        }
91
92
        return new ValidEmail();
93
    }
94
95
    protected function parseLocalFWS() : Result 
96
    {
97
        $resultFWS = $this->foldingWS->parse('remove');
98
        if ($resultFWS->isValid()) {
99
            $warns = $this->foldingWS->getWarnings();
100
            foreach ($warns as $code => $dWarning) {
101
                $this->warnings[$code] = $dWarning;
102
            }
103
        }
104
        return $resultFWS;
105
    }
106
107
    protected function hasDotAtStart() : bool
108
    {
109
            return $this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type'];
110
    }
111
112 View Code Duplication
    protected function parseDoubleQuote() : Result
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
113
    {
114
        $dquoteParser = new DoubleQuote($this->lexer);
115
        $parseAgain = $dquoteParser->parse("remove useless arg");
116
        $warns = $dquoteParser->getWarnings();
117
        foreach ($warns as $code => $dWarning) {
118
            $this->warnings[$code] = $dWarning;
119
        }
120
121
        return $parseAgain;
122
    }
123
124 View Code Duplication
    protected function parseComments()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
125
    {
126
        $commentParser = new Comment($this->lexer);
127
        $result = $commentParser->parse('remove');
128
        if($result->isInvalid()) {
129
            return $result;
130
        }
131
        $warns = $commentParser->getWarnings();
132
        foreach ($warns as $code => $dWarning) {
133
            $this->warnings[$code] = $dWarning;
134
        }
135
        return $result;
136
    }
137
}