Skip to content

Commit

Permalink
Fix TLD updater script
Browse files Browse the repository at this point in the history
In laminas#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 <[email protected]>
  • Loading branch information
gsteel committed Jul 4, 2024
1 parent 2c2d75a commit 797b955
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 54 deletions.
47 changes: 8 additions & 39 deletions bin/update_hostname_validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string> $newFileContent */
$newFileContent = []; // new file content
$insertDone = false; // becomes 'true' when we find start of $validTlds declaration
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
};
}
15 changes: 0 additions & 15 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
<file src="bin/update_hostname_validator.php">
<MissingClosureParamType>
<code><![CDATA[$domain]]></code>
<code><![CDATA[$encode]]></code>
</MissingClosureParamType>
<MissingClosureReturnType>
<code><![CDATA[function ($encode) use ($hostnameValidator, $decodePunyCode) {]]></code>
</MissingClosureReturnType>
<MixedArgument>
<code><![CDATA[$decodePunycode(strtolower($line))]]></code>
<code><![CDATA[$domain]]></code>
<code><![CDATA[$encode]]></code>
<code><![CDATA[$encode]]></code>
</MixedArgument>
</file>
<file src="src/AbstractValidator.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[$value]]></code>
Expand Down

0 comments on commit 797b955

Please sign in to comment.