CallbackIterator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 34
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A hasNext() 0 7 2
A next() 0 8 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