Skip to content

Commit

Permalink
Merge pull request #148 from gsteel/v3/refactor-basename-filter
Browse files Browse the repository at this point in the history
Refactor `BaseName` filter
  • Loading branch information
Ocramius committed Jun 14, 2024
2 parents 6da3467 + 6891df9 commit 3c820e3
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 3c820e3

Please sign in to comment.