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 ( d087bb...0f5b77 )
by François
02:30
created

IPv4::getBroadcast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright 2015 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace fkooman\VPN\Server;
19
20
use InvalidArgumentException;
21
22
class IPv4
23
{
24
    /** @var string */
25
    private $ip;
26
27
    /** @var int */
28
    private $prefix;
29
30 View Code Duplication
    public function __construct($cidrIp)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        // must be of form IP/PREFIX
33
        if (1 !== substr_count($cidrIp, '/')) {
34
            throw new InvalidArgumentException('not in CIDR format');
35
        }
36
        list($ip, $prefix) = explode('/', $cidrIp);
37
38
        // check IP
39
        self::validateIP($ip);
40
        $this->ip = $ip;
41
42
        // check prefix
43
        if (!is_numeric($prefix) || 0 > $prefix || 32 < $prefix) {
44
            throw new InvalidArgumentException('invalid prefix, must be >0 and <32');
45
        }
46
47
        $this->prefix = intval($prefix);
48
    }
49
50
    public function getFamily()
51
    {
52
        return 4;
53
    }
54
55
    public function getRange()
56
    {
57
        return sprintf('%s/%d', $this->ip, $this->prefix);
58
    }
59
60
    public function getPrefix()
61
    {
62
        return $this->prefix;
63
    }
64
65
    public function getNetmask()
66
    {
67
        return long2ip(-1 << (32 - $this->prefix));
68
    }
69
70
    public function getNetwork()
71
    {
72
        return long2ip(ip2long($this->ip) & ip2long($this->getNetmask()));
73
    }
74
75
    public function getFirstHost()
76
    {
77
        return long2ip(ip2long($this->getNetwork()) + 1);
78
    }
79
80
    public function getLastHost()
81
    {
82
        return long2ip(ip2long($this->getBroadcast()) + -1);
83
    }
84
85
    public function getBroadcast()
86
    {
87
        return long2ip(
88
            ip2long($this->getNetwork()) | ~ip2long($this->getNetmask())
89
        );
90
    }
91
92
    public function getNumberOfHosts()
93
    {
94
        return pow(2, 32 - $this->prefix) - 2;
95
    }
96
97
    /**
98
     * Check if a given IP address is in the range of the network.
99
     *
100
     * @param string $ip                      the IP address to check
101
     * @param bool   $includeNetworkBroadcast whether or not to consider the
102
     *                                        network and broadcast address of the network also part of the range
103
     */
104
    public function inRange($ip, $includeNetworkBroadcast = false)
105
    {
106
        self::validateIP($ip);
107
108
        $longIp = ip2long($ip);
109
        $startIp = ip2long($this->getNetwork());
110
        $endIp = ip2long($this->getBroadcast());
111
112
        if ($includeNetworkBroadcast) {
113
            return $longIp >= $startIp && $longIp <= $endIp;
114
        }
115
116
        return $longIp > $startIp && $longIp < $endIp;
117
    }
118
119
    /**
120
     * Split the provided range in $no equal sized CIDRs.
121
     */
122
    public function splitRange($no)
123
    {
124
        if (1 === $no) {
125
            $prefixNo = 1;
126
        } elseif (2 === $no) {
127
            $prefixNo = 2;
128
        } elseif (3 === $no || 4 === $no) {
129
            $prefixNo = 4;
130
        } else {
131
            throw new InvalidArgumentException('too many instances, only 1,2,3 or 4 allowed');
132
        }
133
134
        $prefix = $this->prefix + floor($prefixNo / 2);
135
        $ranges = [];
136
        for ($i = 0; $i < $no; ++$i) {
137
            $noHosts = pow(2, 32 - $prefix);
138
            $networkAddress = long2ip($i * $noHosts + ip2long($this->ip));
139
            $ip = new self($networkAddress.'/'.$prefix);
140
            $ranges[] = $ip->getRange();
141
        }
142
143
        return $ranges;
144
    }
145
146
    private static function validateIP($ip)
147
    {
148
        if (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
149
            throw new InvalidArgumentException('invalid IP address');
150
        }
151
    }
152
}
153