GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CachingIterator::offsetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Gielfeldt\Iterators;
4
5
class CachingIterator extends \ArrayIterator
6
{
7
    const CLONE_KEY = 1;
8
    const CLONE_CURRENT = 2;
9
10
    private $uncachedIterator;
11
    private $uncachedIteratorCount;
12
    private $modified = false;
13
    private $finished = false;
14
    private $flags;
15
    private $index;
16
17
    public function __construct(\Traversable $iterator, int $flags = self::CLONE_KEY | self::CLONE_CURRENT)
18
    {
19
        $this->flags = $flags;
20
        $this->uncachedIteratorCount = $iterator instanceof \Countable ? count($iterator) : null;
21
        $this->uncachedIterator = new TraversableIterator($iterator);
22
        parent::__construct();
23
    }
24
25
    private function setupInnerIterator()
26
    {
27
        if ($this->uncachedIterator->getIndex() === null) {
28
            $this->uncachedIterator->rewind();
29
            parent::rewind();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (rewind() instead of setupInnerIterator()). 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...
30
            $this->collect();
31
        }
32
    }
33
34
    public function count()
35
    {
36
        if (!$this->finished && !$this->modified && $this->uncachedIteratorCount !== null) {
37
            return $this->uncachedIteratorCount;
38
        }
39
        $this->collectRest();
40
        return parent::count();
41
    }
42
43
    public function getIndex()
44
    {
45
        return $this->index;
46
    }
47
48
    public function rewind()
49
    {
50
        $this->index = 0;
51
        $this->setupInnerIterator();
52
        parent::rewind();
53
    }
54
55
    public function next()
56
    {
57
        $this->index++;
58
        parent::next();
59
        if (!parent::valid()) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (valid() instead of next()). 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...
60
            $this->collect();
61
        }
62
    }
63
64
    public function offsetSet($offset, $value)
65
    {
66
        $this->collectRest();
67
        $this->modified = true;
68
        return parent::offsetSet($offset, $value);
69
    }
70
71
    public function collectRest($until = null)
72
    {
73
        if ($this->finished) {
74
            return;
75
        }
76
77
        $this->setupInnerIterator();
78
79
        while (!$this->finished && ($until == null || $until >= $this->uncachedIterator->getIndex())) {
80
            $this->collect();
81
        }
82
    }
83
84
    public function collect()
85
    {
86
        if ($this->uncachedIterator->valid()) {
87
            $key = $this->uncachedIterator->key();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $key is correct as $this->uncachedIterator->key() (which targets IteratorIterator::key()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
            $key = ($this->flags & self::CLONE_KEY) && is_object($key) ? clone $key : $key;
89
            $current = $this->uncachedIterator->current();
90
            $current = ($this->flags & self::CLONE_CURRENT) && is_object($current) ? clone $current : $current;
91
            parent::offsetSet($key, $current);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetSet() instead of collect()). Are you sure this is correct? If so, you might want to change this to $this->offsetSet().

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...
92
            $this->uncachedIterator->next();
93
        } else {
94
            $this->finished = true;
95
        }
96
    }
97
98
    public function prev()
99
    {
100
        $this->seek($this->index - 1);
101
    }
102
103
    // Ensure entire inner iterator is collected before applying the follwing.
104
    public function seek($pos)
105
    {
106
        if ($pos >= $this->uncachedIterator->getIndex()) {
107
            $this->collectRest($pos);
108
        }
109
        $this->index = $pos;
110
        return parent::seek($pos);
111
    }
112
113
    public function offsetGet($offset)
114
    {
115
        $this->collectRest();
116
        return parent::offsetGet($offset);
117
    }
118
119
    public function offsetExists($offset)
120
    {
121
        $this->collectRest();
122
        return parent::offsetExists($offset);
123
    }
124
125
    public function offsetUnset($offset)
126
    {
127
        $this->collectRest();
128
        $this->modified = true;
129
        return parent::offsetUnset($offset);
130
    }
131
132
    public function getArrayCopy()
133
    {
134
        $this->collectRest();
135
        return iterator_to_array($this);
136
    }
137
138
    public function ksort()
139
    {
140
        $this->collectRest();
141
        return parent::ksort();
142
    }
143
    public function natcasesort()
144
    {
145
        $this->collectRest();
146
        return parent::natcasesort();
147
    }
148
    public function natsort()
149
    {
150
        $this->collectRest();
151
        return parent::natsort();
152
    }
153
    public function uasort($cmp)
154
    {
155
        $this->collectRest();
156
        return parent::uasort($cmp);
157
    }
158
    public function uksort($cmp)
159
    {
160
        $this->collectRest();
161
        return parent::uksort($cmp);
162
    }
163
    public function serialize()
164
    {
165
        $this->collectRest();
166
        $serialized = "x:i:0;";
167
        $serialized .= serialize($this->getArrayCopy()) . ";";
168
        $serialized .= "m:a:0:{}";
169
        return $serialized;
170
    }
171
    public function unserialize($serialized)
172
    {
173
        $this->finished = true;
174
        $this->modified = false;
175
        $this->uncachedIterator = new TraversableIterator(new \ArrayIterator());
176
        $this->uncachedIteratorCount = null;
177
        return parent::unserialize($serialized);
178
    }
179
}
180