Test Failed
Push — 119 ( 7d10e7...2da52b )
by Max
02:37
created

SafeArrayIterator::rewind()   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
use Iterator;
8
9
/**
10
 * Safe array iterator. Copies array when cloned.
11
 * @template TKey
12
 * @template TValue
13
 * @implements ArrayAccess<TKey, TValue>
14
 * @implements Iterator<TKey, TValue>
15
 * 
16
 * @since 0.3
17
 */
18
final class SafeArrayIterator implements ArrayAccess, Countable, Iterator
19
{
20
21
    /**
22
     * Ctor.
23
     * 
24
     * @phpstan-param array<TKey, TValue> $stored
25
     * @param array<mixed, mixed> $stored array for stored values.
26
     */
27
    public function __construct(
28
        /**
29
         * Array for stored values.
30
         *
31
         * @phpstan-var array<TKey, TValue>
32
         * @var array
33
         */
34
        private array $stored = []
35
    ) {
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     * @phpstan-param TKey   $offset
41
     * @phpstan-param TValue $value
42
     */
43
    public function offsetSet(mixed $offset, mixed $value): void
44
    {
45
        $this->stored[$offset] = $value;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     * @phpstan-param TKey $offset
51
     */
52
    public function offsetGet(mixed $offset): mixed
53
    {
54
        return $this->stored[$offset];
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     * @phpstan-param TKey $offset
60
     */
61
    public function offsetExists(mixed $offset): bool
62
    {
63
        return isset($this->stored[$offset]);
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     * @phpstan-param TKey $offset
69
     */
70
    public function offsetUnset(mixed $offset): void
71
    {
72
        unset($this->stored);
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function count(): int
79
    {
80
        return count($this->stored);
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     * @phpstan-return TValue
86
     */
87
    public function current(): mixed
88
    {
89
        return current($this->stored);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     * @phpstan-return TKey
95
     */
96
    public function key(): mixed
97
    {
98
        return key($this->stored);
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function next(): void
105
    {
106
        next($this->stored);
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    public function valid(): bool
113
    {
114
        return key($this->stored) !== null;
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120
    public function rewind(): void
121
    {
122
        reset($this->stored);
123
    }
124
}
125