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 ( 0f5b77...f26127 )
by François
05:01
created

Pool::setRange6()   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 1
1
<?php
2
/**
3
 * Copyright 2016 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 RuntimeException;
21
22
class Pool
23
{
24
    /** @var int */
25
    private $id;
26
27
    /** @var string */
28
    private $poolName;
29
30
    /** @var string */
31
    private $hostName;
32
33
    /** @var bool */
34
    private $defaultGateway;
35
36
    /** @var IP */
37
    private $range;
38
39
    /** @var IP */
40
    private $range6;
41
42
    /** @var array */
43
    private $routes;
44
45
    /** @var array */
46
    private $dns;
47
48
    /** @var bool */
49
    private $twoFactor;
50
51
    /** @var bool */
52
    private $clientToClient;
53
54
    /** @var string */
55
    private $listen;
56
57
    /** @var array */
58
    private $instances;
59
60
    public function __construct($poolNumber, array $poolData)
61
    {
62
        $this->setId(self::validate($poolData, 'id'));
63
        $this->setName(self::validate($poolData, 'name', false, $this->getId()));
0 ignored issues
show
Documentation introduced by
$this->getId() is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
        $this->setHostName(self::validate($poolData, 'hostName'));
65
        $this->setDefaultGateway(self::validate($poolData, 'defaultGateway', false, false));
66
        $this->setRange(new IP(self::validate($poolData, 'range')));
67
        $this->setRange6(new IP(self::validate($poolData, 'range6')));
68
        $this->setRoutes(self::validate($poolData, 'routes', false, []));
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
        $this->setDns(self::validate($poolData, 'dns', false, []));
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
        $this->setTwoFactor(self::validate($poolData, 'twoFactor', false, false));
71
        $this->setClientToClient(self::validate($poolData, 'clientToClient', false, false));
72
        $this->setManagementIp(new IP(sprintf('127.42.%d.1', $poolNumber)));
73
        $this->setListen(new IP(self::validate($poolData, 'listen', false, '::')));
0 ignored issues
show
Documentation introduced by
'::' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
75
        $this->populateInstances();
76
    }
77
78
    public function setId($id)
79
    {
80
        // XXX validate
81
        $this->id = $id;
82
    }
83
84
    public function getId()
85
    {
86
        return $this->id;
87
    }
88
89
    public function setName($poolName)
90
    {
91
        // XXX validate
92
        $this->poolName = $poolName;
93
    }
94
95
    public function getName()
96
    {
97
        return $this->poolName;
98
    }
99
100
    public function setHostName($hostName)
101
    {
102
        // XXX validate
103
        $this->hostName = $hostName;
104
    }
105
106
    public function getHostName()
107
    {
108
        return $this->hostName;
109
    }
110
111
    public function setDefaultGateway($defaultGateway)
112
    {
113
        $this->defaultGateway = (bool) $defaultGateway;
114
    }
115
116
    public function getDefaultGateway()
117
    {
118
        return $this->defaultGateway;
119
    }
120
121
    public function setRange(IP $range)
122
    {
123
        $this->range = $range;
124
    }
125
126
    public function getRange()
127
    {
128
        return $this->range;
129
    }
130
131
    public function setRange6(IP $range6)
132
    {
133
        $this->range6 = $range6;
134
    }
135
136
    public function getRange6()
137
    {
138
        return $this->range6;
139
    }
140
141
    public function setRoutes(array $routes)
142
    {
143
        $this->routes = [];
144
145
        foreach ($routes as $route) {
146
            $this->routes[] = new IP($route);
147
        }
148
    }
149
150
    public function getRoutes()
151
    {
152
        return $this->routes;
153
    }
154
155
    public function setDns(array $dns)
156
    {
157
        $this->dns = [];
158
159
        foreach ($dns as $server) {
160
            $this->dns[] = new IP($server);
161
        }
162
    }
163
164
    public function getDns()
165
    {
166
        return $this->dns;
167
    }
168
169
    public function setTwoFactor($twoFactor)
170
    {
171
        $this->twoFactor = (bool) $twoFactor;
172
    }
173
174
    public function getTwoFactor()
175
    {
176
        return $this->twoFactor;
177
    }
178
179
    public function setClientToClient($clientToClient)
180
    {
181
        $this->clientToClient = (bool) $clientToClient;
182
    }
183
184
    public function getClientToClient()
185
    {
186
        return $this->clientToClient;
187
    }
188
189
    public function setManagementIp(IP $managementIp)
190
    {
191
        $this->managementIp = $managementIp;
0 ignored issues
show
Bug introduced by
The property managementIp does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
192
    }
193
194
    public function getManagementIp()
195
    {
196
        return $this->managementIp;
197
    }
198
199
    public function setListen(IP $listen)
200
    {
201
        $this->listen = $listen;
0 ignored issues
show
Documentation Bug introduced by
It seems like $listen of type object<fkooman\VPN\Server\IP> is incompatible with the declared type string of property $listen.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
202
    }
203
204
    public function getListen()
205
    {
206
        return $this->listen;
207
    }
208
209
    public function getInstances()
210
    {
211
        return $this->instances;
212
    }
213
214
    private function populateInstances()
215
    {
216
        $instanceCount = self::getNetCount($this->getRange()->getPrefix());
217
        $splitRange = $this->getRange()->split($instanceCount);
218
        $splitRange6 = $this->getRange6()->split($instanceCount);
219
220
        for ($i = 0; $i < $instanceCount; ++$i) {
221
            // protocol is udp unless it is the last instance when there is
222
            // not just one instance
223
            if (1 !== $instanceCount && $i !== $instanceCount - 1) {
224
                $proto = 'udp';
225
                $port = 1194 + $i;
226
            } else {
227
                $proto = 'tcp';
228
                $port = 1194;
229
            }
230
231
            $this->instances[] = new Instance(
232
                [
233
                    'range' => $splitRange[$i],
234
                    'range6' => $splitRange6[$i],
235
                    'dev' => sprintf('tun-%s-%d', $this->getId(), $i),
236
                    'proto' => $proto,
237
                    'port' => $port,
238
                    'managementPort' => 11940 + $i,
239
                ]
240
            );
241
        }
242
    }
243
244
    /**
245
     * Depending on the prefix we will divide it in a number of nets to 
246
     * balance the load over the instances, it is recommended to use a least
247
     * a /24.
248
     * 
249
     * A /24 or 'bigger' will be split in 4 networks, everything 'smaller'  
250
     * will be either be split in 2 networks or remain 1 network.
251
     */
252
    private static function getNetCount($prefix)
253
    {
254
        switch ($prefix) {
255
            case 32:    // 1 IP   
256
            case 31:    // 2 IPs
257
                throw new RuntimeException('not enough available IPs in range');
258
            case 30:    // 4 IPs
259
                return 1;
260
            case 29:    // 8 IPs
261
            case 28:    // 16 IPs
262
            case 27:    // 32 IPs
263
            case 26:    // 64 IPs
264
            case 25:    // 128 IPs
265
                return 2;
266
        }
267
268
        return 4;
269
    }
270
271
    public function toArray()
272
    {
273
        $routesList = [];
274
        foreach ($this->getRoutes() as $route) {
275
            $routesList[] = $route->getAddressPrefix();
276
        }
277
278
        $dnsList = [];
279
        foreach ($this->getDns() as $dns) {
280
            $dnsList[] = $dns->getAddress();
281
        }
282
283
        $instancesList = [];
284
        foreach ($this->getInstances() as $instance) {
285
            $instancesList[] = $instance->toArray();
286
        }
287
288
        return [
289
            'clientToClient' => $this->getClientToClient(),
290
            'defaultGateway' => $this->getDefaultGateway(),
291
            'dns' => $dnsList,
292
            'hostName' => $this->getHostName(),
293
            'id' => $this->getId(),
294
            'instances' => $instancesList,
295
            'listen' => $this->getListen()->getAddress(),
0 ignored issues
show
Bug introduced by
The method getAddress cannot be called on $this->getListen() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
296
            'managementIp' => $this->getManagementIp()->getAddress(),
297
            'name' => $this->getName(),
298
            'range' => $this->getRange()->getAddressPrefix(),
299
            'range6' => $this->getRange6()->getAddressPrefix(),
300
            'routes' => $routesList,
301
            'twoFactor' => $this->getTwoFactor(),
302
        ];
303
    }
304
305
    private static function validate(array $configData, $configName, $requiredField = true, $defaultValue = false)
306
    {
307
        if (!array_key_exists($configName, $configData)) {
308
            if ($requiredField) {
309
                throw new RuntimeException(sprintf('missing configuration field "%s"', $configName));
310
            }
311
312
            return $defaultValue;
313
        }
314
315
        return $configData[$configName];
316
    }
317
}
318