Skip to content

Commit

Permalink
Improve package DX
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jun 20, 2023
1 parent 6a54545 commit afcfaf3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 45 deletions.
2 changes: 1 addition & 1 deletion DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testDefaultConstructor(): void
{
self::assertSame(
'data:text/plain;charset=us-ascii,',
(string) Uri::new('data:')
Uri::new('data:')->toString()
);
}

Expand Down
16 changes: 8 additions & 8 deletions FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function validFilePath(): array
*/
public function testCreateFromUnixPath(string $uri, string $expected): void
{
self::assertSame($expected, (string) Uri::fromUnixPath($uri));
self::assertSame($expected, Uri::fromUnixPath($uri)->toString());
}

public static function unixpathProvider(): array
Expand Down Expand Up @@ -99,7 +99,7 @@ public static function unixpathProvider(): array
*/
public function testCreateFromWindowsLocalPath(string $uri, string $expected): void
{
self::assertSame($expected, (string) Uri::fromWindowsPath($uri));
self::assertSame($expected, Uri::fromWindowsPath($uri)->toString());
}

public static function windowLocalPathProvider(): array
Expand Down Expand Up @@ -149,8 +149,8 @@ public function testCreateFromUri(): void
$uriFromPsr7 = Uri::new($psr7);
$uriFromLeagueUri = Uri::new($leagueUri);

self::assertSame((string) $psr7, (string) $uriFromPsr7);
self::assertSame((string) $psr7, (string) $uriFromLeagueUri);
self::assertSame((string) $psr7, $uriFromPsr7->toString());
self::assertSame((string) $psr7, $uriFromLeagueUri->toString());

$uribis = Http::new();
self::assertSame((string) $uribis, Uri::new($uribis)->toString());
Expand All @@ -161,7 +161,7 @@ public function testCreateFromUri(): void
*/
public function testCreateFromServer(string $expected, array $input): void
{
self::assertSame($expected, (string) Uri::fromServer($input));
self::assertSame($expected, Uri::fromServer($input)->toString());
self::assertSame($expected, (string) Http::fromServer($input));
}

Expand Down Expand Up @@ -339,7 +339,7 @@ public function testFailCreateFromServerWithoutInvalidUserInfo(): void
*/
public function testCreateFromBaseUri(string $base_uri, string $uri, string $expected): void
{
self::assertSame($expected, (string) Uri::fromBaseUri($uri, $base_uri));
self::assertSame($expected, Uri::fromBaseUri($uri, $base_uri)->toString());
}

public static function createProvider(): array
Expand Down Expand Up @@ -405,15 +405,15 @@ public function testCreateWithUriWithoutAuthority(): void
{
self::assertSame(
'data:text/plain;charset=us-ascii,',
(string) Uri::fromBaseUri('data:text/plain;charset=us-ascii,')
Uri::fromBaseUri('data:text/plain;charset=us-ascii,')->toString()
);
}

public function testCreateWithAbsoluteUriWithoutBaseUri(): void
{
self::assertSame(
'scheme://host/sky?q#f',
(string) Uri::fromBaseUri('scheme://host/path/../sky?q#f')
Uri::fromBaseUri('scheme://host/path/../sky?q#f')->toString()
);
}

Expand Down
2 changes: 1 addition & 1 deletion Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private function filterInput(string $str): string|null
private function newInstance(UriInterface $uri): self
{
return match (true) {
(string) $uri === (string) $this->uri => $this,
$uri->toString() === $this->uri->toString() => $this,
default => new self($uri),
};
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Highlights
System Requirements
-------

- You require **PHP >= 7.3** but the latest stable version of PHP is recommended
- You require **PHP >= 8.1** but the latest stable version of PHP is recommended
- You will need the **ext-intl** to handle i18n URI.
- Since version 6.2.0 you will need the **ext-fileinfo** to handle Data URI creation from a filepath.

Expand Down
24 changes: 10 additions & 14 deletions UriInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private function __construct()
/**
* Input URI normalization to allow Stringable and string URI.
*/
private static function filterUri(Psr7UriInterface|UriInterface|Stringable|string $uri): Psr7UriInterface|UriInterface
private static function filterUri(Stringable|string $uri): Psr7UriInterface|UriInterface
{
return match (true) {
$uri instanceof Psr7UriInterface, $uri instanceof UriInterface => $uri,
Expand Down Expand Up @@ -88,7 +88,7 @@ private static function normalize(Psr7UriInterface|UriInterface $uri): Psr7UriIn
/**
* Tells whether the URI represents an absolute URI.
*/
public static function isAbsolute(Psr7UriInterface|UriInterface|Stringable|string $uri): bool
public static function isAbsolute(Stringable|string $uri): bool
{
$uri = self::filterUri($uri);

Expand All @@ -98,7 +98,7 @@ public static function isAbsolute(Psr7UriInterface|UriInterface|Stringable|strin
/**
* Tell whether the URI represents a network path.
*/
public static function isNetworkPath(Psr7UriInterface|UriInterface|Stringable|string $uri): bool
public static function isNetworkPath(Stringable|string $uri): bool
{
$uri = self::filterUri($uri);
$null = self::emptyComponentValue($uri);
Expand All @@ -109,7 +109,7 @@ public static function isNetworkPath(Psr7UriInterface|UriInterface|Stringable|st
/**
* Tells whether the URI represents an absolute path.
*/
public static function isAbsolutePath(Psr7UriInterface|UriInterface|Stringable|string $uri): bool
public static function isAbsolutePath(Stringable|string $uri): bool
{
$uri = self::filterUri($uri);
$null = self::emptyComponentValue($uri);
Expand All @@ -123,7 +123,7 @@ public static function isAbsolutePath(Psr7UriInterface|UriInterface|Stringable|s
* Tell whether the URI represents a relative path.
*
*/
public static function isRelativePath(Psr7UriInterface|UriInterface|Stringable|string $uri): bool
public static function isRelativePath(Stringable|string $uri): bool
{
$uri = self::filterUri($uri);
$null = self::emptyComponentValue($uri);
Expand All @@ -136,10 +136,8 @@ public static function isRelativePath(Psr7UriInterface|UriInterface|Stringable|s
/**
* Tells whether both URI refers to the same document.
*/
public static function isSameDocument(
Psr7UriInterface|UriInterface|Stringable|string $uri,
Psr7UriInterface|UriInterface|Stringable|string $baseUri
): bool {
public static function isSameDocument(Stringable|string $uri, Stringable|string $baseUri): bool
{
$uri = self::normalize(self::filterUri($uri));
$baseUri = self::normalize(self::filterUri($baseUri));

Expand All @@ -156,7 +154,7 @@ public static function isSameDocument(
* For URI with the file scheme the method will return null (as this is left to the implementation decision)
* For URI with a special scheme the method returns the scheme followed by its authority (without the userinfo part)
*/
public static function getOrigin(Psr7UriInterface|UriInterface|Stringable|string $uri): ?string
public static function getOrigin(Stringable|string $uri): ?string
{
$uri = self::filterUri($uri);
$scheme = $uri->getScheme();
Expand All @@ -179,10 +177,8 @@ public static function getOrigin(Psr7UriInterface|UriInterface|Stringable|string
*
* @see UriInfo::getOrigin()
*/
public static function isCrossOrigin(
Psr7UriInterface|UriInterface|Stringable|string $uri,
Psr7UriInterface|UriInterface|Stringable|string $baseUri
): bool {
public static function isCrossOrigin(Stringable|string $uri, Stringable|string $baseUri): bool
{
$uri = self::filterUri($uri);
$baseUri = self::filterUri($baseUri);

Expand Down
27 changes: 11 additions & 16 deletions UriResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private function __construct()
/**
* Input URI normalization to allow Stringable and string URI.
*/
private static function filterUri(Psr7UriInterface|UriInterface|Stringable|string $uri): Psr7UriInterface|UriInterface
private static function filterUri(Stringable|string $uri): Psr7UriInterface|UriInterface
{
return match (true) {
$uri instanceof Psr7UriInterface, $uri instanceof UriInterface => $uri,
Expand All @@ -58,13 +58,10 @@ private static function filterUri(Psr7UriInterface|UriInterface|Stringable|strin
* If the first argument is a UriInterface the method returns a UriInterface object
* If the first argument is a Psr7UriInterface the method returns a Psr7UriInterface object
*/
public static function resolve(
Psr7UriInterface|UriInterface|Stringable|string $uri,
Psr7UriInterface|UriInterface|Stringable|string $baseUri
): Psr7UriInterface|UriInterface {
public static function resolve(Stringable|string $uri, Stringable|string $baseUri): Psr7UriInterface|UriInterface
{
$uri = self::filterUri($uri);
$baseUri = self::filterUri($baseUri);

$null = $uri instanceof Psr7UriInterface ? '' : null;

if ($null !== $uri->getScheme()) {
Expand All @@ -85,11 +82,11 @@ public static function resolve(
[$user, $pass] = explode(':', $userInfo, 2) + [1 => null];
}

[$uri_path, $uri_query] = self::resolvePathAndQuery($uri, $baseUri);
[$path, $query] = self::resolvePathAndQuery($uri, $baseUri);

return $uri
->withPath(self::removeDotSegments($uri_path))
->withQuery($uri_query)
->withPath(self::removeDotSegments($path))
->withQuery($query)
->withHost($baseUri->getHost())
->withPort($baseUri->getPort())
->withUserInfo((string) $user, $pass)
Expand All @@ -106,9 +103,9 @@ private static function removeDotSegments(string $path): string
return $path;
}

$old_segments = explode('/', $path);
$new_path = implode('/', array_reduce($old_segments, UriResolver::reducer(...), []));
if (isset(self::DOT_SEGMENTS[end($old_segments)])) {
$oldSegments = explode('/', $path);
$new_path = implode('/', array_reduce($oldSegments, UriResolver::reducer(...), []));
if (isset(self::DOT_SEGMENTS[end($oldSegments)])) {
$new_path .= '/';
}

Expand Down Expand Up @@ -201,10 +198,8 @@ private static function resolvePathAndQuery(
* This method MUST be transparent when dealing with error and exceptions.
* It MUST not alter of silence them apart from validating its own parameters.
*/
public static function relativize(
Psr7UriInterface|UriInterface|Stringable|string $uri,
Psr7UriInterface|UriInterface|Stringable|string $baseUri
): Psr7UriInterface|UriInterface {
public static function relativize(Stringable|string $uri, Stringable|string $baseUri): Psr7UriInterface|UriInterface
{
$uri = self::filterUri($uri);
$baseUri = self::filterUri($baseUri);

Expand Down
3 changes: 2 additions & 1 deletion UriTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function withDefaultVariables(iterable $defaultVariables): self
}

/**
* @throws TemplateCanNotBeExpanded only if the variable contains nested array values
* @throws TemplateCanNotBeExpanded if the variable contains nested array values
* @throws UriException if the resulting expansion can not be converted to a UriInterface instance
*/
public function expand(iterable $variables = []): UriInterface
Expand All @@ -82,6 +82,7 @@ public function expand(iterable $variables = []): UriInterface

/**
* @throws TemplateCanNotBeExpanded if the expansion fails
* @throws TemplateCanNotBeExpanded if the variable contains nested array values
* @throws UriException if the resulting expansion can not be converted to a UriInterface instance
*/
public function expandOrFail(iterable $variables): UriInterface
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"require": {
"php": "^8.1",
"ext-json": "*",
"league/uri-interfaces": "dev-master",
"league/uri-interfaces": "7.x-dev",
"psr/http-message": "^2.0"
},
"autoload": {
Expand All @@ -58,10 +58,11 @@
"league/uri-schemes": "^1.0"
},
"suggest": {
"league/uri-components" : "Needed to easily manipulate URI objects",
"ext-intl" : "Needed to improve host validation",
"ext-fileinfo": "Needed to create Data URI from a filepath",
"psr/http-factory": "Needed to use the URI factory"
"league/uri-components" : "Needed to easily manipulate URI objects",
"psr/http-factory": "Needed to use the URI factory",
"symfony/polyfill-intl-idn": "to use the IDNA feature via Symfony Polyfill"
},
"extra": {
"branch-alias": {
Expand Down

0 comments on commit afcfaf3

Please sign in to comment.