|
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); |
|
|
|
|
|
|
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'] !== '') { |
|
|
|
|
|
|
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'] !== '') { |
|
|
|
|
|
|
109
|
|
|
$this->authority .= ':'.$this->components['port']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->authority = trim($this->authority); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.