IteratorTransfer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 12
c 2
b 0
f 2
dl 0
loc 43
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toTarget() 0 15 2
A __construct() 0 9 1
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Fakes;
4
5
use Iterator;
6
use MaxGoryunov\SavingIterator\Src\AddingIterator;
7
use MaxGoryunov\SavingIterator\Src\ValidTernary;
8
9
/**
10
 * Performs transfer of values from iterator to adding iterator.
11
 * @template TKey
12
 * @template TValue
13
 */
14
final class IteratorTransfer
15
{
16
17
    /**
18
     * Ctor.
19
     * 
20
     * @phpstan-param Iterator<TKey, TValue> $origin
21
     * @param Iterator $origin original iterator.
22
     */
23
    public function __construct(
24
        /**
25
         * Original iterator.
26
         *
27
         * @phpstan-var Iterator<TKey, TValue>
28
         * @var Iterator
29
         */
30
        private Iterator $origin
31
    ) {
32
    }
33
34
    /**
35
     * Transfers all values from origin to target.
36
     *
37
     * @phpstan-param AddingIterator<TKey, TValue> $target
38
     * @param AddingIterator $target
39
     * @phpstan-return AddingIterator<TKey, TValue>
40
     * @return AddingIterator
41
     */
42
    public function toTarget(AddingIterator $target): AddingIterator
43
    {
44
        $this->origin->rewind();
45
        while ($this->origin->valid()) {
46
            $target = (new ValidTernary(
47
                $this->origin,
48
                function (Iterator $source) use ($target) {
49
                    $target = $target->from($source);
50
                    $source->next();
51
                    return $target;
52
                },
53
                fn () => $target
54
            ))->value();
55
        }
56
        return $target;
57
    }
58
}
59