From 6891df93ef0c0232736b3ceb460dfd8f59c7f96e Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 13 Jun 2024 17:13:20 +0100 Subject: [PATCH] Refactor basename filter Remove inheritance, return unfiltered values for anything except string. It does not make sense to call basename on `(string) 123` or `(string) true` for example. Signed-off-by: George Steel --- src/BaseName.php | 24 ++++++++++++------------ test/BaseNameTest.php | 3 +++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/BaseName.php b/src/BaseName.php index a13da6f5..9a37da6b 100644 --- a/src/BaseName.php +++ b/src/BaseName.php @@ -5,29 +5,29 @@ namespace Laminas\Filter; use function basename; -use function is_scalar; +use function is_string; -/** - * @psalm-type Options = array{} - * @extends AbstractFilter - */ -final class BaseName extends AbstractFilter +/** @psalm-immutable */ +final class BaseName implements FilterInterface { /** - * Defined by Laminas\Filter\FilterInterface - * * Returns basename($value). * - * If the value provided is non-scalar, the value will remain unfiltered + * If the value provided is non-string, the value will remain unfiltered * - * @psalm-return ($value is scalar ? string : mixed) + * @psalm-return ($value is string ? string : mixed) */ public function filter(mixed $value): mixed { - if (! is_scalar($value)) { + if (! is_string($value)) { return $value; } - return basename((string) $value); + return basename($value); + } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); } } diff --git a/test/BaseNameTest.php b/test/BaseNameTest.php index 5f46ff9c..bc34a3c0 100644 --- a/test/BaseNameTest.php +++ b/test/BaseNameTest.php @@ -31,6 +31,9 @@ public static function returnUnfilteredDataProvider(): array { return [ [null], + [1], + [2.5], + [true], [new stdClass()], [ [