CallbackIterator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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