Completed
Push — master ( 0e0bd4...d314c1 )
by Christoffer
03:35 queued 01:20
created

compareTypes()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 8
nop 2
dl 0
loc 22
rs 6.412
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Validation;
4
5
use Digia\GraphQL\Language\Node\ArgumentNode;
6
use Digia\GraphQL\Type\Definition\LeafTypeInterface;
7
use Digia\GraphQL\Type\Definition\ListType;
8
use Digia\GraphQL\Type\Definition\NonNullType;
9
use Digia\GraphQL\Type\Definition\TypeInterface;
10
use function Digia\GraphQL\printNode;
11
use function Digia\GraphQL\Util\arrayEvery;
12
use function Digia\GraphQL\Util\find;
13
14
function compareArguments($argumentsA, $argumentsB): bool
15
{
16
    if (\count($argumentsA) !== \count($argumentsB)) {
17
        return false;
18
    }
19
20
    return arrayEvery($argumentsA, function (ArgumentNode $argumentA) use ($argumentsB) {
21
        $argumentB = find($argumentsB, function (ArgumentNode $argument) use ($argumentA) {
22
            return $argument->getNameValue() === $argumentA->getNameValue();
23
        });
24
25
        if (null === $argumentB) {
26
            return false;
27
        }
28
29
        return compareValues($argumentA->getValue(), $argumentB->getValue());
30
    });
31
}
32
33
/**
34
 * @param $valueA
35
 * @param $valueB
36
 * @return bool
37
 */
38
function compareValues($valueA, $valueB): bool
39
{
40
    return printNode($valueA) === printNode($valueB);
41
}
42
43
/**
44
 * Two types conflict if both types could not apply to a value simultaneously.
45
 * Composite types are ignored as their individual field types will be compared
46
 * later recursively. However List and Non-Null types must match.
47
 *
48
 * @param TypeInterface $typeA
49
 * @param TypeInterface $typeB
50
 * @return bool
51
 */
52
function compareTypes(TypeInterface $typeA, TypeInterface $typeB): bool
53
{
54
    if ($typeA instanceof ListType) {
55
        return $typeB instanceof ListType
56
            ? compareTypes($typeA->getOfType(), $typeB->getOfType())
57
            : true;
58
    }
59
    if ($typeB instanceof ListType) {
60
        return true;
61
    }
62
    if ($typeA instanceof NonNullType) {
63
        return $typeB instanceof NonNullType
64
            ? compareTypes($typeA->getOfType(), $typeB->getOfType())
65
            : true;
66
    }
67
    if ($typeB instanceof NonNullType) {
68
        return true;
69
    }
70
    if ($typeA instanceof LeafTypeInterface || $typeB instanceof LeafTypeInterface) {
71
        return $typeA !== $typeB;
72
    }
73
    return false;
74
}
75