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

IPv6::splitRange()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 2
eloc 16
nc 2
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 IPv6
23
{
24
    /** @var string */
25
    private $ip;
26
27
    /** @var int */
28
    private $prefix;
29
30 View Code Duplication
    public function __construct($prefixIp)
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($prefixIp, '/')) {
34
            throw new InvalidArgumentException('not a prefix');
35
        }
36
        list($ip, $prefix) = explode('/', $prefixIp);
37
38
        // check IP
39
        self::validateIP($ip);
40
        $this->ip = inet_ntop(inet_pton($ip));
41
42
        // check prefix
43
        if (!is_numeric($prefix) || 0 > $prefix || 64 < $prefix) {
44
            throw new InvalidArgumentException('invalid prefix, must be <= /64');
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
    /**
56
     * Split the provided range in $no /64s.
57
     */
58
    public function splitRange($no)
59
    {
60
        // XXX must be at least /60
61
62
        $bitIp = inet_pton($this->ip);
63
        $hexIp = bin2hex($bitIp);
64
        $splitIp = str_split($hexIp, 4);
65
66
        $ranges = [];
67
68
        for ($i = 0; $i < $no; ++$i) {
69
            // the last 4 fields become 0, the last digit of field 3 becomes 0, and 1, ...
70
71
            $splitIp[3] = dechex(
72
                (
73
                    hexdec($splitIp[3]) & 0xfff0
74
                ) + $i
75
            );
76
77
            $splitIp[4] = 0;
78
            $splitIp[5] = 0;
79
            $splitIp[6] = 0;
80
            $splitIp[7] = 0;
81
            $rangeIp = inet_ntop(inet_pton(implode($splitIp, ':')));
82
83
            $ranges[] = $rangeIp.'/64';
84
        }
85
86
        return $ranges;
87
    }
88
89
    private static function validateIP($ip)
90
    {
91
        if (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
92
            throw new InvalidArgumentException('invalid IP address');
93
        }
94
    }
95
}
96