From e386a6004d15971e977da150edf574fde03237ac Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Wed, 12 Aug 2020 17:43:33 +0200 Subject: [PATCH] bug fix issue #171 --- CHANGELOG.md | 1 + composer.json | 2 +- src/Uri.php | 6 +++--- src/UriString.php | 2 +- tests/UriTest.php | 8 ++++++++ 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d19dc0b1..55c02b27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ imporove `UriTemplate` implementation. - `UriTemplate` complete rewrite by reducing deep nested array usage. - Exception misleading message see issue [#167](https://github.com/thephpleague/uri/issues/167) +- `Uri::withScheme` Uri validation failed to catch the empty string as an invalid scheme. ### Deprecated diff --git a/composer.json b/composer.json index 2a30c17e..b0d95691 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ } ], "require": { - "php": "^7.2", + "php": ">=7.2", "ext-json": "*", "psr/http-message": "^1.0", "league/uri-interfaces": "^2.1" diff --git a/src/Uri.php b/src/Uri.php index f8f6074b..19067fb0 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -310,7 +310,7 @@ private function __construct( */ private function formatScheme(?string $scheme): ?string { - if ('' === $scheme || null === $scheme) { + if (null === $scheme) { return $scheme; } @@ -319,7 +319,7 @@ private function formatScheme(?string $scheme): ?string return $formatted_scheme; } - throw new SyntaxError(sprintf('The scheme `%s` is invalid', $scheme)); + throw new SyntaxError(sprintf('The scheme `%s` is invalid.', $scheme)); } /** @@ -438,7 +438,7 @@ private function formatRegisteredName(string $host): string $arr ); - if ($arr === []) { + if ([] === $arr) { throw new SyntaxError(sprintf('Host `%s` is invalid', $host)); } diff --git a/src/UriString.php b/src/UriString.php index 6e0987f0..7010cac8 100644 --- a/src/UriString.php +++ b/src/UriString.php @@ -472,7 +472,7 @@ private static function filterRegisteredName(string $host): string $retval = idn_to_ascii($formatted_host, 0, INTL_IDNA_VARIANT_UTS46, $arr); - if ($arr === []) { + if ([] === $arr) { throw new SyntaxError(sprintf('Host `%s` is not a valid IDN host', $host)); } diff --git a/tests/UriTest.php b/tests/UriTest.php index ffdef355..19a6ef6b 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -580,4 +580,12 @@ public function testIssue167ExceptionReasonMisleadingMessage(): void Uri::createFromString('file://example.org:80/home/jsmith/foo.txt'); } + + public function testIssue171TheEmptySchemeShouldThrow(): void + { + self::expectException(SyntaxError::class); + self::expectExceptionMessage('The scheme `` is invalid.'); + + Uri::createFromString('domain.com')->withScheme(''); + } }