Passed
Pull Request — master (#203)
by
unknown
02:16
created

EmailLexer   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 1
dl 0
loc 225
ccs 58
cts 58
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 10 1
A hasInvalidTokens() 0 4 1
A find() 0 10 2
A getPrevious() 0 4 1
A moveNext() 0 6 1
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 248
    public function __construct()
81
    {
82 248
        $this->reset();
83 248
    }
84
85 241
    public function reset()
86
    {
87 241
        $this->hasInvalidTokens = false;
88 241
        parent::reset();
89 241
        $this->previous = $this->token = [
90 241
            'type' => null,
91 241
            'value' => '',
92 241
            'position' => 0,
93
        ];
94 241
    }
95
96 52
    public function hasInvalidTokens()
97
    {
98 52
        return $this->hasInvalidTokens;
99
    }
100
101
    /**
102
     * @param string $type
103
     * @throws \UnexpectedValueException
104
     * @return boolean
105
     */
106 48
    public function find($type)
107
    {
108 48
        $search = clone $this;
109 48
        $search->skipUntil($type);
110
111 48
        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...
112 7
            throw new \UnexpectedValueException($type . ' not found');
113
        }
114 41
        return true;
115
    }
116
117
    /**
118
     * getPrevious
119
     *
120
     * @return array token
121
     */
122 119
    public function getPrevious()
123
    {
124 119
        return $this->previous;
125
    }
126
127
    /**
128
     * moveNext
129
     *
130
     * @return boolean
131
     */
132 225
    public function moveNext()
133
    {
134 225
        $this->previous = $this->token;
135
136 225
        return parent::moveNext();
137
    }
138
139
    /**
140
     * Lexical catchable patterns.
141
     *
142
     * @return string[]
143
     */
144 1
    protected function getCatchablePatterns()
145
    {
146
        return array(
147 1
            '[a-zA-Z_]+[46]?', //ASCII and domain literal
148 1
            '[^\x00-\x7F]',  //UTF-8
149 1
            '[0-9]+',
150 1
            '\r\n',
151 1
            '::',
152 1
            '\s+?',
153 1
            '.',
154 1
            );
155
    }
156
157
    /**
158
     * Lexical non-catchable patterns.
159
     *
160
     * @return string[]
161
     */
162 1
    protected function getNonCatchablePatterns()
163
    {
164 1
        return array('[\xA0-\xff]+');
165
    }
166
167
    /**
168
     * Retrieve token type. Also processes the token value if necessary.
169
     *
170
     * @param string $value
171
     * @throws \InvalidArgumentException
172
     * @return integer
173
     */
174 224
    protected function getType(&$value)
175
    {
176 224
        if ($this->isNullType($value)) {
177 2
            return self::C_NUL;
178
        }
179
180 223
        if ($this->isValid($value)) {
181 156
            return $this->charValue[$value];
182
        }
183
184 196
        if ($this->isUTF8Invalid($value)) {
185 62
            $this->hasInvalidTokens = true;
186 62
            return self::INVALID;
187
        }
188
189 134
        return  self::GENERIC;
190
    }
191
192 223
    protected function isValid($value)
193
    {
194 223
        if (isset($this->charValue[$value])) {
195 156
            return true;
196
        }
197
198 196
        return false;
199
    }
200
201
    /**
202
     * @param string $value
203
     * @return bool
204
     */
205 224
    protected function isNullType($value)
206
    {
207 224
        if ($value === "\0") {
208 2
            return true;
209
        }
210
211 223
        return false;
212
    }
213
214
    /**
215
     * @param string $value
216
     * @return bool
217
     */
218 196
    protected function isUTF8Invalid($value)
219
    {
220 196
        if (preg_match('/\p{Cc}+/u', $value)) {
221 62
            return true;
222
        }
223
224 134
        return false;
225
    }
226
227 1
    protected function getModifiers()
228
    {
229 1
        return 'iu';
230
    }
231
}
232