Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor DenyList filter #174

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,6 @@
<code><![CDATA[$value]]></code>
</PossiblyNullArgument>
</file>
<file src="src/DenyList.php">
<PossiblyUnusedMethod>
<code><![CDATA[setList]]></code>
<code><![CDATA[setStrict]]></code>
</PossiblyUnusedMethod>
<RedundantCastGivenDocblockType>
<code><![CDATA[(bool) $strict]]></code>
</RedundantCastGivenDocblockType>
</file>
<file src="src/File/Rename.php">
<DocblockTypeContradiction>
<code><![CDATA[is_array($options)]]></code>
Expand Down
77 changes: 18 additions & 59 deletions src/DenyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,47 @@
namespace Laminas\Filter;

use Laminas\Stdlib\ArrayUtils;
use Traversable;

use function array_values;
use function in_array;
use function is_array;

/**
* @psalm-type Options = array{
* strict?: bool,
* list?: array,
* ...
* list?: iterable<array-key, mixed>,
* }
* @extends AbstractFilter<Options>
* @implements FilterInterface<null>
*/
final class DenyList extends AbstractFilter
final class DenyList implements FilterInterface
{
/** @var bool */
protected $strict = false;
private readonly array $list;
private readonly bool $strict;

/** @var array */
protected $list = [];

/**
* @param null|array|Traversable $options
*/
public function __construct($options = null)
/** @param Options $options */
public function __construct(array $options = [])
{
if (null !== $options) {
$this->setOptions($options);
}
$this->strict = $options['strict'] ?? false;
$list = ArrayUtils::iteratorToArray($options['list'] ?? []);
$this->list = array_values($list);
}

/**
* Determine whether the in_array() call should be "strict" or not. See in_array docs.
*
* @param bool $strict
*/
public function setStrict($strict = true): void
{
$this->strict = (bool) $strict;
}

/**
* Returns whether the in_array() call should be "strict" or not. See in_array docs.
*
* @return bool
*/
public function getStrict()
{
return $this->strict;
}

/**
* Set the list of items to black-list.
*
* @param array|Traversable $list
*/
public function setList($list = []): void
{
if (! is_array($list)) {
$list = ArrayUtils::iteratorToArray($list);
}

$this->list = $list;
}

/**
* Get the list of items to black-list
* {@inheritDoc}
*
* @return array
* Will return null if $value is present in the deny-list. If $value is NOT present then it will return $value.
*/
public function getList()
public function filter(mixed $value): mixed
{
return $this->list;
return in_array($value, $this->list, $this->strict) ? null : $value;
}

/**
* {@inheritDoc}
*
* Will return null if $value is present in the black-list. If $value is NOT present then it will return $value.
* Will return null if $value is present in the deny-list. If $value is NOT present then it will return $value.
*/
public function filter(mixed $value): mixed
public function __invoke(mixed $value): mixed
{
return in_array($value, $this->getList(), $this->getStrict()) ? null : $value;
return $this->filter($value);
}
}
39 changes: 24 additions & 15 deletions test/DenyListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

use Laminas\Filter\DenyList as DenyListFilter;
use Laminas\Stdlib\ArrayObject;
use Laminas\Stdlib\Exception;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Throwable;
use TypeError;

use function gettype;
use function sprintf;
Expand All @@ -23,16 +24,16 @@ public function testConstructorOptions(): void
'strict' => true,
]);

self::assertSame(true, $filter->getStrict());
self::assertSame(['test', 1], $filter->getList());
self::assertSame('1', $filter->filter('1'), 'Strict options infer that string 1 is not in the list');
self::assertNull($filter->filter('test'));
self::assertNull($filter->filter(1));
}

public function testConstructorDefaults(): void
{
$filter = new DenyListFilter();

self::assertSame(false, $filter->getStrict());
self::assertSame([], $filter->getList());
self::assertSame('test', $filter->filter('test'));
}

public function testWithPluginManager(): void
Expand All @@ -43,30 +44,38 @@ public function testWithPluginManager(): void
self::assertInstanceOf(DenyListFilter::class, $filter);
}

public function testNullListShouldThrowException(): void
public function testListOptionShouldBeIterable(): void
{
$this->expectException(Exception\InvalidArgumentException::class);
$this->expectException(Throwable::class);
/** @psalm-suppress InvalidArgument */
new DenyListFilter([
'list' => null,
'list' => 'foo',
]);
}

public function testTraversableConvertsToArray(): void
{
$array = ['test', 1];
$obj = new ArrayObject(['test', 1]);
$filter = new DenyListFilter([
'list' => $obj,
'list' => new ArrayObject([1, 2, 'test']),
]);
self::assertSame($array, $filter->getList());
self::assertSame(null, $filter->filter('1'));
self::assertSame(null, $filter->filter('test'));
}

public function testSetStrictShouldCastToBoolean(): void
public function testStrictOptionShouldBeBoolean(): void
{
$filter = new DenyListFilter([
$this->expectException(TypeError::class);
/** @psalm-suppress InvalidArgument */
new DenyListFilter([
'strict' => 1,
]);
self::assertSame(true, $filter->getStrict());
}

#[DataProvider('defaultTestProvider')]
public function testWillReturnValueWhenNoListHasBeenProvided(mixed $value): void
{
$filter = new DenyListFilter();
self::assertSame($value, $filter->filter($value));
}

#[DataProvider('defaultTestProvider')]
Expand Down