Failed Conditions
Push — master ( 8be1e3...e3936d )
by Marco
14s
created

Lexer::getType()   D

Complexity

Conditions 28
Paths 26

Size

Total Lines 81
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 28.0059

Importance

Changes 0
Metric Value
cc 28
eloc 52
nc 26
nop 1
dl 0
loc 81
ccs 50
cts 51
cp 0.9804
crap 28.0059
rs 4.9163
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Query;
6
7
/**
8
 * Scans a DQL query for tokens.
9
 */
10
class Lexer extends \Doctrine\Common\Lexer
11
{
12
    // All tokens that are not valid identifiers must be < 100
13
    public const T_NONE              = 1;
14
    public const T_INTEGER           = 2;
15
    public const T_STRING            = 3;
16
    public const T_INPUT_PARAMETER   = 4;
17
    public const T_FLOAT             = 5;
18
    public const T_CLOSE_PARENTHESIS = 6;
19
    public const T_OPEN_PARENTHESIS  = 7;
20
    public const T_COMMA             = 8;
21
    public const T_DIVIDE            = 9;
22
    public const T_DOT               = 10;
23
    public const T_EQUALS            = 11;
24
    public const T_GREATER_THAN      = 12;
25
    public const T_LOWER_THAN        = 13;
26
    public const T_MINUS             = 14;
27
    public const T_MULTIPLY          = 15;
28
    public const T_NEGATE            = 16;
29
    public const T_PLUS              = 17;
30
    public const T_OPEN_CURLY_BRACE  = 18;
31
    public const T_CLOSE_CURLY_BRACE = 19;
32
33
    // All tokens that are identifiers or keywords that could be considered as identifiers should be >= 100
34
    public const T_ALIASED_NAME         = 100;
35
    public const T_FULLY_QUALIFIED_NAME = 101;
36
    public const T_IDENTIFIER           = 102;
37
38
    // All keyword tokens should be >= 200
39
    public const T_ALL      = 200;
40
    public const T_AND      = 201;
41
    public const T_ANY      = 202;
42
    public const T_AS       = 203;
43
    public const T_ASC      = 204;
44
    public const T_AVG      = 205;
45
    public const T_BETWEEN  = 206;
46
    public const T_BOTH     = 207;
47
    public const T_BY       = 208;
48
    public const T_CASE     = 209;
49
    public const T_COALESCE = 210;
50
    public const T_COUNT    = 211;
51
    public const T_DELETE   = 212;
52
    public const T_DESC     = 213;
53
    public const T_DISTINCT = 214;
54
    public const T_ELSE     = 215;
55
    public const T_EMPTY    = 216;
56
    public const T_END      = 217;
57
    public const T_ESCAPE   = 218;
58
    public const T_EXISTS   = 219;
59
    public const T_FALSE    = 220;
60
    public const T_FROM     = 221;
61
    public const T_GROUP    = 222;
62
    public const T_HAVING   = 223;
63
    public const T_HIDDEN   = 224;
64
    public const T_IN       = 225;
65
    public const T_INDEX    = 226;
66
    public const T_INNER    = 227;
67
    public const T_INSTANCE = 228;
68
    public const T_IS       = 229;
69
    public const T_JOIN     = 230;
70
    public const T_LEADING  = 231;
71
    public const T_LEFT     = 232;
72
    public const T_LIKE     = 233;
73
    public const T_MAX      = 234;
74
    public const T_MEMBER   = 235;
75
    public const T_MIN      = 236;
76
    public const T_NEW      = 237;
77
    public const T_NOT      = 238;
78
    public const T_NULL     = 239;
79
    public const T_NULLIF   = 240;
80
    public const T_OF       = 241;
81
    public const T_OR       = 242;
82
    public const T_ORDER    = 243;
83
    public const T_OUTER    = 244;
84
    public const T_PARTIAL  = 245;
85
    public const T_SELECT   = 246;
86
    public const T_SET      = 247;
87
    public const T_SOME     = 248;
88
    public const T_SUM      = 249;
89
    public const T_THEN     = 250;
90
    public const T_TRAILING = 251;
91
    public const T_TRUE     = 252;
92
    public const T_UPDATE   = 253;
93
    public const T_WHEN     = 254;
94
    public const T_WHERE    = 255;
95
    public const T_WITH     = 256;
96
97
    /**
98
     * Creates a new query scanner object.
99
     *
100
     * @param string $input A query string.
101
     */
102 871
    public function __construct($input)
103
    {
104 871
        $this->setInput($input);
105 871
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 1
    protected function getCatchablePatterns()
111
    {
112
        return [
113 1
            '[a-z_][a-z0-9_]*\:[a-z_][a-z0-9_]*(?:\\\[a-z_][a-z0-9_]*)*', // aliased name
114
            '[a-z_\\\][a-z0-9_]*(?:\\\[a-z_][a-z0-9_]*)*', // identifier or qualified name
115
            '(?:[0-9]+(?:[\.][0-9]+)*)(?:e[+-]?[0-9]+)?', // numbers
116
            "'(?:[^']|'')*'", // quoted strings
117
            '\?[0-9]*|:[a-z_][a-z0-9_]*', // parameters
118
        ];
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1
    protected function getNonCatchablePatterns()
125
    {
126 1
        return ['\s+', '(.)'];
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 870
    protected function getType(&$value)
133
    {
134 870
        $type = self::T_NONE;
135
136
        switch (true) {
137
            // Recognize numeric values
138 870
            case (is_numeric($value)):
139 147
                if (strpos($value, '.') !== false || stripos($value, 'e') !== false) {
140 7
                    return self::T_FLOAT;
141
                }
142
143 140
                return self::T_INTEGER;
144
145
            // Recognize quoted strings
146 864
            case ($value[0] === "'"):
147 102
                $value = str_replace("''", "'", substr($value, 1, strlen($value) - 2));
148
149 102
                return self::T_STRING;
150
151
            // Recognize identifiers, aliased or qualified names
152 862
            case (ctype_alpha($value[0]) || $value[0] === '_' || $value[0] === '\\'):
153 858
                $name = 'Doctrine\ORM\Query\Lexer::T_' . strtoupper($value);
154
155 858
                if (defined($name)) {
156 850
                    $type = constant($name);
157
158 850
                    if ($type > 100) {
159 849
                        return $type;
160
                    }
161
                }
162
163 857
                if (strpos($value, ':') !== false) {
164 12
                    return self::T_ALIASED_NAME;
165
                }
166
167 855
                if (strpos($value, '\\') !== false) {
168 835
                    return self::T_FULLY_QUALIFIED_NAME;
169
                }
170
171 851
                return self::T_IDENTIFIER;
172
173
            // Recognize input parameters
174 733
            case ($value[0] === '?' || $value[0] === ':'):
175 193
                return self::T_INPUT_PARAMETER;
176
177
            // Recognize symbols
178 715
            case ($value === '.'):
179 684
                return self::T_DOT;
180 627
            case ($value === ','):
181 351
                return self::T_COMMA;
182 501
            case ($value === '('):
183 260
                return self::T_OPEN_PARENTHESIS;
184 501
            case ($value === ')'):
185 260
                return self::T_CLOSE_PARENTHESIS;
186 363
            case ($value === '='):
187 275
                return self::T_EQUALS;
188 120
            case ($value === '>'):
189 48
                return self::T_GREATER_THAN;
190 90
            case ($value === '<'):
191 17
                return self::T_LOWER_THAN;
192 82
            case ($value === '+'):
193 20
                return self::T_PLUS;
194 73
            case ($value === '-'):
195 9
                return self::T_MINUS;
196 67
            case ($value === '*'):
197 40
                return self::T_MULTIPLY;
198 31
            case ($value === '/'):
199 16
                return self::T_DIVIDE;
200 15
            case ($value === '!'):
201 6
                return self::T_NEGATE;
202 9
            case ($value === '{'):
203 9
                return self::T_OPEN_CURLY_BRACE;
204 9
            case ($value === '}'):
205 9
                return self::T_CLOSE_CURLY_BRACE;
206
207
            // Default
208
            default:
209
                // Do nothing
210
        }
211
212
        return $type;
213
    }
214
}
215