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

RandomIterator::getIterator()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 3
eloc 10
nc 2
nop 0
crap 3
1
<?php
2
3
namespace Gielfeldt\Iterators;
4
5
class RandomIterator implements \IteratorAggregate, \Countable
6
{
7
    private $count;
8
    private $innerIterator;
9
10 2
    public function __construct(\Traversable $iterator, int $count)
11
    {
12 2
        $this->innerIterator = new CountableIterator($iterator, CountableIterator::CACHE_COUNT);
13 2
        $this->count = $count;
14 2
    }
15
16 2
    public function getIterator()
17
    {
18 2
        $innerCount = $this->innerIterator->count();
19 2
        $count = min($this->count, $innerCount);
20 2
        $indexes = [];
21 2
        for ($i = 0; $i < $count; $i++) {
22
            do {
23 2
                $index = rand(0, $innerCount - 1);
24 2
            } while (isset($indexes[$index]));
25 2
            $indexes[$index] = $index;
26
        }
27 2
        return new IndexIterator($this->innerIterator, $indexes);
28 2
    }
29 2
30
    public function count()
31
    {
32
        return min(count($this->innerIterator), $this->count);
33
    }
34
}
35