Passed
Push — master ( 6ac985...96b623 )
by Bruno
09:22 queued 14s
created

Parser::fromFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use Exception;
6
use GraphQL\Type\Definition\ObjectType;
7
use GraphQL\Utils\AST;
8
use GraphQL\Type\Definition\Type;
9
use GraphQL\Type\Schema;
10
use GraphQL\Utils\SchemaExtender;
11
12
class Parser
13
{
14
    /**
15
     * @var \GraphQL\Language\AST\DocumentNode
16
     */
17
    protected $ast;
18
19
    /**
20
     * @var \GraphQL\Type\Schema
21
     */
22
    protected $schema;
23
24
    public static function extendDatatypes(array $typeConfig, $typeDefinitionNode): array
0 ignored issues
show
Unused Code introduced by
The parameter $typeDefinitionNode is not used and could be removed. ( Ignorable by Annotation )

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

24
    public static function extendDatatypes(array $typeConfig, /** @scrutinizer ignore-unused */ $typeDefinitionNode): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        /* TODO: extended datatypes
27
        if ($typeConfig['name'] === 'Email') {
28
            $typeConfig = array_merge($typeConfig, [
29
                'serialize' => function ($value) {
30
                    // ...
31
                },
32
                'parseValue' => function ($value) {
33
                    // ...
34
                },
35
                'parseLiteral' => function ($ast) {
36
                    // ...
37
                }
38
            ]);
39
        } */
40
        return $typeConfig;
41
    }
42
43
    protected function __construct()
44
    {
45
        // empty
46
    }
47
48
    /**
49
     *
50
     * @param array $files
51
     * @return self
52
     * @throws \Safe\Exceptions\FilesystemException
53
     */
54
    public static function fromFiles(array $files): self
55
    {
56
        $p = new self();
0 ignored issues
show
Unused Code introduced by
The assignment to $p is dead and can be removed.
Loading history...
57
        $sources = array_map('\Safe\file_get_contents', $files);
58
        return static::fromStrings($sources);
59
    }
60
61
62
    /**
63
     * Returns a Parser from a file path
64
     *
65
     * @param string $path The file path
66
     * @return Parser
67
     * @throws \Safe\Exceptions\FilesystemException If file is not found or parsing fails.
68
     */
69
    public static function fromFile(string $path): self
70
    {
71
        $data = \Safe\file_get_contents($path);
72
        return self::fromString($data);
73
    }
74
75
    /**
76
     * Returns a Parser from a string
77
     *
78
     * @param string $data the string
79
     * @return Parser
80
     * @throws Exception If parsing fails.
81
     */
82
    public static function fromString(string $data): self
83
    {
84
        $p = new self();
85
        $p->ast = \GraphQL\Language\Parser::parse($data);
86
        $schemaBuilder = new \GraphQL\Utils\BuildSchema(
87
            $p->ast,
88
            [__CLASS__, 'extendDatatypes']
89
        );
90
        $p->schema = $schemaBuilder->buildSchema();
91
        return $p;
92
    }
93
94
    /**
95
     *
96
     * @param string[] $files
97
     * @return self
98
     */
99
    public static function fromStrings(array $sources): self
100
    {
101
        $p = new self();
102
        $schema = new Schema([
103
            'query' => new ObjectType(['name' => 'Query']),
104
            'mutation' => new ObjectType(['name' => 'Mutation']),
105
        ]);
106
107
        foreach ($sources as &$s) {
108
            $s = preg_replace('/^type Mutation/m', 'extend type Mutation', $s);
109
            $s = preg_replace('/^type Query/m', 'extend type Query', $s);
110
        }
111
        $extensionSource = implode("\n", $sources);
112
        $p->ast = \GraphQL\Language\Parser::parse($extensionSource);
113
114
        // TODO: extendDatatypes
115
        $p->schema = SchemaExtender::extend(
116
            $schema,
117
            $p->ast
118
        );
119
        // $schemaBuilder = new \GraphQL\Utils\BuildSchema(
120
        //     $p->ast,
121
        //     [__CLASS__, 'extendDatatypes']
122
        // );
123
124
        // $p->schema = $schemaBuilder->buildSchema();
125
        return $p;
126
    }
127
128
129
    public function getSchema(): Schema
130
    {
131
        return $this->schema;
132
    }
133
134
    public function getType(string $name) : ?Type
135
    {
136
        return $this->schema->getType($name);
137
    }
138
}
139