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
Push — master ( 39960c...7c460d )
by Freek
01:16
created

Downloader::withFullChain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\SslCertificate;
4
5
use Throwable;
6
use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate;
7
8
class Downloader
9
{
10
    /** @var int */
11
    protected $port = 443;
12
13
    /** @var int */
14
    protected $timeout = 30;
15
16
    /** @var bool */
17
    protected $sni = true;
18
19
    /** @var bool */
20
    protected $ca_chain = false;
21
22
    /**
23
     * @param int $port
24
     *
25
     * @return $this
26
     */
27
    public function usingPort(int $port)
28
    {
29
        $this->port = $port;
30
31
        return $this;
32
    }
33
34
    /**
35
     * @param int $sni
36
     *
37
     * @return $this
38
     */
39
    public function usingSni(bool $sni)
40
    {
41
        $this->sni = $sni;
0 ignored issues
show
Documentation Bug introduced by
It seems like $sni can also be of type integer. However, the property $sni is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42
43
        return $this;
44
    }
45
46
    /**
47
     * @param int $ca_chain
48
     *
49
     * @return $this
50
     */
51
    public function withFullChain(bool $ca_chain)
52
    {
53
        $this->ca_chain = $ca_chain;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ca_chain can also be of type integer. However, the property $ca_chain is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
54
55
        return $this;
56
    }
57
58
    /**
59
     * @param int $timeOutInSeconds
60
     *
61
     * @return $this
62
     */
63
    public function setTimeout(int $timeOutInSeconds)
64
    {
65
        $this->timeout = $timeOutInSeconds;
66
67
        return $this;
68
    }
69
70
    public function getCertificates(string $hostName): array
71
    {
72
        $hostName = (new Url($hostName))->getHostName();
73
74
        $ssl_options = [
75
            'capture_peer_cert' => true,
76
            'capture_peer_cert_chain' => $this->ca_chain,
77
            'SNI_enabled' => $this->sni,
78
        ];
79
80
        $streamContext = stream_context_create([
81
            'ssl' => $ssl_options,
82
        ]);
83
84
        try {
85
            $client = stream_socket_client(
86
                "ssl://{$hostName}:{$this->port}",
87
                $errorNumber,
88
                $errorDescription,
89
                $this->timeout,
90
                STREAM_CLIENT_CONNECT,
91
                $streamContext
92
            );
93
        } catch (Throwable $thrown) {
94
            if (str_contains($thrown->getMessage(), 'getaddrinfo failed')) {
95
                throw CouldNotDownloadCertificate::hostDoesNotExist($hostName);
96
            }
97
98
            if (str_contains($thrown->getMessage(), 'error:14090086')) {
99
                throw CouldNotDownloadCertificate::noCertificateInstalled($hostName);
100
            }
101
102
            throw CouldNotDownloadCertificate::unknownError($hostName, $thrown->getMessage());
103
        }
104
105
        if (! $client) {
106
            throw CouldNotDownloadCertificate::unknownError($hostName, "Could not connect to `{$hostName}`.");
107
        }
108
109
        $response = stream_context_get_params($client);
110
111
        $peer_certificate = $response['options']['ssl']['peer_certificate'];
112
        $peer_certificate_chain = $response['options']['ssl']['peer_certificate_chain'] ?? [];
113
        $certificates = array_merge([$peer_certificate], $peer_certificate_chain);
114
115
        $return = [];
116
        foreach ($certificates as $certificate) {
117
            $certificateFields = openssl_x509_parse($certificate);
118
            $return[] = new SslCertificate($certificateFields);
119
        }
120
121
        return $return;
122
    }
123
124
    public function forHost(string $hostName): SslCertificate
125
    {
126
        $hostName = (new Url($hostName))->getHostName();
127
128
        $certificates = $this->getCertificates($hostName);
129
130
        return $certificates[0] ?? false;
131
    }
132
133
    public static function downloadCertificateFromUrl(string $url, int $timeout = 30): SslCertificate
134
    {
135
        return (new static())
136
            ->setTimeout($timeout)
137
            ->forHost($url);
138
    }
139
}
140