@@ -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?: string|list<string>, only?: string|list<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 ' ])) {
1056- $ this ->setScheme (rtrim ( $ parts ['scheme ' ], ' :/ ' ) );
1077+ $ this ->setScheme ($ 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 }
0 commit comments