Skip to content

Commit

Permalink
Fix PhpStan errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Aug 4, 2021
1 parent 6698280 commit 42b7f16
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"php": "^8.0"
},
"require-dev": {
"nette/di": "^3.0",
"tracy/tracy": "^2.8",
"phpstan/phpstan": "^0.12.94",
"phpstan/extension-installer": "^1.0",
Expand Down
78 changes: 40 additions & 38 deletions src/HtmlHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
*/
final class HtmlHeader implements \Stringable
{
/** @var string[] */
/** @var array<int, string> */
private array $order = ['title', 'meta', 'og', 'twitter', 'link', 'json-ld'];

/** @var string[][]|string[][][] */
/** @var array<string, array<int|string, array<int, string>|string>> */
private array $tags = [];

private ?string $title = null;
Expand All @@ -51,7 +51,7 @@ public function __toString(): string
/**
* Render all or a specific group of HTML meta tags.
*
* @param string[] $groups render specific groups or keep empty for all records.
* @param array<int, string> $groups render specific groups or keep empty for all records.
*/
public function render(?array $groups = null): string
{
Expand All @@ -68,7 +68,7 @@ public function render(?array $groups = null): string


/**
* @param string[] $order
* @param array<int, string> $order
*/
public function setCustomOrderingStrategy(array $order): void
{
Expand All @@ -79,28 +79,29 @@ public function setCustomOrderingStrategy(array $order): void
/**
* Build an HTML link tag.
*
* @param string|string[]|null $value
* @param array<string, string>|string|null $value
*/
public function link(string $key, string|array|null $value): void
{
if (!empty($value)) {
$attributes = ['rel' => $key];
if (is_array($value)) {
foreach ($value as $valueKey => $v) {
$attributes[$valueKey] = $v;
}
} else {
$attributes['href'] = $value;
if ($value === [] || $value === '') {
return;
}
$attributes = ['rel' => $key];
if (is_array($value)) {
foreach ($value as $valueKey => $v) {
$attributes[$valueKey] = $v;
}

$this->addToTagsGroup('link', $key, $this->createTag('link', $attributes));
} else {
$attributes['href'] = $value;
}
$this->addToTagsGroup('link', $key, $this->createTag('link', $attributes));
}


public function metaDescription(string $content): void
{
if (($content = $this->normalize($content)) === '') {
$content = $this->normalize($content);
if ($content === '') {
return;
}
$this->description = $content;
Expand All @@ -111,32 +112,32 @@ public function metaDescription(string $content): void
/**
* Build an HTML meta tag.
*
* @param string|string[]|null $value
* @param string|array<string, string>|null $value
*/
public function meta(string $key, string|array|null $value): void
{
if (!empty($value)) {
if ($key === 'description' && is_string($value)) {
$value = $this->truncate($value, 153);
}
$attributes = ['name' => $key];
if (is_array($value)) {
foreach ($value as $valueKey => $v) {
$attributes[$valueKey] = $v;
}
} else {
$attributes['content'] = $value;
if ($value === [] || $value === '') {
return;
}
if ($key === 'description' && is_string($value)) {
$value = $this->truncate($value, 153);
}
$attributes = ['name' => $key];
if (is_array($value)) {
foreach ($value as $valueKey => $v) {
$attributes[$valueKey] = $v;
}

$this->addToTagsGroup('meta', $key, $this->createTag('meta', $attributes));
} else {
$attributes['content'] = $value;
}
$this->addToTagsGroup('meta', $key, $this->createTag('meta', $attributes));
}


/** Build an Open Graph meta tag. */
public function og(string $key, string $value, bool $prefixed = true): void
{
if (!empty($value)) {
if ($value !== '') {
$key = $prefixed ? 'og:' . $key : $key;
$this->addToTagsGroup('og', $key, $this->createTag('meta', [
'property' => $key,
Expand All @@ -149,7 +150,7 @@ public function og(string $key, string $value, bool $prefixed = true): void
/**
* Build an JSON linked data meta tag.
*
* @param mixed[] $schema
* @param array<string, mixed> $schema
*/
public function jsonld(array $schema): void
{
Expand Down Expand Up @@ -182,7 +183,7 @@ public function title(?string $value): void
/** Build a Twitter Card meta tag. */
public function twitter(string $key, string $value, bool $prefixed = true): void
{
if (!empty($value)) {
if ($value !== '') {
$key = $prefixed ? 'twitter:' . $key : $key;
$this->addToTagsGroup('twitter', $key, $this->createTag('meta', [
'name' => $key,
Expand All @@ -201,7 +202,7 @@ public function setAutomaticOpenGraph(bool $automaticOpenGraph): void
/** Render all HTML meta tags from a specific group. */
private function renderGroup(string $group): string
{
if (!isset($this->tags[$group])) {
if (isset($this->tags[$group]) === false) {
return '';
}

Expand Down Expand Up @@ -240,7 +241,7 @@ private function addToTagsGroup(string $group, string $key, string $tag): void
/**
* Build an HTML tag
*
* @param string[] $attributes
* @param array<string, string|null> $attributes
*/
private function createTag(string $tagName, array $attributes = []): string
{
Expand All @@ -254,9 +255,10 @@ private function createTag(string $tagName, array $attributes = []): string

$attrItems = [];
foreach ($attributes as $key => $value) {
if ($value !== null) {
$attrItems[] = $escapeAttr((string) $key) . '="' . $escapeAttr($value) . '"';
if ($value === null) {
continue;
}
$attrItems[] = $escapeAttr($key) . '="' . $escapeAttr($value) . '"';
}

return '<' . $tagName . (count($attrItems) > 0 ? ' ' . implode(' ', $attrItems) : '') . '>';
Expand Down Expand Up @@ -295,7 +297,7 @@ private function truncate(string $s, int $maxLen, string $append = "\u{2026}"):
if ($maxLen < 1) {
return $append;
}
if (preg_match('#^.{1,' . $maxLen . '}(?=[\s\x00-/:-@\[-`{-~])#us', $s, $matches)) {
if (preg_match('#^.{1,' . $maxLen . '}(?=[\s\x00-/:-@\[-`{-~])#us', $s, $matches) === 1) {
return $matches[0] . $append;
}

Expand Down

0 comments on commit 42b7f16

Please sign in to comment.