Completed
Push — master ( cd32d2...8d0057 )
by Vincent
04:21 queued 38s
created

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