RangeIterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 83
ccs 19
cts 19
cp 1
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 3 1
A next() 0 3 1
A rewind() 0 3 1
A valid() 0 11 2
A __construct() 0 4 1
A key() 0 3 1
1
<?php
2
/**
3
 * Copyright (c) 2018 Mattheu Racine
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace mracine\IPTools\Iterators;
10
11
use mracine\IPTools\IPv4\Range;
12
13
/**
14
 * Range implementation of Iterator
15
 *
16
 * @package IPTools
17
 */
18
class RangeIterator implements \Iterator
19
{
20
    /**
21
     * @var Range $range local reference to the range to iterate
22
     */
23
    protected $range;
24
25
    /**
26
     * @var int $index internal pointer in the range
27
     */
28
    protected $index;
29
30
    /**
31
     * Créates the iterator
32
     *
33
     * Initialize internal pointer to the first position of the Range
34
     *
35
     * @param Range $range
36
     */
37 1
    public function __construct(Range $range)
38
    {
39 1
        $this->range = $range;
40 1
        $this->rewind();
41 1
    }
42
43
    /**
44
     * Return an Address representing the current element of the Range 
45
     *
46
     * Use the ArrayAccess interface of the Range. 
47
     *  
48
     * @return \mracine\IPTools\IPv4\Address
49
     */
50 1
    public function current()
51
    {
52 1
        return $this->range[$this->index];
53
    }
54
55
    /**
56
     * Return the key of the current element 
57
     *
58
     * @return int
59
     */
60 1
    public function key()
61
    {
62 1
        return $this->index;
63
    }
64
65
    /**
66
     * Move the internal pointer to the next element 
67
     *
68
     * @return void
69
     */
70 1
    public function next()
71
    {
72 1
        $this->index++;
73 1
    }
74
75
    /**
76
     * Move the internal pointer to the first element 
77
     *
78
     * @return void
79
     */
80 1
    public function rewind()
81
    {
82 1
        $this->index = 0;
83 1
    }
84
85
    /**
86
     * Tells if the internal pointer designs a valid element 
87
     *
88
     * @return bool
89
     */
90 1
    public function valid()
91
    {
92
        try
93
        {
94 1
            $this->range[$this->index];       
95
        }
96 1
        catch(\Exception $e)
97
        {
98 1
            return false;
99
        }
100 1
        return true;
101
    }
102
}
103
104
105