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

CertificationRequestInfo::attributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X509\CertificationRequest;
6
7
use Sop\ASN1\Element;
8
use Sop\ASN1\Type\Constructed\Sequence;
9
use Sop\ASN1\Type\Primitive\Integer;
10
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType;
11
use Sop\CryptoBridge\Crypto;
12
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier;
13
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo;
14
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo;
15
use Sop\X501\ASN1\Attribute;
16
use Sop\X501\ASN1\Name;
17
use Sop\X509\Certificate\Extensions;
18
use Sop\X509\CertificationRequest\Attribute\ExtensionRequestValue;
19
20
/**
21
 * Implements <i>CertificationRequestInfo</i> ASN.1 type.
22
 *
23
 * @see https://tools.ietf.org/html/rfc2986#section-4
24
 */
25
class CertificationRequestInfo
26
{
27
    const VERSION_1 = 0;
28
29
    /**
30
     * Version.
31
     *
32
     * @var int
33
     */
34
    protected $_version;
35
36
    /**
37
     * Subject.
38
     *
39
     * @var Name
40
     */
41
    protected $_subject;
42
43
    /**
44
     * Public key info.
45
     *
46
     * @var PublicKeyInfo
47
     */
48
    protected $_subjectPKInfo;
49
50
    /**
51
     * Attributes.
52
     *
53
     * @var null|Attributes
54
     */
55
    protected $_attributes;
56
57
    /**
58
     * Constructor.
59
     *
60
     * @param Name          $subject Subject
61
     * @param PublicKeyInfo $pkinfo  Public key info
62
     */
63 10
    public function __construct(Name $subject, PublicKeyInfo $pkinfo)
64
    {
65 10
        $this->_version = self::VERSION_1;
66 10
        $this->_subject = $subject;
67 10
        $this->_subjectPKInfo = $pkinfo;
68 10
    }
69
70
    /**
71
     * Initialize from ASN.1.
72
     *
73
     * @param Sequence $seq
74
     *
75
     * @throws \UnexpectedValueException
76
     *
77
     * @return self
78
     */
79 6
    public static function fromASN1(Sequence $seq): self
80
    {
81 6
        $version = $seq->at(0)->asInteger()->intNumber();
82 6
        if (self::VERSION_1 != $version) {
83 1
            throw new \UnexpectedValueException(
84 1
                "Version {$version} not supported.");
85
        }
86 5
        $subject = Name::fromASN1($seq->at(1)->asSequence());
87 5
        $pkinfo = PublicKeyInfo::fromASN1($seq->at(2)->asSequence());
88 5
        $obj = new self($subject, $pkinfo);
89 5
        if ($seq->hasTagged(0)) {
90 2
            $obj->_attributes = Attributes::fromASN1(
91 2
                $seq->getTagged(0)->asImplicit(Element::TYPE_SET)->asSet());
92
        }
93 5
        return $obj;
94
    }
95
96
    /**
97
     * Get version.
98
     *
99
     * @return int
100
     */
101 2
    public function version(): int
102
    {
103 2
        return $this->_version;
104
    }
105
106
    /**
107
     * Get self with subject.
108
     *
109
     * @param Name $subject
110
     *
111
     * @return self
112
     */
113 1
    public function withSubject(Name $subject): self
114
    {
115 1
        $obj = clone $this;
116 1
        $obj->_subject = $subject;
117 1
        return $obj;
118
    }
119
120
    /**
121
     * Get subject.
122
     *
123
     * @return Name
124
     */
125 4
    public function subject(): Name
126
    {
127 4
        return $this->_subject;
128
    }
129
130
    /**
131
     * Get subject public key info.
132
     *
133
     * @return PublicKeyInfo
134
     */
135 4
    public function subjectPKInfo(): PublicKeyInfo
136
    {
137 4
        return $this->_subjectPKInfo;
138
    }
139
140
    /**
141
     * Whether certification request info has attributes.
142
     *
143
     * @return bool
144
     */
145 6
    public function hasAttributes(): bool
146
    {
147 6
        return isset($this->_attributes);
148
    }
149
150
    /**
151
     * Get attributes.
152
     *
153
     * @throws \LogicException If not set
154
     *
155
     * @return Attributes
156
     */
157 6
    public function attributes(): Attributes
158
    {
159 6
        if (!$this->hasAttributes()) {
160 1
            throw new \LogicException('No attributes.');
161
        }
162 5
        return $this->_attributes;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->_attributes could return the type null which is incompatible with the type-hinted return Sop\X509\CertificationRequest\Attributes. Consider adding an additional type-check to rule them out.
Loading history...
163
    }
164
165
    /**
166
     * Get instance of self with attributes.
167
     *
168
     * @param Attributes $attribs
169
     */
170 1
    public function withAttributes(Attributes $attribs): self
171
    {
172 1
        $obj = clone $this;
173 1
        $obj->_attributes = $attribs;
174 1
        return $obj;
175
    }
176
177
    /**
178
     * Get self with extension request attribute.
179
     *
180
     * @param Extensions $extensions Extensions to request
181
     *
182
     * @return self
183
     */
184 3
    public function withExtensionRequest(Extensions $extensions): self
185
    {
186 3
        $obj = clone $this;
187 3
        if (!isset($obj->_attributes)) {
188 2
            $obj->_attributes = new Attributes();
189
        }
190 3
        $obj->_attributes = $obj->_attributes->withUnique(
1 ignored issue
show
Bug introduced by
The method withUnique() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
        /** @scrutinizer ignore-call */ 
191
        $obj->_attributes = $obj->_attributes->withUnique(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191 3
            Attribute::fromAttributeValues(
192 3
                new ExtensionRequestValue($extensions)));
193 3
        return $obj;
194
    }
195
196
    /**
197
     * Generate ASN.1 structure.
198
     *
199
     * @return Sequence
200
     */
201 9
    public function toASN1(): Sequence
202
    {
203 9
        $elements = [new Integer($this->_version),
204 9
            $this->_subject->toASN1(), $this->_subjectPKInfo->toASN1(), ];
205 9
        if (isset($this->_attributes)) {
206 3
            $elements[] = new ImplicitlyTaggedType(0,
207 3
                $this->_attributes->toASN1());
1 ignored issue
show
Bug introduced by
The method toASN1() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

207
                $this->_attributes->/** @scrutinizer ignore-call */ 
208
                                    toASN1());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
208
        }
209 9
        return new Sequence(...$elements);
210
    }
211
212
    /**
213
     * Create signed CertificationRequest.
214
     *
215
     * @param SignatureAlgorithmIdentifier $algo         Algorithm used for signing
216
     * @param PrivateKeyInfo               $privkey_info Private key used for signing
217
     * @param null|Crypto                  $crypto       Crypto engine, use default if not set
218
     *
219
     * @return CertificationRequest
220
     */
221 2
    public function sign(SignatureAlgorithmIdentifier $algo,
222
        PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): CertificationRequest
223
    {
224 2
        $crypto = $crypto ?? Crypto::getDefault();
225 2
        $data = $this->toASN1()->toDER();
226 2
        $signature = $crypto->sign($data, $privkey_info, $algo);
227 2
        return new CertificationRequest($this, $algo, $signature);
228
    }
229
}
230