Completed
Push — master ( b0b79e...9ac4d8 )
by Hans
18s
created

Lexer::lex()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 59
Code Lines 40

Duplication

Lines 35
Ratio 59.32 %

Code Coverage

Tests 35
CRAP Score 10.1953

Importance

Changes 0
Metric Value
dl 35
loc 59
ccs 35
cts 40
cp 0.875
rs 6.5919
c 0
b 0
f 0
cc 10
eloc 40
nc 8
nop 1
crap 10.1953

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
namespace HansOtt\GraphQL\Schema;
4
5
use HansOtt\GraphQL\Shared\ScannerGeneric;
6
use HansOtt\GraphQL\Shared\ScannerWithLocation;
7
use HansOtt\GraphQL\Shared\Lexer as LexerShared;
8
9
final class Lexer implements LexerShared
10
{
11
    /**
12
     * @var ScannerWithLocation
13
     */
14
    private $scanner;
15
    private $tokens;
16
17 9
    private function emit($type, $value, $location)
18
    {
19 9
        $this->tokens[] = new Token($type, $value, $location);
20 9
    }
21
22 9
    private function getLocation()
23
    {
24 9
        $line = $this->scanner->getLine();
25 9
        $column = $this->scanner->getColumn();
26
27 9
        return new Location($line, $column);
28
    }
29
30
    private function getError($message)
31
    {
32
        $line = $this->scanner->getLine();
33
        $column = $this->scanner->getColumn();
34
35
        return new SyntaxError($message . " (line {$line}, column {$column})");
36
    }
37
38 9
    private function name()
39
    {
40 9
        $name = $this->scanner->next();
41 9
        $location = $this->getLocation();
42
43 9
        if ($this->scanner->eof()) {
44
            $this->emit(Token::T_NAME, $name, $location);
45
            return;
46
        }
47
48 9
        $next = $this->scanner->peek();
49 9 View Code Duplication
        while ($next === '_' || ctype_alpha($next) || ctype_digit($next)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 9
            $name .= $this->scanner->next();
51 9
            if ($this->scanner->eof()) {
52
                break;
53
            }
54 9
            $next = $this->scanner->peek();
55 9
        }
56
57 9
        $type = Token::T_NAME;
58 9
        if ($name === 'true') {
59
            $type = Token::T_TRUE;
60 9
        } elseif ($name === 'false') {
61 3
            $type = Token::T_FALSE;
62 9
        } elseif ($name === 'null') {
63
            $type = Token::T_NULL;
64
        }
65
66 9
        $this->emit($type, $name, $location);
67 9
    }
68
69 View Code Duplication
    private function comment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $this->scanner->next();
72
        $next = $this->scanner->peek();
73
        while ($this->scanner->eof() === false && $next !== "\n" && $next !== "\r") {
74
            $this->scanner->next();
75
            $next = $this->scanner->peek();
76
        }
77
    }
78
79 View Code Duplication
    private function str()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        $this->scanner->next();
82
        $location = $this->getLocation();
83
        $string = '';
84
        $previousChar = false;
85
86
        while (true) {
87
            if ($this->scanner->eof()) {
88
                throw $this->getError('Unclosed quote');
89
            }
90
            $next = $this->scanner->peek();
91
            if ($previousChar !== '\\' && $next === '"') {
92
                $this->scanner->next();
93
                break;
94
            }
95
            $previousChar = $this->scanner->next();
96
            $string .= $previousChar;
97
        }
98
99
        $string = json_decode('"' . $string . '"');
100
        $this->emit(Token::T_STRING, $string, $location);
101
    }
102
103 3 View Code Duplication
    private function integerPart()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105 3
        $number = $this->scanner->next();
106 3
        $location = $this->getLocation();
107 3
        if ($number === '-') {
108
            if ($this->scanner->eof()) {
109
                throw $this->getError('Expected a digit but instead reached end');
110
            }
111
            $next = $this->scanner->peek();
112
            if (ctype_digit($next) === false) {
113
                throw $this->getError("Expected a digit but instead found \"{$next}\"");
114
            }
115
        }
116
117 3
        $next = $this->scanner->peek();
118 3
        if ($next === '0') {
119
            $number .= $this->scanner->next();
120
            return array($number, $location);
121
        }
122
123 3
        $next = $this->scanner->peek();
124 3
        while ($this->scanner->eof() === false && ctype_digit($next)) {
125
            $number .= $this->scanner->next();
126
            $next = $this->scanner->peek();
127
        }
128
129 3
        return array($number, $location);
130
    }
131
132 3 View Code Duplication
    private function fractionalPart()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134 3
        $part = $this->scanner->next();
135
136 3
        if ($this->scanner->eof()) {
137
            throw $this->getError('Expected a digit but instead reached end');
138
        }
139
140 3
        $next = $this->scanner->peek();
141 3
        if (ctype_digit($next) === false) {
142
            throw $this->getError("Expected a digit but instead found \"{$next}\"");
143
        }
144
145 3
        $next = $this->scanner->peek();
146 3
        while ($this->scanner->eof() === false && ctype_digit($next)) {
147 3
            $part .= $this->scanner->next();
148 3
            $next = $this->scanner->peek();
149 3
        }
150
151 3
        return $part;
152
    }
153
154 View Code Duplication
    private function exponentPart()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        $part = $this->scanner->next();
157
158
        if ($this->scanner->eof()) {
159
            throw $this->getError('Expected a digit but instead reached end');
160
        }
161
162
        $next = $this->scanner->peek();
163
        if ($next === '+' || $next === '-') {
164
            $part .= $this->scanner->next();
165
        }
166
167
        $next = $this->scanner->peek();
168
        if (ctype_digit($next) === false) {
169
            throw $this->getError("Expected a digit but instead found \"{$next}\"");
170
        }
171
172
        $next = $this->scanner->peek();
173
        while ($this->scanner->eof() === false && ctype_digit($next)) {
174
            $part .= $this->scanner->next();
175
            $next = $this->scanner->peek();
176
        }
177
178
        return $part;
179
    }
180
181 3 View Code Duplication
    private function number()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183 3
        list ($integerPart, $location) = $this->integerPart();
184 3
        if ($this->scanner->eof()) {
185
            $this->emit(Token::T_INT, $integerPart, $location);
186
            return;
187
        }
188
189 3
        $next = $this->scanner->peek();
190 3
        if ($next !== '.' && $next !== 'e' && $next !== 'E') {
191
            $this->emit(Token::T_INT, $integerPart, $location);
192
            return;
193
        }
194
195 3
        $number = $integerPart;
196 3
        if ($next === '.') {
197 3
            $number .= $this->fractionalPart();
198 3
        }
199
200 3
        $next = $this->scanner->peek();
201 3
        if ($next === 'e' || $next === 'E') {
202
            $number .= $this->exponentPart();
203
        }
204
205 3
        $this->emit(Token::T_FLOAT, $number, $location);
206 3
    }
207
208
    /**
209
     * @param string $query
210
     *
211
     * @throws SyntaxError
212
     *
213
     * @return Token[]
214
     */
215 9
    public function lex($query)
216
    {
217 9
        $flags = PREG_SPLIT_NO_EMPTY;
218 9
        $chars = preg_split('//u', $query, -1, $flags);
219 9
        $scanner = new ScannerGeneric($chars);
220 9
        $this->scanner = new ScannerWithLocation($scanner);
221 9
        $this->tokens = array();
222
        $punctuators = array(
223 9
            '!' => Token::T_EXCLAMATION,
224 9
            '(' => Token::T_PAREN_LEFT,
225 9
            ')' => Token::T_PAREN_RIGHT,
226 9
            '{' => Token::T_BRACE_LEFT,
227 9
            '}' => Token::T_BRACE_RIGHT,
228 9
            ':' => Token::T_COLON,
229 9
            ',' => Token::T_COMMA,
230 9
            '[' => Token::T_BRACKET_LEFT,
231 9
            ']' => Token::T_BRACKET_RIGHT,
232 9
            '=' => Token::T_EQUAL,
233 9
            '|' => Token::T_PIPE,
234 9
        );
235
236 9 View Code Duplication
        while ($this->scanner->eof() === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237 9
            $next = $this->scanner->peek();
238
239 9
            if (ctype_space($next)) {
240 9
                $this->scanner->next();
241 9
                continue;
242
            }
243
244 9
            if ($next === '#') {
245
                $this->comment();
246
                continue;
247
            }
248
249 9
            if ($next === '_' || ctype_alpha($next)) {
250 9
                $this->name();
251 9
                continue;
252
            }
253
254 9
            if ($next === '"') {
255
                $this->str();
256
                continue;
257
            }
258
259 9
            if ($next === '-' || ctype_digit($next)) {
260 3
                $this->number();
261 3
                continue;
262
            }
263
264 9
            if (isset($punctuators[$next])) {
265 9
                $this->emit($punctuators[$next], $this->scanner->next(), $this->getLocation());
266 9
                continue;
267
            }
268
269
            throw $this->getError("Unknown character: \"{$next}\"");
270
        }
271
272 9
        return $this->tokens;
273
    }
274
}
275