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.
Completed
Push — master ( 0c8b13...b1ee3a )
by Thomas
02:04
created

CachingIterator::getIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
16
    public function __construct(\Traversable $iterator, int $flags = self::CLONE_KEY | self::CLONE_CURRENT)
17
    {
18
        $this->flags = $flags;
19
        $this->uncachedIteratorCount = $iterator instanceof \Countable ? count($iterator) : null;
20
        $this->uncachedIterator = new TraversableIterator($iterator);
21
        parent::__construct();
22
    }
23
24
    private function setupInnerIterator()
25
    {
26
        if ($this->uncachedIterator->getIndex() === null) {
27
            $this->uncachedIterator->rewind();
28
            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...
29
            $this->collect();
30
        }
31
    }
32
33
    public function count()
34
    {
35
        if (!$this->finished && !$this->modified && $this->uncachedIteratorCount !== null) {
36
            return $this->uncachedIteratorCount;
37
        }
38
        $this->collectRest();
39
        return parent::count();
40
    }
41
42
    public function getIndex()
43
    {
44
        return $this->index;
0 ignored issues
show
Bug introduced by
The property index does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
    }
46
47
    public function rewind()
48
    {
49
        $this->index = 0;
50
        $this->setupInnerIterator();
51
        parent::rewind();
52
    }
53
54
    public function next()
55
    {
56
        $this->index++;
57
        parent::next();
58
        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...
59
            $this->collect();
60
        }
61
    }
62
63
    public function offsetSet($offset, $value)
64
    {
65
        $this->collectRest();
66
        $this->modified = true;
67
        return parent::offsetSet($offset, $value);
68
    }
69
70
    public function collectRest($until = null)
71
    {
72
        if ($this->finished) {
73
            return;
74
        }
75
76
        $this->setupInnerIterator();
77
78
        while (!$this->finished && ($until == null || $until >= $this->uncachedIterator->getIndex())) {
79
            $this->collect();
80
        }
81
    }
82
83
    public function collect()
84
    {
85
        if ($this->uncachedIterator->valid()) {
86
            $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...
87
            $key = ($this->flags & self::CLONE_KEY) && is_object($key) ? clone $key : $key;
88
            $current = $this->uncachedIterator->current();
89
            $current = ($this->flags & self::CLONE_CURRENT) && is_object($current) ? clone $current : $current;
90
            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...
91
            $this->uncachedIterator->next();
92
        } else {
93
            $this->finished = true;
94
        }
95
    }
96
97
    public function prev()
98
    {
99
        $this->seek($this->index - 1);
100
    }
101
102
    // Ensure entire inner iterator is collected before applying the follwing.
103
    public function seek($pos)
104
    {
105
        if ($pos >= $this->uncachedIterator->getIndex()) {
106
            $this->collectRest($pos);
107
        }
108
        $this->index = $pos;
109
        return parent::seek($pos);
110
    }
111
112
    public function offsetGet($offset)
113
    {
114
        $this->collectRest();
115
        return parent::offsetGet($offset);
116
    }
117
118
    public function offsetExists($offset)
119
    {
120
        $this->collectRest();
121
        return parent::offsetExists($offset);
122
    }
123
124
    public function offsetUnset($offset)
125
    {
126
        $this->collectRest();
127
        $this->modified = true;
128
        return parent::offsetUnset($offset);
129
    }
130
131
    public function getArrayCopy()
132
    {
133
        $this->collectRest();
134
        return iterator_to_array($this);
135
    }
136
137
    public function ksort()
138
    {
139
        $this->collectRest();
140
        return parent::ksort();
141
    }
142
    public function natcasesort()
143
    {
144
        $this->collectRest();
145
        return parent::natcasesort();
146
    }
147
    public function natsort()
148
    {
149
        $this->collectRest();
150
        return parent::natsort();
151
    }
152
    public function uasort($cmp)
153
    {
154
        $this->collectRest();
155
        return parent::uasort($cmp);
156
    }
157
    public function uksort($cmp)
158
    {
159
        $this->collectRest();
160
        return parent::uksort($cmp);
161
    }
162
    public function serialize()
163
    {
164
        $this->collectRest();
165
        $serialized = "x:i:0;";
166
        $serialized .= serialize($this->getArrayCopy()) . ";";
167
        $serialized .= "m:a:0:{}";
168
        return $serialized;
169
    }
170
    public function unserialize($serialized)
171
    {
172
        $this->finished = true;
173
        $this->modified = false;
174
        $this->uncachedIterator = new TraversableIterator(new \ArrayIterator());
175
        $this->uncachedIteratorCount = null;
176
        return parent::unserialize($serialized);
177
    }
178
}
179