Test Failed
Pull Request — master (#267)
by
unknown
02:14
created

LocalPart::isInvalidToken()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

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