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 BaseName filter #148

Merged
merged 1 commit into from
Jun 14, 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
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