Block   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 12
Bugs 2 Features 1
Metric Value
wmc 20
c 12
b 2
f 1
lcom 3
cbo 3
dl 0
loc 165
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 38 11
A isNetworkAddress() 0 4 1
A isBroadcastAddress() 0 4 1
A getNetworkAddress() 0 4 1
A getBroadcastAddress() 0 4 1
A getSubnetMask() 0 4 1
A getAddressCount() 0 4 1
A getUsableAddressCount() 0 4 1
A calculateNetworkAddress() 0 4 1
A calculateBroadcastAddress() 0 4 1
1
<?php
2
3
namespace JAAulde\IP\V4;
4
5
/**
6
 * Represents a block (network) of IPV4 addresses.
7
 *
8
 * @author Jim Auldridge <[email protected]>
9
 * @copyright 2006-2015 Jim Auldridge
10
 * @license MIT
11
 */
12
class Block extends Range
13
{
14
    /**
15
     * @var \JAAulde\IP\V4\SubnetMask The subnet mask of the represented block
16
     */
17
    protected $subnetMask;
18
19
    /**
20
     * Constructor
21
     *
22
     * @param \JAAulde\IP\V4\Address|string                               $a1 The base address with which the network and broadcast addresses of this block will be calculated. This can be an
23
     *                                                                        an instance of \JAAulde\IP\V4\Address, in which case the second parameter will be required, or a string in dot-notated format with optional CIDR
24
     *                                                                        slash prefix. If a string without CIDR slash prefix, second parameter is required. If there is a CIDR slash prefix, second parameter will be ignored.
25
     * @param \JAAulde\IP\V4\SubnetMask|int|string|\JAAulde\IP\V4\Address $a2 Optional depending on what was given as the first parameter. This is a subnet mask
26
     *                                                                        to determine the size of this block, or a CIDR prefix for calculating a subnet mask, or a second \JAAulde\IP\V4\Address instance to fit into the derived
27
     *                                                                        block.
28
     *
29
     * @throws Exception
30
     */
31
    public function __construct($a1, $a2 = null)
32
    {
33
        $subnetMask = null;
34
35
        if (is_string($a1)) {
36
            $a1parts = explode('/', $a1, 2);
37
            $a1sub1 = $a1parts[0];
38
            $a1sub2 = isset($a1parts[1]) ? $a1parts[1] : null;
39
40
            $a1 = new Address($a1sub1);
41
42
            if ($a1sub2 !== null) {
43
                $a2 = (int) $a1sub2;
44
            }
45
        }
46
47
        if ($a2 instanceof SubnetMask) {
48
            $subnetMask = $a2;
49
        } else {
50
            if ($a2 instanceof Address) {
51
                $a2 = SubnetMask::calculateCIDRToFit($a1, $a2);
52
            }
53
54
            if (is_string($a2) && count(explode('.', $a2)) === 4) {
55
                $subnetMask = new SubnetMask($a2);
56
            } elseif (is_int($a2) || is_string($a2)) {
57
                $subnetMask = SubnetMask::fromCIDRPrefix((int) preg_replace('/[^\d]/', '', (string) $a2));
58
            }
59
        }
60
61
        if (!($subnetMask instanceof SubnetMask)) {
62
            throw new \Exception(__METHOD__.' could not derive a subnet mask. See documentation for second param, $a2.');
63
        }
64
65
        $this->subnetMask = $subnetMask;
66
67
        parent::__construct(self::calculateNetworkAddress($a1, $subnetMask), self::calculateBroadcastAddress($a1, $subnetMask));
68
    }
69
70
    /**
71
     * Determine if a given IPV4 address is this block's network address (first address in range).
72
     *
73
     * @param \JAAulde\IP\V4\Address $address The address we want to know about
74
     *
75
     * @return bool
76
     */
77
    public function isNetworkAddress(Address $address)
78
    {
79
        return $address->get() === $this->firstAddress->get();
80
    }
81
82
    /**
83
     * Determine if a given IPV4 address is this block's broadcast address (last address in range).
84
     *
85
     * @param \JAAulde\IP\V4\Address $address The address we want to know about
86
     *
87
     * @return bool
88
     */
89
    public function isBroadcastAddress(Address $address)
90
    {
91
        return $address->get() === $this->lastAddress->get();
92
    }
93
94
    /**
95
     * Retrieve the block's network address (first address in range) (Alias to \JAAulde\IP\V4\Range::getFirstAddress).
96
     *
97
     * @uses \JAAulde\IP\V4\Range::getFirstAddress
98
     *
99
     * @return \JAAulde\IP\V4\Address
100
     */
101
    public function getNetworkAddress()
102
    {
103
        return $this->getFirstAddress();
104
    }
105
106
    /**
107
     * Retrieve the block's broadcast address (last address in range). (Alias to \JAAulde\IP\V4\Range::getLastAddress).
108
     *
109
     * @uses \JAAulde\IP\V4\Range::getLastAddress
110
     *
111
     * @return \JAAulde\IP\V4\Address
112
     */
113
    public function getBroadcastAddress()
114
    {
115
        return $this->getLastAddress();
116
    }
117
118
    /**
119
     * Retrieve the block's subnet mask.
120
     *
121
     * @return \JAAulde\IP\V4\SubnetMask
122
     */
123
    public function getSubnetMask()
124
    {
125
        return $this->subnetMask;
126
    }
127
128
    /**
129
     * Retrieve the total number of IPV4 addresses represented in this block.
130
     *
131
     * @return int
132
     */
133
    public function getAddressCount()
134
    {
135
        return pow(2, $this->subnetMask->getHostBitsCount());
136
    }
137
138
    /**
139
     * Retrieve the total number of usable IPV4 addresses represented in this block.
140
     *
141
     * The total number of usable addresses is generally considered to be 2 less than the total number represented by the block.
142
     * This accounts for the fact that the first and last addresses in a block are used for the network and broadcast addresses.
143
     *
144
     * @return int
145
     */
146
    public function getUsableAddressCount()
147
    {
148
        return $this->getAddressCount() - 2;
149
    }
150
151
    /**
152
     * Calculate the network address of a Block given an IPV4 network address and SubnetMask.
153
     *
154
     * @param \JAAulde\IP\V4\Address    $address    The IP address from which a network address will be derived
155
     * @param \JAAulde\IP\V4\SubnetMask $subnetMask The subnet mask (in address form) used in the derivation
156
     *
157
     * @return \JAAulde\IP\V4\Address
158
     */
159
    public static function calculateNetworkAddress(Address $address, SubnetMask $subnetMask)
160
    {
161
        return new Address($address->get() & $subnetMask->get());
162
    }
163
164
    /**
165
     * Calculate the broadcast address of a Block given an IPV4 network address and SubnetMask.
166
     *
167
     * @param \JAAulde\IP\V4\Address    $address    The IP address from which a broadcast address will be derived
168
     * @param \JAAulde\IP\V4\SubnetMask $subnetMask The subnet mask (in address form) used in the derivation
169
     *
170
     * @return \JAAulde\IP\V4\Address
171
     */
172
    public static function calculateBroadcastAddress(Address $address, SubnetMask $subnetMask)
173
    {
174
        return new Address($address->get() | ~$subnetMask->get());
175
    }
176
}
177