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

UniqueIterator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 46
ccs 15
cts 15
cp 1
rs 10
c 3
b 0
f 0
wmc 4
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A accept() 0 10 2
A rewind() 0 5 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 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