Skip to content

Commit

Permalink
Improve codebase and simplify named constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 25, 2025
1 parent eb00c26 commit 1ec3d8b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
10 changes: 8 additions & 2 deletions interfaces/IPv4/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 31 additions & 21 deletions uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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.
*/
Expand Down

0 comments on commit 1ec3d8b

Please sign in to comment.