SavingIterator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 3
Metric Value
cc 1
eloc 12
c 7
b 1
f 3
nc 1
nop 2
dl 0
loc 18
rs 9.8666
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