Completed
Pull Request — master (#38)
by Christoffer
05:12 queued 02:56
created

typeFromAST()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 6
nop 2
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Util;
4
5
use Digia\GraphQL\Language\AST\Node\ListTypeNode;
6
use Digia\GraphQL\Language\AST\Node\NamedTypeNode;
7
use Digia\GraphQL\Language\AST\Node\NonNullTypeNode;
8
use Digia\GraphQL\Language\AST\Node\TypeNodeInterface;
9
use Digia\GraphQL\Type\Definition\TypeInterface;
10
use Digia\GraphQL\Type\SchemaInterface;
11
use function Digia\GraphQL\Type\GraphQLList;
12
use function Digia\GraphQL\Type\GraphQLNonNull;
13
14
/**
15
 * @param SchemaInterface   $schema
16
 * @param TypeNodeInterface $typeNode
17
 * @return TypeInterface|null
18
 * @throws \TypeError
19
 * @throws \Exception
20
 */
21
function typeFromAST(SchemaInterface $schema, TypeNodeInterface $typeNode): ?TypeInterface
22
{
23
    $innerType = null;
24
25
    if ($typeNode instanceof ListTypeNode) {
26
        $innerType = typeFromAST($schema, $typeNode->getType());
27
        return null !== $innerType ? GraphQLList($innerType) : null;
28
    }
29
30
    if ($typeNode instanceof NonNullTypeNode) {
31
        $innerType = typeFromAST($schema, $typeNode->getType());
32
        return null !== $innerType ? GraphQLNonNull($innerType) : null;
33
    }
34
35
    if ($typeNode instanceof NamedTypeNode) {
36
        return $schema->getType($typeNode->getNameValue());
37
    }
38
39
    throw new \Exception(sprintf('Unexpected type kind: %s', $typeNode->getKind()));
40
}
41