Completed
Pull Request — master (#69)
by Christoffer
02:08
created

Map::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Digia\GraphQL\Validation\Conflict;
4
5
class Map
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $keys = [];
11
12
    /**
13
     * @var array
14
     */
15
    protected $values = [];
16
17
    /**
18
     * Map constructor.
19
     * @param array $values
20
     */
21
    public function __construct(array $values = [])
22
    {
23
        foreach ($values as $key => $value) {
24
            $this->keys[]   = $key;
25
            $this->values[] = $value;
26
        }
27
    }
28
29
30
    /**
31
     * @param $key
32
     * @param $value
33
     */
34
    public function set($key, $value): void
35
    {
36
        $this->keys[]   = $key;
37
        $this->values[] = $value;
38
    }
39
40
    /**
41
     * @param $key
42
     * @return mixed|null
43
     */
44
    public function get($key)
45
    {
46
        $index = array_search($key, $this->keys, true);
47
48
        if ($index === false) {
49
            return null;
50
        }
51
52
        return $this->values[$index] ?? null;
53
    }
54
55
    /**
56
     * @param $key
57
     * @return bool
58
     */
59
    public function has($key): bool
60
    {
61
        return null !== $this->get($key);
62
    }
63
64
    /**
65
     * @param $key
66
     */
67
    public function delete($key): void
68
    {
69
        $index = array_search($key, $this->keys);
70
71
        array_splice($this->keys, $index, 1);
0 ignored issues
show
Bug introduced by
It seems like $index can also be of type string and false; however, parameter $offset of array_splice() does only seem to accept integer, 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

71
        array_splice($this->keys, /** @scrutinizer ignore-type */ $index, 1);
Loading history...
72
        array_splice($this->values, $index, 1);
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function keys(): array
79
    {
80
        return $this->keys;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function values(): array
87
    {
88
        return $this->values;
89
    }
90
91
    /**
92
     *
93
     */
94
    public function clear(): void
95
    {
96
        $this->keys   = [];
97
        $this->values = [];
98
    }
99
}
100