AccumulateIterator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 4 1
A __construct() 0 6 1
A rewind() 0 5 2
A key() 0 4 1
A next() 0 11 2
A valid() 0 4 1
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
// phpcs:disable Zicht.Commenting.PropertyComment.VarTypeAvoidMixed
7
8
namespace Zicht\Itertools\lib;
9
10
use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface;
11
use Zicht\Itertools\lib\Traits\FiniteIterableTrait;
12
13
class AccumulateIterator implements FiniteIterableInterface
14
{
15
    use FiniteIterableTrait;
16
17
    /** @var \Iterator */
18
    protected $iterable;
19
20
    /** @var \Closure */
21
    protected $func;
22
23
    /** @var mixed */
24
    protected $value;
25
26
    /**
27
     * @param \Iterator $iterable
28
     * @param \Closure $func
29
     */
30 12
    public function __construct(\Iterator $iterable, \Closure $func)
31
    {
32 12
        $this->iterable = $iterable;
33 12
        $this->func = $func;
34 12
        $this->value = null;
35 12
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 11
    public function rewind()
41
    {
42 11
        $this->iterable->rewind();
43 11
        $this->value = $this->iterable->valid() ? $this->iterable->current() : null;
44 11
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 9
    public function current()
50
    {
51 9
        return $this->value;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 9
    public function key()
58
    {
59 9
        return $this->iterable->key();
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65 9
    public function next()
66
    {
67 9
        $this->iterable->next();
68 9
        if ($this->iterable->valid()) {
69
            // must assign $this->func to $func before calling the closure
70
            // because otherwise it will try fo find a method called func,
71
            // which doesn't exist
72 9
            $func = $this->func;
73 9
            $this->value = $func($this->value, $this->iterable->current());
74
        }
75 9
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 11
    public function valid()
81
    {
82 11
        return $this->iterable->valid();
83
    }
84
}
85