From 797b955a73452722a70abe7b8732bcda52017b5e Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 4 Jul 2024 10:10:14 +0100 Subject: [PATCH] Fix TLD updater script In #298 the `$validTlds` property declaration was changed but the regex in the script was not. This patch also removes the check for `ext-intl` which is already available in CI, and instead outputs an error when not available. This has the side effect of 100% type coverage Signed-off-by: George Steel --- bin/update_hostname_validator.php | 47 ++++++------------------------- psalm-baseline.xml | 15 ---------- 2 files changed, 8 insertions(+), 54 deletions(-) diff --git a/bin/update_hostname_validator.php b/bin/update_hostname_validator.php index 0a9b49c6..4fc4d868 100644 --- a/bin/update_hostname_validator.php +++ b/bin/update_hostname_validator.php @@ -2,8 +2,6 @@ declare(strict_types=1); -use Laminas\Validator\Hostname; - require __DIR__ . '/../vendor/autoload.php'; const IANA_URL = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt'; @@ -19,6 +17,11 @@ exit(1); } +if (! extension_loaded('intl')) { + printf("Error: ext-intl is required by this script%s", PHP_EOL); + exit(1); +} + /** @psalm-var list $newFileContent */ $newFileContent = []; // new file content $insertDone = false; // becomes 'true' when we find start of $validTlds declaration @@ -49,14 +52,14 @@ } // Detect where the $validTlds declaration begins - if (preg_match('/^\s+protected\s+\$validTlds\s+=\s+\[\s*$/', $line)) { + if (preg_match('/^\s+private\s+array\s+\$validTlds\s+=\s+\[\s*$/', $line)) { $newFileContent = array_merge($newFileContent, getNewValidTlds($response)); $insertDone = true; } } if (! $insertDone) { - printf('Error: cannot find line with "protected $validTlds"%s', PHP_EOL); + printf('Error: cannot find line with "private array $validTlds"%s', PHP_EOL); exit(1); } @@ -110,49 +113,15 @@ function getOfficialTLDs(): string */ function getNewValidTlds(string $string): array { - $decodePunycode = getPunycodeDecoder(); - // Get new TLDs from the list previously fetched $newValidTlds = []; foreach (preg_grep('/^[^#]/', preg_split("#\r?\n#", $string)) as $line) { $newValidTlds [] = sprintf( "%s'%s',\n", str_repeat(' ', 8), - $decodePunycode(strtolower($line)) + idn_to_utf8(strtolower($line), 0, INTL_IDNA_VARIANT_UTS46), ); } return $newValidTlds; } - -/** - * Retrieve and return a punycode decoder. - * - * TLDs are puny encoded. - * - * We need a decodePunycode function to translate TLDs to UTF-8: - * - * - use idn_to_utf8 if available - * - otherwise, use Hostname::decodePunycode() - * - * @return callable - */ -function getPunycodeDecoder() -{ - if (function_exists('idn_to_utf8')) { - return function ($domain) { - return idn_to_utf8($domain, 0, INTL_IDNA_VARIANT_UTS46); - }; - } - - $hostnameValidator = new Hostname(); - $reflection = new ReflectionClass($hostnameValidator::class); - $decodePunyCode = $reflection->getMethod('decodePunycode'); - - return function ($encode) use ($hostnameValidator, $decodePunyCode) { - if (strpos($encode, 'xn--') === 0) { - return $decodePunyCode->invokeArgs($hostnameValidator, [substr($encode, 4)]); - } - return $encode; - }; -} diff --git a/psalm-baseline.xml b/psalm-baseline.xml index df2c7599..28d5369d 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,20 +1,5 @@ - - - - - - - - - - - - - - -