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.
Test Failed
Push — master ( 405cf3...79c9ba )
by Joni
04:48
created

AAControlsExtension::hasExcludedAttrs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Primitive\Boolean;
10
use Sop\ASN1\Type\Primitive\Integer;
11
use Sop\ASN1\Type\Primitive\ObjectIdentifier;
12
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType;
13
use Sop\ASN1\Type\UnspecifiedType;
14
15
/**
16
 * Implements 'AA Controls' certificate extension.
17
 *
18
 * @see https://tools.ietf.org/html/rfc5755#section-7.4
19
 */
20
class AAControlsExtension extends Extension
21
{
22
    /**
23
     * Path length contraint.
24
     *
25
     * @var null|int
26
     */
27
    protected $_pathLenConstraint;
28
29
    /**
30
     * Permitted attributes.
31
     *
32
     * Array of OID's.
33
     *
34
     * @var null|string[]
35
     */
36
    protected $_permittedAttrs;
37
38
    /**
39
     * Excluded attributes.
40
     *
41
     * Array of OID's.
42
     *
43
     * @var null|string[]
44
     */
45
    protected $_excludedAttrs;
46
47
    /**
48
     * Whether to permit unspecified attributes.
49
     *
50
     * @var bool
51
     */
52
    protected $_permitUnSpecified;
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param bool          $critical
58
     * @param null|int      $path_len
59
     * @param null|string[] $permitted
60
     * @param null|string[] $excluded
61
     * @param bool          $permit_unspecified
62
     */
63 4
    public function __construct(bool $critical, ?int $path_len = null,
64
        ?array $permitted = null, ?array $excluded = null, bool $permit_unspecified = true)
65
    {
66 4
        parent::__construct(self::OID_AA_CONTROLS, $critical);
67 4
        $this->_pathLenConstraint = $path_len;
68 4
        $this->_permittedAttrs = $permitted;
69 4
        $this->_excludedAttrs = $excluded;
70 4
        $this->_permitUnSpecified = $permit_unspecified;
71 4
    }
72
73
    /**
74
     * Check whether path length constraint is present.
75
     *
76
     * @return bool
77
     */
78 2
    public function hasPathLen(): bool
79
    {
80 2
        return isset($this->_pathLenConstraint);
81 2
    }
82 2
83 2
    /**
84 2
     * Get path length constraint.
85 2
     *
86 2
     * @throws \LogicException If not set
87 1
     *
88 1
     * @return int
89 1
     */
90
    public function pathLen(): int
91 2
    {
92 1
        if (!$this->hasPathLen()) {
93 1
            throw new \LogicException('pathLen not set.');
94 1
        }
95 1
        return $this->_pathLenConstraint;
96 1
    }
97 1
98 1
    /**
99 1
     * Check whether permitted attributes are present.
100
     *
101 2
     * @return bool
102 1
     */
103 1
    public function hasPermittedAttrs(): bool
104 1
    {
105 1
        return isset($this->_permittedAttrs);
106 1
    }
107 1
108 1
    /**
109 1
     * Get OID's of permitted attributes.
110
     *
111 2
     * @throws \LogicException If not set
112 1
     *
113 1
     * @return string[]
114 1
     */
115
    public function permittedAttrs(): array
116 2
    {
117 2
        if (!$this->hasPermittedAttrs()) {
118
            throw new \LogicException('permittedAttrs not set.');
119
        }
120
        return $this->_permittedAttrs;
121
    }
122
123
    /**
124
     * Check whether excluded attributes are present.
125 2
     *
126
     * @return bool
127 2
     */
128
    public function hasExcludedAttrs(): bool
129
    {
130
        return isset($this->_excludedAttrs);
131
    }
132
133
    /**
134
     * Get OID's of excluded attributes.
135
     *
136 2
     * @throws \LogicException If not set
137
     *
138 2
     * @return string[]
139 1
     */
140
    public function excludedAttrs(): array
141 1
    {
142
        if (!$this->hasExcludedAttrs()) {
143
            throw new \LogicException('excludedAttrs not set.');
144
        }
145
        return $this->_excludedAttrs;
146
    }
147
148
    /**
149 2
     * Whether to permit attributes that are not explicitly specified in
150
     * neither permitted nor excluded list.
151 2
     *
152
     * @return bool
153
     */
154
    public function permitUnspecified(): bool
155
    {
156
        return $this->_permitUnSpecified;
157
    }
158
159
    /**
160 2
     * {@inheritdoc}
161
     */
162 2
    protected static function _fromDER(string $data, bool $critical): Extension
163 1
    {
164
        $seq = UnspecifiedType::fromDER($data)->asSequence();
165 1
        $path_len = null;
166
        $permitted = null;
167
        $excluded = null;
168
        $permit_unspecified = true;
169
        $idx = 0;
170
        if ($seq->has($idx, Element::TYPE_INTEGER)) {
171
            $path_len = $seq->at($idx++)->asInteger()->intNumber();
172
        }
173 2
        if ($seq->hasTagged(0)) {
174
            $attr_seq = $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE)
175 2
                ->asSequence();
176
            $permitted = array_map(
177
                function (UnspecifiedType $el) {
178
                    return $el->asObjectIdentifier()->oid();
179
                }, $attr_seq->elements());
180
            ++$idx;
181
        }
182
        if ($seq->hasTagged(1)) {
183
            $attr_seq = $seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE)
184 2
                ->asSequence();
185
            $excluded = array_map(
186 2
                function (UnspecifiedType $el) {
187 1
                    return $el->asObjectIdentifier()->oid();
188
                }, $attr_seq->elements());
189 1
            ++$idx;
190
        }
191
        if ($seq->has($idx, Element::TYPE_BOOLEAN)) {
192
            $permit_unspecified = $seq->at($idx++)->asBoolean()->value();
193
        }
194
        return new self($critical, $path_len, $permitted, $excluded, $permit_unspecified);
195
    }
196
197
    /**
198 1
     * {@inheritdoc}
199
     */
200 1
    protected function _valueASN1(): Element
201
    {
202
        $elements = [];
203
        if (isset($this->_pathLenConstraint)) {
204
            $elements[] = new Integer($this->_pathLenConstraint);
205
        }
206
        if (isset($this->_permittedAttrs)) {
207
            $oids = array_map(
208 2
                function ($oid) {
209
                    return new ObjectIdentifier($oid);
210 2
                }, $this->_permittedAttrs);
211 2
            $elements[] = new ImplicitlyTaggedType(0, new Sequence(...$oids));
212 1
        }
213
        if (isset($this->_excludedAttrs)) {
214 2
            $oids = array_map(
215 1
                function ($oid) {
216 1
                    return new ObjectIdentifier($oid);
217 1
                }, $this->_excludedAttrs);
218 1
            $elements[] = new ImplicitlyTaggedType(1, new Sequence(...$oids));
219 1
        }
220
        if (true !== $this->_permitUnSpecified) {
221 2
            $elements[] = new Boolean(false);
222 1
        }
223 1
        return new Sequence(...$elements);
224 1
    }
225
}
226