Skip to content

Commit

Permalink
Refactor basename filter
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
gsteel committed Jun 13, 2024
1 parent 6da3467 commit 6891df9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/BaseName.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
namespace Laminas\Filter;

use function basename;
use function is_scalar;
use function is_string;

/**
* @psalm-type Options = array{}
* @extends AbstractFilter<Options>
*/
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);
}
}
3 changes: 3 additions & 0 deletions test/BaseNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static function returnUnfilteredDataProvider(): array
{
return [
[null],
[1],
[2.5],
[true],
[new stdClass()],
[
[
Expand Down

0 comments on commit 6891df9

Please sign in to comment.