Completed
Push — master ( 9ac4d8...c56c43 )
by Hans
02:19
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 37
CRAP Score 10.0421

Importance

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

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 12
    private function emit($type, $value, $location)
18
    {
19 12
        $this->tokens[] = new Token($type, $value, $location);
20 12
    }
21
22 12
    private function getLocation()
23
    {
24 12
        $line = $this->scanner->getLine();
25 12
        $column = $this->scanner->getColumn();
26
27 12
        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 12
    private function name()
39
    {
40 12
        $name = $this->scanner->next();
41 12
        $location = $this->getLocation();
42
43 12
        if ($this->scanner->eof()) {
44
            $this->emit(Token::T_NAME, $name, $location);
45
            return;
46
        }
47
48 12
        $next = $this->scanner->peek();
49 12 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 12
            $name .= $this->scanner->next();
51 12
            if ($this->scanner->eof()) {
52
                break;
53
            }
54 12
            $next = $this->scanner->peek();
55 12
        }
56
57 12
        $type = Token::T_NAME;
58 12
        if ($name === 'true') {
59
            $type = Token::T_TRUE;
60 12
        } elseif ($name === 'false') {
61 3
            $type = Token::T_FALSE;
62 12
        } elseif ($name === 'null') {
63
            $type = Token::T_NULL;
64
        }
65
66 12
        $this->emit($type, $name, $location);
67 12
    }
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 3 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 3
        $this->scanner->next();
82 3
        $location = $this->getLocation();
83 3
        $string = '';
84 3
        $previousChar = false;
85
86 3
        while (true) {
87 3
            if ($this->scanner->eof()) {
88
                throw $this->getError('Unclosed quote');
89
            }
90 3
            $next = $this->scanner->peek();
91 3
            if ($previousChar !== '\\' && $next === '"') {
92 3
                $this->scanner->next();
93 3
                break;
94
            }
95 3
            $previousChar = $this->scanner->next();
96 3
            $string .= $previousChar;
97 3
        }
98
99 3
        $string = json_decode('"' . $string . '"');
100 3
        $this->emit(Token::T_STRING, $string, $location);
101 3
    }
102
103 6 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 6
        $number = $this->scanner->next();
106 6
        $location = $this->getLocation();
107 6
        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 6
        $next = $this->scanner->peek();
118 6
        if ($next === '0') {
119
            $number .= $this->scanner->next();
120
            return array($number, $location);
121
        }
122
123 6
        $next = $this->scanner->peek();
124 6
        while ($this->scanner->eof() === false && ctype_digit($next)) {
125
            $number .= $this->scanner->next();
126
            $next = $this->scanner->peek();
127
        }
128
129 6
        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 6 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 6
        list ($integerPart, $location) = $this->integerPart();
184 6
        if ($this->scanner->eof()) {
185
            $this->emit(Token::T_INT, $integerPart, $location);
186
            return;
187
        }
188
189 6
        $next = $this->scanner->peek();
190 6
        if ($next !== '.' && $next !== 'e' && $next !== 'E') {
191 3
            $this->emit(Token::T_INT, $integerPart, $location);
192 3
            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 12
    public function lex($query)
216
    {
217 12
        $flags = PREG_SPLIT_NO_EMPTY;
218 12
        $chars = preg_split('//u', $query, -1, $flags);
219 12
        $scanner = new ScannerGeneric($chars);
220 12
        $this->scanner = new ScannerWithLocation($scanner);
221 12
        $this->tokens = array();
222
        $punctuators = array(
223 12
            '!' => Token::T_EXCLAMATION,
224 12
            '(' => Token::T_PAREN_LEFT,
225 12
            ')' => Token::T_PAREN_RIGHT,
226 12
            '{' => Token::T_BRACE_LEFT,
227 12
            '}' => Token::T_BRACE_RIGHT,
228 12
            ':' => Token::T_COLON,
229 12
            ',' => Token::T_COMMA,
230 12
            '[' => Token::T_BRACKET_LEFT,
231 12
            ']' => Token::T_BRACKET_RIGHT,
232 12
            '=' => Token::T_EQUAL,
233 12
            '|' => Token::T_PIPE,
234 12
        );
235
236 12 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 12
            $next = $this->scanner->peek();
238
239 12
            if (ctype_space($next)) {
240 12
                $this->scanner->next();
241 12
                continue;
242
            }
243
244 12
            if ($next === '#') {
245
                $this->comment();
246
                continue;
247
            }
248
249 12
            if ($next === '_' || ctype_alpha($next)) {
250 12
                $this->name();
251 12
                continue;
252
            }
253
254 12
            if ($next === '"') {
255 3
                $this->str();
256 3
                continue;
257
            }
258
259 12
            if ($next === '-' || ctype_digit($next)) {
260 6
                $this->number();
261 6
                continue;
262
            }
263
264 12
            if (isset($punctuators[$next])) {
265 12
                $this->emit($punctuators[$next], $this->scanner->next(), $this->getLocation());
266 12
                continue;
267
            }
268
269
            throw $this->getError("Unknown character: \"{$next}\"");
270
        }
271
272 12
        return $this->tokens;
273
    }
274
}
275