Issues (22)

Storage/Storage.php (5 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2024 Mykhailo Shtanko [email protected]
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15
16
namespace FRZB\Component\TransactionalMessenger\Storage;
17
18
/**
19
 * @psalm-template TValue
20
 *
21
 * @psalm-extends StorageInterface<TValue>
22
 */
23
class Storage implements StorageInterface
24
{
25
    public function __construct(
26
        /** @param \Iterator<TValue> $items */
27
        private iterable $items = [],
28
    ) {}
29
30
    public function init(iterable|StorageInterface $storage): static
31
    {
32
        $this->items = $storage instanceof StorageInterface ? $storage->list() : $storage;
33
34
        return $this;
35
    }
36
37
    public function append(object ...$items): static
38
    {
39
        array_push($this->items, ...$items);
0 ignored issues
show
$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

39
        array_push(/** @scrutinizer ignore-type */ $this->items, ...$items);
Loading history...
40
41
        return $this;
42
    }
43
44
    public function prepend(object ...$items): static
45
    {
46
        array_unshift($this->items, ...$items);
0 ignored issues
show
$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

46
        array_unshift(/** @scrutinizer ignore-type */ $this->items, ...$items);
Loading history...
47
48
        return $this;
49
    }
50
51
    public function next(): ?object
52
    {
53
        return array_shift($this->items);
0 ignored issues
show
$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

53
        return array_shift(/** @scrutinizer ignore-type */ $this->items);
Loading history...
54
    }
55
56
    public function iterate(): iterable
57
    {
58
        while ($item = $this->next()) {
59
            yield $item;
60
        }
61
    }
62
63
    public function map(callable $callback): static
64
    {
65
        return new static(array_map($callback, $this->items));
0 ignored issues
show
$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

65
        return new static(array_map($callback, /** @scrutinizer ignore-type */ $this->items));
Loading history...
66
    }
67
68
    public function filter(callable $callback): static
69
    {
70
        return new static(array_filter($this->items, $callback));
0 ignored issues
show
$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

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