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.

OrderedInterleaveIterator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 0
dl 0
loc 74
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A setCompare() 0 5 1
A compare() 0 4 1
A defaultCompare() 0 4 1
A add() 0 5 2
A rewind() 0 14 4
A key() 0 4 1
A current() 0 4 1
A next() 0 10 2
1
<?php
2
3
namespace Gielfeldt\Iterators;
4
5
class OrderedInterleaveIterator extends \SplHeap
6
{
7
    const DEFAULT_COMPARE = [__CLASS__, 'defaultCompare'];
8
9
    protected $iterators;
10
    protected $compareFunction = self::DEFAULT_COMPARE;
11
12
13
    public function __construct(\Traversable ...$iterators)
14
    {
15
        $this->iterators = new \ArrayIterator();
16
        foreach ($iterators as $iterator) {
17
            $this->add($iterator);
18
        }
19
    }
20
21
    public function setCompare($compareFunction)
22
    {
23
        $this->compareFunction = $compareFunction;
24
        return $this;
25
    }
26
27
    public function compare($a, $b)
28
    {
29
        return call_user_func_array($this->compareFunction, [$b[2], $a[2]]);
30
    }
31
32
    public function defaultCompare($a, $b)
33
    {
34
        return $a <=> $b;
35
    }
36
37
    public function add(\Traversable $iterator)
38
    {
39
        $this->iterators->append($iterator instanceof \Iterator ? $iterator : new \IteratorIterator($iterator));
40
        return $this;
41
    }
42
43
    public function rewind()
44
    {
45
        while (!$this->isEmpty()) {
46
            $this->extract();
47
        }
48
        foreach ($this->iterators as $no => $iterator) {
49
            $iterator->rewind();
50
            if ($iterator->valid()) {
51
                $this->insert([$no, $iterator->key(), $iterator->current()]);
52
                $iterator->next();
53
            }
54
        }
55
        $this->idx = 0;
0 ignored issues
show
Bug introduced by
The property idx 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...
56
    }
57
58
    public function key()
59
    {
60
        return $this->top()[1];
61
    }
62
63
    public function current()
64
    {
65
        return $this->top()[2];
66
    }
67
68
    public function next()
69
    {
70
        $this->idx++;
71
        $used = $this->extract();
72
        $no = $used[0];
73
        if ($this->iterators[$no]->valid()) {
74
            $this->insert([$no, $this->iterators[$no]->key(), $this->iterators[$no]->current()]);
75
            $this->iterators[$no]->next();
76
        }
77
    }
78
}
79