Test Failed
Pull Request — 3.x (#339)
by
unknown
01:46
created

EmailLexer::startRecording()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Egulias\EmailValidator;
4
5
use Doctrine\Common\Lexer\AbstractLexer;
6
use Doctrine\Common\Lexer\Token;
7
8
/** @extends AbstractLexer<int, string> */
9
class EmailLexer extends AbstractLexer
10
{
11
    //ASCII values
12
    public const S_EMPTY            = null;
13
    public const C_NUL              = 0;
14
    public const S_HTAB             = 9;
15
    public const S_LF               = 10;
16
    public const S_CR               = 13;
17
    public const S_SP               = 32;
18
    public const EXCLAMATION        = 33;
19
    public const S_DQUOTE           = 34;
20
    public const NUMBER_SIGN        = 35;
21
    public const DOLLAR             = 36;
22
    public const PERCENTAGE         = 37;
23
    public const AMPERSAND          = 38;
24
    public const S_SQUOTE           = 39;
25
    public const S_OPENPARENTHESIS  = 40;
26
    public const S_CLOSEPARENTHESIS = 41;
27
    public const ASTERISK           = 42;
28
    public const S_PLUS             = 43;
29
    public const S_COMMA            = 44;
30
    public const S_HYPHEN           = 45;
31
    public const S_DOT              = 46;
32
    public const S_SLASH            = 47;
33
    public const S_COLON            = 58;
34
    public const S_SEMICOLON        = 59;
35
    public const S_LOWERTHAN        = 60;
36
    public const S_EQUAL            = 61;
37
    public const S_GREATERTHAN      = 62;
38
    public const QUESTIONMARK       = 63;
39
    public const S_AT               = 64;
40
    public const S_OPENBRACKET      = 91;
41
    public const S_BACKSLASH        = 92;
42
    public const S_CLOSEBRACKET     = 93;
43
    public const CARET              = 94;
44
    public const S_UNDERSCORE       = 95;
45
    public const S_BACKTICK         = 96;
46
    public const S_OPENCURLYBRACES  = 123;
47
    public const S_PIPE             = 124;
48
    public const S_CLOSECURLYBRACES = 125;
49
    public const S_TILDE            = 126;
50
    public const C_DEL              = 127;
51
    public const INVERT_QUESTIONMARK = 168;
52
    public const INVERT_EXCLAMATION = 173;
53
    public const GENERIC            = 300;
54
    public const S_IPV6TAG          = 301;
55
    public const INVALID            = 302;
56
    public const CRLF               = 1310;
57
    public const S_DOUBLECOLON      = 5858;
58
    public const ASCII_INVALID_FROM = 127;
59
    public const ASCII_INVALID_TO   = 199;
60
61
    /**
62
     * US-ASCII visible characters not valid for atext (@link http://tools.ietf.org/html/rfc5322#section-3.2.3)
63
     *
64
     * @var array
65
     */
66
    protected $charValue = [
67
        '{'    => self::S_OPENCURLYBRACES,
68
        '}'    => self::S_CLOSECURLYBRACES,
69
        '('    => self::S_OPENPARENTHESIS,
70
        ')'    => self::S_CLOSEPARENTHESIS,
71
        '<'    => self::S_LOWERTHAN,
72
        '>'    => self::S_GREATERTHAN,
73
        '['    => self::S_OPENBRACKET,
74
        ']'    => self::S_CLOSEBRACKET,
75
        ':'    => self::S_COLON,
76
        ';'    => self::S_SEMICOLON,
77
        '@'    => self::S_AT,
78
        '\\'   => self::S_BACKSLASH,
79
        '/'    => self::S_SLASH,
80
        ','    => self::S_COMMA,
81
        '.'    => self::S_DOT,
82
        "'"    => self::S_SQUOTE,
83
        "`"    => self::S_BACKTICK,
84
        '"'    => self::S_DQUOTE,
85
        '-'    => self::S_HYPHEN,
86
        '::'   => self::S_DOUBLECOLON,
87
        ' '    => self::S_SP,
88
        "\t"   => self::S_HTAB,
89
        "\r"   => self::S_CR,
90
        "\n"   => self::S_LF,
91
        "\r\n" => self::CRLF,
92
        'IPv6' => self::S_IPV6TAG,
93
        ''     => self::S_EMPTY,
94
        '\0'   => self::C_NUL,
95
        '*'    => self::ASTERISK,
96
        '!'    => self::EXCLAMATION,
97
        '&'    => self::AMPERSAND,
98
        '^'    => self::CARET,
99
        '$'    => self::DOLLAR,
100
        '%'    => self::PERCENTAGE,
101
        '~'    => self::S_TILDE,
102
        '|'    => self::S_PIPE,
103
        '_'    => self::S_UNDERSCORE,
104
        '='    => self::S_EQUAL,
105
        '+'    => self::S_PLUS,
106
        '¿'    => self::INVERT_QUESTIONMARK,
107
        '?'    => self::QUESTIONMARK,
108
        '#'    => self::NUMBER_SIGN,
109
        '¡'    => self::INVERT_EXCLAMATION,
110
    ];
111
112
    public const INVALID_CHARS_REGEX = "/[^\p{S}\p{C}\p{Cc}]+/iu";
113
114
    public const VALID_UTF8_REGEX = '/\p{Cc}+/u';
115
116
    public const CATCHABLE_PATTERNS = [
117
        '[a-zA-Z]+[46]?', //ASCII and domain literal
118
        '[^\x00-\x7F]',  //UTF-8
119
        '[0-9]+',
120
        '\r\n',
121
        '::',
122
        '\s+?',
123
        '.',
124
    ];
125
126
    public const NON_CATCHABLE_PATTERNS = [
127
        '[\xA0-\xff]+',
128
    ];
129
130
    public const MODIFIERS = 'iu';
131
132
    /** @var bool */
133
    protected $hasInvalidTokens = false;
134
135
    /**
136
     * @var Token<int, string>
137
     */
138
    protected $previous;
139
140
    /**
141
     * The last matched/seen token.
142
     *
143
     * @var Token<int, string>
144
     */
145
    public $token;
146
147
    /**
148
     * The next token in the input.
149
     *
150
     * @var Token<int, string>|null
151
     */
152
    public $lookahead;
153
154
    /**
155
     * @var Token<int, string>
156
     */
157
    private $nullToken;
158
159
    /** @var string */
160
    private $accumulator = '';
161
162
    /** @var bool */
163
    private $hasToRecord = false;
164
165 340
    public function __construct()
166
    {
167
        /** @var Token<int, string> $nullToken */
168 340
        $nullToken = new Token((string)'', null, 0);
0 ignored issues
show
Bug introduced by
(string)'' of type string is incompatible with the type Doctrine\Common\Lexer\V expected by parameter $value of Doctrine\Common\Lexer\Token::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
        $nullToken = new Token(/** @scrutinizer ignore-type */ (string)'', null, 0);
Loading history...
169 340
        $this->nullToken = $nullToken;
170
171 340
        $this->previous = $this->token = $this->nullToken;
172 340
        $this->lookahead = null;
173
    }
174
175 308
    public function reset(): void
176
    {
177 308
        $this->hasInvalidTokens = false;
178 308
        parent::reset();
179 308
        $this->previous = $this->token = $this->nullToken;
180
    }
181
182
    /**
183
     * @param int $type
184
     * @throws \UnexpectedValueException
185
     * @return boolean
186
     *
187
     * @psalm-suppress InvalidScalarArgument
188
     */
189 54
    public function find($type): bool
190
    {
191 54
        $search = clone $this;
192 54
        $search->skipUntil($type);
0 ignored issues
show
Bug introduced by
$type of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $type of Doctrine\Common\Lexer\AbstractLexer::skipUntil(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

192
        $search->skipUntil(/** @scrutinizer ignore-type */ $type);
Loading history...
193
194 54
        if (!$search->lookahead) {
195 6
            throw new \UnexpectedValueException($type . ' not found');
196
        }
197 48
        return true;
198
    }
199
200
    /**
201
     * moveNext
202
     *
203
     * @return boolean
204
     */
205 297
    public function moveNext(): bool
206
    {
207 297
        if ($this->hasToRecord && $this->previous === $this->nullToken) {
208 179
            $this->accumulator .= $this->token->value;
209
        }
210
211 297
        $this->previous = $this->token;
212
213 297
        if ($this->lookahead === null) {
214 297
            $this->lookahead = $this->nullToken;
215
        }
216
217 297
        $hasNext = parent::moveNext();
218
219 297
        if ($this->hasToRecord) {
220 179
            $this->accumulator .= $this->token->value;
221
        }
222
223 297
        return $hasNext;
224
    }
225
226
    /**
227
     * Retrieve token type. Also processes the token value if necessary.
228
     *
229
     * @param string $value
230
     * @throws \InvalidArgumentException
231
     * @return integer
232
     */
233 300
    protected function getType(&$value): int
234
    {
235 300
        $encoded = $value;
236
237 300
        if (mb_detect_encoding($value, 'auto', true) !== 'UTF-8') {
238 237
            $encoded = mb_convert_encoding($value, 'UTF-8', 'Windows-1252');
239
        }
240
241 300
        if ($this->isValid($encoded)) {
0 ignored issues
show
Bug introduced by
It seems like $encoded can also be of type array; however, parameter $value of Egulias\EmailValidator\EmailLexer::isValid() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

241
        if ($this->isValid(/** @scrutinizer ignore-type */ $encoded)) {
Loading history...
242 229
            return $this->charValue[$encoded];
243
        }
244
245 262
        if ($this->isNullType($encoded)) {
0 ignored issues
show
Bug introduced by
It seems like $encoded can also be of type array; however, parameter $value of Egulias\EmailValidator\EmailLexer::isNullType() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

245
        if ($this->isNullType(/** @scrutinizer ignore-type */ $encoded)) {
Loading history...
246 2
            return self::C_NUL;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::C_NUL returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
247
        }
248
249 261
        if ($this->isInvalidChar($encoded)) {
0 ignored issues
show
Bug introduced by
It seems like $encoded can also be of type array; however, parameter $value of Egulias\EmailValidator\EmailLexer::isInvalidChar() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

249
        if ($this->isInvalidChar(/** @scrutinizer ignore-type */ $encoded)) {
Loading history...
250 66
            $this->hasInvalidTokens = true;
251 66
            return self::INVALID;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::INVALID returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
252
        }
253
254
255 199
        return  self::GENERIC;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::GENERIC returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
256
    }
257
258 300
    protected function isValid(string $value): bool
259
    {
260 300
        return isset($this->charValue[$value]);
261
    }
262
263 262
    protected function isNullType(string $value): bool
264
    {
265 262
        return $value === "\0";
266
    }
267
268 261
    protected function isInvalidChar(string $value): bool
269
    {
270 261
        return !preg_match(self::INVALID_CHARS_REGEX, $value);
271
    }
272
273
    protected function isUTF8Invalid(string $value): bool
274
    {
275
        return preg_match(self::VALID_UTF8_REGEX, $value) !== false;
276
    }
277
278 191
    public function hasInvalidTokens(): bool
279
    {
280 191
        return $this->hasInvalidTokens;
281
    }
282
283
    /**
284
     * getPrevious
285
     *
286
     * @return Token<int, string>
287
     */
288 178
    public function getPrevious(): Token
289
    {
290 178
        return $this->previous;
291
    }
292
293
    /**
294
     * Lexical catchable patterns.
295
     *
296
     * @return string[]
297
     */
298 301
    protected function getCatchablePatterns(): array
299
    {
300 301
        return self::CATCHABLE_PATTERNS;
301
    }
302
303
    /**
304
     * Lexical non-catchable patterns.
305
     *
306
     * @return string[]
307
     */
308 301
    protected function getNonCatchablePatterns(): array
309
    {
310 301
        return self::NON_CATCHABLE_PATTERNS;
311
    }
312
313 301
    protected function getModifiers(): string
314
    {
315 301
        return self::MODIFIERS;
316
    }
317
318 151
    public function getAccumulatedValues(): string
319
    {
320 151
        return $this->accumulator;
321
    }
322
323 188
    public function startRecording(): void
324
    {
325 188
        $this->hasToRecord = true;
326
    }
327
328 148
    public function stopRecording(): void
329
    {
330 148
        $this->hasToRecord = false;
331
    }
332
333 149
    public function clearRecorded(): void
334
    {
335 149
        $this->accumulator = '';
336
    }
337
}
338