Passed
Pull Request — master (#93)
by Max
02:24
created

SavingIterator::added()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 8
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
 * @implements Iterator<TKey, TValue>
12
 */
13
class SavingIterator implements Iterator
14
{
15
16
    /**
17
     * Ctor.
18
     * 
19
     * @phpstan-param Iterator<TKey, TValue>       $origin
20
     * @phpstan-param AddingIterator<TKey, TValue> $target
21
     * @param Iterator       $origin original iterator.
22
     * @param AddingIterator $target iterator to which the values are saved.
23
     */
24
    public function __construct(
25
        /**
26
         * Original iterator.
27
         *
28
         * @phpstan-var Iterator<TKey, TValue>
29
         * @var Iterator
30
         */
31
        private Iterator $origin,
32
33
        /**
34
         * Iterator to which the values are saved.
35
         *
36
         * @phpstan-var AddingIterator<TKey, TValue>
37
         * @var AddingIterator
38
         */
39
        private AddingIterator $target
40
    ) {
41
    }
42
43
    /**
44
     * Returns target after adding a value from origin.
45
     *
46
     * @return AddingIterator
47
     */
48
    private function added(): AddingIterator
49
    {
50
        /**
51
         * @todo #85:40min There is a private function in this class. It should
52
         *  be removed without creating the code duplication problem again.
53
         */
54
        $this->target = $this->target->from($this->origin);
55
        return $this->target;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     * @return TValue|false
0 ignored issues
show
Bug introduced by
The type MaxGoryunov\SavingIterator\Src\TValue 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
     */
62
    public function current(): mixed
63
    {
64
        return $this->added()->current();
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     * @return TKey|null
0 ignored issues
show
Bug introduced by
The type MaxGoryunov\SavingIterator\Src\TKey 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
     */
71
    public function key(): mixed
72
    {
73
        return $this->added()->key();
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function valid(): bool
80
    {
81
        return ($this->origin->valid()) || ($this->target->valid());
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function next(): void
88
    {
89
        if ($this->origin->valid()) {
90
            $this->origin->next();
91
        }
92
        $this->target->next();
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function rewind(): void
99
    {
100
        $this->target->rewind();
101
    }
102
}
103