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

getNamedType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Type;
4
5
use Digia\GraphQL\Type\Definition\AbstractTypeInterface;
6
use Digia\GraphQL\Type\Definition\CompositeTypeInterface;
7
use Digia\GraphQL\Type\Definition\Directive;
8
use Digia\GraphQL\Type\Definition\EnumType;
9
use Digia\GraphQL\Type\Definition\InputObjectType;
10
use Digia\GraphQL\Type\Definition\InputTypeInterface;
11
use Digia\GraphQL\Type\Definition\InterfaceType;
12
use Digia\GraphQL\Type\Definition\LeafTypeInterface;
13
use Digia\GraphQL\Type\Definition\ListType;
14
use Digia\GraphQL\Type\Definition\NamedTypeInterface;
15
use Digia\GraphQL\Type\Definition\NonNullType;
16
use Digia\GraphQL\Type\Definition\ObjectType;
17
use Digia\GraphQL\Type\Definition\OutputTypeInterface;
18
use Digia\GraphQL\Type\Definition\ScalarType;
19
use Digia\GraphQL\Type\Definition\TypeInterface;
20
use Digia\GraphQL\Type\Definition\UnionType;
21
use Digia\GraphQL\Type\Definition\WrappingTypeInterface;
22
use function Digia\GraphQL\Util\invariant;
23
use function Digia\GraphQL\Util\toString;
24
25
/**
26
 * @param $thunk
27
 * @return null|array
28
 */
29
function resolveThunk($thunk): ?array
30
{
31
    return \is_callable($thunk) ? $thunk() : $thunk;
32
}
33
34
/**
35
 * @param mixed $value
36
 * @return bool
37
 */
38
function isAssocArray($value): bool
39
{
40
    if (!is_array($value)) {
41
        return false;
42
    }
43
    if (empty($value)) {
44
        return true;
45
    }
46
    $keys = array_keys($value);
47
    return $keys !== array_keys($keys);
48
}
49
50
/**
51
 * @param $resolver
52
 * @return bool
53
 */
54
function isValidResolver($resolver): bool
55
{
56
    return $resolver === null || \is_callable($resolver);
57
}
58
59
/**
60
 * @param $type
61
 * @throws \Exception
62
 */
63
function assertType($type)
64
{
65
    invariant(
66
        $type instanceof TypeInterface,
67
        sprintf(sprintf('Expected %s to be a GraphQL type.', $type), toString($type))
68
    );
69
}
70
71
/**
72
 * @param TypeInterface $type
73
 * @throws \Exception
74
 */
75
function assertScalarType(TypeInterface $type)
76
{
77
    invariant(
78
        $type instanceof ScalarType,
79
        sprintf('Expected %s to be a GraphQL Scalar type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

79
        sprintf('Expected %s to be a GraphQL Scalar type.', /** @scrutinizer ignore-type */ $type)
Loading history...
80
    );
81
}
82
83
/**
84
 * @param TypeInterface $type
85
 * @throws \Exception
86
 */
87
function assertObjectType(TypeInterface $type)
88
{
89
    invariant(
90
        $type instanceof ObjectType,
91
        sprintf('Expected %s to be a GraphQL Object type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

91
        sprintf('Expected %s to be a GraphQL Object type.', /** @scrutinizer ignore-type */ $type)
Loading history...
92
    );
93
}
94
95
/**
96
 * @param TypeInterface $type
97
 * @throws \Exception
98
 */
99
function assertInterfaceType(TypeInterface $type)
100
{
101
    invariant(
102
        $type instanceof InterfaceType,
103
        sprintf('Expected %s to be a GraphQL Interface type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

103
        sprintf('Expected %s to be a GraphQL Interface type.', /** @scrutinizer ignore-type */ $type)
Loading history...
104
    );
105
}
106
107
/**
108
 * @param TypeInterface $type
109
 * @throws \Exception
110
 */
111
function assertUnionType(TypeInterface $type)
112
{
113
    invariant(
114
        $type instanceof UnionType,
115
        sprintf('Expected %s to be a GraphQL Union type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

115
        sprintf('Expected %s to be a GraphQL Union type.', /** @scrutinizer ignore-type */ $type)
Loading history...
116
    );
117
}
118
119
/**
120
 * @param TypeInterface $type
121
 * @throws \Exception
122
 */
123
function assertEnumType(TypeInterface $type)
124
{
125
    invariant(
126
        $type instanceof EnumType,
127
        sprintf('Expected %s to be a GraphQL Enum type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

127
        sprintf('Expected %s to be a GraphQL Enum type.', /** @scrutinizer ignore-type */ $type)
Loading history...
128
    );
129
}
130
131
/**
132
 * @param TypeInterface $type
133
 * @throws \Exception
134
 */
135
function assertInputObjectType(TypeInterface $type)
136
{
137
    invariant(
138
        $type instanceof InputObjectType,
139
        sprintf('Expected %s to be a GraphQL InputObject type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

139
        sprintf('Expected %s to be a GraphQL InputObject type.', /** @scrutinizer ignore-type */ $type)
Loading history...
140
    );
141
}
142
143
/**
144
 * @param TypeInterface $type
145
 * @throws \Exception
146
 */
147
function assertListType(TypeInterface $type)
148
{
149
    invariant(
150
        $type instanceof ListType,
151
        sprintf('Expected %s to be a GraphQL List type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

151
        sprintf('Expected %s to be a GraphQL List type.', /** @scrutinizer ignore-type */ $type)
Loading history...
152
    );
153
}
154
155
/**
156
 * @param TypeInterface $type
157
 * @throws \Exception
158
 */
159
function assertNonNullType(TypeInterface $type)
160
{
161
    invariant(
162
        $type instanceof NonNullType,
163
        sprintf('Expected %s to be a GraphQL NonNull type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

163
        sprintf('Expected %s to be a GraphQL NonNull type.', /** @scrutinizer ignore-type */ $type)
Loading history...
164
    );
165
}
166
167
/**
168
 * @param TypeInterface|null $type
169
 * @return bool
170
 */
171
function isInputType(?TypeInterface $type): bool
172
{
173
    return null !== $type && ($type instanceof InputTypeInterface || ($type instanceof WrappingTypeInterface && isInputType($type->getOfType())));
174
}
175
176
/**
177
 * @param TypeInterface $type
178
 * @throws \Exception
179
 */
180
function assertInputType(TypeInterface $type)
181
{
182
    invariant(
183
        isInputType($type),
184
        sprintf('Expected %s to be a GraphQL input type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

184
        sprintf('Expected %s to be a GraphQL input type.', /** @scrutinizer ignore-type */ $type)
Loading history...
185
    );
186
}
187
188
/**
189
 * @param TypeInterface|null $type
190
 * @return bool
191
 */
192
function isOutputType(?TypeInterface $type): bool
193
{
194
    return null !== $type && ($type instanceof OutputTypeInterface || ($type instanceof WrappingTypeInterface && isOutputType($type->getOfType())));
195
}
196
197
/**
198
 * @param TypeInterface $type
199
 * @throws \Exception
200
 */
201
function assertOutputType(TypeInterface $type)
202
{
203
    invariant(
204
        isOutputType($type),
205
        sprintf('Expected %s to be a GraphQL output type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

205
        sprintf('Expected %s to be a GraphQL output type.', /** @scrutinizer ignore-type */ $type)
Loading history...
206
    );
207
}
208
209
/**
210
 * @param TypeInterface $type
211
 * @throws \Exception
212
 */
213
function assertLeafType(TypeInterface $type)
214
{
215
    invariant(
216
        $type instanceof LeafTypeInterface,
217
        sprintf('Expected %s to be a GraphQL leaf type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

217
        sprintf('Expected %s to be a GraphQL leaf type.', /** @scrutinizer ignore-type */ $type)
Loading history...
218
    );
219
}
220
221
/**
222
 * @param TypeInterface $type
223
 * @throws \Exception
224
 */
225
function assertCompositeType(TypeInterface $type)
226
{
227
    invariant(
228
        $type instanceof CompositeTypeInterface,
229
        sprintf('Expected %s to be a GraphQL composite type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

229
        sprintf('Expected %s to be a GraphQL composite type.', /** @scrutinizer ignore-type */ $type)
Loading history...
230
    );
231
}
232
233
/**
234
 * @param TypeInterface $type
235
 * @throws \Exception
236
 */
237
function assertAbstractType(TypeInterface $type)
238
{
239
    invariant(
240
        $type instanceof AbstractTypeInterface,
241
        sprintf('Expected %s to be a GraphQL abstract type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

241
        sprintf('Expected %s to be a GraphQL abstract type.', /** @scrutinizer ignore-type */ $type)
Loading history...
242
    );
243
}
244
245
/**
246
 * @param TypeInterface $type
247
 * @throws \Exception
248
 */
249
function assertWrappingType(TypeInterface $type)
250
{
251
    invariant(
252
        $type instanceof WrappingTypeInterface,
253
        sprintf('Expected %s to be a GraphQL wrapping type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

253
        sprintf('Expected %s to be a GraphQL wrapping type.', /** @scrutinizer ignore-type */ $type)
Loading history...
254
    );
255
}
256
257
/**
258
 * @param TypeInterface $type
259
 * @return bool
260
 */
261
function isNullableType(TypeInterface $type): bool
262
{
263
    return !$type instanceof NonNullType;
264
}
265
266
/**
267
 * @param TypeInterface $type
268
 * @return TypeInterface
269
 * @throws \Exception
270
 */
271
function assertNullableType(TypeInterface $type): TypeInterface
272
{
273
    invariant(
274
        isNullableType($type),
275
        sprintf('Expected %s to be a GraphQL nullable type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

275
        sprintf('Expected %s to be a GraphQL nullable type.', /** @scrutinizer ignore-type */ $type)
Loading history...
276
    );
277
278
    return $type;
279
}
280
281
/**
282
 * @param TypeInterface|null $type
283
 * @return TypeInterface|null
284
 */
285
function getNullableType(?TypeInterface $type): ?TypeInterface
286
{
287
    if (null !== $type) {
288
        return null;
289
    }
290
291
    return $type instanceof NonNullType ? $type->getOfType() : $type;
292
}
293
294
/**
295
 * @param TypeInterface $type
296
 * @throws \Exception
297
 */
298
function assertNamedType(TypeInterface $type)
299
{
300
    invariant(
301
        $type instanceof NamedTypeInterface,
302
        sprintf('Expected %s to be a GraphQL named type.', $type)
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

302
        sprintf('Expected %s to be a GraphQL named type.', /** @scrutinizer ignore-type */ $type)
Loading history...
303
    );
304
}
305
306
/**
307
 * @param TypeInterface|null $type
308
 * @return NamedTypeInterface|null
309
 */
310
function getNamedType(?TypeInterface $type): ?NamedTypeInterface
311
{
312
    if (!$type) {
313
        return null;
314
    }
315
316
    $unwrappedType = $type;
317
318
    while ($unwrappedType instanceof WrappingTypeInterface) {
319
        $unwrappedType = $unwrappedType->getOfType();
320
    }
321
322
    return $unwrappedType;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $unwrappedType returns the type Digia\GraphQL\Type\Definition\TypeInterface which is incompatible with the type-hinted return null|Digia\GraphQL\Type\...tion\NamedTypeInterface.
Loading history...
323
}
324
325
/**
326
 * @param array $config
327
 * @return ScalarType
328
 */
329
function GraphQLScalarType(array $config = []): ScalarType
330
{
331
    return new ScalarType($config);
332
}
333
334
/**
335
 * @param array $config
336
 * @return EnumType
337
 */
338
function GraphQLEnumType(array $config = []): EnumType
339
{
340
    return new EnumType($config);
341
}
342
343
/**
344
 * @param array $config
345
 * @return InputObjectType
346
 */
347
function GraphQLInputObjectType(array $config = []): InputObjectType
348
{
349
    return new InputObjectType($config);
350
}
351
352
/**
353
 * @param array $config
354
 * @return InterfaceType
355
 */
356
function GraphQLInterfaceType(array $config = []): InterfaceType
357
{
358
    return new InterfaceType($config);
359
}
360
361
/**
362
 * @param array $config
363
 * @return ObjectType
364
 */
365
function GraphQLObjectType(array $config = []): ObjectType
366
{
367
    return new ObjectType($config);
368
}
369
370
/**
371
 * @param array $config
372
 * @return UnionType
373
 */
374
function GraphQLUnionType(array $config = []): UnionType
375
{
376
    return new UnionType($config);
377
}
378
379
/**
380
 * @param array $config
381
 * @return Schema
382
 */
383
function GraphQLSchema(array $config = []): Schema
384
{
385
    return new Schema($config);
386
}
387
388
/**
389
 * @param array $config
390
 * @return Directive
391
 */
392
function GraphQLDirective(array $config = []): Directive
393
{
394
    return new Directive($config);
395
}
396
397
/**
398
 * @param TypeInterface $ofType
399
 * @return ListType
400
 * @throws \TypeError
401
 */
402
function GraphQLList(TypeInterface $ofType): ListType
403
{
404
    return new ListType($ofType);
405
}
406
407
/**
408
 * @param TypeInterface $ofType
409
 * @return NonNullType
410
 * @throws \TypeError
411
 */
412
function GraphQLNonNull(TypeInterface $ofType): NonNullType
413
{
414
    return new NonNullType($ofType);
415
}
416