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 ( 7c460d...d25add )
by Freek
01:14
created

Downloader::usingPort()   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 $enableSni = true;
18
19
    /** @var bool */
20
    protected $capturePeerChain = 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->enableSni = $sni;
0 ignored issues
show
Documentation Bug introduced by
It seems like $sni can also be of type integer. However, the property $enableSni 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->capturePeerChain = $ca_chain;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ca_chain can also be of type integer. However, the property $capturePeerChain 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
        $response = $this->fetchCertificates($hostName);
73
74
        $peerCertificate = $response['options']['ssl']['peer_certificate'];
75
76
        $peerCertificateChain = $response['options']['ssl']['peer_certificate_chain'] ?? [];
77
78
        $fullCertificateChain = array_merge([$peerCertificate], $peerCertificateChain);
79
80
        return array_map(function($certificate) {
81
            $certificateFields = openssl_x509_parse($certificate);
82
83
            return new SslCertificate($certificateFields);
84
        }, $fullCertificateChain);
85
    }
86
87
    public function forHost(string $hostName): SslCertificate
88
    {
89
        $hostName = (new Url($hostName))->getHostName();
90
91
        $certificates = $this->getCertificates($hostName);
92
93
        return $certificates[0] ?? false;
94
    }
95
96
    public static function downloadCertificateFromUrl(string $url, int $timeout = 30): SslCertificate
97
    {
98
        return (new static())
99
            ->setTimeout($timeout)
100
            ->forHost($url);
101
    }
102
103
    protected function fetchCertificates(string $hostName): array
104
    {
105
        $hostName = (new Url($hostName))->getHostName();
106
107
        $sslOptions = [
108
            'capture_peer_cert' => true,
109
            'capture_peer_cert_chain' => $this->capturePeerChain,
110
            'SNI_enabled' => $this->enableSni,
111
        ];
112
113
        $streamContext = stream_context_create([
114
            'ssl' => $sslOptions,
115
        ]);
116
117
        try {
118
            $client = stream_socket_client(
119
                "ssl://{$hostName}:{$this->port}",
120
                $errorNumber,
121
                $errorDescription,
122
                $this->timeout,
123
                STREAM_CLIENT_CONNECT,
124
                $streamContext
125
            );
126
        } catch (Throwable $thrown) {
127
            $this->handleRequestFailure($hostName, $thrown);
128
        }
129
130
        if (!$client) {
131
            throw CouldNotDownloadCertificate::unknownError($hostName, "Could not connect to `{$hostName}`.");
132
        }
133
134
        $response = stream_context_get_params($client);
135
        return $response;
136
    }
137
138
    protected function handleRequestFailure(string $hostName, Throwable $thrown)
139
    {
140
        if (str_contains($thrown->getMessage(), 'getaddrinfo failed')) {
141
            throw CouldNotDownloadCertificate::hostDoesNotExist($hostName);
142
        }
143
144
        if (str_contains($thrown->getMessage(), 'error:14090086')) {
145
            throw CouldNotDownloadCertificate::noCertificateInstalled($hostName);
146
        }
147
148
        throw CouldNotDownloadCertificate::unknownError($hostName, $thrown->getMessage());
149
    }
150
}
151