Skip to content

Commit

Permalink
Merge pull request #13 from hyvor/phpstan-fix
Browse files Browse the repository at this point in the history
phpstan fixes
  • Loading branch information
supun-io authored Oct 22, 2024
2 parents f86155a + 67c6dd1 commit 97df091
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
},
"require": {
"php": ">=8.0",
"php": ">=8.1",
"symfony/dom-crawler": "^7.1",
"psr/http-client": "^1.0",
"php-http/discovery": "^1.20",
Expand Down
12 changes: 6 additions & 6 deletions src/Embed/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public static function getParsers(): array
{
$namespace = __NAMESPACE__ . '\\Platforms\\';
return array_map(
fn($file) => $namespace . pathinfo($file, PATHINFO_FILENAME),
glob(__DIR__ . '/Platforms/*.php')
fn($file) => $namespace . pathinfo((string)$file, PATHINFO_FILENAME),
(array)glob(__DIR__ . '/Platforms/*.php')
);
}

Expand All @@ -29,24 +29,24 @@ public static function getParsers(): array
public static function parse(
string $url,
?UnfoldConfig $config = null,
): ?EmbedResponseObject {
): EmbedResponseObject {
foreach (self::getParsers() as $parserClass) {
/** @var EmbedParserAbstract $parser */
$parser = new $parserClass($url, $config);
if ($parser->match()) {
return $parser->parse();
}
}
throw new EmbedUnableToResolveException($url);
throw new EmbedUnableToResolveException();
}

/**
* @return $context->method is EmbedMethodEnum::EMBED ? Unfolded : ?Unfolded
* @throws UnfoldException
*/
public static function getUnfoldedObject(
string $url,
UnfoldCallContext $context,
) {
): Unfolded {
$oembed = self::parse($url, $context->config);

return Unfolded::fromEmbed(
Expand Down
6 changes: 4 additions & 2 deletions src/Embed/EmbedParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ public function parse(): EmbedResponseObject
}
}

public function parseOEmbed(): ?EmbedResponseObject
public function parseOEmbed(): EmbedResponseObject
{
/** @var self&EmbedParserOEmbedInterface $this */
$oEmbedUrl = $this->oEmbedUrl();

$uri = Uri::withQueryValues(
Expand Down Expand Up @@ -136,8 +137,9 @@ public function parseOEmbed(): ?EmbedResponseObject
return EmbedResponseObject::fromArray($parsed);
}

private function parseCustom()
private function parseCustom(): EmbedResponseObject
{
/** @var self&EmbedParserCustomInterface $this */
$html = $this->getEmbedHtml();

return EmbedResponseObject::fromArray([
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/EmbedUnableToResolveException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class EmbedUnableToResolveException extends UnfoldException
{
public function __construct(string $message = "", int $code = 0, ?\Throwable $previous = null)
public function __construct()
{
parent::__construct('Unable to resolve embed', $code, $previous);
parent::__construct('Unable to resolve embed');
}

}
2 changes: 1 addition & 1 deletion src/Link/Metadata/MetadataKeyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ enum MetadataKeyType
* Gets the value of the metadata from a given content string
* ex: article:published_time is converted to DateTimeInterface
*/
public function getValue(string $content)
public function getValue(string $content): string|UnfoldedAuthor|UnfoldedTag|\DateTimeInterface|null
{
if (
$this === MetadataKeyType::OG_ARTICLE_PUBLISHED_TIME ||
Expand Down
5 changes: 5 additions & 0 deletions src/Link/Metadata/MetadataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public function addMetadataFromMetaTags(array $keys): void
}

$content = $keyType->getValue($content);

if ($content === null) {
return;
}

$metadata[] = new MetadataObject($keyType, $content);
});

Expand Down
19 changes: 16 additions & 3 deletions src/Link/Metadata/MetadataPriority.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(private array $metadata)

/**
* @param MetadataKeyType[] $keys
* @return mixed[]
*/
private function prioritizedAll(array $keys)
{
Expand Down Expand Up @@ -56,15 +57,15 @@ 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);
return intval(array_search($a, $keysNames)) - intval(array_search($b, $keysNames));
});

// 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] ?? []);
}

/**
Expand All @@ -78,6 +79,7 @@ private function prioritized(array $keys)

public function title(): ?string
{
/** @var string|null */
return $this->prioritized(
[
MetadataKeyType::TITLE,
Expand All @@ -89,6 +91,7 @@ public function title(): ?string

public function description(): ?string
{
/** @var string|null */
return $this->prioritized([
MetadataKeyType::DESCRIPTION,
MetadataKeyType::OG_DESCRIPTION,
Expand All @@ -101,6 +104,7 @@ public function description(): ?string
*/
public function authors()
{
/** @var UnfoldedAuthor[] */
return $this->prioritizedAll([
MetadataKeyType::RICH_SCHEMA_AUTHOR,
MetadataKeyType::OG_ARTICLE_AUTHOR,
Expand All @@ -113,6 +117,7 @@ public function authors()
*/
public function tags(): array
{
/** @var UnfoldedTag[] */
return $this->prioritizedAll([
MetadataKeyType::OG_ARTICLE_TAG
]);
Expand All @@ -121,6 +126,7 @@ public function tags(): array

public function siteName(): ?string
{
/** @var string|null */
return $this->prioritized([
MetadataKeyType::OG_SITE_NAME,
MetadataKeyType::TWITTER_SITE
Expand All @@ -130,6 +136,7 @@ public function siteName(): ?string

public function siteUrl(string $url): ?string
{
/** @var string|null $currentUrl */
$currentUrl = $this->prioritized([
MetadataKeyType::CANONICAL_URL,
MetadataKeyType::OG_URL
Expand All @@ -150,6 +157,7 @@ public function siteUrl(string $url): ?string

public function canonicalUrl(): ?string
{
/** @var string|null */
return $this->prioritized([
MetadataKeyType::CANONICAL_URL,
MetadataKeyType::OG_URL
Expand All @@ -159,6 +167,7 @@ public function canonicalUrl(): ?string

public function publishedTime(): ?DateTimeInterface
{
/** @var ?DateTimeInterface */
return $this->prioritized([
MetadataKeyType::RICH_SCHEMA_PUBLISHED_TIME,
MetadataKeyType::OG_ARTICLE_PUBLISHED_TIME
Expand All @@ -168,6 +177,7 @@ public function publishedTime(): ?DateTimeInterface

public function modifiedTime(): ?DateTimeInterface
{
/** @var ?DateTimeInterface */
return $this->prioritized([
MetadataKeyType::RICH_SCHEMA_MODIFIED_TIME,
MetadataKeyType::OG_ARTICLE_MODIFIED_TIME
Expand All @@ -177,6 +187,7 @@ public function modifiedTime(): ?DateTimeInterface

public function thumbnailUrl(): ?string
{
/** @var ?string */
return $this->prioritized([
MetadataKeyType::OG_IMAGE,
MetadataKeyType::OG_IMAGE_URL,
Expand All @@ -188,6 +199,7 @@ public function thumbnailUrl(): ?string

public function iconUrl(): ?string
{
/** @var ?string */
return $this->prioritized([
MetadataKeyType::FAVICON_URL
]);
Expand All @@ -196,6 +208,7 @@ public function iconUrl(): ?string

public function locale(): ?string
{
/** @var ?string */
return $this->prioritized([
MetadataKeyType::LOCALE,
MetadataKeyType::OG_LOCALE
Expand Down
2 changes: 1 addition & 1 deletion src/Link/Metadata/Parsers/JsonLdParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function add(): void
$this->parser->crawler->filterXPath('//script[@type="application/ld+json"]')->each(function ($node) {
$json = json_decode($node->text(), true);

if (json_last_error() !== JSON_ERROR_NONE) {
if (!is_array($json)) {
return;
}

Expand Down

0 comments on commit 97df091

Please sign in to comment.