From 527193b98d51299704a5537ec548849472accf7e Mon Sep 17 00:00:00 2001 From: Marcel Thole Date: Fri, 3 Jan 2025 10:40:07 +0100 Subject: [PATCH] Refactor Dir filter to implement FlterInterface Signed-off-by: Marcel Thole --- docs/book/v3/migration/v2-to-v3.md | 5 +++++ psalm-baseline.xml | 5 ----- src/Dir.php | 17 +++++++++-------- test/DirTest.php | 29 ++++++++++++++++++++--------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/docs/book/v3/migration/v2-to-v3.md b/docs/book/v3/migration/v2-to-v3.md index 64ea5528..6bf4e247 100644 --- a/docs/book/v3/migration/v2-to-v3.md +++ b/docs/book/v3/migration/v2-to-v3.md @@ -152,6 +152,11 @@ The following methods have been removed: The constructor now only accepts an associative array of [documented options](../standard-filters.md#denylist). +#### `Dir` + +This filter will not cast a given integer or float value to a string anymore. +The return value changed from `.` to return the given integer or float value. + #### `HtmlEntities` The following methods have been removed: diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 9f9b753f..50999eb1 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -16,11 +16,6 @@ - - - - - diff --git a/src/Dir.php b/src/Dir.php index a0045ad3..eea87d9a 100644 --- a/src/Dir.php +++ b/src/Dir.php @@ -5,28 +5,29 @@ namespace Laminas\Filter; use function dirname; -use function is_scalar; +use function is_string; /** - * @psalm-type Options = array{} - * @extends AbstractFilter + * @implements FilterInterface */ -final class Dir extends AbstractFilter +final class Dir implements FilterInterface { /** * Defined by Laminas\Filter\FilterInterface * * Returns dirname($value) - * - * @psalm-return ($value is scalar ? string : mixed) */ public function filter(mixed $value): mixed { - if (! is_scalar($value)) { + if (! is_string($value) || $value === '') { return $value; } - $value = (string) $value; return dirname($value); } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); + } } diff --git a/test/DirTest.php b/test/DirTest.php index 7b1eb91d..fb94bb3e 100644 --- a/test/DirTest.php +++ b/test/DirTest.php @@ -14,17 +14,24 @@ class DirTest extends TestCase /** * Ensures that the filter follows expected behavior */ - public function testBasic(): void + #[DataProvider('defaultSettingsDataProvider')] + public function testBasic(string $input, string $expected): void { - $filter = new DirFilter(); - $valuesExpected = [ - 'filename' => '.', - '/path/to/filename' => '/path/to', - '/path/to/filename.ext' => '/path/to', + $filter = new DirFilter(); + + self::assertSame($expected, $filter($input)); + self::assertSame($expected, $filter->__invoke($input)); + self::assertSame($expected, $filter->filter($input)); + } + + public static function defaultSettingsDataProvider(): array + { + return [ + ['12345', '.'], + ['filename', '.'], + ['/path/to/filename', '/path/to'], + ['/path/to/filename.ext', '/path/to'], ]; - foreach ($valuesExpected as $input => $output) { - self::assertSame($output, $filter($input)); - } } /** @return list */ @@ -33,6 +40,10 @@ public static function returnUnfilteredDataProvider(): array return [ [null], [new stdClass()], + [''], + [12345], + [true], + [false], [ [ '/path/to/filename',