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 ( a4ad96...a25367 )
by François
02:17
created

IPv4::splitRange()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 17
nc 7
nop 1
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 getRange()
51
    {
52
        return sprintf('%s/%d', $this->ip, $this->prefix);
53
    }
54
55
    public function getNetmask()
56
    {
57
        return long2ip(-1 << (32 - $this->prefix));
58
    }
59
60
    public function getNetwork()
61
    {
62
        return long2ip(ip2long($this->ip) & ip2long($this->getNetmask()));
63
    }
64
65
    public function getFirstHost()
66
    {
67
        return long2ip(ip2long($this->getNetwork()) + 1);
68
    }
69
70
    public function getLastHost()
71
    {
72
        return long2ip(ip2long($this->getBroadcast()) + -1);
73
    }
74
75
    public function getBroadcast()
76
    {
77
        return long2ip(
78
            ip2long($this->getNetwork()) | ~ip2long($this->getNetmask())
79
        );
80
    }
81
82
    public function getNumberOfHosts()
83
    {
84
        return pow(2, 32 - $this->prefix) - 2;
85
    }
86
87
    /**
88
     * Check if a given IP address is in the range of the network.
89
     *
90
     * @param string $ip                      the IP address to check
91
     * @param bool   $includeNetworkBroadcast whether or not to consider the
92
     *                                        network and broadcast address of the network also part of the range
93
     */
94
    public function inRange($ip, $includeNetworkBroadcast = false)
95
    {
96
        self::validateIP($ip);
97
98
        $longIp = ip2long($ip);
99
        $startIp = ip2long($this->getNetwork());
100
        $endIp = ip2long($this->getBroadcast());
101
102
        if ($includeNetworkBroadcast) {
103
            return $longIp >= $startIp && $longIp <= $endIp;
104
        }
105
106
        return $longIp > $startIp && $longIp < $endIp;
107
    }
108
109
    /**
110
     * Split the provided range in $no equal sized CIDRs.
111
     */
112
    public function splitRange($no)
113
    {
114
        if (1 === $no) {
115
            $prefixNo = 1;
116
        } elseif (2 === $no) {
117
            $prefixNo = 2;
118
        } elseif (3 === $no || 4 === $no) {
119
            $prefixNo = 4;
120
        } else {
121
            throw new InvalidArgumentException('too many instances, only 1,2,3 or 4 allowed');
122
        }
123
124
        $prefix = $this->prefix + floor($prefixNo / 2);
125
        $ranges = [];
126
        for ($i = 0; $i < $no; ++$i) {
127
            $noHosts = pow(2, 32 - $prefix);
128
            $networkAddress = long2ip($i * $noHosts + ip2long($this->ip));
129
            $ip = new self($networkAddress.'/'.$prefix);
130
            $ranges[] = $ip->getRange();
131
        }
132
133
        return $ranges;
134
    }
135
136
    private static function validateIP($ip)
137
    {
138
        if (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
139
            throw new InvalidArgumentException('invalid IP address');
140
        }
141
    }
142
}
143