Skip to content

Commit

Permalink
Simplify URI named constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jun 19, 2023
1 parent db2bec0 commit ba7aaaa
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 253 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ All Notable changes to `League\Uri` will be documented in this file

### Deprecated

- `League\Uri\Uri::createFromString` use `League\Uri\Uri::fromString`
- `League\Uri\Uri::createFromString` use `League\Uri\Uri::new`
- `League\Uri\Uri::createFromComponents` use `League\Uri\Uri::fromComponents`
- `League\Uri\Uri::createFromUri` use `League\Uri\Uri::fromUri`
- `League\Uri\Uri::createFromUri` use `League\Uri\Uri::new`
- `League\Uri\Uri::createFromBaseUri` use `League\Uri\Uri::fromBaseUri`
- `League\Uri\Uri::createFromServer` use `League\Uri\Uri::fromServer`
- `League\Uri\Uri::createFromWindowsPath` use `League\Uri\Uri::fromWindowsPath`
- `League\Uri\Uri::createFromUnixPath` use `League\Uri\Uri::fromUnixPath`
- `League\Uri\Uri::createFromDataPath` use `League\Uri\Uri::fromDataPath`
- `League\Uri\Http::createFromString` use `League\Uri\Http::fromString`
- `League\Uri\Http::createFromString` use `League\Uri\Http::new`
- `League\Uri\Http::createFromComponents` use `League\Uri\Http::fromComponents`
- `League\Uri\Http::createFromUri` use `League\Uri\Http::fromUri`
- `League\Uri\Http::createFromUri` use `League\Uri\Http::new`
- `League\Uri\Http::createFromBaseUri` use `League\Uri\Http::fromBaseUri`
- `League\Uri\Http::createFromServer` use `League\Uri\Http::fromServer`
- `League\Uri\UriTemplate\Template::createFromString` use `League\Uri\UriTemplate\Template::fromString`
Expand Down
18 changes: 9 additions & 9 deletions 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::fromString('data:')
(string) Uri::new('data:')
);
}

Expand All @@ -34,7 +34,7 @@ public function testDefaultConstructor(): void
*/
public function testCreateFromString(string $uri, string $path): void
{
self::assertSame($path, Uri::fromString($uri)->getPath());
self::assertSame($path, Uri::new($uri)->getPath());
}

public static function validUrlProvider(): array
Expand Down Expand Up @@ -69,7 +69,7 @@ public static function validUrlProvider(): array
public function testCreateFromStringFailed(string $uri): void
{
self::expectException(SyntaxError::class);
Uri::fromString($uri);
Uri::new($uri);
}

public static function invalidUrlProvider(): array
Expand All @@ -86,7 +86,7 @@ public static function invalidUrlProvider(): array
public function testCreateFromStringFailedWithWrongComponent(string $uri): void
{
self::expectException(SyntaxError::class);
Uri::fromString($uri);
Uri::new($uri);
}

public static function invalidComponentProvider(): array
Expand All @@ -101,31 +101,31 @@ public static function invalidComponentProvider(): array
public function testCreateFromComponentsFailedWithInvalidArgumentException(): void
{
self::expectException(SyntaxError::class);
Uri::fromString('data:image/png;base64,°28');
Uri::new('data:image/png;base64,°28');
}

public function testCreateFromComponentsFailedInvalidMediatype(): void
{
self::expectException(SyntaxError::class);
Uri::fromString('data:image/png;base64=toto;base64,dsqdfqfd');
Uri::new('data:image/png;base64=toto;base64,dsqdfqfd');
}

public function testCreateFromComponentsFailedWithException(): void
{
self::expectException(SyntaxError::class);
Uri::fromString('data:text/plain;charset=us-ascii,Bonjour%20le%20monde%21#fragment');
Uri::new('data:text/plain;charset=us-ascii,Bonjour%20le%20monde%21#fragment');
}

public function testWithPath(): void
{
$path = 'text/plain;charset=us-ascii,Bonjour%20le%20monde%21';
$uri = Uri::fromString('data:'.$path);
$uri = Uri::new('data:'.$path);
self::assertSame($uri, $uri->withPath($path));
}

public function testSyntaxError(): void
{
self::expectException(SyntaxError::class);
Uri::fromString('http:text/plain;charset=us-ascii,Bonjour%20le%20monde%21');
Uri::new('http:text/plain;charset=us-ascii,Bonjour%20le%20monde%21');
}
}
12 changes: 6 additions & 6 deletions FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,17 @@ public static function windowLocalPathProvider(): array
public function testCreateFromUri(): void
{
$expected = 'https://login:[email protected]:443/test/query.php?kingkong=toto#doc3';
$psr7 = Http::fromString($expected);
$leagueUri = Uri::fromString($expected);
$psr7 = Http::new($expected);
$leagueUri = Uri::new($expected);

$uriFromPsr7 = Uri::fromUri($psr7);
$uriFromLeagueUri = Uri::fromUri($leagueUri);
$uriFromPsr7 = Uri::new($psr7);
$uriFromLeagueUri = Uri::new($leagueUri);

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

$uribis = Http::fromString();
self::assertSame((string) $uribis, Uri::fromUri($uribis)->toString());
$uribis = Http::new();
self::assertSame((string) $uribis, Uri::new($uribis)->toString());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ final class FileTest extends TestCase
{
public function testDefaultConstructor(): void
{
self::assertSame('', (string) Uri::fromString());
self::assertSame('', (string) Uri::new());
}

/**
* @dataProvider validUrlProvider
*/
public function testCreateFromString(string $uri, string $expected): void
{
self::assertSame($expected, (string) Uri::fromString($uri));
self::assertSame($expected, (string) Uri::new($uri));
}

public static function validUrlProvider(): array
Expand Down Expand Up @@ -86,7 +86,7 @@ public static function validUrlProvider(): array
public function testConstructorThrowsException(string $uri): void
{
self::expectException(SyntaxError::class);
Uri::fromString($uri);
Uri::new($uri);
}

public static function invalidUrlProvider(): array
Expand Down
8 changes: 4 additions & 4 deletions FtpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class FtpTest extends TestCase
*/
public function testCreateFromString(string $uri, string $expected): void
{
self::assertSame($expected, (string) Uri::fromString($uri));
self::assertSame($expected, (string) Uri::new($uri));
}

public static function validUrlProvider(): array
Expand Down Expand Up @@ -67,7 +67,7 @@ public static function validUrlProvider(): array
public function testConstructorThrowInvalidArgumentException(string $uri): void
{
self::expectException(SyntaxError::class);
Uri::fromString($uri);
Uri::new($uri);
}

public static function invalidUrlProvider(): array
Expand All @@ -83,7 +83,7 @@ public static function invalidUrlProvider(): array
public function testModificationFailedWithEmptyAuthority(): void
{
self::expectException(SyntaxError::class);
Uri::fromString('ftp://example.com/path')
Uri::new('ftp://example.com/path')
->withScheme(null)
->withHost(null)
->withPath('//toto');
Expand All @@ -94,7 +94,7 @@ public function testModificationFailedWithEmptyAuthority(): void
*/
public function testPort(string $uri, ?int $port): void
{
self::assertSame($port, Uri::fromString($uri)->getPort());
self::assertSame($port, Uri::new($uri)->getPort());
}

public static function portProvider(): array
Expand Down
22 changes: 7 additions & 15 deletions Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,16 @@ private function validate(UriInterface $uri): void
}

/**
* Create a new instance from a URI object.
* Create a new instance from a string or a stringable object.
*/
public static function fromUri(Psr7UriInterface|UriInterface $uri): self
public static function new(Stringable|string $uri = ''): self
{
return match (true) {
$uri instanceof UriInterface => new self($uri),
default => new self(Uri::fromUri($uri)),
default => new self(Uri::new($uri)),
};
}

/**
* Create a new instance from a string.
*/
public static function fromString(Stringable|string $uri = ''): self
{
return new self(Uri::fromString($uri));
}

/**
* Create a new instance from a hash of parse_url parts.
*
Expand Down Expand Up @@ -205,13 +197,13 @@ public function withFragment(string $fragment): self
*
* @deprecated Since version 7.0.0
* @codeCoverageIgnore
* @see Http::fromString()
* @see Http::new()
*
* Create a new instance from a string.
*/
public static function createFromString(Stringable|string $uri = ''): self
{
return self::fromString($uri);
return self::new($uri);
}

/**
Expand Down Expand Up @@ -250,13 +242,13 @@ public static function createFromServer(array $server): self
*
* @deprecated Since version 7.0.0
* @codeCoverageIgnore
* @see Http::fromUri()
* @see Http::new()
*
* Create a new instance from a URI object.
*/
public static function createFromUri(Psr7UriInterface|UriInterface $uri): self
{
return self::fromUri($uri);
return self::new($uri);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion HttpBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function benchBuildingAnUriFromUriComponents(): void
public function benchBuildingAnUriFromUriComponentsMutation(): void
{
for ($i = 0; $i < 100_000; $i++) {
Http::fromString()
Http::new()
->withPath('/5.0')
->withQuery('q=val1&q=val2&query[3]=val3')
->withFragment('foobar')
Expand Down
2 changes: 1 addition & 1 deletion HttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ final class HttpFactory implements UriFactoryInterface
{
public function createUri(string $uri = ''): UriInterface
{
return Http::fromString($uri);
return Http::new($uri);
}
}
Loading

0 comments on commit ba7aaaa

Please sign in to comment.