Passed
Pull Request — master (#320)
by Dmitriy
02:52
created

Ip::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 173
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 173
ccs 7
cts 8
cp 0.875
rs 10
cc 4
nc 5
nop 17
crap 4.0312

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use RuntimeException;
10
use Yiisoft\NetworkUtilities\IpHelper;
11
use Yiisoft\Validator\BeforeValidationInterface;
12
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
13
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
14
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
15
use Yiisoft\Validator\SerializableRuleInterface;
16
use Yiisoft\Validator\SkipOnEmptyInterface;
17
use Yiisoft\Validator\ValidationContext;
18
19
use function array_key_exists;
20
use function strlen;
21
22
/**
23
 * Checks if the value is a valid IPv4/IPv6 address or subnet.
24
 *
25
 * It also may change the value if normalization of IPv6 expansion is enabled.
26
 */
27
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
28
final class Ip implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
29
{
30
    use BeforeValidationTrait;
31
    use RuleNameTrait;
32
    use SkipOnEmptyTrait;
33
34
    /**
35
     * Negation char.
36
     *
37
     * Used to negate {@see $ranges} or {@see $network} or to negate validating value when {@see $allowNegation}
38
     * is used.
39
     */
40
    private const NEGATION_CHAR = '!';
41
    /**
42
     * @see $networks
43
     */
44
    private array $defaultNetworks = [
45
        '*' => ['any'],
46
        'any' => ['0.0.0.0/0', '::/0'],
47
        'private' => ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'fd00::/8'],
48
        'multicast' => ['224.0.0.0/4', 'ff00::/8'],
49
        'linklocal' => ['169.254.0.0/16', 'fe80::/10'],
50
        'localhost' => ['127.0.0.0/8', '::1'],
51
        'documentation' => ['192.0.2.0/24', '198.51.100.0/24', '203.0.113.0/24', '2001:db8::/32'],
52
        'system' => ['multicast', 'linklocal', 'localhost', 'documentation'],
53
    ];
54
55 8
    public function __construct(
56
        /**
57
         * @var array Custom network aliases, that can be used in {@see $ranges}.
58
         *
59
         *  - key - alias name
60
         *  - value - array of strings. String can be an IP range, IP address or another alias. String can be
61
         *    negated with {@see NEGATION_CHAR} (independent of {@see $allowNegation} option).
62
         *
63
         * The following aliases are defined by default in {@see $defaultNetworks} and will be merged with custom ones:
64
         *
65
         *  - `*`: `any`
66
         *  - `any`: `0.0.0.0/0, ::/0`
67
         *  - `private`: `10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8`
68
         *  - `multicast`: `224.0.0.0/4, ff00::/8`
69
         *  - `linklocal`: `169.254.0.0/16, fe80::/10`
70
         *  - `localhost`: `127.0.0.0/8', ::1`
71
         *  - `documentation`: `192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32`
72
         *  - `system`: `multicast, linklocal, localhost, documentation`
73
         *
74
         * @see $defaultNetworks
75
         */
76
        private array $networks = [],
77
        /**
78
         * @var bool whether the validating value can be an IPv4 address. Defaults to `true`.
79
         */
80
        private bool $allowIpv4 = true,
81
        /**
82
         * @var bool whether the validating value can be an IPv6 address. Defaults to `true`.
83
         */
84
        private bool $allowIpv6 = true,
85
        /**
86
         * @var bool whether the address can be an IP with CIDR subnet, like `192.168.10.0/24`.
87
         * The following values are possible:
88
         *
89
         * - `false` - the address must not have a subnet (default).
90
         * - `true` - specifying a subnet is optional.
91
         */
92
        private bool $allowSubnet = false,
93
        private bool $requireSubnet = false,
94
        /**
95
         * @var bool whether address may have a {@see NEGATION_CHAR} character at the beginning.
96
         * Defaults to `false`.
97
         */
98
        private bool $allowNegation = false,
99
        /**
100
         * @var string user-defined error message is used when validation fails due to the wrong IP address format.
101
         *
102
         * You may use the following placeholders in the message:
103
         *
104
         * - `{attribute}`: the label of the attribute being validated
105
         * - `{value}`: the value of the attribute being validated
106
         */
107
        private string $message = 'Must be a valid IP address.',
108
        /**
109
         * @var string user-defined error message is used when validation fails due to the disabled IPv4 validation.
110
         *
111
         * You may use the following placeholders in the message:
112
         *
113
         * - `{attribute}`: the label of the attribute being validated
114
         * - `{value}`: the value of the attribute being validated
115
         *
116
         * @see $allowIpv4
117
         */
118
        private string $ipv4NotAllowedMessage = 'Must not be an IPv4 address.',
119
        /**
120
         * @var string user-defined error message is used when validation fails due to the disabled IPv6 validation.
121
         *
122
         * You may use the following placeholders in the message:
123
         *
124
         * - `{attribute}`: the label of the attribute being validated
125
         * - `{value}`: the value of the attribute being validated
126
         *
127
         * @see $allowIpv6
128
         */
129
        private string $ipv6NotAllowedMessage = 'Must not be an IPv6 address.',
130
        /**
131
         * @var string user-defined error message is used when validation fails due to the wrong CIDR.
132
         *
133
         * You may use the following placeholders in the message:
134
         *
135
         * - `{attribute}`: the label of the attribute being validated
136
         * - `{value}`: the value of the attribute being validated
137
         *
138
         * @see $allowSubnet
139
         */
140
        private string $wrongCidrMessage = 'Contains wrong subnet mask.',
141
        /**
142
         * @var string user-defined error message is used when validation fails due to subnet {@see $allowSubnet} is
143
         * used, but the CIDR prefix is not set.
144
         *
145
         * You may use the following placeholders in the message:
146
         *
147
         * - `{attribute}`: the label of the attribute being validated
148
         * - `{value}`: the value of the attribute being validated
149
         *
150
         * @see $allowSubnet
151
         */
152
        private string $noSubnetMessage = 'Must be an IP address with specified subnet.',
153
        /**
154
         * @var string user-defined error message is used when validation fails
155
         * due to {@see $allowSubnet} is false, but CIDR prefix is present.
156
         *
157
         * You may use the following placeholders in the message:
158
         *
159
         * - `{attribute}`: the label of the attribute being validated
160
         * - `{value}`: the value of the attribute being validated
161
         *
162
         * @see $allowSubnet
163
         */
164
        private string $hasSubnetMessage = 'Must not be a subnet.',
165
        /**
166
         * @var string user-defined error message is used when validation fails due to IP address
167
         * is not allowed by {@see $ranges} check.
168
         *
169
         * You may use the following placeholders in the message:
170
         *
171
         * - `{attribute}`: the label of the attribute being validated
172
         * - `{value}`: the value of the attribute being validated
173
         *
174
         * @see $ranges
175
         */
176
        private string $notInRangeMessage = 'Is not in the allowed range.',
177
        /**
178
         * @var string[] The IPv4 or IPv6 ranges that are allowed or forbidden.
179
         *
180
         * The following preparation tasks are performed:
181
         *
182
         * - Recursively substitutes aliases (described in {@see $networks}) with their values.
183
         * - Removes duplicates.
184
         *
185
         * When the array is empty, or the option not set, all IP addresses are allowed.
186
         *
187
         * Otherwise, the rules are checked sequentially until the first match is found.
188
         * An IP address is forbidden, when it has not matched any of the rules.
189
         *
190
         * Example:
191
         *
192
         * ```php
193
         * (new Ip(ranges: [
194
         *     '192.168.10.128'
195
         *     '!192.168.10.0/24',
196
         *     'any' // allows any other IP addresses
197
         * ]);
198
         * ```
199
         *
200
         * In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the `192.168.10.0/24`
201
         * subnet. IPv4 address `192.168.10.128` is also allowed, because it is listed before the restriction.
202
         */
203
        private array $ranges = [],
204
205
        /**
206
         * @var bool|callable|null
207
         */
208
        private $skipOnEmpty = null,
209
        private bool $skipOnError = false,
210
        /**
211
         * @var Closure(mixed, ValidationContext):bool|null
212
         */
213
        private ?Closure $when = null,
214
    ) {
215 8
        foreach ($networks as $key => $_values) {
216 1
            if (array_key_exists($key, $this->defaultNetworks)) {
217 1
                throw new RuntimeException("Network alias \"{$key}\" already set as default.");
218
            }
219
        }
220
221 7
        $this->networks = array_merge($this->defaultNetworks, $this->networks);
222
223 7
        if ($requireSubnet) {
224
            $this->allowSubnet = true;
225
        }
226
227 7
        $this->ranges = $this->prepareRanges($ranges);
228
    }
229
230
    public function getNetworks(): array
231
    {
232
        return $this->networks;
233
    }
234
235 117
    public function isAllowIpv4(): bool
236
    {
237 117
        return $this->allowIpv4;
238
    }
239
240 35
    public function isAllowIpv6(): bool
241
    {
242 35
        return $this->allowIpv6;
243
    }
244
245 34
    public function isAllowSubnet(): bool
246
    {
247 34
        return $this->allowSubnet;
248
    }
249
250 52
    public function isRequireSubnet(): bool
251
    {
252 52
        return $this->requireSubnet;
253
    }
254
255 8
    public function isAllowNegation(): bool
256
    {
257 8
        return $this->allowNegation;
258
    }
259
260 34
    public function getMessage(): string
261
    {
262 34
        return $this->message;
263
    }
264
265 2
    public function getIpv4NotAllowedMessage(): string
266
    {
267 2
        return $this->ipv4NotAllowedMessage;
268
    }
269
270 1
    public function getIpv6NotAllowedMessage(): string
271
    {
272 1
        return $this->ipv6NotAllowedMessage;
273
    }
274
275 2
    public function getWrongCidrMessage(): string
276
    {
277 2
        return $this->wrongCidrMessage;
278
    }
279
280 4
    public function getNoSubnetMessage(): string
281
    {
282 4
        return $this->noSubnetMessage;
283
    }
284
285 4
    public function getHasSubnetMessage(): string
286
    {
287 4
        return $this->hasSubnetMessage;
288
    }
289
290 16
    public function getNotInRangeMessage(): string
291
    {
292 16
        return $this->notInRangeMessage;
293
    }
294
295 4
    public function getRanges(): array
296
    {
297 4
        return $this->ranges;
298
    }
299
300
    /**
301
     * Parses IP address/range for the negation with {@see NEGATION_CHAR}.
302
     *
303
     * @param $string
304
     *
305
     * @return array `[0 => bool, 1 => string]`
306
     *  - boolean: whether the string is negated
307
     *  - string: the string without negation (when the negation were present)
308
     */
309 42
    private function parseNegatedRange($string): array
310
    {
311 42
        $isNegated = str_starts_with($string, self::NEGATION_CHAR);
312 42
        return [$isNegated, $isNegated ? substr($string, strlen(self::NEGATION_CHAR)) : $string];
313
    }
314
315
    /**
316
     * Prepares array to fill in {@see $ranges}.
317
     *
318
     *  - Recursively substitutes aliases, described in {@see $networks} with their values,
319
     *  - Removes duplicates.
320
     *
321
     * @see $networks
322
     */
323 7
    private function prepareRanges(array $ranges): array
324
    {
325 7
        $result = [];
326 7
        foreach ($ranges as $string) {
327 4
            [$isRangeNegated, $range] = $this->parseNegatedRange($string);
328 4
            if (isset($this->networks[$range])) {
329 3
                $replacements = $this->prepareRanges($this->networks[$range]);
330 3
                foreach ($replacements as &$replacement) {
331 3
                    [$isReplacementNegated, $replacement] = $this->parseNegatedRange($replacement);
332 3
                    $result[] = ($isRangeNegated && !$isReplacementNegated ? self::NEGATION_CHAR : '') . $replacement;
333
                }
334
            } else {
335 4
                $result[] = $string;
336
            }
337
        }
338
339 7
        return array_unique($result);
340
    }
341
342
    /**
343
     * The method checks whether the IP address with specified CIDR is allowed according to the {@see $ranges} list.
344
     */
345 69
    public function isAllowed(string $ip): bool
346
    {
347 69
        if (empty($this->ranges)) {
348 31
            return true;
349
        }
350
351 38
        foreach ($this->ranges as $string) {
352 38
            [$isNegated, $range] = $this->parseNegatedRange($string);
353 38
            if (IpHelper::inRange($ip, $range)) {
354 32
                return !$isNegated;
355
            }
356
        }
357
358 6
        return false;
359
    }
360
361 7
    public function getOptions(): array
362
    {
363
        return [
364 7
            'networks' => $this->networks,
365 7
            'allowIpv4' => $this->allowIpv4,
366 7
            'allowIpv6' => $this->allowIpv6,
367 7
            'allowSubnet' => $this->allowSubnet,
368 7
            'requireSubnet' => $this->requireSubnet,
369 7
            'allowNegation' => $this->allowNegation,
370
            'message' => [
371 7
                'message' => $this->message,
372
            ],
373
            'ipv4NotAllowedMessage' => [
374 7
                'message' => $this->ipv4NotAllowedMessage,
375
            ],
376
            'ipv6NotAllowedMessage' => [
377 7
                'message' => $this->ipv6NotAllowedMessage,
378
            ],
379
            'wrongCidrMessage' => [
380 7
                'message' => $this->wrongCidrMessage,
381
            ],
382
            'noSubnetMessage' => [
383 7
                'message' => $this->noSubnetMessage,
384
            ],
385
            'hasSubnetMessage' => [
386 7
                'message' => $this->hasSubnetMessage,
387
            ],
388
            'notInRangeMessage' => [
389 7
                'message' => $this->notInRangeMessage,
390
            ],
391 7
            'ranges' => $this->ranges,
392 7
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
393 7
            'skipOnError' => $this->skipOnError,
394
        ];
395
    }
396
397 1
    public function getHandlerClassName(): string
398
    {
399 1
        return IpHandler::class;
400
    }
401
}
402