From 1ec3d8be5341182f7cd78b5f3fa75b6b20c0c162 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sat, 25 Jan 2025 15:02:31 +0100 Subject: [PATCH] Improve codebase and simplify named constructor --- interfaces/IPv4/Converter.php | 10 +++++-- phpstan.neon | 1 + uri/Uri.php | 52 +++++++++++++++++++++-------------- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/interfaces/IPv4/Converter.php b/interfaces/IPv4/Converter.php index 71c0bb9f..63e9ab43 100644 --- a/interfaces/IPv4/Converter.php +++ b/interfaces/IPv4/Converter.php @@ -21,6 +21,8 @@ use function count; use function explode; use function extension_loaded; +use function hexdec; +use function long2ip; use function ltrim; use function preg_match; use function str_ends_with; @@ -125,9 +127,13 @@ public function isIpv4(Stringable|string|null $host): bool } $hexParts = explode(':', substr($ipAddress, 5, 9)); + if (count($hexParts) < 2) { + return false; + } + + $ipAddress = long2ip((int) hexdec($hexParts[0]) * 65536 + (int) hexdec($hexParts[1])); - return count($hexParts) > 1 - && false !== long2ip((int) hexdec($hexParts[0]) * 65536 + (int) hexdec($hexParts[1])); + return '' !== $ipAddress && false !== $ipAddress; } public function toIPv6Using6to4(Stringable|string|null $host): ?string diff --git a/phpstan.neon b/phpstan.neon index 5e790c3a..837423f5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -22,5 +22,6 @@ parameters: path: components/Modifier.php - '#Attribute class Deprecated does not exist.#' - '#deprecated class League\\Uri\\BaseUri#' + - '#\Dom\\HTMLDocument#' reportUnmatchedIgnoredErrors: true treatPhpDocTypesAsCertain: false diff --git a/uri/Uri.php b/uri/Uri.php index 24a7a008..eaa272a1 100644 --- a/uri/Uri.php +++ b/uri/Uri.php @@ -740,27 +740,7 @@ public static function fromMarkdownAnchor(Stringable|string $markdown, Stringabl */ public static function fromHtmlAnchor(Stringable|string $html, Stringable|string|null $baseUri = null): self { - FeatureDetection::supportsDom(); - $html = (string) $html; - set_error_handler(fn (int $errno, string $errstr, string $errfile, int $errline) => true); - try { - $result = true; - $exception = null; - if (class_exists(HTMLDocument::class)) { - $dom = HTMLDocument::createFromString($html); - } else { - $dom = new DOMDocument(); - $result = $dom->loadHTML($html); - } - } catch (Throwable $exception) { - $result = false; - $dom = null; - } - restore_error_handler(); - if (false === $result || null === $dom) { - throw $exception ?? new DOMException('The content could not be parsed as a valid HTML content.'); - } - + $dom = self::loadDom($html); $element = $dom->getElementsByTagName('a')->item(0); if (null === $element) { throw new DOMException('No anchor element was found in the content.'); @@ -778,6 +758,36 @@ public static function fromHtmlAnchor(Stringable|string $html, Stringable|string }; } + /** + * @throws DOMException + * @throws Throwable + */ + private static function loadDom(Stringable|string $html): DOMDocument|HTMLDocument + { + FeatureDetection::supportsDom(); + + $html = (string) $html; + if (class_exists(HTMLDocument::class)) { + try { + set_error_handler(fn (int $errno, string $errstr, string $errfile, int $errline) => true); + + return HTMLDocument::createFromString($html); + } finally { + restore_error_handler(); + } + } + + set_error_handler(fn (int $errno, string $errstr, string $errfile, int $errline) => true); + $dom = new DOMDocument(); + $result = $dom->loadHTML($html); + restore_error_handler(); + if (false === $result) { + throw new DOMException('The content could not be parsed as a valid HTML content.'); + } + + return $dom; + } + /** * Returns the environment scheme. */