Skip to content

Commit 34deca9

Browse files
committed
refactor: fix phpstan errors in URI and SiteURI
1 parent e7bce5f commit 34deca9

File tree

7 files changed

+74
-101
lines changed

7 files changed

+74
-101
lines changed

system/HTTP/SiteURI.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -331,35 +331,48 @@ public function refreshPath()
331331

332332
/**
333333
* Saves our parts from a parse_url() call.
334+
*
335+
* @param array{
336+
* host?: string,
337+
* user?: string,
338+
* path?: string,
339+
* query?: string,
340+
* fragment?: string,
341+
* scheme?: string,
342+
* port?: int,
343+
* pass?: string,
344+
* } $parts
334345
*/
335346
protected function applyParts(array $parts): void
336347
{
337-
if (! empty($parts['host'])) {
348+
if (isset($parts['host']) && $parts['host'] !== '') {
338349
$this->host = $parts['host'];
339350
}
340-
if (! empty($parts['user'])) {
351+
352+
if (isset($parts['user']) && $parts['user'] !== '') {
341353
$this->user = $parts['user'];
342354
}
355+
343356
if (isset($parts['path']) && $parts['path'] !== '') {
344357
$this->path = $this->filterPath($parts['path']);
345358
}
346-
if (! empty($parts['query'])) {
359+
360+
if (isset($parts['query']) && $parts['query'] !== '') {
347361
$this->setQuery($parts['query']);
348362
}
349-
if (! empty($parts['fragment'])) {
363+
364+
if (isset($parts['fragment']) && $parts['fragment'] !== '') {
350365
$this->fragment = $parts['fragment'];
351366
}
352367

353-
// Scheme
354368
if (isset($parts['scheme'])) {
355369
$this->setScheme(rtrim($parts['scheme'], ':/'));
356370
} else {
357371
$this->setScheme('http');
358372
}
359373

360-
// Port
361-
if (isset($parts['port']) && $parts['port'] !== null) {
362-
// Valid port numbers are enforced by earlier parse_url() or setPort()
374+
if (isset($parts['port'])) {
375+
// Valid port numbers are enforced by earlier parse_url or setPort()
363376
$this->port = $parts['port'];
364377
}
365378

system/HTTP/URI.php

+48-28
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,19 @@ class URI implements Stringable
113113
/**
114114
* The query string.
115115
*
116-
* @var array
116+
* @var array<string, string>
117117
*/
118118
protected $query = [];
119119

120120
/**
121121
* Default schemes/ports.
122122
*
123-
* @var array
123+
* @var array{
124+
* http: int,
125+
* https: int,
126+
* ftp: int,
127+
* sftp: int,
128+
* }
124129
*/
125130
protected $defaultPorts = [
126131
'http' => 80,
@@ -166,25 +171,26 @@ public static function createURIString(
166171
?string $fragment = null,
167172
): string {
168173
$uri = '';
169-
if ($scheme !== null && $scheme !== '') {
174+
175+
if ((string) $scheme !== '') {
170176
$uri .= $scheme . '://';
171177
}
172178

173-
if ($authority !== null && $authority !== '') {
179+
if ((string) $authority !== '') {
174180
$uri .= $authority;
175181
}
176182

177-
if (isset($path) && $path !== '') {
183+
if ((string) $path !== '') {
178184
$uri .= ! str_ends_with($uri, '/')
179185
? '/' . ltrim($path, '/')
180186
: ltrim($path, '/');
181187
}
182188

183-
if ($query !== '' && $query !== null) {
189+
if ((string) $query !== '') {
184190
$uri .= '?' . $query;
185191
}
186192

187-
if ($fragment !== '' && $fragment !== null) {
193+
if ((string) $fragment !== '') {
188194
$uri .= '#' . $fragment;
189195
}
190196

@@ -329,7 +335,7 @@ public function setURI(?string $uri = null)
329335
* The trailing ":" character is not part of the scheme and MUST NOT be
330336
* added.
331337
*
332-
* @see https://tools.ietf.org/html/rfc3986#section-3.1
338+
* @see https://tools.ietf.org/html/rfc3986#section-3.1
333339
*
334340
* @return string The URI scheme.
335341
*/
@@ -359,19 +365,18 @@ public function getScheme(): string
359365
*/
360366
public function getAuthority(bool $ignorePort = false): string
361367
{
362-
if (empty($this->host)) {
368+
if ($this->host === '') {
363369
return '';
364370
}
365371

366372
$authority = $this->host;
367373

368-
if (! empty($this->getUserInfo())) {
374+
if ((string) $this->getUserInfo() !== '') {
369375
$authority = $this->getUserInfo() . '@' . $authority;
370376
}
371377

372-
// Don't add port if it's a standard port for
373-
// this scheme
374-
if (! empty($this->port) && ! $ignorePort && $this->port !== $this->defaultPorts[$this->scheme]) {
378+
// Don't add port if it's a standard port for this scheme
379+
if ($this->port !== 0 && ! $ignorePort && $this->port !== $this->defaultPorts[$this->scheme]) {
375380
$authority .= ':' . $this->port;
376381
}
377382

@@ -404,7 +409,7 @@ public function getUserInfo()
404409
{
405410
$userInfo = $this->user;
406411

407-
if ($this->showPassword === true && ! empty($this->password)) {
412+
if ($this->showPassword === true && $this->password !== '') {
408413
$userInfo .= ':' . $this->password;
409414
}
410415

@@ -434,13 +439,13 @@ public function showPassword(bool $val = true)
434439
* The value returned MUST be normalized to lowercase, per RFC 3986
435440
* Section 3.2.2.
436441
*
437-
* @see http://tools.ietf.org/html/rfc3986#section-3.2.2
442+
* @see http://tools.ietf.org/html/rfc3986#section-3.2.2
438443
*
439444
* @return string The URI host.
440445
*/
441446
public function getHost(): string
442447
{
443-
return $this->host ?? '';
448+
return $this->host;
444449
}
445450

446451
/**
@@ -491,11 +496,13 @@ public function getPort()
491496
*/
492497
public function getPath(): string
493498
{
494-
return $this->path ?? '';
499+
return $this->path;
495500
}
496501

497502
/**
498503
* Retrieve the query string
504+
*
505+
* @param array{except?: list<string>|string, only?: list<string>|string} $options
499506
*/
500507
public function getQuery(array $options = []): string
501508
{
@@ -525,15 +532,15 @@ public function getQuery(array $options = []): string
525532
$vars = $temp;
526533
}
527534

528-
return empty($vars) ? '' : http_build_query($vars);
535+
return $vars === [] ? '' : http_build_query($vars);
529536
}
530537

531538
/**
532539
* Retrieve a URI fragment
533540
*/
534541
public function getFragment(): string
535542
{
536-
return $this->fragment ?? '';
543+
return $this->fragment;
537544
}
538545

539546
/**
@@ -690,7 +697,7 @@ public function setAuthority(string $str)
690697
$parts['path'] = $this->getPath();
691698
}
692699

693-
if (empty($parts['host']) && $parts['path'] !== '') {
700+
if (isset($parts['host']) && $parts['path'] !== '') {
694701
$parts['host'] = $parts['path'];
695702
unset($parts['path']);
696703
}
@@ -827,7 +834,7 @@ public function setPath(string $path)
827834
/**
828835
* Sets the current baseURL.
829836
*
830-
* @interal
837+
* @internal
831838
*
832839
* @deprecated Use SiteURI instead.
833840
*/
@@ -1031,35 +1038,48 @@ protected function filterPath(?string $path = null): string
10311038
/**
10321039
* Saves our parts from a parse_url call.
10331040
*
1041+
* @param array{
1042+
* host?: string,
1043+
* user?: string,
1044+
* path?: string,
1045+
* query?: string,
1046+
* fragment?: string,
1047+
* scheme?: string,
1048+
* port?: int,
1049+
* pass?: string,
1050+
* } $parts
1051+
*
10341052
* @return void
10351053
*/
10361054
protected function applyParts(array $parts)
10371055
{
1038-
if (! empty($parts['host'])) {
1056+
if (isset($parts['host']) && $parts['host'] !== '') {
10391057
$this->host = $parts['host'];
10401058
}
1041-
if (! empty($parts['user'])) {
1059+
1060+
if (isset($parts['user']) && $parts['user'] !== '') {
10421061
$this->user = $parts['user'];
10431062
}
1063+
10441064
if (isset($parts['path']) && $parts['path'] !== '') {
10451065
$this->path = $this->filterPath($parts['path']);
10461066
}
1047-
if (! empty($parts['query'])) {
1067+
1068+
if (isset($parts['query']) && $parts['query'] !== '') {
10481069
$this->setQuery($parts['query']);
10491070
}
1050-
if (! empty($parts['fragment'])) {
1071+
1072+
if (isset($parts['fragment']) && $parts['fragment'] !== '') {
10511073
$this->fragment = $parts['fragment'];
10521074
}
10531075

1054-
// Scheme
10551076
if (isset($parts['scheme'])) {
10561077
$this->setScheme(rtrim($parts['scheme'], ':/'));
10571078
} else {
10581079
$this->setScheme('http');
10591080
}
10601081

1061-
// Port
1062-
if (isset($parts['port']) && $parts['port'] !== null) {
1082+
if (isset($parts['port'])) {
10631083
// Valid port numbers are enforced by earlier parse_url or setPort()
10641084
$this->port = $parts['port'];
10651085
}

utils/phpstan-baseline/empty.notAllowed.neon

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 265 errors
1+
# total 251 errors
22

33
parameters:
44
ignoreErrors:
@@ -252,16 +252,6 @@ parameters:
252252
count: 2
253253
path: ../../system/HTTP/Response.php
254254

255-
-
256-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
257-
count: 4
258-
path: ../../system/HTTP/SiteURI.php
259-
260-
-
261-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
262-
count: 10
263-
path: ../../system/HTTP/URI.php
264-
265255
-
266256
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
267257
count: 2

utils/phpstan-baseline/loader.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 3541 errors
1+
# total 3517 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

utils/phpstan-baseline/missingType.iterableValue.neon

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 1628 errors
1+
# total 1623 errors
22

33
parameters:
44
ignoreErrors:
@@ -3932,11 +3932,6 @@ parameters:
39323932
count: 1
39333933
path: ../../system/HTTP/ResponseInterface.php
39343934

3935-
-
3936-
message: '#^Method CodeIgniter\\HTTP\\SiteURI\:\:applyParts\(\) has parameter \$parts with no value type specified in iterable type array\.$#'
3937-
count: 1
3938-
path: ../../system/HTTP/SiteURI.php
3939-
39403935
-
39413936
message: '#^Method CodeIgniter\\HTTP\\SiteURI\:\:baseUrl\(\) has parameter \$relativePath with no value type specified in iterable type array\.$#'
39423937
count: 1
@@ -3972,21 +3967,11 @@ parameters:
39723967
count: 1
39733968
path: ../../system/HTTP/SiteURI.php
39743969

3975-
-
3976-
message: '#^Method CodeIgniter\\HTTP\\URI\:\:applyParts\(\) has parameter \$parts with no value type specified in iterable type array\.$#'
3977-
count: 1
3978-
path: ../../system/HTTP/URI.php
3979-
39803970
-
39813971
message: '#^Method CodeIgniter\\HTTP\\URI\:\:changeSchemeAndPath\(\) return type has no value type specified in iterable type array\.$#'
39823972
count: 1
39833973
path: ../../system/HTTP/URI.php
39843974

3985-
-
3986-
message: '#^Method CodeIgniter\\HTTP\\URI\:\:getQuery\(\) has parameter \$options with no value type specified in iterable type array\.$#'
3987-
count: 1
3988-
path: ../../system/HTTP/URI.php
3989-
39903975
-
39913976
message: '#^Method CodeIgniter\\HTTP\\URI\:\:getSegments\(\) return type has no value type specified in iterable type array\.$#'
39923977
count: 1
@@ -4002,16 +3987,6 @@ parameters:
40023987
count: 1
40033988
path: ../../system/HTTP/URI.php
40043989

4005-
-
4006-
message: '#^Property CodeIgniter\\HTTP\\URI\:\:\$defaultPorts type has no value type specified in iterable type array\.$#'
4007-
count: 1
4008-
path: ../../system/HTTP/URI.php
4009-
4010-
-
4011-
message: '#^Property CodeIgniter\\HTTP\\URI\:\:\$query type has no value type specified in iterable type array\.$#'
4012-
count: 1
4013-
path: ../../system/HTTP/URI.php
4014-
40153990
-
40163991
message: '#^Property CodeIgniter\\HTTP\\URI\:\:\$segments type has no value type specified in iterable type array\.$#'
40173992
count: 1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
# total 3 errors
1+
# total 1 error
22

33
parameters:
44
ignoreErrors:
55
-
66
message: '#^Strict comparison using \!\=\= between mixed and null will always evaluate to true\.$#'
77
count: 1
88
path: ../../system/BaseModel.php
9-
10-
-
11-
message: '#^Strict comparison using \!\=\= between mixed and null will always evaluate to true\.$#'
12-
count: 1
13-
path: ../../system/HTTP/SiteURI.php
14-
15-
-
16-
message: '#^Strict comparison using \!\=\= between mixed and null will always evaluate to true\.$#'
17-
count: 1
18-
path: ../../system/HTTP/URI.php

0 commit comments

Comments
 (0)