diff --git a/composer.json b/composer.json index 68f7d43..625e4fe 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ } }, "require": { + "php": ">=8.0", "symfony/dom-crawler": "^7.1", "psr/http-client": "^1.0", "php-http/discovery": "^1.20", diff --git a/src/Embed/Embed.php b/src/Embed/Embed.php index d4dd125..40acce0 100644 --- a/src/Embed/Embed.php +++ b/src/Embed/Embed.php @@ -2,22 +2,15 @@ namespace Hyvor\Unfold\Embed; -//use Hyvor\Unfold\Embed\Platforms\GithubGist; -//use Hyvor\Unfold\Embed\Platforms\Reddit; -//use Hyvor\Unfold\Embed\Platforms\Tiktok; -//use Hyvor\Unfold\Embed\Platforms\Twitter; -//use Hyvor\Unfold\Embed\Platforms\Youtube; use Hyvor\Unfold\Exception\EmbedUnableToResolveException; use Hyvor\Unfold\Exception\EmbedParserException; use Hyvor\Unfold\Exception\UnfoldException; use Hyvor\Unfold\UnfoldCallContext; use Hyvor\Unfold\UnfoldConfig; use Hyvor\Unfold\Unfolded\Unfolded; -use Hyvor\Unfold\UnfoldMethod; class Embed { - /** * @return string[] */ @@ -43,7 +36,7 @@ public static function parse( return $parser->parse(); } } - return null; + throw new EmbedUnableToResolveException($url); } /** @@ -56,14 +49,6 @@ public static function getUnfoldedObject( ) { $oembed = self::parse($url, $context->config); - if ($oembed === null) { - if ($context->method === UnfoldMethod::EMBED) { - throw new EmbedUnableToResolveException(); - } else { - return null; - } - } - return Unfolded::fromEmbed( $oembed, $url, diff --git a/src/Embed/EmbedParserAbstract.php b/src/Embed/EmbedParserAbstract.php index 994b86e..96750a8 100644 --- a/src/Embed/EmbedParserAbstract.php +++ b/src/Embed/EmbedParserAbstract.php @@ -31,6 +31,7 @@ public function __construct( * If the URL needs to be replaced before sending to the oEmbed endpoint, * return the new URL here. Otherwise, return null * Ex: m.facebook.com -> www.facebook.com + * @codeCoverageIgnore */ public function replaceUrl(): ?string { @@ -76,9 +77,11 @@ public function parse(): EmbedResponseObject } elseif ($this instanceof EmbedParserCustomInterface) { return $this->parseCustom(); } else { + // @codeCoverageIgnoreStart throw new \Exception( 'EmbedParserAbstract should be implemented with either EmbedParserOEmbedInterface or EmbedParserCustomInterface' - ); // @codeCoverageIgnore + ); + // @codeCoverageIgnoreEnd } } diff --git a/src/Link/Metadata/MetadataParser.php b/src/Link/Metadata/MetadataParser.php index 96a847d..264b6e0 100644 --- a/src/Link/Metadata/MetadataParser.php +++ b/src/Link/Metadata/MetadataParser.php @@ -18,7 +18,6 @@ class MetadataParser { - // TODO: Rich Schema /** * @var MetadataObject[] diff --git a/src/Link/Metadata/MetadataPriority.php b/src/Link/Metadata/MetadataPriority.php index 9edbeca..15f451d 100644 --- a/src/Link/Metadata/MetadataPriority.php +++ b/src/Link/Metadata/MetadataPriority.php @@ -56,7 +56,7 @@ private function prioritizedAll(array $keys) * 'OG_TITLE' => [MetadataObject], * ] */ - $keysNames = array_map(fn ($key) => $key->name, $keys); + $keysNames = array_map(fn($key) => $key->name, $keys); uksort($keyedMetadata, function ($a, $b) use ($keysNames) { return array_search($a, $keysNames) - array_search($b, $keysNames); }); @@ -64,7 +64,7 @@ private function prioritizedAll(array $keys) // index by 0,1,2 $keyedMetadata = array_values($keyedMetadata); // return the values - return array_map(fn ($metadata) => $metadata->value, $keyedMetadata[0] ?? []); + return array_map(fn($metadata) => $metadata->value, $keyedMetadata[0] ?? []); } /** @@ -144,7 +144,7 @@ public function siteUrl(string $url): ?string return $host ? $scheme . '://' . $host : null; } - return null; + return null; // @ignoreCodeCoverage } diff --git a/src/Link/Metadata/Parsers/LinkParser.php b/src/Link/Metadata/Parsers/LinkParser.php index c005636..2a2bf8e 100644 --- a/src/Link/Metadata/Parsers/LinkParser.php +++ b/src/Link/Metadata/Parsers/LinkParser.php @@ -13,15 +13,11 @@ public function add(): void $rel = $node->attr('rel'); $href = $node->attr('href'); - if (!$rel && !$href) { - return; - } - - if ($rel === 'canonical') { + if ($rel === 'canonical' && is_string($href)) { $this->parser->addMetadata(new MetadataObject(MetadataKeyType::CANONICAL_URL, $href)); } - if ($rel === 'icon') { + if ($rel === 'icon' && is_string($href)) { $this->parser->addMetadata(new MetadataObject(MetadataKeyType::FAVICON_URL, $href)); } }); diff --git a/src/Unfold.php b/src/Unfold.php index 7e6efda..22aabad 100644 --- a/src/Unfold.php +++ b/src/Unfold.php @@ -29,6 +29,7 @@ public static function unfold( return Embed::getUnfoldedObject($url, $context); } else { // both + // TODO: } } } diff --git a/src/UnfoldCallContext.php b/src/UnfoldCallContext.php index 88ac4d0..b76c2bd 100644 --- a/src/UnfoldCallContext.php +++ b/src/UnfoldCallContext.php @@ -15,7 +15,7 @@ public function __construct( public function duration(): int { - return (microtime(true) - $this->startTime) * 1000; + return (int)((microtime(true) - $this->startTime) * 1000); } } diff --git a/tests/Feature/UnfoldEmbedTest.php b/tests/Feature/UnfoldEmbedTest.php index 06bc3e4..4e72f97 100644 --- a/tests/Feature/UnfoldEmbedTest.php +++ b/tests/Feature/UnfoldEmbedTest.php @@ -5,6 +5,7 @@ use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; +use Hyvor\Unfold\Exception\EmbedUnableToResolveException; use Hyvor\Unfold\Unfold; use Hyvor\Unfold\UnfoldConfig; use Hyvor\Unfold\UnfoldMethod; @@ -61,4 +62,12 @@ method: UnfoldMethod::EMBED, ); expect($response->embed)->toBe(''); +}); + +it('on unable to resolve', function () { + expect(fn() => Unfold::unfold( + 'https://hyvor.com', + method: UnfoldMethod::EMBED, + config: new UnfoldConfig() + ))->toThrow(EmbedUnableToResolveException::class); }); \ No newline at end of file