Test Setup Failed
Push — 3.0.0-dev ( 442f13...4e7276 )
by Eduardo Gulias
01:46
created

LocalPart::hasDotAtStart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Egulias\EmailValidator\Parser;
4
5
use Egulias\EmailValidator\Exception\DotAtEnd;
6
use Egulias\EmailValidator\EmailLexer;
7
use Egulias\EmailValidator\Exception\ExpectingAT;
8
use Egulias\EmailValidator\Exception\ExpectingATEXT;
9
use Egulias\EmailValidator\Exception\UnclosedQuotedString;
10
use Egulias\EmailValidator\Exception\UnopenedComment;
11
use Egulias\EmailValidator\Result\InvalidEmail;
12
use Egulias\EmailValidator\Result\Reason\DotAtStart;
13
use Egulias\EmailValidator\Result\Result;
14
use Egulias\EmailValidator\Result\ValidEmail;
15
use Egulias\EmailValidator\Warning\CFWSWithFWS;
16
use Egulias\EmailValidator\Warning\LocalTooLong;
17
18
class LocalPart extends Parser
19
{
20
    public function parse($localPart) : Result
21
    {
22
        $parseDQuote = true;
23
        $closingQuote = false;
24
        $openedParenthesis = 0;
25
        $totalLength = 0;
26
27
        while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
28
            if ($this->hasDotAtStart()) {
29
                return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']);
30
            }
31
32
            $closingQuote = $this->checkDQUOTE($closingQuote);
33
            if ($closingQuote && $parseDQuote) {
34
                $parseDQuote = $this->parseDoubleQuote();
35
            }
36
37
            if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
38
                $this->parseComments();
39
                $openedParenthesis += $this->getOpenedParenthesis();
40
            }
41
42 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
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...
43
                if ($openedParenthesis === 0) {
44
                    throw new UnopenedComment();
45
                }
46
47
                $openedParenthesis--;
48
            }
49
50
            $this->checkConsecutiveDots();
51
52 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...
53
                $this->lexer->isNextToken(EmailLexer::S_AT)
54
            ) {
55
                throw new DotAtEnd();
56
            }
57
58
            $this->warnEscaping();
59
            $this->isInvalidToken($this->lexer->token, $closingQuote);
60
61
            if ($this->isFWS()) {
62
                $this->parseFWS();
63
            }
64
65
            $totalLength += strlen($this->lexer->token['value']);
66
            $this->lexer->moveNext();
67
        }
68
69
        if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) {
70
            $this->warnings[LocalTooLong::CODE] = new LocalTooLong();
71
        }
72
73
        return new ValidEmail();
74
    }
75
76
    protected function hasDotAtStart() : bool
77
    {
78
            return $this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type'];
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    protected function parseDoubleQuote()
85
    {
86
        $parseAgain = true;
87
        $special = array(
88
            EmailLexer::S_CR => true,
89
            EmailLexer::S_HTAB => true,
90
            EmailLexer::S_LF => true
91
        );
92
93
        $invalid = array(
94
            EmailLexer::C_NUL => true,
95
            EmailLexer::S_HTAB => true,
96
            EmailLexer::S_CR => true,
97
            EmailLexer::S_LF => true
98
        );
99
        $setSpecialsWarning = true;
100
101
        $this->lexer->moveNext();
102
103
        while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) {
104
            $parseAgain = false;
105
            if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) {
106
                $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
107
                $setSpecialsWarning = false;
108
            }
109 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH && $this->lexer->isNextToken(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...
110
                $this->lexer->moveNext();
111
            }
112
113
            $this->lexer->moveNext();
114
115
            if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) {
116
                throw new ExpectingATEXT();
117
            }
118
        }
119
120
        $prev = $this->lexer->getPrevious();
121
122
        if ($prev['type'] === EmailLexer::S_BACKSLASH) {
123
            if (!$this->checkDQUOTE(false)) {
124
                throw new UnclosedQuotedString();
125
            }
126
        }
127
128
        if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) {
129
            throw new ExpectingAT();
130
        }
131
132
        return $parseAgain;
133
    }
134
135
    /**
136
     * @param bool $closingQuote
137
     */
138
    protected function isInvalidToken(array $token, $closingQuote)
139
    {
140
        $forbidden = array(
141
            EmailLexer::S_COMMA,
142
            EmailLexer::S_CLOSEBRACKET,
143
            EmailLexer::S_OPENBRACKET,
144
            EmailLexer::S_GREATERTHAN,
145
            EmailLexer::S_LOWERTHAN,
146
            EmailLexer::S_COLON,
147
            EmailLexer::S_SEMICOLON,
148
            EmailLexer::INVALID
149
        );
150
151
        if (in_array($token['type'], $forbidden) && !$closingQuote) {
152
            throw new ExpectingATEXT();
153
        }
154
    }
155
}
156