Skip to content

Commit 594a841

Browse files
committed
🚿 extract uriIsDefaultPort()
1 parent 9a348c1 commit 594a841

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/Psr7/Uri.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@
2020

2121
class Uri implements UriInterface{
2222

23-
protected const DEFAULT_PORTS = [
24-
'http' => 80,
25-
'https' => 443,
26-
'ftp' => 21,
27-
'gopher' => 70,
28-
'nntp' => 119,
29-
'news' => 119,
30-
'telnet' => 23,
31-
'tn3270' => 23,
32-
'imap' => 143,
33-
'pop' => 110,
34-
'ldap' => 389,
35-
];
36-
3723
protected string $scheme = '';
3824

3925
protected string $user = '';
@@ -498,7 +484,7 @@ function(array $match):string{
498484
*/
499485
protected function removeDefaultPort():void{
500486

501-
if($this->port !== null && (isset($this::DEFAULT_PORTS[$this->scheme]) && $this->port === $this::DEFAULT_PORTS[$this->scheme])){
487+
if(uriIsDefaultPort($this)){
502488
$this->port = null;
503489
}
504490

src/Psr7/message_helpers.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,27 @@ function decompress_content(MessageInterface $message):string{
500500

501501
}
502502

503+
const URI_DEFAULT_PORTS = [
504+
'http' => 80,
505+
'https' => 443,
506+
'ftp' => 21,
507+
'gopher' => 70,
508+
'nntp' => 119,
509+
'news' => 119,
510+
'telnet' => 23,
511+
'tn3270' => 23,
512+
'imap' => 143,
513+
'pop' => 110,
514+
'ldap' => 389,
515+
];
516+
517+
function uriIsDefaultPort(UriInterface $uri):bool{
518+
$port = $uri->getPort();
519+
$scheme = $uri->getScheme();
520+
521+
return $port === null || (isset(URI_DEFAULT_PORTS[$scheme]) && $port === URI_DEFAULT_PORTS[$scheme]);
522+
}
523+
503524
/**
504525
* Whether the URI is absolute, i.e. it has a scheme.
505526
*

0 commit comments

Comments
 (0)