LookAheadIterator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace itertools;
4
5
use IteratorIterator;
6
7
8
class LookAheadIterator extends IteratorIterator
9
{
10
    protected $lookAheads;
11
    protected $skipAutoRewind;
12
    protected $skipNextNormalRewind;
13
14
    public static function newInstance($innerIterator)
15
    {
16
        if($innerIterator instanceof self) {
17
            return $innerIterator;
18
        }
19
        return new self($innerIterator);
20
    }
21
22
    public function __construct($innerIterator)
23
    {
24
        parent::__construct(IterUtil::asIterator($innerIterator));
25
        $this->lookAheads = new Queue();
26
        $this->skipAutoRewind = false;
27
        $this->skipNextNormalRewind = false;
28
    }
29
30
    public function next()
31
    {
32
        if($this->lookAheads->isEmpty()) {
33
            return parent::next();
34
        }
35
        $this->lookAheads->shift();
36
    }
37
38
    public function valid()
39
    {
40
        if(! $this->lookAheads->isEmpty()) {
41
            return true;
42
        }
43
        return parent::valid();
44
    }
45
46
    public function key()
47
    {
48
        if(! $this->lookAheads->isEmpty()) {
49
            return $this->lookAheads->bottom()->key;
50
        }
51
        return parent::key();
52
    }
53
54
    public function current()
55
    {
56
        if(! $this->lookAheads->isEmpty()) {
57
            return $this->lookAheads->bottom()->value;
58
        }
59
        return parent::current();
60
    }
61
62
    public function prefetchUpTo($prefetchCount)
63
    {
64
        $this->autoRewind();
65
        $prefetchCount = max(0, $prefetchCount - $this->lookAheads->count());
66
        for($i = 0; $i < $prefetchCount; $i += 1) {
67
            if(! parent::valid()) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (valid() instead of prefetchUpTo()). Are you sure this is correct? If so, you might want to change this to $this->valid().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
68
                return;
69
            }
70
            $this->lookAheads->push((object) array('value' => parent::current(), 'key' => parent::key()));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (current() instead of prefetchUpTo()). Are you sure this is correct? If so, you might want to change this to $this->current().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (key() instead of prefetchUpTo()). Are you sure this is correct? If so, you might want to change this to $this->key().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
71
            parent::next();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (next() instead of prefetchUpTo()). Are you sure this is correct? If so, you might want to change this to $this->next().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
72
        }
73
    }
74
75
    public function getNext($n = 1)
76
    {
77
        $this->prefetchUpTo($n + 1);
78
        return $this->lookAheads->offsetGet($n)->value;
79
    }
80
81
    public function hasAny()
82
    {
83
        return $this->hasNext(0);
84
    }
85
86
    public function getNextKey($n = 1)
87
    {
88
        $this->prefetchUpTo($n + 1);
89
        return $this->lookAheads->offsetGet($n)->key;
90
    }
91
92
    public function hasNext($n = 1)
93
    {
94
        $this->prefetchUpTo($n + 1);
95
        return $this->lookAheads->count() > $n;
96
    }
97
98
    public function rewind()
99
    {
100
        if($this->skipNextNormalRewind) {
101
            $this->skipNextNormalRewind = false;
102
            return;
103
        }
104
        parent::rewind();
105
        $this->skipAutoRewind = true;
106
    }
107
108
    protected function autoRewind()
109
    {
110
        if(! $this->skipAutoRewind) {
111
            parent::rewind();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (rewind() instead of autoRewind()). Are you sure this is correct? If so, you might want to change this to $this->rewind().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
112
            $this->skipNextNormalRewind = true;
113
            $this->skipAutoRewind = true;
114
        }
115
    }
116
}
117