Passed
Push — master ( 41f82c...2e19c8 )
by Christoffer
02:41
created

newArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Type;
4
5
use Digia\GraphQL\Error\InvalidTypeException;
6
use Digia\GraphQL\Error\InvariantException;
7
use Digia\GraphQL\Schema\Schema;
8
use Digia\GraphQL\Type\Definition\AbstractTypeInterface;
9
use Digia\GraphQL\Type\Definition\Argument;
10
use Digia\GraphQL\Type\Definition\CompositeTypeInterface;
11
use Digia\GraphQL\Type\Definition\Directive;
12
use Digia\GraphQL\Type\Definition\EnumType;
13
use Digia\GraphQL\Type\Definition\EnumValue;
14
use Digia\GraphQL\Type\Definition\Field;
15
use Digia\GraphQL\Type\Definition\InputField;
16
use Digia\GraphQL\Type\Definition\InputObjectType;
17
use Digia\GraphQL\Type\Definition\InputTypeInterface;
18
use Digia\GraphQL\Type\Definition\InterfaceType;
19
use Digia\GraphQL\Type\Definition\LeafTypeInterface;
20
use Digia\GraphQL\Type\Definition\ListType;
21
use Digia\GraphQL\Type\Definition\NamedTypeInterface;
22
use Digia\GraphQL\Type\Definition\NonNullType;
23
use Digia\GraphQL\Type\Definition\ObjectType;
24
use Digia\GraphQL\Type\Definition\OutputTypeInterface;
25
use Digia\GraphQL\Type\Definition\ScalarType;
26
use Digia\GraphQL\Type\Definition\TypeInterface;
27
use Digia\GraphQL\Type\Definition\UnionType;
28
use Digia\GraphQL\Type\Definition\WrappingTypeInterface;
29
use function Digia\GraphQL\Util\invariant;
30
31
/**
32
 * @param $thunk
33
 * @return null|array
34
 */
35
function resolveThunk($thunk): ?array
36
{
37
    return \is_callable($thunk) ? $thunk() : $thunk;
38
}
39
40
/**
41
 * @param mixed $value
42
 * @return bool
43
 */
44
function isAssocArray($value): bool
45
{
46
    if (!\is_array($value)) {
47
        return false;
48
    }
49
    if (empty($value)) {
50
        return true;
51
    }
52
    $keys = \array_keys($value);
53
    return $keys !== \array_keys($keys);
54
}
55
56
/**
57
 * @param $type
58
 * @throws InvariantException
59
 */
60
function assertType($type)
61
{
62
    invariant(
63
        $type instanceof TypeInterface,
64
        \sprintf('Expected %s to be a GraphQL type.', (string)$type)
65
    );
66
}
67
68
/**
69
 * @param TypeInterface $type
70
 * @throws InvariantException
71
 */
72
function assertScalarType(TypeInterface $type)
73
{
74
    invariant(
75
        $type instanceof ScalarType,
76
        \sprintf('Expected %s to be a GraphQL Scalar type.', (string)$type)
77
    );
78
}
79
80
/**
81
 * @param TypeInterface $type
82
 * @throws InvariantException
83
 */
84
function assertObjectType(TypeInterface $type)
85
{
86
    invariant(
87
        $type instanceof ObjectType,
88
        \sprintf('Expected %s to be a GraphQL Object type.', (string)$type)
89
    );
90
}
91
92
/**
93
 * @param TypeInterface $type
94
 * @throws InvariantException
95
 */
96
function assertInterfaceType(TypeInterface $type)
97
{
98
    invariant(
99
        $type instanceof InterfaceType,
100
        \sprintf('Expected %s to be a GraphQL Interface type.', (string)$type)
101
    );
102
}
103
104
/**
105
 * @param TypeInterface $type
106
 * @throws InvariantException
107
 */
108
function assertUnionType(TypeInterface $type)
109
{
110
    invariant(
111
        $type instanceof UnionType,
112
        \sprintf('Expected %s to be a GraphQL Union type.', (string)$type)
113
    );
114
}
115
116
/**
117
 * @param TypeInterface $type
118
 * @throws InvariantException
119
 */
120
function assertEnumType(TypeInterface $type)
121
{
122
    invariant(
123
        $type instanceof EnumType,
124
        \sprintf('Expected %s to be a GraphQL Enum type.', (string)$type)
125
    );
126
}
127
128
/**
129
 * @param TypeInterface $type
130
 * @throws InvariantException
131
 */
132
function assertInputObjectType(TypeInterface $type)
133
{
134
    invariant(
135
        $type instanceof InputObjectType,
136
        \sprintf('Expected %s to be a GraphQL InputObject type.', (string)$type)
137
    );
138
}
139
140
/**
141
 * @param TypeInterface $type
142
 * @throws InvariantException
143
 */
144
function assertListType(TypeInterface $type)
145
{
146
    invariant(
147
        $type instanceof ListType,
148
        \sprintf('Expected %s to be a GraphQL List type.', (string)$type)
149
    );
150
}
151
152
/**
153
 * @param TypeInterface $type
154
 * @throws InvariantException
155
 */
156
function assertNonNullType(TypeInterface $type)
157
{
158
    invariant(
159
        $type instanceof NonNullType,
160
        \sprintf('Expected %s to be a GraphQL NonNull type.', (string)$type)
161
    );
162
}
163
164
/**
165
 * Whether a type is an input type cannot be determined with `instanceof`
166
 * because lists and non-nulls can also be output types if the wrapped type is an output type.
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 InvariantException
179
 */
180
function assertInputType(TypeInterface $type)
181
{
182
    invariant(
183
        isInputType($type),
184
        \sprintf('Expected %s to be a GraphQL input type.', (string)$type)
185
    );
186
}
187
188
/**
189
 * Whether a type is an output type cannot be determined with `instanceof`
190
 * because lists and non-nulls can also be output types if the wrapped type is an output type.
191
 *
192
 * @param TypeInterface|null $type
193
 * @return bool
194
 */
195
function isOutputType(?TypeInterface $type): bool
196
{
197
    return null !== $type && ($type instanceof OutputTypeInterface || ($type instanceof WrappingTypeInterface && isOutputType($type->getOfType())));
198
}
199
200
/**
201
 * @param TypeInterface $type
202
 * @throws InvariantException
203
 */
204
function assertOutputType(TypeInterface $type)
205
{
206
    invariant(
207
        isOutputType($type),
208
        \sprintf('Expected %s to be a GraphQL output type.', (string)$type)
209
    );
210
}
211
212
/**
213
 * @param TypeInterface $type
214
 * @throws InvariantException
215
 */
216
function assertLeafType(TypeInterface $type)
217
{
218
    invariant(
219
        $type instanceof LeafTypeInterface,
220
        \sprintf('Expected %s to be a GraphQL leaf type.', (string)$type)
221
    );
222
}
223
224
/**
225
 * @param TypeInterface $type
226
 * @throws InvariantException
227
 */
228
function assertCompositeType(TypeInterface $type)
229
{
230
    invariant(
231
        $type instanceof CompositeTypeInterface,
232
        \sprintf('Expected %s to be a GraphQL composite type.', (string)$type)
233
    );
234
}
235
236
/**
237
 * @param TypeInterface $type
238
 * @throws InvariantException
239
 */
240
function assertAbstractType(TypeInterface $type)
241
{
242
    invariant(
243
        $type instanceof AbstractTypeInterface,
244
        \sprintf('Expected %s to be a GraphQL abstract type.', (string)$type)
245
    );
246
}
247
248
/**
249
 * @param TypeInterface $type
250
 * @throws InvariantException
251
 */
252
function assertWrappingType(TypeInterface $type)
253
{
254
    invariant(
255
        $type instanceof WrappingTypeInterface,
256
        \sprintf('Expected %s to be a GraphQL wrapping type.', (string)$type)
257
    );
258
}
259
260
/**
261
 * @param TypeInterface $type
262
 * @return bool
263
 */
264
function isNullableType(TypeInterface $type): bool
265
{
266
    return !($type instanceof NonNullType);
267
}
268
269
/**
270
 * @param TypeInterface $type
271
 * @return TypeInterface
272
 * @throws InvariantException
273
 */
274
function assertNullableType(TypeInterface $type): TypeInterface
275
{
276
    invariant(
277
        isNullableType($type),
278
        \sprintf('Expected %s to be a GraphQL nullable type.', (string)$type)
279
    );
280
281
    return $type;
282
}
283
284
/**
285
 * @param TypeInterface|null $type
286
 * @return TypeInterface|null
287
 */
288
function getNullableType(?TypeInterface $type): ?TypeInterface
289
{
290
    if (null === $type) {
291
        return null;
292
    }
293
294
    return $type instanceof NonNullType ? $type->getOfType() : $type;
295
}
296
297
/**
298
 * @param TypeInterface $type
299
 * @throws InvariantException
300
 */
301
function assertNamedType(TypeInterface $type)
302
{
303
    invariant(
304
        $type instanceof NamedTypeInterface,
305
        \sprintf('Expected %s to be a GraphQL named type.', (string)$type)
306
    );
307
}
308
309
/**
310
 * @param TypeInterface|null $type
311
 * @return NamedTypeInterface|null
312
 */
313
function getNamedType(?TypeInterface $type): ?NamedTypeInterface
314
{
315
    if (!$type) {
316
        return null;
317
    }
318
319
    $unwrappedType = $type;
320
321
    while ($unwrappedType instanceof WrappingTypeInterface) {
322
        $unwrappedType = $unwrappedType->getOfType();
323
    }
324
325
    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...
326
}
327
328
/**
329
 * @param array $config
330
 * @return ScalarType
331
 * @throws InvariantException
332
 */
333
function newScalarType(array $config = []): ScalarType
334
{
335
    return new ScalarType(
336
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...alarType::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

336
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
337
        $config['description'] ?? null,
338
        $config['serialize'] ?? null,
339
        $config['parseValue'] ?? null,
340
        $config['parseLiteral'] ?? null,
341
        $config['astNode'] ?? null
342
    );
343
}
344
345
/**
346
 * @param array $config
347
 * @return EnumType
348
 * @throws InvariantException
349
 */
350
function newEnumType(array $config = []): EnumType
351
{
352
    return new EnumType(
353
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...EnumType::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

353
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
354
        $config['description'] ?? null,
355
        $config['values'] ?? [],
356
        $config['astNode'] ?? null
357
    );
358
}
359
360
/**
361
 * @param array $config
362
 * @return EnumValue
363
 */
364
function newEnumValue(array $config = []): EnumValue
365
{
366
    return new EnumValue(
367
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...numValue::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

367
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
368
        $config['description'] ?? null,
369
        $config['deprecationReason'] ?? null,
370
        $config['astNode'] ?? null,
371
        $config['value'] ?? null
372
    );
373
}
374
375
/**
376
 * @param array $config
377
 * @return InputObjectType
378
 * @throws InvariantException
379
 */
380
function newInputObjectType(array $config = []): InputObjectType
381
{
382
    return new InputObjectType(
383
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...jectType::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

383
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
384
        $config['description'] ?? null,
385
        $config['fields'] ?? [],
386
        $config['astNode'] ?? null
387
    );
388
}
389
390
/**
391
 * @param array $config
392
 * @return InputField
393
 */
394
function newInputField(array $config = []): InputField
395
{
396
    return new InputField(
397
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...putField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

397
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
398
        $config['type'] ?? null,
399
        $config['defaultValue'] ?? null,
400
        $config['description'] ?? null,
401
        $config['astNode'] ?? null
402
    );
403
}
404
405
/**
406
 * @param array $config
407
 * @return InterfaceType
408
 * @throws InvariantException
409
 */
410
function newInterfaceType(array $config = []): InterfaceType
411
{
412
    return new InterfaceType(
413
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...faceType::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

413
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
414
        $config['description'] ?? null,
415
        $config['fields'] ?? [],
416
        $config['resolveType'] ?? null,
417
        $config['astNode'] ?? null,
418
        $config['extensionASTNodes'] ?? []
419
    );
420
}
421
422
/**
423
 * @param array $config
424
 * @return ObjectType
425
 * @throws InvariantException
426
 */
427
function newObjectType(array $config = []): ObjectType
428
{
429
    return new ObjectType(
430
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...jectType::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

430
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
431
        $config['description'] ?? null,
432
        $config['fields'] ?? [],
433
        $config['interfaces'] ?? [],
434
        $config['isTypeOf'] ?? null,
435
        $config['astNode'] ?? null,
436
        $config['extensionASTNodes'] ?? []
437
    );
438
}
439
440
/**
441
 * @param array $config
442
 * @return Field
443
 * @throws InvariantException
444
 */
445
function newField(array $config = []): Field
446
{
447
    return new Field(
448
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Definition\Field::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

448
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
449
        $config['description'] ?? null,
450
        $config['type'] ?? null,
451
        $config['args'] ?? [],
452
        $config['resolve'] ?? null,
453
        $config['subscribe'] ?? null,
454
        $config['deprecationReason'] ?? null,
455
        $config['astNode'] ?? null,
456
        $config['typeName'] ?? ''
457
    );
458
}
459
460
function newArgument(array $config = []): Argument
461
{
462
    return new Argument(
463
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...Argument::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

463
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
464
        $config['description'] ?? null,
465
        $config['type'] ?? null,
466
        $config['defaultValue'] ?? null,
467
        $config['astNode'] ?? null
468
    );
469
}
470
471
/**
472
 * @param array $config
473
 * @return UnionType
474
 * @throws InvariantException
475
 */
476
function newUnionType(array $config = []): UnionType
477
{
478
    return new UnionType(
479
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...nionType::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

479
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
480
        $config['description'] ?? null,
481
        $config['types'] ?? [],
482
        $config['resolveType'] ?? null,
483
        $config['astNode'] ?? null
484
    );
485
}
486
487
/**
488
 * @param array $config
489
 * @return Schema
490
 * @throws InvariantException
491
 */
492
function newSchema(array $config = []): Schema
493
{
494
    return new Schema(
495
        $config['query'] ?? null,
496
        $config['mutation'] ?? null,
497
        $config['subscription'] ?? null,
498
        $config['types'] ?? [],
499
        $config['directives'] ?? [],
500
        $config['assumeValue'] ?? false,
501
        $config['astNode'] ?? null
502
    );
503
}
504
505
/**
506
 * @param array $config
507
 * @return Directive
508
 * @throws InvariantException
509
 */
510
function newDirective(array $config = []): Directive
511
{
512
    return new Directive(
513
        $config['name'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $config['name'] ?? null can also be of type null; however, parameter $name of Digia\GraphQL\Type\Defin...irective::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

513
        /** @scrutinizer ignore-type */ $config['name'] ?? null,
Loading history...
514
        $config['description'] ?? null,
515
        $config['locations'] ?? [],
516
        $config['args'] ?? [],
517
        $config['astNode'] ?? null,
518
        $config['typeName'] ?? ''
519
    );
520
}
521
522
/**
523
 * @param TypeInterface $ofType
524
 * @return ListType
525
 */
526
function newList(TypeInterface $ofType): ListType
527
{
528
    return new ListType($ofType);
529
}
530
531
/**
532
 * @param TypeInterface $ofType
533
 * @return NonNullType
534
 * @throws InvalidTypeException
535
 */
536
function newNonNull(TypeInterface $ofType): NonNullType
537
{
538
    return new NonNullType($ofType);
539
}
540