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.
Passed
Push — php72 ( a14e51...94fc0a )
by Joni
02:27
created

IPAddress::fromChosenASN1()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
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
     * Check whether mask is present.
90
     *
91
     * @return bool
92
     */
93 3
    public function hasMask(): bool
94
    {
95 3
        return isset($this->_mask);
96
    }
97
98
    /**
99
     * Get subnet mask as a string.
100
     *
101
     * @throws \LogicException If not set
102
     *
103
     * @return string
104
     */
105 3
    public function mask(): string
106
    {
107 3
        if (!$this->hasMask()) {
108 1
            throw new \LogicException('mask is not set.');
109
        }
110 2
        return $this->_mask;
1 ignored issue
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...
111
    }
112
113
    /**
114
     * Get octet representation of the IP address.
115
     *
116
     * @return string
117
     */
118
    abstract protected function _octets(): string;
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 18
    protected function _choiceASN1(): TaggedType
124
    {
125 18
        return new ImplicitlyTaggedType($this->_tag,
126 18
            new OctetString($this->_octets()));
127
    }
128
}
129