• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

voku / Arrayy / 3787581697

pending completion
3787581697

push

github

Lars Moelleken
[*]: update the changelog

2479 of 2793 relevant lines covered (88.76%)

30.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/ArrayyRewindableGenerator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Arrayy;
6

7
/**
8
 * @template   XKey as array-key
9
 * @template   X
10
 * @extends  \ArrayIterator<XKey,X>
11
 *
12
 * @internal
13
 */
14
class ArrayyRewindableGenerator extends \ArrayIterator
15
{
16
    /**
17
     * @var string
18
     *
19
     * @phpstan-var string|class-string<\Arrayy\Arrayy<XKey,X>>
20
     */
21
    protected $class;
22

23
    /**
24
     * @var callable
25
     */
26
    protected $generatorFunction;
27

28
    /**
29
     * @var \Generator
30
     *
31
     * @phpstan-var \Generator<XKey,X>
32
     */
33
    protected $generator;
34

35
    /**
36
     * @var callable|null
37
     */
38
    protected $onRewind;
39

40
    /**
41
     * @param callable      $generatorConstructionFunction
42
     *                                                     <p>A callable that should return a Generator.</p>
43
     * @param callable|null $onRewind
44
     *                                                     <p>Callable that gets invoked with 0 arguments after the iterator
45
     *                                                     was rewinded.</p>
46
     * @param string        $class
47
     *
48
     * @throws \InvalidArgumentException
49
     */
50
    public function __construct(
51
        callable $generatorConstructionFunction,
52
        callable $onRewind = null,
53
        string $class = ''
54
    ) {
55
        $this->class = $class;
98✔
56
        $this->generatorFunction = $generatorConstructionFunction;
98✔
57
        $this->onRewind = $onRewind;
98✔
58
        $this->generateGenerator();
98✔
59
    }
98✔
60

61
    /**
62
     * Return the current element.
63
     *
64
     * @return mixed
65
     *
66
     * @see  http://php.net/manual/en/iterator.current.php
67
     * @see  Iterator::current
68
     *
69
     * @phpstan-return X
70
     */
71
    #[\ReturnTypeWillChange]
72
    public function current()
73
    {
74
        return $this->generator->current();
71✔
75
    }
76

77
    /**
78
     * Return the key of the current element.
79
     *
80
     * @return mixed scalar on success, or null on failure
81
     *
82
     * @see  http://php.net/manual/en/iterator.key.php
83
     * @see  Iterator::key
84
     *
85
     * @phpstan-return XKey
86
     */
87
    #[\ReturnTypeWillChange]
88
    public function key()
89
    {
90
        return $this->generator->key();
71✔
91
    }
92

93
    /**
94
     * Move forward to next element.
95
     *
96
     * @return void
97
     *
98
     * @see  http://php.net/manual/en/iterator.next.php
99
     * @see  Iterator::next
100
     */
101
    #[\ReturnTypeWillChange]
102
    public function next()
103
    {
104
        $this->generator->next();
60✔
105
    }
60✔
106

107
    /**
108
     * Rewind the Iterator to the first element.
109
     *
110
     * @return void
111
     *
112
     * @see  http://php.net/manual/en/iterator.rewind.php
113
     * @see  Iterator::rewind
114
     */
115
    #[\ReturnTypeWillChange]
116
    public function rewind()
117
    {
118
        $this->generateGenerator();
97✔
119

120
        if (\is_callable($this->onRewind)) {
97✔
121
            \call_user_func($this->onRewind);
×
122
        }
123
    }
97✔
124

125
    /**
126
     * Checks if current position is valid.
127
     *
128
     * @return bool
129
     *
130
     * @see  http://php.net/manual/en/iterator.valid.php
131
     * @see  Iterator::rewind
132
     */
133
    public function valid(): bool
134
    {
135
        return $this->generator->valid();
97✔
136
    }
137

138
    /**
139
     * @return void
140
     */
141
    private function generateGenerator()
142
    {
143
        $this->generator = \call_user_func($this->generatorFunction);
98✔
144

145
        if (!($this->generator instanceof \Generator)) {
98✔
146
            throw new \InvalidArgumentException('The callable needs to return a Generator');
×
147
        }
148
    }
98✔
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc