diff --git a/Idna/Idna.php b/Idna/Idna.php index e626da7..87f2293 100644 --- a/Idna/Idna.php +++ b/Idna/Idna.php @@ -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([ @@ -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); } diff --git a/Idna/IdnaInfo.php b/Idna/IdnaInfo.php index d445979..780ea1a 100644 --- a/Idna/IdnaInfo.php +++ b/Idna/IdnaInfo.php @@ -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', @@ -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; } /** diff --git a/Idna/IdnaInfoTest.php b/Idna/IdnaInfoTest.php index 1760d70..28b5820 100644 --- a/Idna/IdnaInfoTest.php +++ b/Idna/IdnaInfoTest.php @@ -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 diff --git a/Idna/IdnaTest.php b/Idna/IdnaTest.php index 3a47843..c7716e3 100644 --- a/Idna/IdnaTest.php +++ b/Idna/IdnaTest.php @@ -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()); } /** @@ -46,9 +44,7 @@ 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()); } /** @@ -56,10 +52,7 @@ public function testToUnicodeThrowsException(): void */ 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()); } /** @@ -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()); } /** @@ -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()); } }