Issues (35)

tests/example/src/MyCollection.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Test;
6
7
use ArrayIterator;
8
use Closure;
9
use Doctrine\Common\Collections\Collection;
0 ignored issues
show
The type Doctrine\Common\Collections\Collection 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...
10
use Traversable;
11
use const ARRAY_FILTER_USE_BOTH;
12
use function array_filter;
13
use function array_key_exists;
14
use function array_keys;
15
use function array_map;
16
use function array_search;
17
use function array_slice;
18
use function array_values;
19
use function count;
20
use function current;
21
use function end;
22
use function in_array;
23
use function key;
24
use function next;
25
use function reset;
26
use function spl_object_hash;
27
28
final class MyCollection implements Collection
29
{
30
    /**
31
     * An array containing the entries of this collection.
32
     *
33
     * @psalm-var array<TKey,T>
34
     * @var array<mixed, mixed>
35
     */
36
    private $elements;
37
38
    /**
39
     * Initializes a new ArrayCollection.
40
     *
41
     * @param array<mixed, mixed> $elements
42
     *
43
     * @psalm-param array<TKey,T> $elements
44
     */
45
    public function __construct(array $elements = [])
46
    {
47
        $this->elements = $elements;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function toArray() : array
54
    {
55
        return $this->elements;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    public function first()
62
    {
63
        return reset($this->elements);
64
    }
65
66
    /**
67
     * Creates a new instance from the specified elements.
68
     *
69
     * This method is provided for derived classes to specify how a new
70
     * instance should be created when constructor semantics have changed.
71
     *
72
     * @param array<mixed, mixed> $elements Elements.
73
     *
74
     * @return static
75
     *
76
     * @psalm-param array<TKey,T> $elements
77
     * @psalm-return ArrayCollection<TKey,T>
78
     */
79
    protected function createFrom(array $elements)
80
    {
81
        return new static($elements);
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function last()
88
    {
89
        return end($this->elements);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function key()
96
    {
97
        return key($this->elements);
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    public function next()
104
    {
105
        return next($this->elements);
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function current()
112
    {
113
        return current($this->elements);
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function remove($key)
120
    {
121
        if (! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
122
            return null;
123
        }
124
125
        $removed = $this->elements[$key];
126
        unset($this->elements[$key]);
127
128
        return $removed;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function removeElement($element) : bool
135
    {
136
        $key = array_search($element, $this->elements, true);
137
138
        if ($key === false) {
139
            return false;
140
        }
141
142
        unset($this->elements[$key]);
143
144
        return true;
145
    }
146
147
    /**
148
     * Required by interface ArrayAccess.
149
     *
150
     * {@inheritDoc}
151
     *
152
     * @param int|string $offset
153
     */
154
    public function offsetExists($offset) : bool
155
    {
156
        return $this->containsKey($offset);
157
    }
158
159
    /**
160
     * Required by interface ArrayAccess.
161
     *
162
     * @param int|string $offset
163
     *
164
     * @return mixed
165
     */
166
    public function offsetGet($offset)
167
    {
168
        return $this->get($offset);
169
    }
170
171
    /**
172
     * Required by interface ArrayAccess.
173
     *
174
     * @param int|string|null $offset
175
     * @param mixed           $value
176
     */
177
    public function offsetSet($offset, $value) : void
178
    {
179
        if ($offset === null) {
180
            $this->add($value);
181
182
            return;
183
        }
184
185
        $this->set($offset, $value);
186
    }
187
188
    /**
189
     * Required by interface ArrayAccess.
190
     *
191
     * @param int|string $offset
192
     */
193
    public function offsetUnset($offset) : void
194
    {
195
        $this->remove($offset);
196
    }
197
198
    /**
199
     * {@inheritDoc}
200
     */
201
    public function containsKey($key) : bool
202
    {
203
        return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
204
    }
205
206
    /**
207
     * {@inheritDoc}
208
     */
209
    public function contains($element) : bool
210
    {
211
        return in_array($element, $this->elements, true);
212
    }
213
214
    /**
215
     * {@inheritDoc}
216
     */
217
    public function exists(Closure $p) : bool
218
    {
219
        foreach ($this->elements as $key => $element) {
220
            if ($p($key, $element)) {
221
                return true;
222
            }
223
        }
224
225
        return false;
226
    }
227
228
    /**
229
     * {@inheritDoc}
230
     */
231
    public function indexOf($element)
232
    {
233
        return array_search($element, $this->elements, true);
234
    }
235
236
    /**
237
     * {@inheritDoc}
238
     */
239
    public function get($key)
240
    {
241
        return $this->elements[$key] ?? null;
242
    }
243
244
    /**
245
     * {@inheritDoc}
246
     */
247
    public function getKeys() : array
248
    {
249
        return array_keys($this->elements);
250
    }
251
252
    /**
253
     * {@inheritDoc}
254
     */
255
    public function getValues() : array
256
    {
257
        return array_values($this->elements);
258
    }
259
260
    /**
261
     * {@inheritDoc}
262
     */
263
    public function count() : int
264
    {
265
        return count($this->elements);
266
    }
267
268
    /**
269
     * {@inheritDoc}
270
     */
271
    public function set($key, $value) : void
272
    {
273
        $this->elements[$key] = $value;
274
    }
275
276
    /**
277
     * {@inheritDoc}
278
     */
279
    public function add($element) : bool
280
    {
281
        $this->elements[] = $element;
282
283
        return true;
284
    }
285
286
    /**
287
     * {@inheritDoc}
288
     */
289
    public function isEmpty() : bool
290
    {
291
        return empty($this->elements);
292
    }
293
294
    /**
295
     * Required by interface IteratorAggregate.
296
     *
297
     * {@inheritDoc}
298
     *
299
     * @psalm-return Traversable<TKey, T>
300
     */
301
    public function getIterator() : Traversable
302
    {
303
        return new ArrayIterator($this->elements);
304
    }
305
306
    /**
307
     * {@inheritDoc}
308
     *
309
     * @return static
310
     */
311
    public function map(Closure $func) : Collection
312
    {
313
        return $this->createFrom(array_map($func, $this->elements));
314
    }
315
316
    /**
317
     * {@inheritDoc}
318
     *
319
     * @return static
320
     *
321
     * @psalm-return ArrayCollection<TKey,T>
322
     */
323
    public function filter(Closure $p) : Collection
324
    {
325
        return $this->createFrom(array_filter($this->elements, $p, ARRAY_FILTER_USE_BOTH));
326
    }
327
328
    /**
329
     * {@inheritDoc}
330
     */
331
    public function forAll(Closure $p) : bool
332
    {
333
        foreach ($this->elements as $key => $element) {
334
            if (! $p($key, $element)) {
335
                return false;
336
            }
337
        }
338
339
        return true;
340
    }
341
342
    /**
343
     * {@inheritDoc}
344
     */
345
    public function partition(Closure $p) : array
346
    {
347
        $matches = $noMatches = [];
348
349
        foreach ($this->elements as $key => $element) {
350
            if ($p($key, $element)) {
351
                $matches[$key] = $element;
352
            } else {
353
                $noMatches[$key] = $element;
354
            }
355
        }
356
357
        return [$this->createFrom($matches), $this->createFrom($noMatches)];
358
    }
359
360
    /**
361
     * Returns a string representation of this object.
362
     */
363
    public function __toString() : string
364
    {
365
        return self::class . '@' . spl_object_hash($this);
366
    }
367
368
    /**
369
     * {@inheritDoc}
370
     */
371
    public function clear() : void
372
    {
373
        $this->elements = [];
374
    }
375
376
    /**
377
     * {@inheritDoc}
378
     */
379
    public function slice(int $offset, ?int $length = null) : array
380
    {
381
        return array_slice($this->elements, $offset, $length, true);
382
    }
383
}
384