1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Innmind\Immutable; |
||
5 | |||
6 | /** |
||
7 | * Simple iterator to cache the results of a generator so it can be iterated |
||
8 | * over multiple times |
||
9 | * |
||
10 | * @template T |
||
11 | * @template S |
||
12 | * @internal Do not use this in your code |
||
13 | * @psalm-immutable Not really immutable but to simplify declaring immutability of other structures |
||
14 | */ |
||
15 | final class Accumulate implements \Iterator |
||
16 | { |
||
17 | /** @var \Generator<T, S> */ |
||
18 | private \Generator $generator; |
||
19 | /** @var list<T> */ |
||
0 ignored issues
–
show
|
|||
20 | private array $keys = []; |
||
21 | /** @var list<S> */ |
||
22 | private array $values = []; |
||
23 | |||
24 | /** |
||
25 | * @param \Generator<T, S> $generator |
||
26 | 72 | */ |
|
27 | public function __construct(\Generator $generator) |
||
28 | 72 | { |
|
29 | 72 | $this->generator = $generator; |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * @return S |
||
34 | 60 | */ |
|
35 | public function current(): mixed |
||
36 | 60 | { |
|
37 | /** @psalm-suppress UnusedMethodCall */ |
||
38 | 60 | $this->pop(); |
|
39 | |||
40 | return \current($this->values); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | 41 | * @return T |
|
0 ignored issues
–
show
The type
Innmind\Immutable\T 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
45 | */ |
||
46 | 41 | public function key(): mixed |
|
47 | { |
||
48 | 41 | /** @psalm-suppress UnusedMethodCall */ |
|
49 | $this->pop(); |
||
50 | |||
51 | 56 | return \current($this->keys); |
|
52 | } |
||
53 | 56 | ||
54 | 56 | public function next(): void |
|
55 | { |
||
56 | 56 | \next($this->keys); |
|
57 | 56 | \next($this->values); |
|
58 | |||
59 | 56 | if ($this->reachedCacheEnd()) { |
|
60 | /** @psalm-suppress ImpureMethodCall */ |
||
61 | 67 | $this->generator->next(); |
|
62 | } |
||
63 | 67 | } |
|
64 | 67 | ||
65 | 67 | public function rewind(): void |
|
66 | { |
||
67 | 67 | \reset($this->keys); |
|
68 | \reset($this->values); |
||
69 | 67 | } |
|
70 | |||
71 | public function valid(): bool |
||
72 | 67 | { |
|
73 | /** @psalm-suppress ImpureMethodCall */ |
||
74 | 67 | $valid = !$this->reachedCacheEnd() || $this->generator->valid(); |
|
75 | |||
76 | if (!$valid) { |
||
77 | 60 | // once the "true" end has been reached we automatically rewind this |
|
78 | // iterator so it is always in a clean state |
||
79 | 60 | /** @psalm-suppress UnusedMethodCall */ |
|
80 | 60 | $this->rewind(); |
|
81 | 60 | } |
|
82 | |||
83 | 60 | return $valid; |
|
84 | } |
||
85 | |||
86 | private function reachedCacheEnd(): bool |
||
87 | { |
||
88 | return \key($this->values) === null; |
||
89 | } |
||
90 | |||
91 | private function pop(): void |
||
92 | { |
||
93 | if ($this->reachedCacheEnd()) { |
||
94 | /** |
||
95 | * @psalm-suppress InaccessibleProperty |
||
96 | * @psalm-suppress ImpureMethodCall |
||
97 | */ |
||
98 | $this->keys[] = $this->generator->key(); |
||
99 | /** |
||
100 | * @psalm-suppress InaccessibleProperty |
||
101 | * @psalm-suppress ImpureMethodCall |
||
102 | */ |
||
103 | $this->values[] = $this->generator->current(); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths