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

UniqueIterator::accept()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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 UniqueIterator
14
 *
15
 * @package Zicht\Itertools\lib
16
 */
17
class UniqueIterator extends \FilterIterator implements FiniteIterableInterface
18
{
19
    use FiniteIterableTrait;
20
21
    /** @var \Closure */
22
    private $func;
23
24
    /** @var array */
25
    private $seen;
26
27
    /**
28
     * UniqueIterator constructor.
29
     *
30
     * @param \Closure $func
31
     * @param \Iterator $iterable
32
     */
33 15
    public function __construct(\Closure $func, \Iterator $iterable)
34
    {
35 15
        $this->func = $func;
36 15
        $this->seen = [];
37 15
        parent::__construct($iterable);
38 15
    }
39
40
    /**
41
     * @{inheritDoc}
42
     */
43 11
    public function accept()
44
    {
45 11
        $checkValue = call_user_func($this->func, $this->current(), $this->key());
46 11
        if (in_array($checkValue, $this->seen)) {
47 6
            return false;
48
        } else {
49 11
            $this->seen [] = $checkValue;
50 11
            return true;
51
        }
52
    }
53
54
    /**
55
     * @{inheritDoc}
56
     */
57 13
    public function rewind()
58
    {
59 13
        $this->seen = [];
60 13
        parent::rewind();
61 13
    }
62
}
63