SubnetIterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

6 Methods

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