Test Failed
Pull Request — master (#203)
by
unknown
02:24
created

EmailLexer   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 1
dl 0
loc 223
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A reset() 0 6 1
A hasInvalidTokens() 0 4 1
A find() 0 10 2
A getPrevious() 0 4 1
A moveNext() 0 8 2
A getCatchablePatterns() 0 12 1
A getNonCatchablePatterns() 0 4 1
A getType() 0 17 4
A isValid() 0 8 2
A isNullType() 0 8 2
A isUTF8Invalid() 0 8 2
A getModifiers() 0 4 1
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 C_DEL              = 127;
11
    const C_NUL              = 0;
12
    const S_AT               = 64;
13
    const S_BACKSLASH        = 92;
14
    const S_DOT              = 46;
15
    const S_DQUOTE           = 34;
16
    const S_OPENPARENTHESIS  = 49;
17
    const S_CLOSEPARENTHESIS = 261;
18
    const S_OPENBRACKET      = 262;
19
    const S_CLOSEBRACKET     = 263;
20
    const S_HYPHEN           = 264;
21
    const S_COLON            = 265;
22
    const S_DOUBLECOLON      = 266;
23
    const S_SP               = 267;
24
    const S_HTAB             = 268;
25
    const S_CR               = 269;
26
    const S_LF               = 270;
27
    const S_IPV6TAG          = 271;
28
    const S_LOWERTHAN        = 272;
29
    const S_GREATERTHAN      = 273;
30
    const S_COMMA            = 274;
31
    const S_SEMICOLON        = 275;
32
    const S_OPENQBRACKET     = 276;
33
    const S_CLOSEQBRACKET    = 277;
34
    const S_SLASH            = 278;
35
    const S_EMPTY            = null;
36
    const GENERIC            = 300;
37
    const CRLF               = 301;
38
    const INVALID            = 302;
39
    const ASCII_INVALID_FROM = 127;
40
    const ASCII_INVALID_TO   = 199;
41
42
    /**
43
     * US-ASCII visible characters not valid for atext (@link http://tools.ietf.org/html/rfc5322#section-3.2.3)
44
     *
45
     * @var array
46
     */
47
    protected $charValue = array(
48
        '('    => self::S_OPENPARENTHESIS,
49
        ')'    => self::S_CLOSEPARENTHESIS,
50
        '<'    => self::S_LOWERTHAN,
51
        '>'    => self::S_GREATERTHAN,
52
        '['    => self::S_OPENBRACKET,
53
        ']'    => self::S_CLOSEBRACKET,
54
        ':'    => self::S_COLON,
55
        ';'    => self::S_SEMICOLON,
56
        '@'    => self::S_AT,
57
        '\\'   => self::S_BACKSLASH,
58
        '/'    => self::S_SLASH,
59
        ','    => self::S_COMMA,
60
        '.'    => self::S_DOT,
61
        '"'    => self::S_DQUOTE,
62
        '-'    => self::S_HYPHEN,
63
        '::'   => self::S_DOUBLECOLON,
64
        ' '    => self::S_SP,
65
        "\t"   => self::S_HTAB,
66
        "\r"   => self::S_CR,
67
        "\n"   => self::S_LF,
68
        "\r\n" => self::CRLF,
69
        'IPv6' => self::S_IPV6TAG,
70
        '{'    => self::S_OPENQBRACKET,
71
        '}'    => self::S_CLOSEQBRACKET,
72
        ''     => self::S_EMPTY,
73
        '\0'   => self::C_NUL,
74
    );
75
76
    protected $hasInvalidTokens = false;
77
78
    protected $previous;
79
80 226
    public function __construct()
81
    {
82 226
        $this->reset();
83 226
    }
84 226
85
    public function reset()
86 52
    {
87
        $this->hasInvalidTokens = false;
88 52
        parent::reset();
89
        $this->previous = $this->token = ['type' => null, 'value' => '', 'position' => 0];
90
    }
91
92
    public function hasInvalidTokens()
93
    {
94
        return $this->hasInvalidTokens;
95
    }
96 48
97
    /**
98 48
     * @param string $type
99 48
     * @throws \UnexpectedValueException
100
     * @return boolean
101 48
     */
102 7
    public function find($type)
103
    {
104 41
        $search = clone $this;
105
        $search->skipUntil($type);
106
107
        if (!$search->lookahead) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $search->lookahead of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
108
            throw new \UnexpectedValueException($type . ' not found');
109
        }
110
        return true;
111
    }
112 119
113
    /**
114 119
     * getPrevious
115
     *
116
     * @return array token
117
     */
118
    public function getPrevious()
119
    {
120
        return $this->previous;
121
    }
122 225
123
    /**
124 225
     * moveNext
125
     *
126 225
     * @return boolean
127
     */
128
    public function moveNext()
129
    {
130
        $this->previous = $this->token;
131
        $hasNext = parent::moveNext();
132
        $this->token = $this->token ?: ['type' => null, 'value' => '', 'position' => 0];
133
134 1
        return $hasNext;
135
    }
136
137 1
    /**
138 1
     * Lexical catchable patterns.
139 1
     *
140 1
     * @return string[]
141 1
     */
142 1
    protected function getCatchablePatterns()
143 1
    {
144 1
        return array(
145
            '[a-zA-Z_]+[46]?', //ASCII and domain literal
146
            '[^\x00-\x7F]',  //UTF-8
147
            '[0-9]+',
148
            '\r\n',
149
            '::',
150
            '\s+?',
151
            '.',
152 1
            );
153
    }
154 1
155
    /**
156
     * Lexical non-catchable patterns.
157
     *
158
     * @return string[]
159
     */
160
    protected function getNonCatchablePatterns()
161
    {
162
        return array('[\xA0-\xff]+');
163
    }
164 224
165
    /**
166 224
     * Retrieve token type. Also processes the token value if necessary.
167 2
     *
168
     * @param string $value
169
     * @throws \InvalidArgumentException
170 223
     * @return integer
171 156
     */
172
    protected function getType(&$value)
173
    {
174 196
        if ($this->isNullType($value)) {
175 62
            return self::C_NUL;
176 62
        }
177
178
        if ($this->isValid($value)) {
179 134
            return $this->charValue[$value];
180
        }
181
182 223
        if ($this->isUTF8Invalid($value)) {
183
            $this->hasInvalidTokens = true;
184 223
            return self::INVALID;
185 156
        }
186
187
        return  self::GENERIC;
188 196
    }
189
190
    protected function isValid($value)
191
    {
192
        if (isset($this->charValue[$value])) {
193
            return true;
194
        }
195 224
196
        return false;
197 224
    }
198 2
199
    /**
200
     * @param string $value
201 223
     * @return bool
202
     */
203
    protected function isNullType($value)
204
    {
205
        if ($value === "\0") {
206
            return true;
207
        }
208 196
209
        return false;
210 196
    }
211 62
212
    /**
213
     * @param string $value
214 134
     * @return bool
215
     */
216
    protected function isUTF8Invalid($value)
217 1
    {
218
        if (preg_match('/\p{Cc}+/u', $value)) {
219 1
            return true;
220
        }
221
222
        return false;
223
    }
224
225
    protected function getModifiers()
226
    {
227
        return 'iu';
228
    }
229
}
230