Passed
Pull Request — master (#374)
by Tim
02:48
created

CIDRTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 45
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A validCIDR() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Assert;
6
7
use SimpleSAML\Assert\AssertionFailedException;
8
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
9
10
/**
11
 * @package simplesamlphp/saml2
12
 */
13
trait CIDRTrait
14
{
15
    private static string $cidr_regex = '/^
16
      (?:
17
        (?:
18
          (
19
            (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
20
            (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
21
            (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
22
            (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])
23
          )
24
          [\/](3[0-2]|[1-2]?[0-9])$
25
        )
26
        |
27
        (
28
          (?:[0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|
29
          (?:[0-9a-fA-F]{1,4}:){1,7}:|
30
          (?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|
31
          (?:[0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|
32
          (?:[0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|
33
          (?:[0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|
34
          (?:[0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|
35
          [0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|
36
          :(?:(:[0-9a-fA-F]{1,4}){1,7}|:)|
37
          ::
38
        )
39
        [\/](12[0-8]|1[0-1][0-9]|[1-9]?[0-9])$
40
      )
41
      $/Dxi';
42
43
44
    /**
45
     * @param string $value
46
     * @param string $message
47
     */
48
    protected static function validCIDR(string $value, string $message = ''): void
49
    {
50
        try {
51
            parent::regex(
52
                $value,
53
                self::$cidr_regex,
54
                $message ?: '%s is not a valid RFC4632 CIDR-block',
55
            );
56
        } catch (AssertionFailedException $e) {
57
            throw new ProtocolViolationException($e->getMessage());
58
        }
59
    }
60
}
61