Passed
Pull Request — master (#69)
by Christoffer
02:29
created

PairSet::has()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 12
nop 3
1
<?php
2
3
namespace Digia\GraphQL\Validation\Conflict;
4
5
/**
6
 * A way to keep track of pairs of things when the ordering of the pair does
7
 * not matter. We do this by maintaining a sort of double adjacency sets.
8
 */
9
class PairSet
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $data = [];
15
16
    /**
17
     * @param string $a
18
     * @param string $b
19
     * @param bool   $areMutuallyExclusive
20
     * @return bool
21
     */
22
    public function has(string $a, string $b, bool $areMutuallyExclusive): bool
23
    {
24
        $first  = $this->data[$a] ?? null;
25
        $result = (null !== $first && isset($first[$b])) ? $first[$b] : null;
26
27
        if (null === $result) {
28
            return false;
29
        }
30
31
        // areMutuallyExclusive being false is a superset of being true,
32
        // hence if we want to know if this PairSet "has" these two with no
33
        // exclusivity, we have to ensure it was added as such.
34
        if ($areMutuallyExclusive === false) {
35
            return $result === false;
36
        }
37
38
        return true;
39
    }
40
41
    /**
42
     * @param string $a
43
     * @param string $b
44
     * @param bool   $areMutuallyExclusive
45
     */
46
    public function add(string $a, string $b, bool $areMutuallyExclusive): void
47
    {
48
        $this->addToData($a, $b, $areMutuallyExclusive);
49
        $this->addToData($b, $a, $areMutuallyExclusive);
50
    }
51
52
    /**
53
     * @param string $a
54
     * @param string $b
55
     * @param bool   $areMutuallyExclusive
56
     */
57
    protected function addToData(string $a, string $b, bool $areMutuallyExclusive): void
58
    {
59
        $map = $this->data[$a] ?? [];
60
61
        $map[$b] = $areMutuallyExclusive;
62
63
        $this->data[$a] = $map;
64
    }
65
}
66