Passed
Push — feature/85 ( 1bf7f6 )
by Max
10:32
created

SavingIterator::added()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
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
        $this->target = $this->target->from($this->origin);
51
        return $this->target;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     * @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...
57
     */
58
    public function current(): mixed
59
    {
60
        /**
61
         * @todo #66:25min Codebeat complains about similar code in two methods.
62
         *  It should be refactored.
63
         */
64
        $this->target = $this->target->from($this->origin);
65
        return $this->added()->current();
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     * @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...
71
     */
72
    public function key(): mixed
73
    {
74
        return $this->added()->key();
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function valid(): bool
81
    {
82
        return ($this->origin->valid()) || ($this->target->valid());
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function next(): void
89
    {
90
        if ($this->origin->valid()) {
91
            $this->origin->next();
92
        }
93
        $this->target->next();
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function rewind(): void
100
    {
101
        $this->target->rewind();
102
    }
103
}
104