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 ( 7dad53...5b266b )
by François
03:26
created

IPv4::getFirstHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\Config;
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
    /**
83
     * Check if a given IP address is in the range of the network.
84
     *
85
     * @param string $ip                      the IP address to check
86
     * @param bool   $includeNetworkBroadcast whether or not to consider the
87
     *                                        network and broadcast address of the network also part of the range
88
     */
89
    public function inRange($ip, $includeNetworkBroadcast = false)
90
    {
91
        self::validateIP($ip);
92
93
        $longIp = ip2long($ip);
94
        $startIp = ip2long($this->getNetwork());
95
        $endIp = ip2long($this->getBroadcast());
96
97
        if ($includeNetworkBroadcast) {
98
            return $longIp >= $startIp && $longIp <= $endIp;
99
        }
100
101
        return $longIp > $startIp && $longIp < $endIp;
102
    }
103
104
    /**
105
     * Split the provided range in $no equal sized CIDRs.
106
     */
107
    public function splitRange($no)
108
    {
109
        if (1 === $no) {
110
            $prefixNo = 1;
111
        } elseif (2 === $no) {
112
            $prefixNo = 2;
113
        } elseif (3 === $no || 4 === $no) {
114
            $prefixNo = 4;
115
        } else {
116
            throw new InvalidArgumentException('too many instances, only 1,2,3 or 4 allowed');
117
        }
118
119
        $prefix = $this->prefix + floor($prefixNo / 2);
120
        $ranges = [];
121
        for ($i = 0; $i < $no; ++$i) {
122
            $noHosts = pow(2, 32 - $prefix);
123
            $networkAddress = long2ip($i * $noHosts + ip2long($this->ip));
124
            $ip = new self($networkAddress.'/'.$prefix);
125
            $ranges[] = $ip->getRange();
126
        }
127
128
        return $ranges;
129
    }
130
131
    private static function validateIP($ip)
132
    {
133
        if (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
134
            throw new InvalidArgumentException('invalid IP address');
135
        }
136
    }
137
}
138