Passed
Pull Request — 3.x (#323)
by Eduardo Gulias
04:35 queued 02:42
created

EmailLexer::moveNext()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 8
nop 0
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Egulias\EmailValidator;
4
5
use Doctrine\Common\Lexer\AbstractLexer;
6
7
class EmailLexer extends AbstractLexer
8
{
9
    //ASCII values
10
    const S_EMPTY            = null;
11
    const C_NUL              = 0;
12
    const S_HTAB             = 9;
13
    const S_LF               = 10;
14
    const S_CR               = 13;
15
    const S_SP               = 32;
16
    const EXCLAMATION        = 33;
17
    const S_DQUOTE           = 34;
18
    const NUMBER_SIGN        = 35;
19
    const DOLLAR             = 36;
20
    const PERCENTAGE         = 37;
21
    const AMPERSAND          = 38;
22
    const S_SQUOTE           = 39;
23
    const S_OPENPARENTHESIS  = 40;
24
    const S_CLOSEPARENTHESIS = 41;
25
    const ASTERISK           = 42;
26
    const S_PLUS             = 43;
27
    const S_COMMA            = 44;
28
    const S_HYPHEN           = 45;
29
    const S_DOT              = 46;
30
    const S_SLASH            = 47;
31
    const S_COLON            = 58;
32
    const S_SEMICOLON        = 59;
33
    const S_LOWERTHAN        = 60;
34
    const S_EQUAL            = 61;
35
    const S_GREATERTHAN      = 62;
36
    const QUESTIONMARK       = 63;
37
    const S_AT               = 64;
38
    const S_OPENBRACKET      = 91;
39
    const S_BACKSLASH        = 92;
40
    const S_CLOSEBRACKET     = 93;
41
    const CARET              = 94;
42
    const S_UNDERSCORE       = 95;
43
    const S_BACKTICK         = 96;
44
    const S_OPENCURLYBRACES  = 123;
45
    const S_PIPE             = 124;
46
    const S_CLOSECURLYBRACES = 125;
47
    const S_TILDE            = 126;
48
    const C_DEL              = 127;
49
    const INVERT_QUESTIONMARK= 168;
50
    const INVERT_EXCLAMATION = 173;
51
    const GENERIC            = 300;
52
    const S_IPV6TAG          = 301;
53
    const INVALID            = 302;
54
    const CRLF               = 1310;
55
    const S_DOUBLECOLON      = 5858;
56
    const ASCII_INVALID_FROM = 127;
57
    const ASCII_INVALID_TO   = 199;
58
59
    /**
60
     * US-ASCII visible characters not valid for atext (@link http://tools.ietf.org/html/rfc5322#section-3.2.3)
61
     *
62
     * @var array
63
     */
64
    protected $charValue = [
65
        '{'    => self::S_OPENCURLYBRACES,
66
        '}'    => self::S_CLOSECURLYBRACES,
67
        '('    => self::S_OPENPARENTHESIS,
68
        ')'    => self::S_CLOSEPARENTHESIS,
69
        '<'    => self::S_LOWERTHAN,
70
        '>'    => self::S_GREATERTHAN,
71
        '['    => self::S_OPENBRACKET,
72
        ']'    => self::S_CLOSEBRACKET,
73
        ':'    => self::S_COLON,
74
        ';'    => self::S_SEMICOLON,
75
        '@'    => self::S_AT,
76
        '\\'   => self::S_BACKSLASH,
77
        '/'    => self::S_SLASH,
78
        ','    => self::S_COMMA,
79
        '.'    => self::S_DOT,
80
        "'"    => self::S_SQUOTE,
81
        "`"    => self::S_BACKTICK,
82
        '"'    => self::S_DQUOTE,
83
        '-'    => self::S_HYPHEN,
84
        '::'   => self::S_DOUBLECOLON,
85
        ' '    => self::S_SP,
86
        "\t"   => self::S_HTAB,
87
        "\r"   => self::S_CR,
88
        "\n"   => self::S_LF,
89
        "\r\n" => self::CRLF,
90
        'IPv6' => self::S_IPV6TAG,
91
        ''     => self::S_EMPTY,
92
        '\0'   => self::C_NUL,
93
        '*'    => self::ASTERISK,
94
        '!'    => self::EXCLAMATION,
95
        '&'    => self::AMPERSAND,
96
        '^'    => self::CARET,
97
        '$'    => self::DOLLAR,
98
        '%'    => self::PERCENTAGE,
99
        '~'    => self::S_TILDE,
100
        '|'    => self::S_PIPE,
101
        '_'    => self::S_UNDERSCORE,
102
        '='    => self::S_EQUAL,
103
        '+'    => self::S_PLUS,
104
        '¿'    => self::INVERT_QUESTIONMARK,
105
        '?'    => self::QUESTIONMARK,
106
        '#'    => self::NUMBER_SIGN,
107
        '¡'    => self::INVERT_EXCLAMATION,
108
    ];
109
110
    const INVALID_CHARS_REGEX = "/[^\p{S}\p{C}\p{Cc}]+/iu";
111
112
    const VALID_UTF8_REGEX = '/\p{Cc}+/u';
113
114
    const CATCHABLE_PATTERNS = [
115
        '[a-zA-Z]+[46]?', //ASCII and domain literal
116
        '[^\x00-\x7F]',  //UTF-8
117
        '[0-9]+',
118
        '\r\n',
119
        '::',
120
        '\s+?',
121
        '.',
122
    ];
123
124
    const NON_CATCHABLE_PATTERNS = [
125
        '[\xA0-\xff]+',
126
    ];
127
128
    const MODIFIERS = 'iu';
129
130
    /** @var bool */
131
    protected $hasInvalidTokens = false;
132
133
    /**
134
     * @var array
135
     *
136
     * @psalm-var array{value:string, type:null|int, position:int}|array<empty, empty>
137
     */
138
    protected $previous = [];
139
140
    /**
141
     * The last matched/seen token.
142
     *
143
     * @var array
144
     *
145
     * @psalm-suppress NonInvariantDocblockPropertyType
146
     * @psalm-var array{value:string, type:null|int, position:int}
147
     * @psalm-suppress NonInvariantDocblockPropertyType
148
     */
149
    public $token;
150
151
    /**
152
     * The next token in the input.
153
     *
154
     * @var array{position: int, type: int|null|string, value: int|string}|null
155
     */
156
    public $lookahead;
157
158
    /** @psalm-var array{value:'', type:null, position:0} */
159
    private static $nullToken = [
160
        'value' => '',
161
        'type' => null,
162
        'position' => 0,
163
    ];
164
165
    /** @var string */
166
    private $accumulator = '';
167
168
    /** @var bool */
169
    private $hasToRecord = false;
170
171 340
    public function __construct()
172
    {
173 340
        $this->previous = $this->token = self::$nullToken;
174 340
        $this->lookahead = null;
175
    }
176
177 308
    public function reset() : void
178
    {
179 308
        $this->hasInvalidTokens = false;
180 308
        parent::reset();
181 308
        $this->previous = $this->token = self::$nullToken;
182
    }
183
184
    /**
185
     * @param int $type
186
     * @throws \UnexpectedValueException
187
     * @return boolean
188
     *
189
     * @psalm-suppress InvalidScalarArgument
190
     */
191 54
    public function find($type) : bool
192
    {
193 54
        $search = clone $this;
194 54
        $search->skipUntil($type);
195
196 54
        if (!$search->lookahead) {
197 6
            throw new \UnexpectedValueException($type . ' not found');
198
        }
199 48
        return true;
200
    }
201
202
    /**
203
     * moveNext
204
     *
205
     * @return boolean
206
     */
207 297
    public function moveNext() : bool
208
    {
209 297
        if ($this->hasToRecord && $this->previous === self::$nullToken) {
210 179
            $this->accumulator .= $this->token['value'];
211
        }
212
213 297
        $this->previous = $this->token;
214
        
215 297
        if($this->lookahead === null) {
216 297
            $this->lookahead = self::$nullToken;
217
        }
218
219 297
        $hasNext = parent::moveNext();
220
221 297
        if ($this->hasToRecord) {
222 179
            $this->accumulator .= $this->token['value'];
223
        }
224
225 297
        return $hasNext;
226
    }
227
228
    /**
229
     * Retrieve token type. Also processes the token value if necessary.
230
     *
231
     * @param string $value
232
     * @throws \InvalidArgumentException
233
     * @return integer
234
     */
235 300
    protected function getType(&$value)
236
    {
237 300
        $encoded = $value;
238
239 300
        if (mb_detect_encoding($value, 'auto', true) !== 'UTF-8') {
240 237
            $encoded = utf8_encode($value);
241
        }
242
243 300
        if ($this->isValid($encoded)) {
244 229
            return $this->charValue[$encoded];
245
        }
246
247 262
        if ($this->isNullType($encoded)) {
248 2
            return self::C_NUL;
249
        }
250
251 261
        if ($this->isInvalidChar($encoded)) {
252 66
            $this->hasInvalidTokens = true;
253 66
            return self::INVALID;
254
        }
255
256
257 199
        return  self::GENERIC;
258
    }
259
260 300
    protected function isValid(string $value) : bool
261
    {
262 300
        return isset($this->charValue[$value]);
263
    }
264
265 262
    protected function isNullType(string $value) : bool
266
    {
267 262
        return $value === "\0";
268
    }
269
270 261
    protected function isInvalidChar(string $value) : bool
271
    {
272 261
        return !preg_match(self::INVALID_CHARS_REGEX, $value);
273
    }
274
275
    protected function isUTF8Invalid(string $value) : bool
276
    {
277
        return preg_match(self::VALID_UTF8_REGEX, $value) !== false;
278
    }
279
280 191
    public function hasInvalidTokens() : bool
281
    {
282 191
        return $this->hasInvalidTokens;
283
    }
284
285
    /**
286
     * getPrevious
287
     *
288
     * @return array
289
     */
290 178
    public function getPrevious() : array
291
    {
292 178
        return $this->previous;
293
    }
294
295
    /**
296
     * Lexical catchable patterns.
297
     *
298
     * @return string[]
299
     */
300 301
    protected function getCatchablePatterns() : array
301
    {
302 301
        return self::CATCHABLE_PATTERNS;
303
    }
304
305
    /**
306
     * Lexical non-catchable patterns.
307
     *
308
     * @return string[]
309
     */
310 301
    protected function getNonCatchablePatterns() : array
311
    {
312 301
        return self::NON_CATCHABLE_PATTERNS;
313
    }
314
315 301
    protected function getModifiers() : string
316
    {
317 301
        return self::MODIFIERS;
318
    }
319
320 151
    public function getAccumulatedValues() : string
321
    {
322 151
        return $this->accumulator;
323
    }
324
325 188
    public function startRecording() : void
326
    {
327 188
        $this->hasToRecord = true;
328
    }
329
330 148
    public function stopRecording() : void
331
    {
332 148
        $this->hasToRecord = false;
333
    }
334
335 149
    public function clearRecorded() : void
336
    {
337 149
        $this->accumulator = '';
338
    }
339
}
340