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 ( dba213...74f8ff )
by Thomas
02:11
created

CachingIterator::natsort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 IteratorIterator($iterator);
21
        parent::__construct();
22
    }
23
24
    public function count()
25
    {
26
        if (!$this->finished && !$this->modified && $this->uncachedIteratorCount !== null) {
27
            return $this->uncachedIteratorCount;
28
        }
29
        $this->collectRest();
30
        return parent::count();
31
    }
32
33
    private function setupInnerIterator()
34
    {
35
        if ($this->uncachedIterator->getIndex() === null) {
36
            $this->uncachedIterator->rewind();
37
            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...
38
            $this->collect();            
39
        }
40
    }
41
42
    public function rewind()
43
    {
44
        $this->setupInnerIterator();
45
        parent::rewind();
46
    }
47
48
    public function next()
49
    {
50
        parent::next();
51
        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...
52
            $this->collect();
53
        }
54
    }
55
56
    public function offsetSet($offset, $value)
57
    {
58
        $this->collectRest();
59
        $this->modified = true;
60
        return parent::offsetSet($offset, $value);
61
    }
62
63
    public function collectRest($until = null)
64
    {
65
        if ($this->finished) {
66
            return;
67
        }
68
69
        $this->setupInnerIterator();
70
71
        while (!$this->finished && ($until == null || $until >= $this->uncachedIterator->getIndex())) {
72
            $this->collect();
73
        }
74
    }
75
76
    public function collect()
77
    {
78
        if ($this->uncachedIterator->valid()) {
79
            $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...
80
            $key = ($this->flags & self::CLONE_KEY) && is_object($key) ? clone $key : $key;
81
            $current = $this->uncachedIterator->current();
82
            $current = ($this->flags & self::CLONE_CURRENT) && is_object($current) ? clone $current : $current;
83
            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...
84
            $this->uncachedIterator->next();
85
        }
86
        else {
87
            $this->finished = true;
88
        }
89
    }
90
91
    // Ensure entire inner iterator is collected before applying the follwing.
92
    public function seek($pos) {
93
        $this->collectRest($pos);
94
        return parent::seek($pos);
95
    }
96
97
    public function offsetGet($offset)
98
    {
99
        $this->collectRest();
100
        return parent::offsetGet($offset);
101
    }
102
103
    public function offsetExists($offset)
104
    {
105
        $this->collectRest();
106
        return parent::offsetExists($offset);
107
    }
108
109
    public function offsetUnset($offset)
110
    {
111
        $this->collectRest();
112
        $this->modified = true;
113
        return parent::offsetUnset($offset);
114
    }
115
116
    public function getArrayCopy()
117
    {
118
        $this->collectRest();
119
        return iterator_to_array($this);
120
    }
121
122
    public function ksort()
123
    {
124
        $this->collectRest();
125
        return parent::ksort();
126
    }
127
    public function natcasesort()
128
    {
129
        $this->collectRest();
130
        return parent::natcasesort();
131
    }
132
    public function natsort()
133
    {
134
        $this->collectRest();
135
        return parent::natsort();
136
    }
137
    public function uasort($cmp)
138
    {
139
        $this->collectRest();
140
        return parent::uasort($cmp);
141
    }
142
    public function uksort($cmp)
143
    {
144
        $this->collectRest();
145
        return parent::uksort($cmp);
146
    }
147
    public function serialize()
148
    {
149
        $this->collectRest();
150
        $serialized = "x:i:0;";
151
        $serialized .= serialize($this->getArrayCopy()) . ";";
152
        $serialized .= "m:a:0:{}";
153
        return $serialized;
154
    }
155
    public function unserialize($serialized)
156
    {
157
        $this->finished = true;
158
        $this->modified = false;
159
        $this->innerIterator = null;
0 ignored issues
show
Bug introduced by
The property innerIterator 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...
160
        $this->innerIteratorCount = null;
0 ignored issues
show
Bug introduced by
The property innerIteratorCount does not seem to exist. Did you mean innerIterator?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
161
        return parent::unserialize($serialized);
162
    }
163
164
}
165