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.

CertificatePoliciesExtension::hasAnyPolicy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X509\Certificate\Extension;
6
7
use Sop\ASN1\Element;
8
use Sop\ASN1\Type\Constructed\Sequence;
9
use Sop\ASN1\Type\UnspecifiedType;
10
use Sop\X509\Certificate\Extension\CertificatePolicy\PolicyInformation;
11
12
/**
13
 * Implements 'Certificate Policies' certificate extension.
14
 *
15
 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.4
16
 */
17
class CertificatePoliciesExtension extends Extension implements \Countable, \IteratorAggregate
18
{
19
    /**
20
     * Policy information terms.
21
     *
22
     * @var PolicyInformation[]
23
     */
24
    protected $_policies;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param bool              $critical
30
     * @param PolicyInformation ...$policies
31
     */
32 13
    public function __construct(bool $critical, PolicyInformation ...$policies)
33
    {
34 13
        parent::__construct(Extension::OID_CERTIFICATE_POLICIES, $critical);
35 13
        $this->_policies = [];
36 13
        foreach ($policies as $policy) {
37 12
            $this->_policies[$policy->oid()] = $policy;
38
        }
39 13
    }
40
41
    /**
42
     * Check whether policy information by OID is present.
43
     *
44
     * @param string $oid
45
     *
46
     * @return bool
47
     */
48 7
    public function has(string $oid): bool
49
    {
50 7
        return isset($this->_policies[$oid]);
51
    }
52
53
    /**
54
     * Get policy information by OID.
55
     *
56
     * @param string $oid
57
     *
58
     * @throws \LogicException If not set
59
     *
60
     * @return PolicyInformation
61
     */
62 4
    public function get(string $oid): PolicyInformation
63
    {
64 4
        if (!$this->has($oid)) {
65 1
            throw new \LogicException("Not certificate policy by OID {$oid}.");
66
        }
67 3
        return $this->_policies[$oid];
68
    }
69
70
    /**
71
     * Check whether anyPolicy is present.
72
     *
73
     * @return bool
74
     */
75 4
    public function hasAnyPolicy(): bool
76
    {
77 4
        return $this->has(PolicyInformation::OID_ANY_POLICY);
78
    }
79
80
    /**
81
     * Get anyPolicy information.
82
     *
83
     * @throws \LogicException if anyPolicy is not present
84
     *
85
     * @return PolicyInformation
86
     */
87 3
    public function anyPolicy(): PolicyInformation
88
    {
89 3
        if (!$this->hasAnyPolicy()) {
90 2
            throw new \LogicException('No anyPolicy.');
91
        }
92 1
        return $this->get(PolicyInformation::OID_ANY_POLICY);
93
    }
94
95
    /**
96
     * Get the number of policies.
97
     *
98
     * @see \Countable::count()
99
     *
100
     * @return int
101
     */
102 1
    public function count(): int
103
    {
104 1
        return count($this->_policies);
105
    }
106
107
    /**
108
     * Get iterator for policy information terms.
109
     *
110
     * @see \IteratorAggregate::getIterator()
111
     *
112
     * @return \ArrayIterator
113
     */
114 15
    public function getIterator(): \ArrayIterator
115
    {
116 15
        return new \ArrayIterator($this->_policies);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 10
    protected static function _fromDER(string $data, bool $critical): Extension
123
    {
124 10
        $policies = array_map(
125
            function (UnspecifiedType $el) {
126 9
                return PolicyInformation::fromASN1($el->asSequence());
127 10
            }, UnspecifiedType::fromDER($data)->asSequence()->elements());
128 10
        if (!count($policies)) {
129 1
            throw new \UnexpectedValueException(
130 1
                'certificatePolicies must contain at least one PolicyInformation.');
131
        }
132 9
        return new self($critical, ...$policies);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 30
    protected function _valueASN1(): Element
139
    {
140 30
        if (!count($this->_policies)) {
141 1
            throw new \LogicException('No policies.');
142
        }
143 29
        $elements = array_map(
144
            function (PolicyInformation $pi) {
145 29
                return $pi->toASN1();
146 29
            }, array_values($this->_policies));
147 29
        return new Sequence(...$elements);
148
    }
149
}
150