Passed
Push — main ( 9c4f1d...6af5e8 )
by Fractal
01:41
created

Storage::iterate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\TransactionalMessenger\Storage;
6
7
/**
8
 * @template T
9
 *
10
 * @extends StorageInterface<T>
11
 */
12
class Storage implements StorageInterface
13
{
14
    public function __construct(
15
        /** @param \Iterator<T> $items */
16
        private iterable $items = [],
17
    ) {
18
    }
19
20
    /** {@inheritdoc} */
21
    public function init(iterable|StorageInterface $storage): static
22
    {
23
        $this->items = $storage instanceof StorageInterface ? $storage->list() : $storage;
24
25
        return $this;
26
    }
27
28
    /** {@inheritdoc} */
29
    public function append(object ...$items): static
30
    {
31
        array_push($this->items, ...$items);
0 ignored issues
show
Bug introduced by
$this->items of type iterable is incompatible with the type array expected by parameter $array of array_push(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        array_push(/** @scrutinizer ignore-type */ $this->items, ...$items);
Loading history...
32
33
        return $this;
34
    }
35
36
    /** {@inheritdoc} */
37
    public function prepend(object ...$items): static
38
    {
39
        array_unshift($this->items, ...$items);
0 ignored issues
show
Bug introduced by
$this->items of type iterable is incompatible with the type array expected by parameter $array of array_unshift(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        array_unshift(/** @scrutinizer ignore-type */ $this->items, ...$items);
Loading history...
40
41
        return $this;
42
    }
43
44
    /** {@inheritdoc} */
45
    public function next(): ?object
46
    {
47
        return array_shift($this->items);
0 ignored issues
show
Bug introduced by
$this->items of type iterable is incompatible with the type array expected by parameter $array of array_shift(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        return array_shift(/** @scrutinizer ignore-type */ $this->items);
Loading history...
48
    }
49
50
    /** {@inheritdoc} */
51
    public function iterate(): iterable
52
    {
53
        while ($item = $this->next()) {
54
            yield $item;
55
        }
56
    }
57
58
    /** {@inheritdoc} */
59
    public function map(callable $callback): static
60
    {
61
        return new static(array_map($callback, $this->items));
0 ignored issues
show
Bug introduced by
$this->items of type iterable is incompatible with the type array expected by parameter $array of array_map(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        return new static(array_map($callback, /** @scrutinizer ignore-type */ $this->items));
Loading history...
62
    }
63
64
    /** {@inheritdoc} */
65
    public function filter(callable $callback): static
66
    {
67
        return new static(array_filter($this->items, $callback));
0 ignored issues
show
Bug introduced by
$this->items of type iterable is incompatible with the type array expected by parameter $array of array_filter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        return new static(array_filter(/** @scrutinizer ignore-type */ $this->items, $callback));
Loading history...
68
    }
69
70
    /** {@inheritdoc} */
71
    public function merge(iterable|StorageInterface $storage): static
72
    {
73
        $this->items = [...$this->items, ...$storage instanceof StorageInterface ? $storage->list() : $storage];
74
75
        return $this;
76
    }
77
78
    /** {@inheritdoc} */
79
    public function clear(): static
80
    {
81
        $this->items = [];
82
83
        return $this;
84
    }
85
86
    /** {@inheritdoc} */
87
    public function list(): iterable
88
    {
89
        return $this->items;
90
    }
91
92
    /** {@inheritdoc} */
93
    public function count(): int
94
    {
95
        return \count($this->items);
96
    }
97
}
98