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
Branch php72 (a7f01e)
by Joni
04:53
created

IPAddress   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
eloc 22
dl 0
loc 92
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A address() 0 3 1
A mask() 0 3 1
A __construct() 0 5 1
A _choiceASN1() 0 4 1
A fromChosenASN1() 0 13 5
A string() 0 3 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X509\GeneralName;
6
7
use Sop\ASN1\Type\Primitive\OctetString;
8
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType;
9
use Sop\ASN1\Type\TaggedType;
10
use Sop\ASN1\Type\UnspecifiedType;
11
12
/**
13
 * Implements <i>iPAddress</i> CHOICE type of <i>GeneralName</i>.
14
 *
15
 * Concrete classes <code>IPv4Address</code> and <code>IPv6Address</code>
16
 * furthermore implement the parsing semantics.
17
 *
18
 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.6
19
 */
20
abstract class IPAddress extends GeneralName
21
{
22
    /**
23
     * IP address.
24
     *
25
     * @var string
26
     */
27
    protected $_ip;
28
29
    /**
30
     * Subnet mask.
31
     *
32
     * @var null|string
33
     */
34
    protected $_mask;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param string      $ip
40
     * @param null|string $mask
41
     */
42 16
    public function __construct(string $ip, ?string $mask = null)
43
    {
44 16
        $this->_tag = self::TAG_IP_ADDRESS;
45 16
        $this->_ip = $ip;
46 16
        $this->_mask = $mask;
47 16
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @return self
53
     */
54 13
    public static function fromChosenASN1(UnspecifiedType $el): GeneralName
55
    {
56 13
        $octets = $el->asOctetString()->string();
57 13
        switch (strlen($octets)) {
58 13
            case 4:
59 12
            case 8:
60 10
                return IPv4Address::fromOctets($octets);
61 11
            case 16:
62 2
            case 32:
63 10
                return IPv6Address::fromOctets($octets);
64
            default:
65 1
                throw new \UnexpectedValueException(
66 1
                    'Invalid octet length for IP address.');
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1
    public function string(): string
74
    {
75 1
        return $this->_ip . (isset($this->_mask) ? '/' . $this->_mask : '');
76
    }
77
78
    /**
79
     * Get IP address as a string.
80
     *
81
     * @return string
82
     */
83 3
    public function address(): string
84
    {
85 3
        return $this->_ip;
86
    }
87
88
    /**
89
     * Get subnet mask as a string.
90
     *
91
     * @return string
92
     */
93 2
    public function mask(): string
94
    {
95 2
        return $this->_mask;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_mask could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
96
    }
97
98
    /**
99
     * Get octet representation of the IP address.
100
     *
101
     * @return string
102
     */
103
    abstract protected function _octets(): string;
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 18
    protected function _choiceASN1(): TaggedType
109
    {
110 18
        return new ImplicitlyTaggedType($this->_tag,
111 18
            new OctetString($this->_octets()));
112
    }
113
}
114