Test Setup Failed
Push — 3.0.0-dev ( e2e234...b76c11 )
by Eduardo Gulias
01:56
created

LocalPart::validateEscaping()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 3
Ratio 17.65 %

Importance

Changes 0
Metric Value
dl 3
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
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;
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
    public function parse() : Result
30
    {
31
        $totalLength = 0;
32
33
        while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
34
            if ($this->hasDotAtStart()) {
35
                return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']);
36
            }
37
38 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_DQUOTE) {
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...
39
                $dquoteParsingResult = $this->parseDoubleQuote();
40
41
                //Invalid double quote parsing
42
                if($dquoteParsingResult->isInvalid()) {
43
                    return $dquoteParsingResult;
44
                }
45
            }
46
47 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS || 
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...
48
                $this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS ) {
49
                $commentsResult = $this->parseComments();
50
51
                //Invalid comment parsing
52
                if($commentsResult->isInvalid()) {
53
                    return $commentsResult;
54
                }
55
            }
56
57 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...
58
                return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']);
59
            }
60
61 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...
62
                $this->lexer->isNextToken(EmailLexer::S_AT)
63
            ) {
64
                return new InvalidEmail(new DotAtEnd(), $this->lexer->token['value']);
65
            }
66
67
            $resultEscaping = $this->validateEscaping();
68
            if ($resultEscaping->isInvalid()) {
69
                return $resultEscaping;
70
            }
71
72 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...
73
                return new InvalidEmail(new ExpectingATEXT('Invalid token found'), $this->lexer->token['value']);
74
            }
75
76
            $resultFWS = $this->parseLocalFWS();
77
            if($resultFWS->isInvalid()) {
78
                return $resultFWS;
79
            }
80
81
            $totalLength += strlen($this->lexer->token['value']);
82
            $this->lexer->moveNext();
83
        }
84
85
        if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) {
86
            $this->warnings[LocalTooLong::CODE] = new LocalTooLong();
87
        }
88
89
        return new ValidEmail();
90
    }
91
92 View Code Duplication
    private function parseLocalFWS() : 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...
93
    {
94
        //use $this->parseFWS()
95
        $foldingWS = new FoldingWhiteSpace($this->lexer);
96
        $resultFWS = $foldingWS->parse();
97
        if ($resultFWS->isValid()) {
98
            $this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
99
        }
100
        return $resultFWS;
101
    }
102
103
    private function hasDotAtStart() : bool
104
    {
105
            return $this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type'];
106
    }
107
108 View Code Duplication
    private 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...
109
    {
110
        $dquoteParser = new DoubleQuote($this->lexer);
111
        $parseAgain = $dquoteParser->parse();
112
        $this->warnings = array_merge($this->warnings, $dquoteParser->getWarnings());
113
114
        return $parseAgain;
115
    }
116
117 View Code Duplication
    private function parseComments(): 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...
118
    {
119
        $commentParser = new Comment($this->lexer, new LocalComment());
120
        $result = $commentParser->parse();
121
        $this->warnings = array_merge($this->warnings, $commentParser->getWarnings());
122
        if($result->isInvalid()) {
123
            return $result;
124
        }
125
        return $result;
126
    }
127
128
    private function validateEscaping() : Result
129
    {
130
        //Backslash found
131
        if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
132
            return new ValidEmail();
133
        }
134
135
        if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
136
            return new InvalidEmail(new ExpectingATEXT('Found ATOM after escaping'), $this->lexer->token['value']);
137
        }
138
139 View Code Duplication
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
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...
140
            return new ValidEmail();
141
        }
142
143
        return new ValidEmail();
144
    }
145
}