Completed
Pull Request — master (#80)
by Christoffer
03:04 queued 54s
created

doTypesOverlap()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 3
1
<?php
2
3
namespace Digia\GraphQL\Util;
4
5
use Digia\GraphQL\Type\Definition\AbstractTypeInterface;
6
use Digia\GraphQL\Type\Definition\CompositeTypeInterface;
7
use Digia\GraphQL\Type\Definition\TypeInterface;
8
use Digia\GraphQL\Type\SchemaInterface;
9
10
/**
11
 * Provided two composite types, determine if they "overlap". Two composite
12
 * types overlap when the Sets of possible concrete types for each intersect.
13
 *
14
 * This is often used to determine if a fragment of a given type could possibly
15
 * be visited in a context of another type.
16
 *
17
 * @param SchemaInterface        $schema
18
 * @param TypeInterface $typeA
19
 * @param TypeInterface $typeB
20
 * @return bool
21
 */
22
function doTypesOverlap(SchemaInterface $schema, TypeInterface $typeA, TypeInterface $typeB): bool
23
{
24
    // Equivalent types overlap
25
    if ($typeA === $typeB) {
26
        return true;
27
    }
28
29
    if ($typeA instanceof AbstractTypeInterface) {
30
        if ($typeB instanceof AbstractTypeInterface) {
31
            // If both types are abstract, then determine if there is any intersection
32
            // between possible concrete types of each.
33
            return arraySome($schema->getPossibleTypes($typeA), function (TypeInterface $type) use ($schema, $typeB) {
34
                return $schema->isPossibleType($typeB, $type);
35
            });
36
        }
37
38
        // Determine if the latter type is a possible concrete type of the former.
39
        /** @noinspection PhpParamsInspection */
40
        return $schema->isPossibleType($typeA, $typeB);
41
    }
42
43
    if ($typeB instanceof AbstractTypeInterface) {
44
        // Determine if the former type is a possible concrete type of the latter.
45
        /** @noinspection PhpParamsInspection */
46
        return $schema->isPossibleType($typeB, $typeA);
47
    }
48
49
    // Otherwise the types do not overlap.
50
    return false;
51
}
52