Completed
Pull Request — master (#136)
by Alex
22:08 queued 15s
created

BidirectionalMap::putAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace AlgoWeb\ODataMetadata;
4
5
class BidirectionalMap
6
{
7
    private $keyToValue = [];
8
    private $valueToKey = [];
9
10
    public function reset()
11
    {
12
        $this->keyToValue = [];
13
        $this->valueToKey = [];
14
    }
15
16
    public function hasKey($key)
17
    {
18
        return isset($this->keyToValue[$key]);
19
    }
20
21
    public function hasValue($value)
22
    {
23
        return isset($this->valueToKey[$value]);
24
    }
25
26
    public function getKey($value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
27
    {
28
        if ($this->hasValue($value)) {
29
            return $this->valueToKey[$value];
30
        }
31
        return null;
32
    }
33
34
    public function getValue($key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
35
    {
36
        if ($this->hasKey($key)) {
37
            return $this->keyToValue[$key];
38
        }
39
        return null;
40
    }
41
42
    public function getAllKeys()
43
    {
44
        if (0 !== count($this->keyToValue)) {
45
            return array_keys($this->keyToValue);
46
        }
47
        return $this->keyToValue;
48
    }
49
50
    public function getAllValues()
51
    {
52
        if (0 !== count($this->valueToKey)) {
53
            return array_keys($this->valueToKey);
54
        }
55
        return $this->valueToKey;
56
    }
57
58
    public function putAll($array)
59
    {
60
        foreach ($array as $key => $value) {
61
            $this->put($key, $value);
62
        }
63
    }
64
65
    public function put($key, $value)
66
    {
67
        if ($this->hasKey($key)) {
68
            $this->removeKey($key);
69
        }
70
        if ($this->hasValue($value)) {
71
            $this->removeValue($value);
72
        }
73
        $this->keyToValue[$key] = $value;
74
        $this->valueToKey[$value] = $key;
75
    }
76
77
    public function removeKey($key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
78
    {
79
        if (!$this->hasKey($key)) {
80
            return null;
81
        }
82
        unset($this->valueToKey[$this->keyToValue[$key]]);
83
        $v = $this->keyToValue[$key];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $v. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
84
        unset($this->keyToValue[$key]);
85
        return $v;
86
    }
87
88
    public function removeValue($value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
89
    {
90
        if (!$this->hasValue($value)) {
91
            return null;
92
        }
93
        unset($this->keyToValue[$this->valueToKey[$value]]);
94
        $k = $this->valueToKey[$value];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
95
        unset($this->valueToKey[$k]);
96
        return $k;
97
    }
98
}
99