SubnetIterator::rewind()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ColinODell\Ipv4;
4
5
/**
6
 * An object that implements a subnet iterator.
7
 */
8
class SubnetIterator implements \Iterator
9
{
10
    private $position = 0;
11
    private $low_dec;
12
    private $hi_dec;
13
14
    /**
15
     * SubnetIterator constructor.
16
     *
17
     * @param Subnet $subnet
18
     */
19 9
    public function __construct(Subnet $subnet)
20
    {
21 9
        $this->low_dec = ip2long($subnet->getFirstHostAddr());
22 9
        $this->hi_dec = ip2long($subnet->getLastHostAddr());
23 9
    }
24
25 6
    public function rewind()
26
    {
27 6
        $this->position = 0;
28 6
    }
29
30 9
    public function current()
31
    {
32 9
        return long2ip($this->low_dec + $this->position);
33
    }
34
35 3
    public function key()
36
    {
37 3
        return $this->position;
38
    }
39
40 9
    public function next()
41
    {
42 9
        $this->position++;
43 9
    }
44
45 6
    public function valid()
46
    {
47 6
        return ($this->low_dec + $this->position) <= $this->hi_dec;
48
    }
49
}
50