Skip to content

Commit

Permalink
Fix Psalm errors
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelthole committed Jan 2, 2025
1 parent a059191 commit 7395ec0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 46 deletions.
23 changes: 0 additions & 23 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,29 +319,6 @@
<code><![CDATA[Module]]></code>
</UnusedClass>
</file>
<file src="src/StripTags.php">
<DeprecatedClass>
<code><![CDATA[AbstractFilter]]></code>
</DeprecatedClass>
<MixedArgument>
<code><![CDATA[$options['allowAttribs']]]></code>
<code><![CDATA[$options['allowTags']]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$attribute]]></code>
<code><![CDATA[$element]]></code>
<code><![CDATA[$temp['allowAttribs']]]></code>
<code><![CDATA[$temp['allowComments']]]></code>
<code><![CDATA[$temp['allowTags']]]></code>
</MixedAssignment>
<PossiblyUndefinedVariable>
<code><![CDATA[$temp]]></code>
</PossiblyUndefinedVariable>
<RedundantConditionGivenDocblockType>
<code><![CDATA[is_array($options)]]></code>
<code><![CDATA[is_string($attribute)]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Word/SeparatorToCamelCase.php">
<MissingClosureParamType>
<code><![CDATA[$matches]]></code>
Expand Down
29 changes: 11 additions & 18 deletions src/StripTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* @psalm-type Options = array{
* allowTags?: array<string|list<string>>,
* allowTags?: list<string>|array<string, list<string>>,
* allowAttribs?: list<string>
* }
* @implements FilterInterface<string>
Expand Down Expand Up @@ -60,8 +60,6 @@ public function __construct(array $options = [])
* Defined by Laminas\Filter\FilterInterface
*
* If the value provided is non-scalar, the value will remain unfiltered
*
* @psalm-return ($value is scalar ? string : mixed)
*/
public function filter(mixed $value): mixed
{
Expand Down Expand Up @@ -117,6 +115,9 @@ public function __invoke(mixed $value): mixed
return $this->filter($value);
}

/**
* @param array<string|list<string>> $tagsAllowed
*/
private function setTagsAllowed(array $tagsAllowed): void
{
foreach ($tagsAllowed as $index => $element) {
Expand All @@ -126,22 +127,16 @@ private function setTagsAllowed(array $tagsAllowed): void
$tagName = strtolower($element);
// Store the tag as allowed with no attributes
$this->tagsAllowed[$tagName] = [];
} elseif (is_string($index) && (is_array($element) || is_string($element))) {
} elseif (is_string($index) && is_array($element)) {
// Otherwise, if a tag was provided with attributes
// Canonicalize the tag name
$tagName = strtolower($index);
// Canonicalize the attributes
if (is_string($element)) {
$element = [$element];
}
// Store the tag as allowed with the provided attributes
$this->tagsAllowed[$tagName] = [];
foreach ($element as $attribute) {
if (is_string($attribute)) {
// Canonicalize the attribute name
$attributeName = strtolower($attribute);
$this->tagsAllowed[$tagName][$attributeName] = null;
}
// Canonicalize the attribute name
$attributeName = strtolower($attribute);
$this->tagsAllowed[$tagName][$attributeName] = null;
}
}
}
Expand All @@ -154,11 +149,9 @@ private function setAttributesAllowed(array $attributesAllowed): void
{
// Store each attribute as allowed
foreach ($attributesAllowed as $attribute) {
if (is_string($attribute)) {
// Canonicalize the attribute name
$attributeName = strtolower($attribute);
$this->attributesAllowed[$attributeName] = null;
}
// Canonicalize the attribute name
$attributeName = strtolower($attribute);
$this->attributesAllowed[$attributeName] = null;
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/StripTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testFilterTagAllowedAttribute(): void
public function testFilterTagAllowedAttributeAllowed(): void
{
$filter = new StripTagsFilter([
'allowTags' => ['img' => 'alt'],
'allowTags' => ['img' => ['alt']],
]);
$input = '<IMG ALT="FOO" />';
$expected = '<img alt="FOO" />';
Expand All @@ -138,7 +138,7 @@ public function testFilterTagAllowedAttributeAllowed(): void
public function testFilterTagAllowedAttributeAllowedGt(): void
{
$filter = new StripTagsFilter([
'allowTags' => ['img' => 'alt'],
'allowTags' => ['img' => ['alt']],
]);
$input = '<img alt="$object->property" />';
$expected = '<img>property" /';
Expand All @@ -151,7 +151,7 @@ public function testFilterTagAllowedAttributeAllowedGt(): void
public function testFilterTagAllowedAttributeAllowedGtEscaped(): void
{
$filter = new StripTagsFilter([
'allowTags' => ['img' => 'alt'],
'allowTags' => ['img' => ['alt']],
]);
$input = '<img alt="$object-&gt;property" />';
$expected = '<img alt="$object-&gt;property" />';
Expand Down Expand Up @@ -242,7 +242,7 @@ public function testClosingAngleBracketInAllowedAttributeValue(): void
{
$filter = new StripTagsFilter([
'allowTags' => [
'a' => 'href',
'a' => ['href'],
],
]);
$input = '<a href="Some &gt; Text">';
Expand All @@ -259,7 +259,7 @@ public function testAllowedAttributeValueMayEndWithEquals(): void
{
$filter = new StripTagsFilter([
'allowTags' => [
'element' => 'attribute',
'element' => ['attribute'],
],
]);
$input = '<element attribute="a=">contents</element>';
Expand Down

0 comments on commit 7395ec0

Please sign in to comment.