SchemaConfig::create()   F
last analyzed

Complexity

Conditions 11
Paths 513

Size

Total Lines 48
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 11.0069

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 48
rs 3.8263
ccs 25
cts 26
cp 0.9615
cc 11
nc 513
nop 1
crap 11.0069

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
declare(strict_types=1);
4
5
namespace GraphQL\Type;
6
7
use GraphQL\Language\AST\SchemaDefinitionNode;
8
use GraphQL\Language\AST\SchemaTypeExtensionNode;
9
use GraphQL\Type\Definition\Directive;
10
use GraphQL\Type\Definition\ObjectType;
11
use GraphQL\Type\Definition\Type;
12
use GraphQL\Utils\Utils;
13
use function is_callable;
14
15
/**
16
 * Schema configuration class.
17
 * Could be passed directly to schema constructor. List of options accepted by **create** method is
18
 * [described in docs](type-system/schema.md#configuration-options).
19
 *
20
 * Usage example:
21
 *
22
 *     $config = SchemaConfig::create()
23
 *         ->setQuery($myQueryType)
24
 *         ->setTypeLoader($myTypeLoader);
25
 *
26
 *     $schema = new Schema($config);
27
 */
28
class SchemaConfig
29
{
30
    /** @var ObjectType */
31
    public $query;
32
33
    /** @var ObjectType */
34
    public $mutation;
35
36
    /** @var ObjectType */
37
    public $subscription;
38
39
    /** @var Type[]|callable */
40
    public $types;
41
42
    /** @var Directive[] */
43
    public $directives;
44
45
    /** @var callable|null */
46
    public $typeLoader;
47
48
    /** @var SchemaDefinitionNode */
49
    public $astNode;
50
51
    /** @var bool */
52
    public $assumeValid;
53
54
    /** @var SchemaTypeExtensionNode[] */
55
    public $extensionASTNodes;
56
57
    /**
58
     * Converts an array of options to instance of SchemaConfig
59
     * (or just returns empty config when array is not passed).
60
     *
61
     * @param mixed[] $options
62
     *
63
     * @return SchemaConfig
64
     *
65
     * @api
66
     */
67 891
    public static function create(array $options = [])
68
    {
69 891
        $config = new static();
70
71 891
        if (! empty($options)) {
72 889
            if (isset($options['query'])) {
73 874
                $config->setQuery($options['query']);
74
            }
75
76 889
            if (isset($options['mutation'])) {
77 83
                $config->setMutation($options['mutation']);
78
            }
79
80 889
            if (isset($options['subscription'])) {
81 35
                $config->setSubscription($options['subscription']);
82
            }
83
84 889
            if (isset($options['types'])) {
85 288
                $config->setTypes($options['types']);
86
            }
87
88 889
            if (isset($options['directives'])) {
89 550
                $config->setDirectives($options['directives']);
90
            }
91
92 889
            if (isset($options['typeLoader'])) {
93 162
                Utils::invariant(
94 162
                    is_callable($options['typeLoader']),
95 162
                    'Schema type loader must be callable if provided but got: %s',
96 162
                    Utils::printSafe($options['typeLoader'])
97
                );
98 161
                $config->setTypeLoader($options['typeLoader']);
99
            }
100
101 888
            if (isset($options['astNode'])) {
102 21
                $config->setAstNode($options['astNode']);
103
            }
104
105 888
            if (isset($options['assumeValid'])) {
106
                $config->setAssumeValid((bool) $options['assumeValid']);
107
            }
108
109 888
            if (isset($options['extensionASTNodes'])) {
110 6
                $config->setExtensionASTNodes($options['extensionASTNodes']);
111
            }
112
        }
113
114 890
        return $config;
115
    }
116
117
    /**
118
     * @return SchemaDefinitionNode
119
     */
120 147
    public function getAstNode()
121
    {
122 147
        return $this->astNode;
123
    }
124
125
    /**
126
     * @return SchemaConfig
127
     */
128 21
    public function setAstNode(SchemaDefinitionNode $astNode)
129
    {
130 21
        $this->astNode = $astNode;
131
132 21
        return $this;
133
    }
134
135
    /**
136
     * @return ObjectType
137
     *
138
     * @api
139
     */
140
    public function getQuery()
141
    {
142
        return $this->query;
143
    }
144
145
    /**
146
     * @param ObjectType $query
147
     *
148
     * @return SchemaConfig
149
     *
150
     * @api
151
     */
152 874
    public function setQuery($query)
153
    {
154 874
        $this->query = $query;
155
156 874
        return $this;
157
    }
158
159
    /**
160
     * @return ObjectType
161
     *
162
     * @api
163
     */
164
    public function getMutation()
165
    {
166
        return $this->mutation;
167
    }
168
169
    /**
170
     * @param ObjectType $mutation
171
     *
172
     * @return SchemaConfig
173
     *
174
     * @api
175
     */
176 83
    public function setMutation($mutation)
177
    {
178 83
        $this->mutation = $mutation;
179
180 83
        return $this;
181
    }
182
183
    /**
184
     * @return ObjectType
185
     *
186
     * @api
187
     */
188
    public function getSubscription()
189
    {
190
        return $this->subscription;
191
    }
192
193
    /**
194
     * @param ObjectType $subscription
195
     *
196
     * @return SchemaConfig
197
     *
198
     * @api
199
     */
200 35
    public function setSubscription($subscription)
201
    {
202 35
        $this->subscription = $subscription;
203
204 35
        return $this;
205
    }
206
207
    /**
208
     * @return Type[]
209
     *
210
     * @api
211
     */
212
    public function getTypes()
213
    {
214
        return $this->types ?: [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->types ?: array() also could return the type callable which is incompatible with the documented return type GraphQL\Type\Definition\Type[].
Loading history...
215
    }
216
217
    /**
218
     * @param Type[]|callable $types
219
     *
220
     * @return SchemaConfig
221
     *
222
     * @api
223
     */
224 288
    public function setTypes($types)
225
    {
226 288
        $this->types = $types;
227
228 288
        return $this;
229
    }
230
231
    /**
232
     * @return Directive[]
233
     *
234
     * @api
235
     */
236
    public function getDirectives()
237
    {
238
        return $this->directives ?: [];
239
    }
240
241
    /**
242
     * @param Directive[] $directives
243
     *
244
     * @return SchemaConfig
245
     *
246
     * @api
247
     */
248 550
    public function setDirectives(array $directives)
249
    {
250 550
        $this->directives = $directives;
251
252 550
        return $this;
253
    }
254
255
    /**
256
     * @return callable
257
     *
258
     * @api
259
     */
260
    public function getTypeLoader()
261
    {
262
        return $this->typeLoader;
263
    }
264
265
    /**
266
     * @return SchemaConfig
267
     *
268
     * @api
269
     */
270 161
    public function setTypeLoader(callable $typeLoader)
271
    {
272 161
        $this->typeLoader = $typeLoader;
273
274 161
        return $this;
275
    }
276
277
    /**
278
     * @return bool
279
     */
280 890
    public function getAssumeValid()
281
    {
282 890
        return $this->assumeValid;
283
    }
284
285
    /**
286
     * @param bool $assumeValid
287
     *
288
     * @return SchemaConfig
289
     */
290
    public function setAssumeValid($assumeValid)
291
    {
292
        $this->assumeValid = $assumeValid;
293
294
        return $this;
295
    }
296
297
    /**
298
     * @return SchemaTypeExtensionNode[]
299
     */
300
    public function getExtensionASTNodes()
301
    {
302
        return $this->extensionASTNodes;
303
    }
304
305
    /**
306
     * @param SchemaTypeExtensionNode[] $extensionASTNodes
307
     */
308 6
    public function setExtensionASTNodes(array $extensionASTNodes)
309
    {
310 6
        $this->extensionASTNodes = $extensionASTNodes;
311 6
    }
312
}
313