Passed
Push — master ( 6b4f8f...36c64c )
by Maxim
02:25 queued 11s
created

HashMap::keys()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Anton Lytkin <[email protected]>
4
 * @license MIT
5
 */
6
7
namespace WS\Utils\Collections;
8
9
use ArrayIterator;
10
use RuntimeException;
11
12
class HashMap implements Map
13
{
14
    private $entries = [];
15
16 35
    public function put($key, $value): bool
17
    {
18 35
        $this->entries[$this->getKeyHash($key)] = new MapEntry($key, $value);
19
20 34
        return true;
21
    }
22
23 1
    public function getIterator()
24
    {
25
        return new ArrayIterator(array_map(static function (MapEntry $entry) {
26 1
            return $entry->getValue();
27 1
        }, $this->entries));
28
    }
29
30 1
    public function values(): Collection
31
    {
32 1
        $values = [];
33
        /** @var MapEntry $entry */
34 1
        foreach ($this->entries as $entry) {
35 1
            $values[] = $entry->getValue();
36
        }
37 1
        return new ArrayList($values);
38
    }
39
40 9
    public function keys(): Collection
41
    {
42 9
        $keys = [];
43
        /** @var MapEntry $entry */
44 9
        foreach ($this->entries as $entry) {
45 8
            $keys[] = $entry->getKey();
46
        }
47 9
        return new ArrayList($keys);
48
    }
49
50 36
    private function getKeyHash($key): string
51
    {
52 36
        if ($key === null) {
53 1
            return '__NULL__';
54
        }
55 36
        if (is_scalar($key)) {
56 25
            return $key.'';
57
        }
58 12
        if (is_array($key)) {
59 1
            return md5(json_encode($key));
60
        }
61 11
        if ($key instanceof HashCodeAware) {
62 6
            return $key->getHashCode();
63
        }
64 5
        if (is_object($key)) {
65 4
            return spl_object_hash($key);
66
        }
67 1
        throw new RuntimeException("The type of $key is not supported");
68
    }
69
70 10
    public function remove($key): bool
71
    {
72 10
        $res = $this->containsKey($key);
73
74 10
        if (!$res) {
75 5
            return false;
76
        }
77 7
        unset($this->entries[$this->getKeyHash($key)]);
78 7
        return true;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 22
    public function containsKey($key): bool
85
    {
86 22
        return isset($this->entries[$this->getKeyHash($key)]);
87
    }
88
89 15
    public function size(): int
90
    {
91 15
        return count($this->entries);
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97 7
    public function get($key)
98
    {
99 7
        if (!$this->containsKey($key)) {
100 6
            return null;
101
        }
102
103 7
        $entry = $this->entries[$this->getKeyHash($key)];
104 7
        return $entry->getValue();
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110 1
    public function containsValue($tested): bool
111
    {
112 1
        foreach ($this->entries as $entry) {
113 1
            if ($entry->getValue() === $tested) {
114 1
                return true;
115
            }
116
        }
117 1
        return false;
118
    }
119
}
120