LazySliceIterator::rewind()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 3
eloc 9
nc 2
nop 0
1
<?php
2
3
namespace Collections\Iterator;
4
5 View Code Duplication
class LazySliceIterator implements \Iterator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6
{
7
    /**
8
     * @var \Iterator
9
     */
10
    private $it;
11
12
    /**
13
     * @var int
14
     */
15
    private $start;
16
17
    /**
18
     * @var int
19
     */
20
    private $len;
21
22
    /**
23
     * @var int
24
     */
25
    private $currentLen;
26
27
    public function __construct($it, $start, $len)
28
    {
29
        $this->it = $it;
30
        $this->start = $start;
31
        $this->len = $len;
32
        $this->currentLen = $len;
33
        while ($start !== 0 && $it->valid()) {
34
            $it->next();
35
            --$start;
36
        }
37
    }
38
39
    public function __clone()
40
    {
41
        $this->it = clone $this->it;
42
    }
43
44
    public function rewind()
45
    {
46
        $it = $this->it;
47
        $start = $this->start;
48
        $len = $this->len;
49
        $it->rewind();
50
        $this->currentLen = $len;
51
        while ($start !== 0 && $it->valid()) {
52
            $it->next();
53
            --$start;
54
        }
55
    }
56
57
    public function valid()
58
    {
59
        return $this->it->valid() && $this->currentLen !== 0;
60
    }
61
62
    public function next()
63
    {
64
        $this->it->next();
65
        if ($this->currentLen !== 0) {
66
            --$this->currentLen;
67
        }
68
    }
69
70
    public function key()
71
    {
72
        return $this->it->key();
73
    }
74
75
    public function current()
76
    {
77
        return $this->it->current();
78
    }
79
}