Skip to content

Commit

Permalink
Update common package documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 28, 2023
1 parent 8555d5c commit 37a0e04
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
3 changes: 0 additions & 3 deletions Idna/Idna.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public static function toAscii(string $domain, int $options): IdnaInfo
if (1 === preg_match(self::REGEXP_IDNA_PATTERN, $domain)) {
self::supportsIdna();

/* @param-out array{errors: int, isTransitionalDifferent: bool, result: string} $idnaInfo */
idn_to_ascii($domain, $options, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
if ([] === $idnaInfo) {
return IdnaInfo::fromIntl([
Expand Down Expand Up @@ -164,13 +163,11 @@ public static function toUnicode(string $domain, int $options): IdnaInfo

self::supportsIdna();

/* @param-out array{errors: int, isTransitionalDifferent: bool, result: string} $idnaInfo */
idn_to_utf8($domain, $options, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
if ([] === $idnaInfo) {
throw IdnaConversionFailed::dueToInvalidHost($domain);
}

/* @var array{errors: int, isTransitionalDifferent: bool, result: string} $idnaInfo */
return IdnaInfo::fromIntl($idnaInfo);
}

Expand Down
9 changes: 8 additions & 1 deletion Idna/IdnaInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
final class IdnaInfo
{
private const ERRORS = [
Idna::ERROR_NONE => 'No error has occurred',
Idna::ERROR_EMPTY_LABEL => 'a non-final domain name label (or the whole domain name) is empty',
Idna::ERROR_LABEL_TOO_LONG => 'a domain name label is longer than 63 bytes',
Idna::ERROR_DOMAIN_NAME_TOO_LONG => 'a domain name is longer than 255 bytes in its storage form',
Expand Down Expand Up @@ -49,11 +50,17 @@ private function __construct(
private readonly bool $isTransitionalDifferent,
private readonly int $errors
) {
$this->errorList = array_filter(
$errorList = array_filter(
self::ERRORS,
fn (int $error): bool => 0 !== ($error & $this->errors),
ARRAY_FILTER_USE_KEY
);

if (0 === $this->errors) {
$errorList[Idna::ERROR_NONE] = self::ERRORS[Idna::ERROR_NONE];
}

$this->errorList = $errorList;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Idna/IdnaInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testItCanBeInstantiatedFromArray(): void
self::assertFalse($result->isTransitionalDifferent());
self::assertSame(0, $result->errors());
self::assertNull($result->error(Idna::ERROR_BIDI));
self::assertCount(0, $result->errorList());
self::assertSame([Idna::ERROR_NONE => 'No error has occurred'], $result->errorList());
}

public function testInvalidSyntaxAfterIDNConversion(): void
Expand Down
26 changes: 6 additions & 20 deletions Idna/IdnaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ final class IdnaTest extends TestCase
*/
public function testToAsciiThrowsException(string $domain): void
{
$result = Idna::toAscii($domain, Idna::IDNA2008_ASCII);

self::assertNotEmpty($result->errors());
self::assertNotEmpty(Idna::toAscii($domain, Idna::IDNA2008_ASCII)->errors());
}

/**
Expand All @@ -46,20 +44,15 @@ public static function invalidDomainProvider(): iterable

public function testToUnicodeThrowsException(): void
{
$result = Idna::toUnicode('xn--a-ecp.ru', Idna::IDNA2008_UNICODE);

self::assertNotEmpty($result->errors());
self::assertNotEmpty(Idna::toUnicode('xn--a-ecp.ru', Idna::IDNA2008_UNICODE)->errors());
}

/**
* @dataProvider toUnicodeProvider
*/
public function testToIDN(string $domain, string $expectedDomain): void
{
self::assertSame(
$expectedDomain,
Idna::toUnicode($domain, Idna::IDNA2008_UNICODE)->result()
);
self::assertSame($expectedDomain, Idna::toUnicode($domain, Idna::IDNA2008_UNICODE)->result());
}

/**
Expand Down Expand Up @@ -100,10 +93,7 @@ public static function toUnicodeProvider(): iterable
*/
public function testToAscii(string $domain, string $expectedDomain): void
{
self::assertSame(
$expectedDomain,
Idna::toAscii($domain, Idna::IDNA2008_ASCII)->result()
);
self::assertSame($expectedDomain, Idna::toAscii($domain, Idna::IDNA2008_ASCII)->result());
}

/**
Expand Down Expand Up @@ -133,15 +123,11 @@ public static function toAsciiProvider(): iterable

public function testExceptionThrownOnConversionToAsciiIfTheDomainIsTooLong(): void
{
$result = Idna::toAscii(str_repeat('A', 255), Idna::IDNA2008_ASCII);

self::assertNotEmpty($result->errors());
self::assertNotEmpty(Idna::toAscii(str_repeat('A', 255), Idna::IDNA2008_ASCII)->errorList());
}

public function testExceptionThrownOnConversionToAsciiIfTheDomainLabelIsTooLong(): void
{
$result = Idna::toAscii('aa'.str_repeat('A', 64), Idna::IDNA2008_ASCII);

self::assertNotEmpty($result->errors());
self::assertNotEmpty(Idna::toAscii('aa'.str_repeat('A', 64), Idna::IDNA2008_ASCII)->errorList());
}
}

0 comments on commit 37a0e04

Please sign in to comment.