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

Parser::parseValue()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 47
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 22.6475

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 13
cts 24
cp 0.5417
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 24
nc 11
nop 0
crap 22.6475

How to fix   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\ScannerTokens;
6
use HansOtt\GraphQL\Shared\ScannerGeneric;
7
use HansOtt\GraphQL\Shared\Parser as ParserShared;
8
9
final class Parser extends ParserShared
10
{
11
    private $objects;
12
    private $enums;
13
    private $unions;
14
    private $interfaces;
15
    private $inputObjects;
16
17 3
    protected function getNameFor($tokenType)
18
    {
19 3
        return Token::getNameFor($tokenType);
20
    }
21
22 3
    private function parseListType()
23
    {
24 3
        $location = $this->expect(Token::T_BRACKET_LEFT)->location;
25 3
        $type = $this->parseType();
26 3
        $this->accept(Token::T_BRACKET_RIGHT);
27
28 3
        return new TypeList($type, $location);
0 ignored issues
show
Documentation introduced by
$location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
    }
30
31 6
    private function parseNamedType()
32
    {
33 6
        $nameToken = $this->expect(Token::T_NAME);
34 6
        $type = new TypeNamed($nameToken->value, $nameToken->location);
0 ignored issues
show
Documentation introduced by
$nameToken->location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
36 6
        return $type;
37
    }
38
39 6
    private function maybeParseNullType(Type $type)
40
    {
41 6
        if ($this->accept(Token::T_EXCLAMATION)) {
42 3
            return new TypeNonNull($type, $type->getLocation());
0 ignored issues
show
Documentation introduced by
$type->getLocation() is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
        }
44
45 6
        return $type;
46
    }
47
48 6 View Code Duplication
    private function parseType()
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...
49
    {
50 6
        if ($this->is(Token::T_BRACKET_LEFT)) {
51 3
            return $this->maybeParseNullType(
52 3
                $this->parseListType()
53 3
            );
54
        }
55
56 6
        if ($this->is(Token::T_NAME)) {
57 6
            return $this->maybeParseNullType(
58 6
                $this->parseNamedType()
59 6
            );
60
        }
61
62
        $message = 'Expected a type';
63
64
        if ($this->scanner->eof()) {
65
            throw $this->getParseError($message . ' but instead reached end');
66
        }
67
68
        $token = $this->scanner->peek();
69
        throw $this->getParseError($message . " but instead found \"{$token->getName()}\" with value \"{$token->value}\"");
70
    }
71
72 3 View Code Duplication
    private function parseObject()
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...
73
    {
74 3
        $location = $this->expect(Token::T_BRACE_LEFT)->location;
75 3
        $fields = array();
76 3
        while ($this->scanner->eof() === false) {
77 3
            if ($this->accept(Token::T_BRACE_RIGHT)) {
78 3
                return new ValueObject($fields, $location);
0 ignored issues
show
Documentation introduced by
$location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
            }
80
81 3
            $nameToken = $this->expect(Token::T_NAME);
82 3
            $this->expect(Token::T_COLON);
83 3
            $fields[] = new ValueObjectField($nameToken->value, $this->parseValue(), $nameToken->location);
0 ignored issues
show
Documentation introduced by
$nameToken->location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84 3
            $this->accept(Token::T_COMMA);
85 3
        }
86
87
        throw $this->getParseError('Unclosed brace of object value');
88
    }
89
90 View Code Duplication
    private function parseList()
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...
91
    {
92
        $location = $this->expect(Token::T_BRACKET_LEFT)->location;
93
        $items = array();
94
        while ($this->scanner->eof() === false) {
95
            if ($this->accept(Token::T_BRACKET_RIGHT)) {
96
                return new ValueList($items, $location);
0 ignored issues
show
Documentation introduced by
$location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
            }
98
99
            $items[] = $this->parseValue();
100
            $this->accept(Token::T_COMMA);
101
        }
102
103
        throw $this->getParseError('Unclosed bracket of list');
104
105
    }
106
107 3
    private function parseValue()
108
    {
109 3
        if ($string = $this->accept(Token::T_STRING)) {
110
            return new ValueString($string->value, $string->location);
0 ignored issues
show
Documentation introduced by
$string->location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
        }
112
113 3
        if ($true = $this->accept(Token::T_TRUE)) {
114
            return new ValueBoolean(true, $true->location);
115
        }
116
117 3
        if ($false = $this->accept(Token::T_FALSE)) {
118 3
            return new ValueBoolean(false, $false->location);
119
        }
120
121 3
        if ($null = $this->accept(Token::T_NULL)) {
122
            return new ValueNull($null->location);
123
        }
124
125 3
        if ($int = $this->accept(Token::T_INT)) {
126
            return new ValueInt($int->value, $int->location);
127
        }
128
129 3
        if ($float = $this->accept(Token::T_FLOAT)) {
130 3
            return new ValueFloat($float->value, $float->location);
131
        }
132
133 3
        if ($name = $this->accept(Token::T_NAME)) {
134
            return new ValueEnum($name->value, $name->location);
135
        }
136
137 3
        if ($this->is(Token::T_BRACKET_LEFT)) {
138
            return $this->parseList();
139
        }
140
141 3
        if ($this->is(Token::T_BRACE_LEFT)) {
142 3
            return $this->parseObject();
143
        }
144
145
        $message = 'Expected a value';
146
147
        if ($this->scanner->eof()) {
148
            throw $this->getParseError($message . ' but instead reached end');
149
        }
150
151
        $token = $this->scanner->peek();
152
        throw $this->getParseError($message . " but instead found \"{$token->getName()}\" with value \"{$token->value}\"");
153
    }
154
155 6
    private function parseFieldArguments()
156
    {
157 6
        if ($this->accept(Token::T_PAREN_LEFT) === false) {
158 6
            return array();
159
        }
160
161 3
        $arguments = array();
162 3
        while ($this->scanner->eof() === false) {
163 3
            if ($this->accept(Token::T_PAREN_RIGHT)) {
164 3
                return $arguments;
165
            }
166
167 3
            $nameToken = $this->expect(Token::T_NAME);
168 3
            $this->expect(Token::T_COLON);
169 3
            $type = $this->parseType();
170 3
            $defaultValue = null;
171
172 3
            if ($this->accept(Token::T_EQUAL)) {
173 3
                $defaultValue = $this->parseValue();
174 3
            }
175
176 3
            $arguments[] = new Argument(
177 3
                $nameToken->value,
178 3
                $type,
179 3
                $defaultValue,
180 3
                $nameToken->location
0 ignored issues
show
Documentation introduced by
$nameToken->location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181 3
            );
182
183 3
            $this->accept(Token::T_COMMA);
184 3
        }
185
186
        return $arguments;
187
    }
188
189 9 View Code Duplication
    private function parseField($forInputObject)
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...
190
    {
191 9
        $nameToken = $this->expect(Token::T_NAME);
192
193 9
        $arguments = array();
194 9
        if ($forInputObject === false) {
195 6
            $arguments = $this->parseFieldArguments();
196 6
        }
197
198 9
        $this->expect(Token::T_COLON);
199 6
        $type = $this->parseType();
200
201 6
        return new Field(
202 6
            $nameToken->value,
203 6
            $type,
204 6
            $arguments,
205 6
            $nameToken->location
0 ignored issues
show
Documentation introduced by
$nameToken->location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
206 6
        );
207
    }
208
209 9
    private function parseObjectDeclaration()
210
    {
211 6
        $location = $this->expect(Token::T_NAME)->location;
212 6
        $name = $this->expect(Token::T_NAME)->value;
213
214 6
        $interface = null;
215 6
        if ($implementsToken = $this->accept(Token::T_NAME)) {
216 3
            if ($implementsToken->value !== 'implements') {
217
                throw $this->getParseError("Expected \"implements\" but instead found \"{$implementsToken->value}\"");
218
            }
219 3
            $interface = $this->expect(Token::T_NAME)->value;
220 3
        }
221
222 6
        $this->expect(Token::T_BRACE_LEFT);
223 9
        $fields = array();
224 6
        while ($this->scanner->eof() === false) {
225 6
            if ($this->accept(Token::T_BRACE_RIGHT)) {
226 6
                return new DeclarationObject(
227 6
                    $name,
228 6
                    $fields,
229 6
                    $interface,
230
                    $location
0 ignored issues
show
Documentation introduced by
$location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
231 6
                );
232
            }
233
234 6
            $fields[] = $this->parseField(false);
235 6
            $this->accept(Token::T_COMMA);
236 6
        }
237
238
        throw $this->getParseError('Expected an object field but instead reached end');
239
    }
240
241 6 View Code Duplication
    private function parseInputObjectDeclaration()
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...
242
    {
243 6
        $location = $this->expect(Token::T_NAME)->location;
244 6
        $name = $this->expect(Token::T_NAME)->value;
245 6
        $this->expect(Token::T_BRACE_LEFT);
246 6
        $fields = array();
247 6
        while ($this->scanner->eof() === false) {
248 6
            if ($this->accept(Token::T_BRACE_RIGHT)) {
249 3
                return new DeclarationInputObject(
250 3
                    $name,
251 3
                    $fields,
252
                    $location
0 ignored issues
show
Documentation introduced by
$location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
253 3
                );
254
            }
255
256 6
            $fields[] = $this->parseField(true);
257 3
            $this->accept(Token::T_COMMA);
258 3
        }
259
260
        throw $this->getParseError('Expected an input object field but instead reached end');
261
    }
262
263 3 View Code Duplication
    private function parseEnumDeclaration()
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...
264
    {
265 3
        $location = $this->expect(Token::T_NAME)->location;
266 3
        $name = $this->expect(Token::T_NAME)->value;
267 3
        $this->expect(Token::T_BRACE_LEFT);
268 3
        $values = array();
269 3
        while ($this->scanner->eof() === false) {
270 3
            if ($this->accept(Token::T_BRACE_RIGHT)) {
271 3
                return new DeclarationEnum(
272 3
                    $name,
273 3
                    $values,
274
                    $location
0 ignored issues
show
Documentation introduced by
$location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
275 3
                );
276
            }
277
278 3
            $values[] = $this->expect(Token::T_NAME)->value;
279 3
            $this->accept(Token::T_COMMA);
280 3
        }
281
282
        throw $this->getParseError('Expected an enumeration value but instead reached end');
283
    }
284
285 3 View Code Duplication
    private function parseInterfaceDeclaration()
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...
286
    {
287 3
        $location = $this->expect(Token::T_NAME)->location;
288 3
        $name = $this->expect(Token::T_NAME)->value;
289 3
        $this->expect(Token::T_BRACE_LEFT);
290 3
        $fields = array();
291 3
        while ($this->scanner->eof() === false) {
292 3
            if ($this->accept(Token::T_BRACE_RIGHT)) {
293 3
                return new DeclarationInterface(
294 3
                    $name,
295 3
                    $fields,
296
                    $location
0 ignored issues
show
Documentation introduced by
$location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
297 3
                );
298
            }
299
300 3
            $fields[] = $this->parseField(false);
301 3
            $this->accept(Token::T_COMMA);
302 3
        }
303
304
        throw $this->getParseError('Expected an interface field but instead reached end');
305
    }
306
307 3
    private function parseUnionDeclaration()
308
    {
309 3
        $location = $this->expect(Token::T_NAME)->location;
310 3
        $name = $this->expect(Token::T_NAME)->value;
311 3
        $this->expect(Token::T_EQUAL);
312 3
        $members = array();
313 3
        while ($this->scanner->eof() === false) {
314 3
            if ($this->is(Token::T_NAME) === false) {
315
                return new DeclarationUnion(
316
                    $name,
317
                    $members,
318
                    $location
0 ignored issues
show
Documentation introduced by
$location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
319
                );
320
            }
321
322 3
            $members[] = $this->expect(Token::T_NAME)->value;
323
324 3
            if ($this->accept(Token::T_PIPE) === false) {
325 3
                return new DeclarationUnion(
326 3
                    $name,
327 3
                    $members,
328
                    $location
0 ignored issues
show
Documentation introduced by
$location is of type object<HansOtt\GraphQL\Shared\Location>, but the function expects a null|object<HansOtt\GraphQL\Schema\Location>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
329 3
                );
330
            }
331 3
        }
332
333
        throw $this->getParseError('Expected an interface field but instead reached end');
334
    }
335
336 9
    private function parseDeclaration()
337
    {
338 9
        $message = 'Expected a type declaration';
339
340 9
        if ($this->scanner->eof()) {
341
            throw $this->getParseError("{$message} but instead reached end");
342
        }
343
344 9
        $token = $this->scanner->peek();
345 9
        switch ($token->value) {
346 9
            case 'type':
347 6
                return $this->parseObjectDeclaration();
348 6
            case 'input':
349 6
                return $this->parseInputObjectDeclaration();
350 3
            case 'enum':
351 3
                return $this->parseEnumDeclaration();
352 3
            case 'interface':
353 3
                return $this->parseInterfaceDeclaration();
354 3
            case 'union':
355 3
                return $this->parseUnionDeclaration();
356
        }
357
358
        throw $this->getParseError("{$message} but instead found {$token->getName()} with value \"{$token->value}\"");
359
    }
360
361 9
    private function parseSchema()
362
    {
363 9
        $declarations = array();
364 9
        while ($this->scanner->eof() === false) {
365 9
            $declarations[] = $this->parseDeclaration();
366 6
        }
367
368 6
        return new Schema($declarations);
369
    }
370
371
    /**
372
     * @param string $schema
373
     *
374
     * @return Schema
375
     */
376 9
    public function parse($schema)
377
    {
378 9
        $tokens = $this->lexer->lex($schema);
379 9
        $scanner = new ScannerGeneric($tokens);
380 9
        $this->scanner = new ScannerTokens($scanner);
381 9
        $this->objects = array();
382 9
        $this->enums = array();
383 9
        $this->unions = array();
384 9
        $this->interfaces = array();
385 9
        $this->inputObjects = array();
386 9
        $declaration = $this->parseSchema();
387
388 6
        return $declaration;
389
    }
390
}
391