Passed
Push — master ( 8e672b...9eb68a )
by Thomas
02:40
created

CborMap::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace MadWizard\WebAuthn\Format;
4
5
use ArrayAccess;
6
use ArrayObject;
7
use JsonSerializable;
8
use MadWizard\WebAuthn\Exception\CborException;
9
10
final class CborMap implements ArrayAccess, JsonSerializable
11
{
12
    /**
13
     * @var array[]
14
     * @phpstan-var array<string, array{0:mixed, 1:mixed}>
15
     */
16
    private $entries = [];
17
18 75
    public function __construct()
19
    {
20 75
    }
21
22 70
    private function getInternalKey($key): string
23
    {
24 70
        $keyType = gettype($key);
25 70
        if ($keyType !== 'string' && $keyType !== 'integer') {
26 1
            throw new CborException('Only string and integer values may be used as keys.');
27
        }
28 69
        return sprintf('%s:%s', $keyType, (string) $key);
29
    }
30
31 66
    public function set($key, $value): void
32
    {
33 66
        $this->entries[$this->getInternalKey($key)] = [$key, $value];
34 65
    }
35
36 61
    public function has($key): bool
37
    {
38 61
        $internalKey = $this->getInternalKey($key);
39 61
        return array_key_exists($internalKey, $this->entries);
40
    }
41
42 58
    public function get($key)
43
    {
44 58
        $internalKey = $this->getInternalKey($key);
45 58
        if (!array_key_exists($internalKey, $this->entries)) {
46 1
            throw new CborException("Key $internalKey is not present in CBOR map.");
47
        }
48 57
        return $this->entries[$internalKey][1];
49
    }
50
51 55
    public function remove($key)
52
    {
53 55
        $internalKey = $this->getInternalKey($key);
54 55
        if (!isset($this->entries[$internalKey])) {
55 1
            throw new CborException("Key $internalKey is not present in CBOR map.");
56
        }
57 54
        unset($this->entries[$internalKey]);
58 54
    }
59
60 52
    public function count(): int
61
    {
62 52
        return count($this->entries);
63
    }
64
65
    /**
66
     * @phpstan-return array<array{0:mixed, 1:mixed}>>
67
     */
68 2
    public function getEntries(): array
69
    {
70 2
        return array_values($this->entries);
71
    }
72
73
    public function getKeys(): array
74
    {
75
        return array_map(function ($item) { return $item[0]; }, array_values($this->entries));
76
    }
77
78 58
    public function copy(): self
79
    {
80 58
        return clone $this;
81
    }
82
83 14
    public static function fromArray(array $array): self
84
    {
85 14
        $map = new CborMap();
86 14
        foreach ($array as $k => $v) {
87 14
            $map->set($k, $v);
88
        }
89 14
        return $map;
90
    }
91
92 11
    public function offsetExists($offset)
93
    {
94 11
        return $this->has($offset);
95
    }
96
97 39
    public function offsetGet($offset)
98
    {
99 39
        return $this->get($offset);
100
    }
101
102 1
    public function offsetSet($offset, $value)
103
    {
104 1
        return $this->set($offset, $value);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set($offset, $value) targeting MadWizard\WebAuthn\Format\CborMap::set() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
105
    }
106
107 1
    public function offsetUnset($offset)
108
    {
109 1
        $this->remove($offset);
110 1
    }
111
112
    public function jsonSerialize()
113
    {
114
        $obj = new ArrayObject();
115
        foreach ($this->entries as [$k, $v]) {
116
            $obj[$k] = $v;
117
        }
118
        return $obj;
119
    }
120
}
121