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 ( bca83a...1ac405 )
by François
03:36
created

IP   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 89
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 5
A getRange() 0 4 1
A getNetmask() 0 4 1
A getNetwork() 0 4 1
A getFirstHost() 0 4 1
A getLastHost() 0 4 1
A getBroadcast() 0 6 1
A inRange() 0 14 4
A validateIP() 0 6 2
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
namespace fkooman\VPN\Server\Config;
18
19
use InvalidArgumentException;
20
21
class IP
22
{
23
    /** @var string */
24
    private $ip;
25
26
    /** @var int */
27
    private $prefix;
28
29
    public function __construct($cidrIp)
30
    {
31
        // must be of form IP/PREFIX
32
        if (1 !==  substr_count($cidrIp, '/')) {
33
            throw new InvalidArgumentException('not in CIDR format');
34
        }
35
        list($ip, $prefix) = explode('/', $cidrIp);
36
37
        // check IP
38
        self::validateIP($ip);
39
        $this->ip = $ip;
40
41
        // check prefix
42
        if (!is_numeric($prefix) || 0 > $prefix || 32 < $prefix) {
43
            throw new InvalidArgumentException('invalid prefix');
44
        }
45
46
        $this->prefix = intval($prefix);
47
    }
48
49
    public function getRange()
50
    {
51
        return sprintf('%s/%d', $this->ip, $this->prefix);
52
    }
53
54
    public function getNetmask()
55
    {
56
        return long2ip(-1 << (32 - $this->prefix));
57
    }
58
59
    public function getNetwork()
60
    {
61
        return long2ip(ip2long($this->ip) & ip2long($this->getNetmask()));
62
    }
63
64
    public function getFirstHost()
65
    {
66
        return long2ip(ip2long($this->getNetwork()) + 1);
67
    }
68
69
    public function getLastHost()
70
    {
71
        return long2ip(ip2long($this->getBroadcast()) + -1);
72
    }
73
74
    public function getBroadcast()
75
    {
76
        return long2ip(
77
            ip2long($this->getNetwork()) | ~ip2long($this->getNetmask())
78
        );
79
    }
80
81
    /**
82
     * Check if a given IP address is in the range of the network.
83
     *
84
     * @param string $ip                      the IP address to check
85
     * @param bool   $includeNetworkBroadcast whether or not to consider the
86
     *                                        network and broadcast address of the network also part of the range
87
     */
88
    public function inRange($ip, $includeNetworkBroadcast = false)
89
    {
90
        self::validateIP($ip);
91
92
        $longIp = ip2long($ip);
93
        $startIp = ip2long($this->getNetwork());
94
        $endIp = ip2long($this->getBroadcast());
95
96
        if ($includeNetworkBroadcast) {
97
            return $longIp >= $startIp && $longIp <= $endIp;
98
        }
99
100
        return $longIp > $startIp && $longIp < $endIp;
101
    }
102
103
    private static function validateIP($ip)
104
    {
105
        if (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
106
            throw new InvalidArgumentException('invalid IP address');
107
        }
108
    }
109
}
110