Passed
Push — 179 ( a735d7...8fc61b )
by Max
11:14
created

SavingIterator::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
            /**
29
             * @todo #179:30min Consider making ValidTernary a variable. Maybe
30
             *  it is possible to implement this with a constant expression.
31
             */
32
            /** @phpstan-ignore-next-line */
33
            new ContextVeil(
34
                $target,
35
                new ClosureReaction(
36
                    function (AddingIterator $stored) use ($origin) {
37
                        return (new ValidTernary(
38
                            $origin,
39
                            function (Iterator $source) use ($stored) {
40
                                $temp = $stored->from($source);
41
                                $source->next();
42
                                return $temp;
43
                            },
44
                            fn () => $stored
45
                        ))->value();
46
                    }
47
                )
48
            )
49
        );
50
    }
51
}
52