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.

PublicKey::fromPEM()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\CryptoTypes\Asymmetric;
6
7
use Sop\CryptoEncoding\PEM;
8
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;
9
10
/**
11
 * Base class for public keys.
12
 */
13
abstract class PublicKey
14
{
15
    /**
16
     * Get the public key algorithm identifier.
17
     *
18
     * @return AlgorithmIdentifierType
19
     */
20
    abstract public function algorithmIdentifier(): AlgorithmIdentifierType;
21
22
    /**
23
     * Get DER encoding of the public key.
24
     *
25
     * @return string
26
     */
27
    abstract public function toDER(): string;
28
29
    /**
30
     * Get the public key data for subjectPublicKey in PublicKeyInfo.
31
     *
32
     * @return string
33
     */
34 3
    public function subjectPublicKeyData(): string
35
    {
36 3
        return $this->toDER();
37
    }
38
39
    /**
40
     * Get the public key as a PublicKeyInfo type.
41
     *
42
     * @return PublicKeyInfo
43
     */
44 7
    public function publicKeyInfo(): PublicKeyInfo
45
    {
46 7
        return PublicKeyInfo::fromPublicKey($this);
47
    }
48
49
    /**
50
     * Initialize public key from PEM.
51
     *
52
     * @param PEM $pem
53
     *
54
     * @throws \UnexpectedValueException
55
     *
56
     * @return PublicKey
57
     */
58 6
    public static function fromPEM(PEM $pem)
59
    {
60 6
        switch ($pem->type()) {
61 6
            case PEM::TYPE_RSA_PUBLIC_KEY:
62 1
                return RSA\RSAPublicKey::fromDER($pem->data());
63 5
            case PEM::TYPE_PUBLIC_KEY:
64 4
                return PublicKeyInfo::fromPEM($pem)->publicKey();
65
        }
66 1
        throw new \UnexpectedValueException(
67 1
            'PEM type ' . $pem->type() . ' is not a valid public key.');
68
    }
69
}
70