Passed
Pull Request — master (#23)
by Christoffer
02:09
created

Lexer::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Digia\GraphQL\Language;
4
5
use Digia\GraphQL\Error\GraphQLError;
6
use Digia\GraphQL\Error\SyntaxError;
7
use Digia\GraphQL\Language\Reader\ReaderInterface;
8
9
class Lexer implements LexerInterface
10
{
11
12
    /**
13
     * @var Source|null
14
     */
15
    protected $source;
16
17
    /**
18
     * @var array
19
     */
20
    protected $options = [];
21
22
    /**
23
     * @var array|ReaderInterface[]
24
     */
25
    protected $readers;
26
27
    /**
28
     * The previously focused non-ignored token.
29
     *
30
     * @var Token
31
     */
32
    protected $lastToken;
33
34
    /**
35
     * The currently focused non-ignored token.
36
     *
37
     * @var Token
38
     */
39
    protected $token;
40
41
    /**
42
     * The (1-indexed) line containing the current token.
43
     *
44
     * @var int
45
     */
46
    protected $line;
47
48
    /**
49
     * The character offset at which the current line begins.
50
     *
51
     * @var int
52
     */
53
    protected $lineStart;
54
55
    /**
56
     * Lexer constructor.
57
     *
58
     * @param ReaderInterface[] $readers
59
     */
60
    public function __construct(array $readers)
61
    {
62
        $startOfFileToken = new Token(TokenKindEnum::SOF);
63
64
        foreach ($readers as $reader) {
65
            $reader->setLexer($this);
66
        }
67
68
        $this->readers   = $readers;
69
        $this->lastToken = $startOfFileToken;
70
        $this->token     = $startOfFileToken;
71
        $this->line      = 1;
72
        $this->lineStart = 0;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function advance(): Token
79
    {
80
        $this->lastToken = $this->token;
81
82
        return $this->token = $this->lookahead();
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function lookahead(): Token
89
    {
90
        $token = $this->token;
91
92
        if (TokenKindEnum::EOF !== $token->getKind()) {
93
            do {
94
                $next = $this->readToken($token);
95
                $token->setNext($next);
96
                $token = $next;
97
            } while (TokenKindEnum::COMMENT === $token->getKind());
98
        }
99
100
        return $token;
101
    }
102
103
    /**
104
     * @param string $name
105
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
106
     * @return mixed|null
107
     */
108
    public function getOption(string $name, $default = null)
109
    {
110
        return $this->options[$name] ?? $default;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function getBody(): string
117
    {
118
        return $this->getSource()->getBody();
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function getTokenKind(): string
125
    {
126
        return $this->token->getKind();
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132
    public function getTokenValue(): ?string
133
    {
134
        return $this->token->getValue();
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function getToken(): Token
141
    {
142
        return $this->token;
143
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148
    public function getSource(): Source
149
    {
150
        if ($this->source instanceof Source) {
151
            return $this->source;
152
        }
153
154
        throw new \Exception('No source has been set.');
155
    }
156
157
    /**
158
     * @inheritdoc
159
     */
160
    public function getLastToken(): Token
161
    {
162
        return $this->lastToken;
163
    }
164
165
    /**
166
     * @param Source $source
167
     * @return Lexer
168
     */
169
    public function setSource(Source $source)
170
    {
171
        $this->source = $source;
172
        return $this;
173
    }
174
175
    /**
176
     * @param array $options
177
     * @return
178
     */
179
    public function setOptions(array $options)
180
    {
181
        $this->options = $options;
182
        return $this;
183
    }
184
185
    /**
186
     * @param int   $code
187
     * @param int   $pos
188
     * @param int   $line
189
     * @param int   $col
190
     * @param Token $prev
191
     * @return Token
192
     * @throws SyntaxError
193
     */
194
    public function read(int $code, int $pos, int $line, int $col, Token $prev): Token
195
    {
196
        if (($reader = $this->getReader($code, $pos)) !== null) {
197
            return $reader->read($code, $pos, $line, $col, $prev);
198
        }
199
200
        throw new SyntaxError($this->unexpectedCharacterMessage($code));
201
    }
202
203
    /**
204
     * @param Token $prev
205
     * @return Token
206
     * @throws GraphQLError
207
     */
208
    protected function readToken(Token $prev): Token
209
    {
210
        $body       = $this->source->getBody();
0 ignored issues
show
Bug introduced by
The method getBody() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

210
        /** @scrutinizer ignore-call */ 
211
        $body       = $this->source->getBody();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
211
        $bodyLength = mb_strlen($body);
212
213
        $pos  = $this->positionAfterWhitespace($body, $prev->getEnd());
214
        $line = $this->line;
215
        $col  = 1 + $pos - $this->lineStart;
216
217
        if ($pos >= $bodyLength) {
218
            return new Token(TokenKindEnum::EOF, $bodyLength, $bodyLength, $line, $col, $prev);
219
        }
220
221
        $code = charCodeAt($body, $pos);
222
223
        if (isSourceCharacter($code)) {
224
            throw new SyntaxError(sprintf('Cannot contain the invalid character %s', printCharCode($code)));
225
        }
226
227
        return $this->read($code, $pos, $line, $col, $prev);
228
    }
229
230
    /**
231
     * @param int $code
232
     * @return string
233
     */
234
    protected function unexpectedCharacterMessage(int $code): string
235
    {
236
        if ($code === 39) {
237
            // '
238
            return 'Unexpected single quote character (\'), did you mean to use a double quote (")?';
239
        }
240
241
        return sprintf('Cannot parse the unexpected character %s', printCharCode($code));
242
    }
243
244
    /**
245
     * @param int $code
246
     * @param int $pos
247
     * @return ReaderInterface|null
248
     */
249
    protected function getReader(int $code, int $pos): ?ReaderInterface
250
    {
251
        foreach ($this->readers as $reader) {
252
            if ($reader instanceof ReaderInterface && $reader->supportsReader($code, $pos)) {
253
                return $reader;
254
            }
255
        }
256
257
        return null;
258
    }
259
260
    /**
261
     * @param string $body
262
     * @param int    $startPosition
263
     * @return int
264
     */
265
    protected function positionAfterWhitespace(string $body, int $startPosition): int
266
    {
267
        $bodyLength = mb_strlen($body);
268
        $pos        = $startPosition;
269
270
        while ($pos < $bodyLength) {
271
            $code = charCodeAt($body, $pos);
272
273
            if ($code === 9 || $code === 32 || $code === 44 || $code === 0xfeff) {
274
                // tab | space | comma | BOM
275
                ++$pos;
276
            } elseif ($code === 10) {
277
                // new line
278
                ++$pos;
279
                $this->advanceLine($pos);
280
            } elseif ($code === 13) {
281
                // carriage return
282
                if (charCodeAt($body, $pos + 1) === 10) {
283
                    $pos += 2;
284
                } else {
285
                    ++$pos;
286
                }
287
                $this->advanceLine($pos);
288
            } else {
289
                break;
290
            }
291
        }
292
293
        return $pos;
294
    }
295
296
    /**
297
     * @param int $pos
298
     */
299
    protected function advanceLine(int $pos)
300
    {
301
        ++$this->line;
302
        $this->lineStart = $pos;
303
    }
304
}
305