Passed
Push — master ( 9e9b54...8c04f5 )
by
unknown
38s
created

AccumulateIterator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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