ArrayCollection   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 259
ccs 76
cts 76
cp 1
rs 9.76
wmc 33

24 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 1
A remove() 0 11 2
A contains() 0 3 1
A __construct() 0 3 1
A empty() 0 3 1
A clear() 0 3 1
A getIterator() 0 3 1
A offsetExists() 0 3 1
A offsetGet() 0 4 1
A stream() 0 3 1
A mutableStream() 0 3 1
A values() 0 3 1
A offsetSet() 0 7 2
A addAll() 0 11 3
A get() 0 7 2
A unset() 0 9 2
A offsetUnset() 0 3 1
A hasKey() 0 3 1
A add() 0 5 1
A keys() 0 3 1
A replace() 0 11 3
A count() 0 3 1
A forEach() 0 4 2
A set() 0 3 1
1
<?php
2
3
namespace Bdf\Collection;
4
5
use ArrayIterator;
6
use Bdf\Collection\Stream\ArrayStream;
7
use Bdf\Collection\Stream\MutableArrayStream;
8
use Bdf\Collection\Stream\StreamInterface;
9
use function array_key_exists;
10
use function array_keys;
11
use function array_merge;
12
use function array_search;
13
use function array_values;
14
use function count;
15
use function in_array;
16
use function is_array;
17
use function iterator_to_array;
18
19
/**
20
 * Collection implementation using native PHP arrays
21
 *
22
 * @template T
23
 * @template K of array-key
24
 * @implements TableInterface<K, T>
25
 */
26
class ArrayCollection implements TableInterface
27
{
28
    /**
29
     * @var array<K, T>
30
     */
31
    private $data;
32
33
34
    /**
35
     * ArrayCollection constructor.
36
     *
37
     * @param array<K, T> $data Initial data
38
     */
39 52
    public function __construct(array $data = [])
40
    {
41 52
        $this->data = $data;
42 52
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 4
    public function contains($element, bool $strict = false): bool
48
    {
49 4
        return in_array($element, $this->data, $strict) !== false;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 2
    public function remove($element, bool $strict = false): bool
56
    {
57 2
        $key = array_search($element, $this->data, $strict);
58
59 2
        if ($key === false) {
60 2
            return false;
61
        }
62
63 2
        unset($this->data[$key]);
64
65 2
        return true;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 3
    public function clear(): void
72
    {
73 3
        $this->data = [];
74 3
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 4
    public function empty(): bool
80
    {
81 4
        return empty($this->data);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 21
    public function toArray(): array
88
    {
89 21
        return $this->data;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 8
    public function getIterator(): \Traversable
96
    {
97 8
        return new ArrayIterator($this->data);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 3
    public function offsetExists($offset): bool
104
    {
105 3
        return array_key_exists($offset, $this->data);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    #[\ReturnTypeWillChange]
112 4
    public function &offsetGet($offset)
113
    {
114 4
        return $this->data[$offset];
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     *
120
     * @param K|null $offset
0 ignored issues
show
Bug introduced by
The type Bdf\Collection\K was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
121
     * @param T $value
122
     *
123
     * @return void
124
     */
125 4
    public function offsetSet($offset, $value): void
126
    {
127 4
        if ($offset === null) {
128
            /** @psalm-suppress InvalidPropertyAssignmentValue */
129 1
            $this->data[] = $value;
130
        } else {
131 4
            $this->data[$offset] = $value;
132
        }
133 4
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 3
    public function offsetUnset($offset): void
139
    {
140 3
        unset($this->data[$offset]);
141 3
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 3
    public function count(): int
147
    {
148 3
        return count($this->data);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 3
    public function stream(): StreamInterface
155
    {
156 3
        return new ArrayStream($this->data);
157
    }
158
159
    /**
160
     * Get a mutable stream
161
     * All transformation methods will be applied directly, and modify the current stream state, instead of creating a new one
162
     *
163
     * /!\ Mutable stream have a slightly different behavior, and cannot be used in all situations
164
     *
165
     * @return StreamInterface
166
     *
167
     * @see MutableArrayStream
168
     */
169 1
    public function mutableStream(): StreamInterface
170
    {
171 1
        return new MutableArrayStream($this->data);
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 2
    public function set($key, $value): void
178
    {
179 2
        $this->data[$key] = $value;
180 2
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 3
    public function &get($key)
186
    {
187 3
        if (!isset($this->data[$key])) {
188 1
            throw new \OutOfBoundsException('Cannot found element at key '.$key);
189
        }
190
191 2
        return $this->data[$key];
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     *
197
     * @psalm-suppress InvalidPropertyAssignmentValue
198
     */
199 3
    public function add($element): bool
200
    {
201 3
        $this->data[] = $element;
202
203 3
        return true;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     *
209
     * @param iterable<mixed, T>|ArrayCollection<T, K> $elements
210
     */
211 4
    public function addAll(iterable $elements): bool
212
    {
213 4
        if ($elements instanceof ArrayCollection) {
214 1
            $elements = $elements->data;
215 3
        } elseif (!is_array($elements)) {
216 1
            $elements = iterator_to_array($elements, false);
217
        }
218
219 4
        $this->data = array_merge($this->data, $elements);
220
221 4
        return true;
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 4
    public function replace(iterable $elements): bool
228
    {
229 4
        if (is_array($elements)) {
230 2
            $this->data = $elements;
231 2
        } elseif ($elements instanceof ArrayCollection) {
232 1
            $this->data = $elements->data;
233
        } else {
234 1
            $this->data = iterator_to_array($elements);
235
        }
236
237 4
        return true;
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243 3
    public function hasKey($key): bool
244
    {
245 3
        return array_key_exists($key, $this->data);
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251 2
    public function unset($key): bool
252
    {
253 2
        if (isset($this->data[$key])) {
254 2
            unset($this->data[$key]);
255
256 2
            return true;
257
        }
258
259 2
        return false;
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265 2
    public function keys(): array
266
    {
267 2
        return array_keys($this->data);
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     */
273 3
    public function values(): array
274
    {
275 3
        return array_values($this->data);
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281 3
    public function forEach(callable $consumer): void
282
    {
283 3
        foreach ($this->data as $key => $value) {
284 3
            $consumer($value, $key);
285
        }
286 3
    }
287
}
288