Skip to content

Commit

Permalink
Merge pull request #216 from marcelthole/refactor-dir-filter
Browse files Browse the repository at this point in the history
Refactor Dir filter to implement FlterInterface
  • Loading branch information
gsteel authored Jan 8, 2025
2 parents 2477249 + 527193b commit 736f67a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
5 changes: 5 additions & 0 deletions docs/book/v3/migration/v2-to-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ The following methods have been removed:

The constructor now only accepts an associative array of [documented options](../standard-filters.md#denylist).

#### `Dir`

This filter will not cast a given integer or float value to a string anymore.
The return value changed from `.` to return the given integer or float value.

#### `HtmlEntities`

The following methods have been removed:
Expand Down
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
<code><![CDATA[isOptions]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/Dir.php">
<DeprecatedClass>
<code><![CDATA[AbstractFilter]]></code>
</DeprecatedClass>
</file>
<file src="src/File/Rename.php">
<DeprecatedClass>
<code><![CDATA[Filter\AbstractFilter]]></code>
Expand Down
17 changes: 9 additions & 8 deletions src/Dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@
namespace Laminas\Filter;

use function dirname;
use function is_scalar;
use function is_string;

/**
* @psalm-type Options = array{}
* @extends AbstractFilter<Options>
* @implements FilterInterface<string>
*/
final class Dir extends AbstractFilter
final class Dir implements FilterInterface
{
/**
* Defined by Laminas\Filter\FilterInterface
*
* Returns dirname($value)
*
* @psalm-return ($value is scalar ? string : mixed)
*/
public function filter(mixed $value): mixed
{
if (! is_scalar($value)) {
if (! is_string($value) || $value === '') {
return $value;
}
$value = (string) $value;

return dirname($value);
}

public function __invoke(mixed $value): mixed
{
return $this->filter($value);
}
}
29 changes: 20 additions & 9 deletions test/DirTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ class DirTest extends TestCase
/**
* Ensures that the filter follows expected behavior
*/
public function testBasic(): void
#[DataProvider('defaultSettingsDataProvider')]
public function testBasic(string $input, string $expected): void
{
$filter = new DirFilter();
$valuesExpected = [
'filename' => '.',
'/path/to/filename' => '/path/to',
'/path/to/filename.ext' => '/path/to',
$filter = new DirFilter();

self::assertSame($expected, $filter($input));
self::assertSame($expected, $filter->__invoke($input));
self::assertSame($expected, $filter->filter($input));
}

public static function defaultSettingsDataProvider(): array
{
return [
['12345', '.'],
['filename', '.'],
['/path/to/filename', '/path/to'],
['/path/to/filename.ext', '/path/to'],
];
foreach ($valuesExpected as $input => $output) {
self::assertSame($output, $filter($input));
}
}

/** @return list<array{0: mixed}> */
Expand All @@ -33,6 +40,10 @@ public static function returnUnfilteredDataProvider(): array
return [
[null],
[new stdClass()],
[''],
[12345],
[true],
[false],
[
[
'/path/to/filename',
Expand Down

0 comments on commit 736f67a

Please sign in to comment.