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 DashToSeparator Filter #184

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Changes from 1 commit
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
54 changes: 48 additions & 6 deletions src/Word/DashToSeparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Laminas\Filter\Word;

use Closure;
use Laminas\Filter\FilterInterface;

use function array_map;
use function is_array;
use function is_scalar;
use function str_replace;

/**
Expand All @@ -14,15 +17,42 @@
* ...
* }
* @template TOptions of Options
* @extends AbstractSeparator<TOptions>
* @implements FilterInterface<mixed>
gsteel marked this conversation as resolved.
Show resolved Hide resolved
*/
final class DashToSeparator extends AbstractSeparator
final class DashToSeparator implements FilterInterface
{
protected string $separator = ' ';
gsteel marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param Options|string $separator Space by default
*/
public function __construct(string|array $separator = ' ')
gsteel marked this conversation as resolved.
Show resolved Hide resolved
{
if (is_array($separator) && isset($separator['separator'])) {
$this->setSeparator($separator['separator']);

return;
}

$this->setSeparator($separator);

Check failure on line 37 in src/Word/DashToSeparator.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyInvalidCast

src/Word/DashToSeparator.php:37:29: PossiblyInvalidCast: array{separator?: string, ...<array-key, mixed>} cannot be cast to string (see https://psalm.dev/190)

Check failure on line 37 in src/Word/DashToSeparator.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyInvalidArgument

src/Word/DashToSeparator.php:37:29: PossiblyInvalidArgument: Argument 1 of Laminas\Filter\Word\DashToSeparator::setSeparator expects string, but possibly different type array{separator?: string, ...<array-key, mixed>}|string provided (see https://psalm.dev/092)
}

public function __invoke(mixed $value): mixed
{
return $this->filter($value);
}

public function filter(mixed $value): mixed
{
return self::applyFilterOnlyToStringableValuesAndStringableArrayValues(
$value,
Closure::fromCallable([$this, 'filterNormalizedValue'])
if (! is_array($value)) {
if (! is_scalar($value)) {
return $value;
}
return $this->filterNormalizedValue((string) $value);
}

return $this->filterNormalizedValue(
array_map(static fn($item) => is_scalar($item) ? (string) $item : $item, $value)

Check failure on line 55 in src/Word/DashToSeparator.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MixedArgumentTypeCoercion

src/Word/DashToSeparator.php:55:13: MixedArgumentTypeCoercion: Argument 1 of Laminas\Filter\Word\DashToSeparator::filterNormalizedValue expects array<array-key, string>|string, but parent type array<array-key, mixed|string> provided (see https://psalm.dev/194)
gsteel marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand All @@ -34,4 +64,16 @@
{
return str_replace('-', $this->separator, $value);
}

/** @return $this */
public function setSeparator(string $separator): self
gsteel marked this conversation as resolved.
Show resolved Hide resolved
{
$this->separator = $separator;
return $this;
}

public function getSeparator(): string

Check failure on line 75 in src/Word/DashToSeparator.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyUnusedMethod

src/Word/DashToSeparator.php:75:21: PossiblyUnusedMethod: Cannot find any calls to method Laminas\Filter\Word\DashToSeparator::getSeparator (see https://psalm.dev/087)
{
return $this->separator;
}
}