Test Failed
Push — 119 ( 857274...7d10e7 )
by Max
03:06
created

SafeArrayIterator::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Src;
4
5
use ArrayAccess;
6
use Countable;
7
8
/**
9
 * Safe array iterator. Copies array when cloned.
10
 * @template TKey
11
 * @template TValue
12
 * @implements ArrayAccess<TKey, TValue>
13
 * 
14
 * @since 0.3
15
 */
16
final class SafeArrayIterator implements ArrayAccess, Countable
17
{
18
19
    /**
20
     * Ctor.
21
     * 
22
     * @phpstan-param array<TKey, TValue> $stored
23
     * @param array<mixed, mixed> $stored array for stored values.
24
     */
25
    public function __construct(
26
        /**
27
         * Array for stored values.
28
         *
29
         * @phpstan-var array<TKey, TValue>
30
         * @var array
31
         */
32
        private array $stored = []
33
    ) {
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     * @phpstan-param TKey   $offset
39
     * @phpstan-param TValue $value
40
     */
41
    public function offsetSet(mixed $offset, mixed $value): void
42
    {
43
        $this->stored[$offset] = $value;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     * @phpstan-param TKey $offset
49
     */
50
    public function offsetGet(mixed $offset): mixed
51
    {
52
        return $this->stored[$offset];
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     * @phpstan-param TKey $offset
58
     */
59
    public function offsetExists(mixed $offset): bool
60
    {
61
        return isset($this->stored[$offset]);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     * @phpstan-param TKey $offset
67
     */
68
    public function offsetUnset(mixed $offset): void
69
    {
70
        unset($this->stored);
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function count(): int
77
    {
78
        return count($this->stored);
79
    }
80
}
81