@@ -113,14 +113,19 @@ class URI implements Stringable
113
113
/**
114
114
* The query string.
115
115
*
116
- * @var array
116
+ * @var array<string, string>
117
117
*/
118
118
protected $ query = [];
119
119
120
120
/**
121
121
* Default schemes/ports.
122
122
*
123
- * @var array
123
+ * @var array{
124
+ * http: int,
125
+ * https: int,
126
+ * ftp: int,
127
+ * sftp: int,
128
+ * }
124
129
*/
125
130
protected $ defaultPorts = [
126
131
'http ' => 80 ,
@@ -166,25 +171,26 @@ public static function createURIString(
166
171
?string $ fragment = null ,
167
172
): string {
168
173
$ uri = '' ;
169
- if ($ scheme !== null && $ scheme !== '' ) {
174
+
175
+ if ((string ) $ scheme !== '' ) {
170
176
$ uri .= $ scheme . ':// ' ;
171
177
}
172
178
173
- if ($ authority !== null && $ authority !== '' ) {
179
+ if (( string ) $ authority !== '' ) {
174
180
$ uri .= $ authority ;
175
181
}
176
182
177
- if (isset ( $ path ) && $ path !== '' ) {
183
+ if (( string ) $ path !== '' ) {
178
184
$ uri .= ! str_ends_with ($ uri , '/ ' )
179
185
? '/ ' . ltrim ($ path , '/ ' )
180
186
: ltrim ($ path , '/ ' );
181
187
}
182
188
183
- if ($ query !== '' && $ query !== null ) {
189
+ if (( string ) $ query !== '' ) {
184
190
$ uri .= '? ' . $ query ;
185
191
}
186
192
187
- if ($ fragment !== '' && $ fragment !== null ) {
193
+ if (( string ) $ fragment !== '' ) {
188
194
$ uri .= '# ' . $ fragment ;
189
195
}
190
196
@@ -329,7 +335,7 @@ public function setURI(?string $uri = null)
329
335
* The trailing ":" character is not part of the scheme and MUST NOT be
330
336
* added.
331
337
*
332
- * @see https://tools.ietf.org/html/rfc3986#section-3.1
338
+ * @see https://tools.ietf.org/html/rfc3986#section-3.1
333
339
*
334
340
* @return string The URI scheme.
335
341
*/
@@ -359,19 +365,18 @@ public function getScheme(): string
359
365
*/
360
366
public function getAuthority (bool $ ignorePort = false ): string
361
367
{
362
- if (empty ( $ this ->host ) ) {
368
+ if ($ this ->host === '' ) {
363
369
return '' ;
364
370
}
365
371
366
372
$ authority = $ this ->host ;
367
373
368
- if (! empty ( $ this ->getUserInfo ()) ) {
374
+ if (( string ) $ this ->getUserInfo () !== '' ) {
369
375
$ authority = $ this ->getUserInfo () . '@ ' . $ authority ;
370
376
}
371
377
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 ]) {
375
380
$ authority .= ': ' . $ this ->port ;
376
381
}
377
382
@@ -404,7 +409,7 @@ public function getUserInfo()
404
409
{
405
410
$ userInfo = $ this ->user ;
406
411
407
- if ($ this ->showPassword === true && ! empty ( $ this ->password ) ) {
412
+ if ($ this ->showPassword === true && $ this ->password !== '' ) {
408
413
$ userInfo .= ': ' . $ this ->password ;
409
414
}
410
415
@@ -434,13 +439,13 @@ public function showPassword(bool $val = true)
434
439
* The value returned MUST be normalized to lowercase, per RFC 3986
435
440
* Section 3.2.2.
436
441
*
437
- * @see http://tools.ietf.org/html/rfc3986#section-3.2.2
442
+ * @see http://tools.ietf.org/html/rfc3986#section-3.2.2
438
443
*
439
444
* @return string The URI host.
440
445
*/
441
446
public function getHost (): string
442
447
{
443
- return $ this ->host ?? '' ;
448
+ return $ this ->host ;
444
449
}
445
450
446
451
/**
@@ -491,11 +496,13 @@ public function getPort()
491
496
*/
492
497
public function getPath (): string
493
498
{
494
- return $ this ->path ?? '' ;
499
+ return $ this ->path ;
495
500
}
496
501
497
502
/**
498
503
* Retrieve the query string
504
+ *
505
+ * @param array{except?: list<string>|string, only?: list<string>|string} $options
499
506
*/
500
507
public function getQuery (array $ options = []): string
501
508
{
@@ -525,15 +532,15 @@ public function getQuery(array $options = []): string
525
532
$ vars = $ temp ;
526
533
}
527
534
528
- return empty ( $ vars) ? '' : http_build_query ($ vars );
535
+ return $ vars === [] ? '' : http_build_query ($ vars );
529
536
}
530
537
531
538
/**
532
539
* Retrieve a URI fragment
533
540
*/
534
541
public function getFragment (): string
535
542
{
536
- return $ this ->fragment ?? '' ;
543
+ return $ this ->fragment ;
537
544
}
538
545
539
546
/**
@@ -690,7 +697,7 @@ public function setAuthority(string $str)
690
697
$ parts ['path ' ] = $ this ->getPath ();
691
698
}
692
699
693
- if (empty ($ parts ['host ' ]) && $ parts ['path ' ] !== '' ) {
700
+ if (isset ($ parts ['host ' ]) && $ parts ['path ' ] !== '' ) {
694
701
$ parts ['host ' ] = $ parts ['path ' ];
695
702
unset($ parts ['path ' ]);
696
703
}
@@ -827,7 +834,7 @@ public function setPath(string $path)
827
834
/**
828
835
* Sets the current baseURL.
829
836
*
830
- * @interal
837
+ * @internal
831
838
*
832
839
* @deprecated Use SiteURI instead.
833
840
*/
@@ -1031,35 +1038,48 @@ protected function filterPath(?string $path = null): string
1031
1038
/**
1032
1039
* Saves our parts from a parse_url call.
1033
1040
*
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
+ *
1034
1052
* @return void
1035
1053
*/
1036
1054
protected function applyParts (array $ parts )
1037
1055
{
1038
- if (! empty ($ parts ['host ' ])) {
1056
+ if (isset ($ parts ['host ' ]) && $ parts [ ' host ' ] !== '' ) {
1039
1057
$ this ->host = $ parts ['host ' ];
1040
1058
}
1041
- if (! empty ($ parts ['user ' ])) {
1059
+
1060
+ if (isset ($ parts ['user ' ]) && $ parts ['user ' ] !== '' ) {
1042
1061
$ this ->user = $ parts ['user ' ];
1043
1062
}
1063
+
1044
1064
if (isset ($ parts ['path ' ]) && $ parts ['path ' ] !== '' ) {
1045
1065
$ this ->path = $ this ->filterPath ($ parts ['path ' ]);
1046
1066
}
1047
- if (! empty ($ parts ['query ' ])) {
1067
+
1068
+ if (isset ($ parts ['query ' ]) && $ parts ['query ' ] !== '' ) {
1048
1069
$ this ->setQuery ($ parts ['query ' ]);
1049
1070
}
1050
- if (! empty ($ parts ['fragment ' ])) {
1071
+
1072
+ if (isset ($ parts ['fragment ' ]) && $ parts ['fragment ' ] !== '' ) {
1051
1073
$ this ->fragment = $ parts ['fragment ' ];
1052
1074
}
1053
1075
1054
- // Scheme
1055
1076
if (isset ($ parts ['scheme ' ])) {
1056
- $ this ->setScheme (rtrim ( $ parts ['scheme ' ], ' :/ ' ) );
1077
+ $ this ->setScheme ($ parts ['scheme ' ]);
1057
1078
} else {
1058
1079
$ this ->setScheme ('http ' );
1059
1080
}
1060
1081
1061
- // Port
1062
- if (isset ($ parts ['port ' ]) && $ parts ['port ' ] !== null ) {
1082
+ if (isset ($ parts ['port ' ])) {
1063
1083
// Valid port numbers are enforced by earlier parse_url or setPort()
1064
1084
$ this ->port = $ parts ['port ' ];
1065
1085
}
0 commit comments