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 ( 84c7ac...92bb5d )
by François
02:04
created

InputValidation   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 273
Duplicated Lines 6.59 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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