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

EmailLexer::hasInvalidTokens()   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 = null;
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 340
        $this->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

167
        $this->nullToken = new Token(/** @scrutinizer ignore-type */ (string)'', null, 0);
Loading history...
168 340
        $this->previous = $this->token = $this->nullToken;
169 340
        $this->lookahead = null;
170
    }
171
172 308
    public function reset(): void
173
    {
174 308
        $this->hasInvalidTokens = false;
175 308
        parent::reset();
176 308
        $this->previous = $this->token = $this->nullToken;
177
    }
178
179
    /**
180
     * @param int $type
181
     * @throws \UnexpectedValueException
182
     * @return boolean
183
     *
184
     * @psalm-suppress InvalidScalarArgument
185
     */
186 54
    public function find($type): bool
187
    {
188 54
        $search = clone $this;
189 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

189
        $search->skipUntil(/** @scrutinizer ignore-type */ $type);
Loading history...
190
191 54
        if (!$search->lookahead) {
192 6
            throw new \UnexpectedValueException($type . ' not found');
193
        }
194 48
        return true;
195
    }
196
197
    /**
198
     * moveNext
199
     *
200
     * @return boolean
201
     */
202 297
    public function moveNext(): bool
203
    {
204 297
        if ($this->hasToRecord && $this->previous === $this->nullToken) {
205 179
            $this->accumulator .= $this->token->value;
206
        }
207
208 297
        $this->previous = $this->token;
209
210 297
        if ($this->lookahead === null) {
211 297
            $this->lookahead = $this->nullToken;
212
        }
213
214 297
        $hasNext = parent::moveNext();
215
216 297
        if ($this->hasToRecord) {
217 179
            $this->accumulator .= $this->token->value;
218
        }
219
220 297
        return $hasNext;
221
    }
222
223
    /**
224
     * Retrieve token type. Also processes the token value if necessary.
225
     *
226
     * @param string $value
227
     * @throws \InvalidArgumentException
228
     * @return integer
229
     */
230 300
    protected function getType(&$value): int
231
    {
232 300
        $encoded = $value;
233
234 300
        if (mb_detect_encoding($value, 'auto', true) !== 'UTF-8') {
235 237
            $encoded = mb_convert_encoding($value, 'UTF-8', 'Windows-1252');
236
        }
237
238 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

238
        if ($this->isValid(/** @scrutinizer ignore-type */ $encoded)) {
Loading history...
239 229
            return $this->charValue[$encoded];
240
        }
241
242 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

242
        if ($this->isNullType(/** @scrutinizer ignore-type */ $encoded)) {
Loading history...
243 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...
244
        }
245
246 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

246
        if ($this->isInvalidChar(/** @scrutinizer ignore-type */ $encoded)) {
Loading history...
247 66
            $this->hasInvalidTokens = true;
248 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...
249
        }
250
251
252 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...
253
    }
254
255 300
    protected function isValid(string $value): bool
256
    {
257 300
        return isset($this->charValue[$value]);
258
    }
259
260 262
    protected function isNullType(string $value): bool
261
    {
262 262
        return $value === "\0";
263
    }
264
265 261
    protected function isInvalidChar(string $value): bool
266
    {
267 261
        return !preg_match(self::INVALID_CHARS_REGEX, $value);
268
    }
269
270
    protected function isUTF8Invalid(string $value): bool
271
    {
272
        return preg_match(self::VALID_UTF8_REGEX, $value) !== false;
273
    }
274
275 191
    public function hasInvalidTokens(): bool
276
    {
277 191
        return $this->hasInvalidTokens;
278
    }
279
280
    /**
281
     * getPrevious
282
     *
283
     * @return Token<int, string>
284
     */
285 178
    public function getPrevious(): Token
286
    {
287 178
        return $this->previous;
288
    }
289
290
    /**
291
     * Lexical catchable patterns.
292
     *
293
     * @return string[]
294
     */
295 301
    protected function getCatchablePatterns(): array
296
    {
297 301
        return self::CATCHABLE_PATTERNS;
298
    }
299
300
    /**
301
     * Lexical non-catchable patterns.
302
     *
303
     * @return string[]
304
     */
305 301
    protected function getNonCatchablePatterns(): array
306
    {
307 301
        return self::NON_CATCHABLE_PATTERNS;
308
    }
309
310 301
    protected function getModifiers(): string
311
    {
312 301
        return self::MODIFIERS;
313
    }
314
315 151
    public function getAccumulatedValues(): string
316
    {
317 151
        return $this->accumulator;
318
    }
319
320 188
    public function startRecording(): void
321
    {
322 188
        $this->hasToRecord = true;
323
    }
324
325 148
    public function stopRecording(): void
326
    {
327 148
        $this->hasToRecord = false;
328
    }
329
330 149
    public function clearRecorded(): void
331
    {
332 149
        $this->accumulator = '';
333
    }
334
}
335