Passed
Push — master ( bf7a75...d9297f )
by Max
47s queued 12s
created

OpenAddingIterator::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 3
c 2
b 0
f 2
nc 1
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Src;
4
5
use ArrayAccess;
6
use Iterator;
7
8
/**
9
 * Adding iterator which stores values in a user provided iterator.
10
 * @template TKey
11
 * @template TValue
12
 * @implements AddingIterator<TKey, TValue>
13
 */
14
final class OpenAddingIterator implements AddingIterator
15
{
16
17
    /**
18
     * Ctor.
19
     * 
20
     * @phpstan-param Iterator<TKey, TValue>&ArrayAccess<TKey, TValue> $added
21
     * @param Iterator&ArrayAccess $added iterator with stored values.
22
     */
23
    public function __construct(
24
        /**
25
         * Iterator with stored values.
26
         * 
27
         * @phpstan-var Iterator<TKey, TValue>&ArrayAccess<TKey, TValue>
28
         * @var Iterator&ArrayAccess
29
         */
30
        private Iterator|ArrayAccess $added
31
    ) {
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function from(Iterator $source): AddingIterator
38
    {
39
        /**
40
         * @todo #83:20min Cover that Iterator works with an immutable
41
         *  iterator.
42
         */
43
        /**
44
         * @todo #83:20min Assert that iterator does not add values if they are already stored.
45
         */
46
        $updated = clone $this->added;
47
        $updated[$source->key()] ??= $source->current();
48
        return new self($updated);
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function current(): mixed
55
    {
56
        return $this->added->current();
0 ignored issues
show
Bug introduced by
The method current() does not exist on ArrayAccess. It seems like you code against a sub-type of ArrayAccess such as SplDoublyLinkedList or Phar or Yaf_Config_Simple or SplFixedArray or SplObjectStorage or Yaf\Session or SimpleXMLElement or TheSeer\Tokenizer\TokenCollection or Yaf_Session or Cassandra\Rows or Cassandra\Map or Yaf\Config\Simple or PDepend\Source\AST\ASTArtifactList or CachingIterator or Yaf\Config\Ini or SolrDocument or Yaf_Config_Ini or ArrayIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        return $this->added->/** @scrutinizer ignore-call */ current();
Loading history...
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function key(): mixed
63
    {
64
        return $this->added->key();
0 ignored issues
show
Bug introduced by
The method key() does not exist on ArrayAccess. It seems like you code against a sub-type of ArrayAccess such as SplDoublyLinkedList or Phar or Yaf_Config_Simple or SplFixedArray or SplObjectStorage or Yaf\Session or SimpleXMLElement or TheSeer\Tokenizer\TokenCollection or Yaf_Session or Cassandra\Rows or Cassandra\Map or Yaf\Config\Simple or PDepend\Source\AST\ASTArtifactList or CachingIterator or Yaf\Config\Ini or SolrDocument or Yaf_Config_Ini or ArrayIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        return $this->added->/** @scrutinizer ignore-call */ key();
Loading history...
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function next(): void
71
    {
72
        $this->added->next();
0 ignored issues
show
Bug introduced by
The method next() does not exist on ArrayAccess. It seems like you code against a sub-type of said class. However, the method does not exist in http\Params or pq\Types or Threaded or HttpQueryString or WeakMap or Thread or SolrObject or http\QueryString or Symfony\Component\EventDispatcher\GenericEvent or Worker or ArrayObject or Symfony\Component\OptionsResolver\Options or Volatile or Thread or Worker or Worker or Symfony\Component\OptionsResolver\OptionsResolver. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $this->added->/** @scrutinizer ignore-call */ 
73
                      next();
Loading history...
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function valid(): bool
79
    {
80
        return $this->added->valid();
0 ignored issues
show
Bug introduced by
The method valid() does not exist on ArrayAccess. It seems like you code against a sub-type of ArrayAccess such as SplDoublyLinkedList or Phar or Yaf_Config_Simple or SplFixedArray or SplObjectStorage or Yaf\Session or SimpleXMLElement or TheSeer\Tokenizer\TokenCollection or Yaf_Session or Cassandra\Rows or Cassandra\Map or Yaf\Config\Simple or PDepend\Source\AST\ASTArtifactList or CachingIterator or Yaf\Config\Ini or SolrDocument or Yaf_Config_Ini or ArrayIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        return $this->added->/** @scrutinizer ignore-call */ valid();
Loading history...
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function rewind(): void
87
    {
88
        /**
89
         * @todo #83:20min Assert that iterator rewinds original iterator.
90
         */
91
        $this->added->rewind();
0 ignored issues
show
Bug introduced by
The method rewind() does not exist on ArrayAccess. It seems like you code against a sub-type of ArrayAccess such as SplDoublyLinkedList or Phar or Yaf_Config_Simple or SplFixedArray or SplObjectStorage or Yaf\Session or SimpleXMLElement or TheSeer\Tokenizer\TokenCollection or Yaf_Session or Cassandra\Rows or Cassandra\Map or Yaf\Config\Simple or PDepend\Source\AST\ASTArtifactList or CachingIterator or Yaf\Config\Ini or SolrDocument or Yaf_Config_Ini or ArrayIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        $this->added->/** @scrutinizer ignore-call */ 
92
                      rewind();
Loading history...
92
    }
93
}
94