Skip to content

Commit

Permalink
Fixes regression, Allow callbacks to be a FilterInterface again.
Browse files Browse the repository at this point in the history
Signed-off-by: Pieter Hoste <[email protected]>
  • Loading branch information
baldwinagency-pieter committed Apr 11, 2024
1 parent a03ad6a commit 3ac1097
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/FilterChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function setOptions($options)
foreach ($value as $spec) {
$callback = $spec['callback'] ?? false;
$priority = $spec['priority'] ?? static::DEFAULT_PRIORITY;
if (is_callable($callback)) {
if (is_callable($callback) || $callback instanceof FilterInterface) {
$this->attach($callback, $priority);
}
}
Expand Down
8 changes: 5 additions & 3 deletions test/FilterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Laminas\Filter\StringToLower;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use LaminasTest\Filter\TestAsset\StrRepeatFilterInterface;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -77,7 +78,7 @@ public function testAllowsConfiguringFilters(): void
$chain = new FilterChain();
$chain->setOptions($config);
$value = '<a name="foo"> abc </a><img id="bar" />';
$valueExpected = 'ABC <IMG ID="BAR" />';
$valueExpected = 'ABC <IMG ID="BAR" />ABC <IMG ID="BAR" />';
self::assertSame($valueExpected, $chain->filter($value));
}

Expand All @@ -86,7 +87,7 @@ public function testAllowsConfiguringFiltersViaConstructor(): void
$config = $this->getChainConfig();
$chain = new FilterChain($config);
$value = '<a name="foo"> abc </a>';
$valueExpected = 'ABC';
$valueExpected = 'ABCABC';
self::assertSame($valueExpected, $chain->filter($value));
}

Expand All @@ -96,7 +97,7 @@ public function testConfigurationAllowsTraversableObjects(): void
$config = new ArrayIterator($config);
$chain = new FilterChain($config);
$value = '<a name="foo"> abc </a>';
$valueExpected = 'ABC';
$valueExpected = 'ABCABC';
self::assertSame($valueExpected, $chain->filter($value));
}

Expand All @@ -117,6 +118,7 @@ private function getChainConfig(): array
return [

Check failure on line 118 in test/FilterChainTest.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

InvalidReturnStatement

test/FilterChainTest.php:118:16: InvalidReturnStatement: The inferred type 'array{callbacks: list{array{callback: list{LaminasTest\Filter\FilterChainTest::class, 'staticUcaseFilter'}}, array{callback: LaminasTest\Filter\TestAsset\StrRepeatFilterInterface}, array{callback: pure-Closure(string):string, priority: 10000}}, filters: list{array{name: Laminas\Filter\StripTags::class, options: array{allowAttribs: 'id', allowTags: 'img'}, priority: 10100}}}' does not match the declared return type 'array{callbacks?: list<array{callback: callable(mixed):mixed, priority?: int}>, filters?: list<array{name: string, options?: array<string, mixed>, priority?: int}>}' for LaminasTest\Filter\FilterChainTest::getChainConfig (see https://psalm.dev/128)
'callbacks' => [
['callback' => [self::class, 'staticUcaseFilter']],
['callback' => new StrRepeatFilterInterface()],
[
'priority' => 10000,
'callback' => static fn(string $value): string => trim($value),
Expand Down
15 changes: 15 additions & 0 deletions test/TestAsset/StrRepeatFilterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Filter\TestAsset;

use Laminas\Filter\FilterInterface;

class StrRepeatFilterInterface implements FilterInterface
{
public function filter($value)
{
return str_repeat((string) $value, 2);

Check failure on line 13 in test/TestAsset/StrRepeatFilterInterface.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPCodeSniffer [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Function str_repeat() should not be referenced via a fallback global name, but via a use statement.
}
}

0 comments on commit 3ac1097

Please sign in to comment.