CallbackIterator::next()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @author Maxim Sokolovsky
4
 */
5
6
namespace WS\Utils\Collections\Iterator;
7
8
use RuntimeException;
9
10
class CallbackIterator implements Iterator
11
{
12
    /**
13
     * @var IterateResult
14
     */
15
    private $currentIterateResult;
16
    private $generator;
17
18
    /**
19
     * CallbackIterator constructor.
20
     * @param $f callable Function with interface Fun(): IterateResult
21
     */
22 13
    public function __construct(callable $f)
23
    {
24 13
        $this->generator = $f;
25 13
    }
26
27 13
    public function next()
28
    {
29 13
        if (!$this->hasNext()) {
30 1
            throw new RuntimeException('Iterated element is absent');
31
        }
32 13
        $value = $this->currentIterateResult->getValue();
33 13
        $this->currentIterateResult = null;
34 13
        return $value;
35
    }
36
37 13
    public function hasNext(): bool
38
    {
39 13
        if ($this->currentIterateResult === null) {
40 13
            $f = $this->generator;
41 13
            $this->currentIterateResult = $f();
42
        }
43 13
        return $this->currentIterateResult->isRunOut() === false;
44
    }
45
}
46