Skip to content

Commit 47476eb

Browse files
committed
improve type safety
1 parent 9efcc86 commit 47476eb

3 files changed

Lines changed: 33 additions & 38 deletions

File tree

src/Components/QueryParameters/ArrayQueryParameter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace UriManage\Components\QueryParameters;
44

55

6+
use UriManage\Constants\Symbol;
7+
68
/**
79
* @internal
810
*/
@@ -35,4 +37,15 @@ function getValueAsInt () : int {
3537
return (int) $this->value;
3638
}
3739

40+
function __toString (): string {
41+
$key = rawurlencode($this->key . Symbol::QUERY_ARRAY_SUFFIX);
42+
43+
return implode(Symbol::QUERY_PAIR_SEPARATOR, array_map(function($value) use ($key) {
44+
if ($value === null) {
45+
return $key;
46+
}
47+
48+
return $key . Symbol::QUERY_KEYVALUE_SEPARATOR . rawurlencode($value);
49+
}, $this->value));
50+
}
3851
}

src/Components/QueryParameters/QueryParameter.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,8 @@ abstract function getValueAsString (): string;
4848
abstract function getValueAsInt (): int;
4949

5050
function __toString () : string {
51-
$value = $this->getValuePlain();
52-
$key = rawurlencode($this->key . (is_array($value) ? Symbol::QUERY_ARRAY_SUFFIX : ''));
53-
54-
if (is_array($value)) {
55-
return implode(Symbol::QUERY_PAIR_SEPARATOR, array_map(function($value) use ($key){
56-
if ($value === null) {
57-
return $key;
58-
}
59-
60-
return $key . Symbol::QUERY_KEYVALUE_SEPARATOR . rawurlencode($value);
61-
}, $value));
62-
} elseif ($value === null) {
51+
$key = rawurlencode($this->key);
52+
if ($this->getValuePlain() === null) {
6353
return $key;
6454
}
6555

src/Uri.php

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212
use UriManage\Exceptions\UriException;
1313

1414
class Uri implements UriInterface {
15-
protected ?string $scheme;
16-
protected ?string $user;
17-
protected ?string $pass;
18-
protected ?string $host;
19-
protected ?int $port;
15+
protected ?string $scheme = null;
16+
protected ?string $user = null;
17+
protected ?string $pass = null;
18+
protected ?string $host = null;
19+
protected ?int $port = null;
2020
protected ?Path $path = null;
2121
protected ?Query $query = null;
22-
protected ?string $fragment;
23-
protected ?string $originalUri;
24-
protected bool $hasBaseUrlRemoved = false;
22+
protected ?string $fragment = null;
23+
protected ?string $originalUri = null;
2524

2625
/**
2726
* @throws InvalidArgumentException
@@ -62,12 +61,12 @@ function getFragment () : string {
6261
* @return string
6362
*/
6463
function getUserInfo () : string {
65-
if (!$this->hasUserInfo()) {
64+
if ($this->user === null) {
6665
return '';
6766
}
6867

69-
if (($this->pass ?? '') === '') {
70-
return $this->user ?? '';
68+
if ($this->pass === null) {
69+
return $this->user;
7170
}
7271

7372
return sprintf('%s:%s', $this->user, $this->pass);
@@ -77,19 +76,19 @@ function getUserInfo () : string {
7776
* @return string
7877
*/
7978
function getAuthority () : string {
80-
if (!$this->hasUserInfo()) {
81-
if ($this->isDefaultPortForScheme() || $this->getPort() === null) {
79+
if ($this->user === null) {
80+
if ($this->isDefaultPortForScheme() || $this->port === null) {
8281
return $this->getHost();
8382
}
8483

85-
return sprintf('%s:%d', $this->getHost(), $this->getPort());
84+
return sprintf('%s:%d', $this->getHost(), $this->port);
8685
}
8786

88-
if ($this->isDefaultPortForScheme() || $this->getPort() === null) {
87+
if ($this->isDefaultPortForScheme() || $this->port === null) {
8988
return sprintf('%s@%s', $this->getUserInfo(), $this->getHost());
9089
}
9190

92-
return sprintf('%s@%s:%d', $this->getUserInfo(), $this->getHost(), $this->getPort());
91+
return sprintf('%s@%s:%d', $this->getUserInfo(), $this->getHost(), $this->port);
9392
}
9493

9594
/**
@@ -180,7 +179,7 @@ function withHost ($host) : static {
180179
$instance = clone $this;
181180

182181
if ($host === '') {
183-
unset($instance->host);
182+
$instance->host = null;
184183
} else {
185184
$instance->host = UriParser::parseHost($host);
186185
}
@@ -259,13 +258,13 @@ function withUserInfo ($user, $password = null) : static {
259258
}
260259

261260
/**
262-
* @param int|null $port
261+
* @param null|int $port
263262
* @return static
264263
*/
265264
function withPort ($port) : static {
266265
if ($port === null) {
267266
$instance = clone $this;
268-
unset($instance->port);
267+
$instance->port = null;
269268

270269
return $instance;
271270
}
@@ -337,11 +336,4 @@ private function isDefaultPortForScheme () : bool {
337336
return (DefaultSchemePorts::PORT_TO_SCHEME[$this->port] ?? null) === $this->getScheme();
338337
}
339338

340-
/**
341-
* @return bool
342-
*/
343-
private function hasUserInfo () : bool {
344-
return ($this->user ?? null) !== null;
345-
}
346-
347339
}

0 commit comments

Comments
 (0)