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
Pull Request — master (#88)
by Sergii
01:41
created

Url::computeAuthority()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 36

Duplication

Lines 6
Ratio 16.67 %

Importance

Changes 0
Metric Value
cc 7
nc 36
nop 0
dl 6
loc 36
rs 8.4106
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\SslCertificate;
4
5
use Spatie\SslCertificate\Exceptions\InvalidUrl;
6
7
class Url
8
{
9
    /**
10
     * @var string
11
     *
12
     * @see \GuzzleHttp\Psr7\Uri::getAuthority()
13
     */
14
    protected $authority = '';
15
16
    /**
17
     * @var string[]
18
     */
19
    protected $components = [];
20
21
    public function __construct(string $url)
22
    {
23
        $this->components = parse_url($url);
0 ignored issues
show
Documentation Bug introduced by
It seems like parse_url($url) can also be of type false. However, the property $components is declared as type array<integer,string>. 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...
24
        $this->components = $this->components === false ? [] : array_map('trim', $this->components);
25
26
        if (empty($this->components['host']) && empty($this->components['path'])) {
27
            throw InvalidUrl::couldNotDetermineHost($url);
28
        }
29
30
        $this->components += [
31
          'host' => '',
32
          'port' => '',
33
          'user' => '',
34
          'pass' => '',
35
          'path' => '',
36
          'query' => '',
37
          'scheme' => 'https',
38
          'fragment' => '',
39
        ];
40
41
        $this->computeAuthority();
42
43
        if (! filter_var((string) $this, FILTER_VALIDATE_URL)) {
44
            throw InvalidUrl::couldNotValidate($url);
45
        }
46
    }
47
48
    public function __toString(): string
49
    {
50
        $string = $this->components['scheme'].'://'.$this->authority.$this->components['path'];
51
52
        if (! empty($this->components['query'])) {
53
            $string .= '?'.$this->components['query'];
54
        }
55
56
        if (! empty($this->components['fragment'])) {
57
            $string .= '#'.$this->components['fragment'];
58
        }
59
60
        return $string;
61
    }
62
63
    public function getHostName(): string
64
    {
65
        return $this->components['host'];
66
    }
67
68
    public function getPort(): int
69
    {
70
        return $this->components['port'] ?: 443;
71
    }
72
73
    /**
74
     * Computes the authority path.
75
     *
76
     * @internal
77
     */
78
    protected function computeAuthority()
79
    {
80
        if ($this->components['user'] !== '') {
81
            $this->authority .= $this->components['user'];
82
83 View Code Duplication
            if ($this->components['pass'] !== '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                $this->authority .= ':'.$this->components['pass'];
85
            }
86
87
            $this->authority .= '@';
88
        }
89
90
        // If "example.com" or "localhost" or something similar passed to the
91
        // constructor the "host" will be empty and the actual value will in
92
        // the "path".
93
        if ($this->components['host'] === '') {
94
            list($this->components['host'], $this->components['path']) = explode('/', $this->components['path'] . '/', 2);
95
96
            $this->components['path'] = '/'.ltrim($this->components['path'], '/');
97
        }
98
99
        if (function_exists('idn_to_ascii')) {
100
            // Transform "économiessanté.ca" to "xn--conomiessant-9dbm.ca". Otherwise,
101
            // the hostname cannot be reached.
102
            $this->components['host'] = idn_to_ascii($this->components['host'], 0, INTL_IDNA_VARIANT_UTS46)
103
              ?: $this->components['host'];
104
        }
105
106
        $this->authority .= $this->components['host'];
107
108 View Code Duplication
        if ($this->components['port'] !== '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            $this->authority .= ':'.$this->components['port'];
110
        }
111
112
        $this->authority = trim($this->authority);
113
    }
114
}
115