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.

InputValidation   B
last analyzed

Complexity

Total Complexity 52

Size/Duplication

Total Lines 273
Duplicated Lines 6.59 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 13
Bugs 0 Features 7
Metric Value
wmc 52
c 13
b 0
f 7
lcom 0
cbo 1
dl 18
loc 273
rs 7.9487

22 Methods

Rating   Name   Duplication   Size   Complexity  
A displayName() 0 10 2
A commonName() 0 8 2
A serverCommonName() 0 8 2
A profileId() 0 8 2
A instanceId() 0 8 2
A languageCode() 0 9 2
A totpSecret() 0 8 2
A yubiKeyOtp() 0 8 2
A totpKey() 0 8 2
A clientId() 0 8 2
A userId() 0 8 2
A dateTime() 0 8 2
A ipAddress() 9 9 2
A ip4() 0 8 2
A ip6() 9 9 2
A connectedAt() 0 8 3
A disconnectedAt() 0 8 3
A bytesTransferred() 0 8 3
A twoFactorType() 0 8 3
A twoFactorValue() 0 8 3
A messageId() 0 8 3
A messageType() 0 8 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like InputValidation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use InputValidation, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Common\Http;
11
12
use DateTime;
13
use SURFnet\VPN\Common\Http\Exception\InputValidationException;
14
15
class InputValidation
16
{
17
    /**
18
     * @return string
19
     */
20
    public static function displayName($displayName)
21
    {
22
        $displayName = filter_var($displayName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
23
24
        if (0 === mb_strlen($displayName)) {
25
            throw new InputValidationException('invalid "display_name"');
26
        }
27
28
        return $displayName;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public static function commonName($commonName)
35
    {
36
        if (1 !== preg_match('/^[a-fA-F0-9]{32}$/', $commonName)) {
37
            throw new InputValidationException('invalid "common_name"');
38
        }
39
40
        return $commonName;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public static function serverCommonName($serverCommonName)
47
    {
48
        if (1 !== preg_match('/^[a-zA-Z0-9-.]+$/', $serverCommonName)) {
49
            throw new InputValidationException('invalid "server_common_name"');
50
        }
51
52
        return $serverCommonName;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public static function profileId($profileId)
59
    {
60
        if (1 !== preg_match('/^[a-zA-Z0-9-.]+$/', $profileId)) {
61
            throw new InputValidationException('invalid "profile_id"');
62
        }
63
64
        return $profileId;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public static function instanceId($instanceId)
71
    {
72
        if (1 !== preg_match('/^[a-zA-Z0-9-.]+$/', $instanceId)) {
73
            throw new InputValidationException('invalid "instance_id"');
74
        }
75
76
        return $instanceId;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public static function languageCode($languageCode)
83
    {
84
        $supportedLanguages = ['en_US', 'nl_NL', 'de_DE', 'fr_FR'];
85
        if (!in_array($languageCode, $supportedLanguages)) {
86
            throw new InputValidationException('invalid "language_code"');
87
        }
88
89
        return $languageCode;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public static function totpSecret($totpSecret)
96
    {
97
        if (1 !== preg_match('/^[A-Z0-9]{16}$/', $totpSecret)) {
98
            throw new InputValidationException('invalid "totp_secret"');
99
        }
100
101
        return $totpSecret;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public static function yubiKeyOtp($yubiKeyOtp)
108
    {
109
        if (1 !== preg_match('/^[a-z]{44}$/', $yubiKeyOtp)) {
110
            throw new InputValidationException('invalid "yubi_key_otp"');
111
        }
112
113
        return $yubiKeyOtp;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public static function totpKey($totpKey)
120
    {
121
        if (1 !== preg_match('/^[0-9]{6}$/', $totpKey)) {
122
            throw new InputValidationException('invalid "totp_key"');
123
        }
124
125
        return $totpKey;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public static function clientId($clientId)
132
    {
133
        if (1 !== preg_match('/^(?:[\x20-\x7E])+$/', $clientId)) {
134
            throw new InputValidationException('invalid "client_id"');
135
        }
136
137
        return $clientId;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public static function userId($userId)
144
    {
145
        if (1 !== preg_match('/^[a-zA-Z0-9-_.|@]+$/', $userId)) {
146
            throw new InputValidationException('invalid "user_id"');
147
        }
148
149
        return $userId;
150
    }
151
152
    /**
153
     * @param string $dateTime
154
     *
155
     * @return DateTime
156
     */
157
    public static function dateTime($dateTime)
158
    {
159
        if (false === $dateTimeObj = DateTime::createFromFormat('Y-m-d H:i:s', $dateTime)) {
160
            throw new InputValidationException('invalid "date_time"');
161
        }
162
163
        return $dateTimeObj;
164
    }
165
166
    /**
167
     * @return string
168
     */
169 View Code Duplication
    public static function ipAddress($ipAddress)
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...
170
    {
171
        if (false === filter_var($ipAddress, FILTER_VALIDATE_IP)) {
172
            throw new InputValidationException('invalid "ip_address"');
173
        }
174
175
        // normalize the IP address (only makes a difference for IPv6)
176
        return inet_ntop(inet_pton($ipAddress));
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public static function ip4($ip4)
183
    {
184
        if (false === filter_var($ip4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
185
            throw new InputValidationException('invalid "ip4"');
186
        }
187
188
        return $ip4;
189
    }
190
191
    /**
192
     * @return string
193
     */
194 View Code Duplication
    public static function ip6($ip6)
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...
195
    {
196
        if (false === filter_var($ip6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
197
            throw new InputValidationException('invalid "ip6"');
198
        }
199
200
        // normalize the IPv6 address
201
        return inet_ntop(inet_pton($ip6));
202
    }
203
204
    /**
205
     * @return int
206
     */
207
    public static function connectedAt($connectedAt)
208
    {
209
        if (!is_numeric($connectedAt) || 0 > intval($connectedAt)) {
210
            throw new InputValidationException('invalid "connected_at"');
211
        }
212
213
        return intval($connectedAt);
214
    }
215
216
    /**
217
     * @return int
218
     */
219
    public static function disconnectedAt($disconnectedAt)
220
    {
221
        if (!is_numeric($disconnectedAt) || 0 > intval($disconnectedAt)) {
222
            throw new InputValidationException('invalid "disconnected_at"');
223
        }
224
225
        return intval($disconnectedAt);
226
    }
227
228
    /**
229
     * @return int
230
     */
231
    public static function bytesTransferred($bytesTransferred)
232
    {
233
        if (!is_numeric($bytesTransferred) || 0 > intval($bytesTransferred)) {
234
            throw new InputValidationException('invalid "bytes_transferred"');
235
        }
236
237
        return intval($bytesTransferred);
238
    }
239
240
    /**
241
     * @return string
242
     */
243
    public static function twoFactorType($twoFactorType)
244
    {
245
        if ('totp' !== $twoFactorType && 'yubi' !== $twoFactorType) {
246
            throw new InputValidationException('invalid "two_factor_type"');
247
        }
248
249
        return $twoFactorType;
250
    }
251
252
    /**
253
     * @return string
254
     */
255
    public static function twoFactorValue($twoFactorValue)
256
    {
257
        if (!is_string($twoFactorValue) || 0 >= strlen($twoFactorValue)) {
258
            throw new InputValidationException('invalid "two_factor_value"');
259
        }
260
261
        return $twoFactorValue;
262
    }
263
264
    /**
265
     * @return int
266
     */
267
    public static function messageId($messageId)
268
    {
269
        if (!is_numeric($messageId) || 0 >= $messageId) {
270
            throw new InputValidationException('invalid "message_id"');
271
        }
272
273
        return (int) $messageId;
274
    }
275
276
    /**
277
     * @return string
278
     */
279
    public static function messageType($messageType)
280
    {
281
        if ('motd' !== $messageType && 'notification' !== $messageType && 'maintenance' !== $messageType) {
282
            throw new InputValidationException('invalid "message_type"');
283
        }
284
285
        return $messageType;
286
    }
287
}
288