Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 765fcd0

Browse files
committed
make string comparison more strict
1 parent bb833f8 commit 765fcd0

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/Uri.php

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function __construct($uri = '')
9898
));
9999
}
100100

101-
if (! empty($uri)) {
101+
if ('' !== $uri) {
102102
$this->parseUri($uri);
103103
}
104104
}
@@ -147,12 +147,12 @@ public function getScheme()
147147
*/
148148
public function getAuthority()
149149
{
150-
if (empty($this->host)) {
150+
if ('' === $this->host) {
151151
return '';
152152
}
153153

154154
$authority = $this->host;
155-
if (! empty($this->userInfo)) {
155+
if ('' !== $this->userInfo) {
156156
$authority = $this->userInfo . '@' . $authority;
157157
}
158158

@@ -476,27 +476,26 @@ private static function createUriString($scheme, $authority, $path, $query, $fra
476476
{
477477
$uri = '';
478478

479-
if (! empty($scheme)) {
479+
if ('' !== $scheme) {
480480
$uri .= sprintf('%s:', $scheme);
481481
}
482482

483-
if (! empty($authority)) {
483+
if ('' !== $authority) {
484484
$uri .= '//' . $authority;
485485
}
486486

487-
if ($path) {
488-
if (empty($path) || '/' !== substr($path, 0, 1)) {
489-
$path = '/' . $path;
490-
}
491-
492-
$uri .= $path;
487+
if ('' !== $path && '/' !== substr($path, 0, 1)) {
488+
$path = '/' . $path;
493489
}
494490

495-
if ($query) {
491+
$uri .= $path;
492+
493+
494+
if ('' !== $query) {
496495
$uri .= sprintf('?%s', $query);
497496
}
498497

499-
if ($fragment) {
498+
if ('' !== $fragment) {
500499
$uri .= sprintf('#%s', $fragment);
501500
}
502501

@@ -513,14 +512,11 @@ private static function createUriString($scheme, $authority, $path, $query, $fra
513512
*/
514513
private function isNonStandardPort($scheme, $host, $port)
515514
{
516-
if (! $scheme) {
517-
if ($host && ! $port) {
518-
return false;
519-
}
520-
return true;
515+
if ('' === $scheme) {
516+
return '' === $host || null !== $port;
521517
}
522518

523-
if (! $host || ! $port) {
519+
if ('' === $host || null === $port) {
524520
return false;
525521
}
526522

@@ -539,7 +535,7 @@ private function filterScheme($scheme)
539535
$scheme = strtolower($scheme);
540536
$scheme = preg_replace('#:(//)?$#', '', $scheme);
541537

542-
if (empty($scheme)) {
538+
if ('' === $scheme) {
543539
return '';
544540
}
545541

@@ -585,7 +581,7 @@ private function filterPath($path)
585581
$path
586582
);
587583

588-
if (empty($path)) {
584+
if ('' === $path) {
589585
// No path
590586
return $path;
591587
}
@@ -609,7 +605,7 @@ private function filterPath($path)
609605
*/
610606
private function filterQuery($query)
611607
{
612-
if (! empty($query) && strpos($query, '?') === 0) {
608+
if ('' !== $query && strpos($query, '?') === 0) {
613609
$query = substr($query, 1);
614610
}
615611

@@ -653,7 +649,7 @@ private function splitQueryValue($value)
653649
*/
654650
private function filterFragment($fragment)
655651
{
656-
if (! empty($fragment) && strpos($fragment, '#') === 0) {
652+
if ('' !== $fragment && strpos($fragment, '#') === 0) {
657653
$fragment = '%23' . substr($fragment, 1);
658654
}
659655

test/UriTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,4 +689,12 @@ public function testHostIsLowercaseWhenIsSetViwWithHost()
689689
$uri = (new Uri())->withHost('NEW-HOST.COM');
690690
$this->assertSame('new-host.com', $uri->getHost());
691691
}
692+
693+
694+
public function testUriDistinguishZeroFromEmptyString()
695+
{
696+
$expected = 'https://0:0@0:1/0?0#0';
697+
$uri = new Uri($expected);
698+
$this->assertSame($expected, (string) $uri);
699+
}
692700
}

0 commit comments

Comments
 (0)