Passed
Push — master ( ade688...2f38a4 )
by Eduardo Gulias
02:31
created

EmailValidator/Parser/LocalPart.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 130
    public function parse($localPart)
18
    {
19 130
        $parseDQuote = true;
20 130
        $closingQuote = false;
21 130
        $openedParenthesis = 0;
22 130
        $totalLength = 0;
23
24 130
        while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
25 130 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']) {
26 2
                throw new DotAtStart();
27
            }
28
29 128
            $closingQuote = $this->checkDQUOTE($closingQuote);
30 127
            if ($closingQuote && $parseDQuote) {
31 20
                $parseDQuote = $this->parseDoubleQuote();
32 12
            }
33
34 119
            if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
35 7
                $this->parseComments();
36 3
                $openedParenthesis += $this->getOpenedParenthesis();
37 3
            }
38
39 116 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
0 ignored issues
show
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 116
            $this->checkConsecutiveDots();
48
49 116 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
50 3
                $this->lexer->isNextToken(EmailLexer::S_AT)
51 116
            ) {
52 2
                throw new DotAtEnd();
53
            }
54
55 116
            $this->warnEscaping();
56 111
            $this->isInvalidToken($this->lexer->token, $closingQuote);
57
58 110
            if ($this->isFWS()) {
59 7
                $this->parseFWS();
60 3
            }
61
62 110
            $totalLength += strlen($this->lexer->token['value']);
63 110
            $this->lexer->moveNext();
64 110
        }
65
66 93
        if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) {
67 3
            $this->warnings[LocalTooLong::CODE] = new LocalTooLong();
68 3
        }
69 93
    }
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)) {
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 111
    protected function isInvalidToken(array $token, $closingQuote)
129
    {
130
        $forbidden = array(
131 111
            EmailLexer::S_COMMA,
132 111
            EmailLexer::S_CLOSEBRACKET,
133 111
            EmailLexer::S_OPENBRACKET,
134 111
            EmailLexer::S_GREATERTHAN,
135 111
            EmailLexer::S_LOWERTHAN,
136 111
            EmailLexer::S_COLON,
137 111
            EmailLexer::S_SEMICOLON,
138
            EmailLexer::INVALID
139 111
        );
140
141 111
        if (in_array($token['type'], $forbidden) && !$closingQuote) {
142 4
            throw new ExpectingATEXT();
143
        }
144 110
    }
145
}
146