SavingIterator   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 14
Bugs 3 Features 5
Metric Value
eloc 13
c 14
b 3
f 5
dl 0
loc 28
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Src;
4
5
use Iterator;
6
7
/**
8
 * Iterator which stores iterated values.
9
 * @template TKey
10
 * @template TValue
11
 * @extends IteratorEnvelope<TKey, TValue>
12
 */
13
final class SavingIterator extends IteratorEnvelope
14
{
15
    /**
16
     * Ctor.
17
     * 
18
     * @phpstan-param Iterator<TKey, TValue>       $origin
19
     * @phpstan-param AddingIterator<TKey, TValue> $target
20
     * @param Iterator       $origin original iterator.
21
     * @param AddingIterator $target iterator to which the values are saved.
22
     */
23
    public function __construct(
24
        Iterator $origin,
25
        AddingIterator $target
26
    ) {
27
        parent::__construct(
28
            /** @phpstan-ignore-next-line */
29
            new ContextVeil(
30
                $target,
31
                new ClosureReaction(
32
                    fn (AddingIterator $stored) => (new ValidTernary(
33
                        $origin,
34
                        function (Iterator $source) use ($stored) {
35
                            $temp = $stored->from($source);
36
                            $source->next();
37
                            return $temp;
38
                        },
39
                        fn () => $stored
40
                    ))->value()
41
                )
42
            )
43
        );
44
    }
45
}
46