Skip to content

Commit

Permalink
Refactor ToInt & ToFloat Filters
Browse files Browse the repository at this point in the history
- Remove inheritance and implement FilterInterface
- Cleanup tests

Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Sep 5, 2024
1 parent f3d2ef8 commit c35b8e6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 102 deletions.
3 changes: 1 addition & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -939,12 +939,11 @@
<file src="test/ToFloatTest.php">
<PossiblyUnusedMethod>
<code><![CDATA[filterableValuesProvider]]></code>
<code><![CDATA[unfilterableValuesProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/ToIntTest.php">
<PossiblyUnusedMethod>
<code><![CDATA[returnUnfilteredDataProvider]]></code>
<code><![CDATA[basicDataProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/ToNullTest.php">
Expand Down
18 changes: 8 additions & 10 deletions src/ToFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@

use function is_scalar;

/**
* @psalm-type Options = array{}
* @extends AbstractFilter<Options>
*/
final class ToFloat extends AbstractFilter
/** @implements FilterInterface<float> */
final class ToFloat implements FilterInterface
{
/**
* Defined by Laminas\Filter\FilterInterface
*
* Returns (float) $value
* Casts scalar values to float
*
* If the value provided is non-scalar, the value will remain unfiltered
*
* @psalm-return ($value is scalar ? float : mixed)
*/
public function filter(mixed $value): mixed
{
Expand All @@ -29,4 +22,9 @@ public function filter(mixed $value): mixed

return (float) $value;
}

public function __invoke(mixed $value): mixed
{
return $this->filter($value);
}
}
18 changes: 8 additions & 10 deletions src/ToInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@

use function is_scalar;

/**
* @psalm-type Options = array{}
* @extends AbstractFilter<Options>
*/
final class ToInt extends AbstractFilter
/** @implements FilterInterface<int> */
final class ToInt implements FilterInterface
{
/**
* Defined by Laminas\Filter\FilterInterface
*
* Returns (int) $value
* Casts scalar values to integer
*
* If the value provided is non-scalar, the value will remain unfiltered
*
* @psalm-return ($value is scalar ? int : mixed)
*/
public function filter(mixed $value): mixed
{
Expand All @@ -29,4 +22,9 @@ public function filter(mixed $value): mixed

return (int) $value;
}

public function __invoke(mixed $value): mixed
{
return $this->filter($value);
}
}
70 changes: 27 additions & 43 deletions test/ToFloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,45 @@

namespace LaminasTest\Filter;

use Laminas\Filter\ToFloat as ToFloatFilter;
use Laminas\Filter\ToFloat;
use LaminasTest\Filter\TestAsset\StringableObject;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;

class ToFloatTest extends TestCase
{
/** @return array<string, array{0: mixed, 1: float}> */
/** @return array<string, array{0: mixed, 1: mixed}> */
public static function filterableValuesProvider(): array
{
return [
'string word' => ['string', 0.0],
'string 1' => ['1', 1.0],
'string -1' => ['-1', -1.0],
'string 1.1' => ['1.1', 1.1],
'string -1.1' => ['-1.1', -1.1],
'string 0.9' => ['0.9', 0.9],
'string -0.9' => ['-0.9', -0.9],
'integer 1' => [1, 1.0],
'integer -1' => [-1, -1.0],
'true' => [true, 1.0],
'false' => [false, 0.0],
'float 1.1' => [1.1, 1.1],
];
}
$object = (object) ['foo' => 'bar'];
$stringable = new StringableObject('Foo');

/**
* Ensures that the filter follows expected behavior
*/
#[DataProvider('filterableValuesProvider')]
public function testCanFilterScalarValuesAsExpected(mixed $input, float $expectedOutput): void
{
$filter = new ToFloatFilter();
self::assertSame($expectedOutput, $filter($input));
}

/** @return array<string, array{0: mixed}> */
public static function unfilterableValuesProvider(): array
{
return [
'null' => [null],
'array' => [
[
'1',
-1,
],
],
'object' => [new stdClass()],
'string word' => ['string', 0.0],
'string 1' => ['1', 1.0],
'string -1' => ['-1', -1.0],
'string 1.1' => ['1.1', 1.1],
'string -1.1' => ['-1.1', -1.1],
'string 0.9' => ['0.9', 0.9],
'string -0.9' => ['-0.9', -0.9],
'integer 1' => [1, 1.0],
'integer -1' => [-1, -1.0],
'true' => [true, 1.0],
'false' => [false, 0.0],
'float 1.1' => [1.1, 1.1],
'null' => [null, null],
'Object' => [$object, $object],
'Stringable' => [$stringable, $stringable],
'Empty String' => ['', 0.0],
'Array' => [[123.0, '123.0', 'foo'], [123.0, '123.0', 'foo']],
];
}

#[DataProvider('unfilterableValuesProvider')]
public function testReturnsUnfilterableInputVerbatim(mixed $input): void
#[DataProvider('filterableValuesProvider')]
public function testCanFilterScalarValuesAsExpected(mixed $input, mixed $expectedOutput): void
{
$filter = new ToFloatFilter();
self::assertSame($input, $filter($input));
$filter = new ToFloat();
self::assertSame($expectedOutput, $filter->filter($input));
self::assertSame($expectedOutput, $filter->__invoke($input));
}
}
63 changes: 26 additions & 37 deletions test/ToIntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,43 @@

namespace LaminasTest\Filter;

use Laminas\Filter\ToInt as ToIntFilter;
use Laminas\Filter\ToInt;
use LaminasTest\Filter\TestAsset\StringableObject;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;

use const PHP_INT_MAX;

class ToIntTest extends TestCase
{
/**
* Ensures that the filter follows expected behavior
*/
public function testBasic(): void
/** @return array<string, array{0: mixed, 1: mixed}> */
public static function basicDataProvider(): array
{
$filter = new ToIntFilter();

$valuesExpected = [
'string' => 0,
'1' => 1,
'-1' => -1,
'1.1' => 1,
'-1.1' => -1,
'0.9' => 0,
'-0.9' => 0,
];
foreach ($valuesExpected as $input => $output) {
self::assertSame($output, $filter($input));
}
}
$object = (object) ['foo' => 'bar'];
$stringable = new StringableObject('Foo');

/** @return list<array{0: mixed}> */
public static function returnUnfilteredDataProvider(): array
{
return [
[null],
[new stdClass()],
[
[
'1',
-1,
],
],
'String' => ['string', 0],
'Digits' => ['123', 123],
'Float String' => ['1.23', 1],
'Float' => [1.23, 1],
'Int' => [123, 123],
'Array' => [[123, '123', 'foo'], [123, '123', 'foo']],
'Object' => [$object, $object],
'Stringable' => [$stringable, $stringable],
'Null' => [null, null],
'Empty String' => ['', 0],
'Negative Int' => [-1, -1],
'Negative String' => ['-1', -1],
'Massive Number' => ['9223372036854775807999', PHP_INT_MAX],
];
}

#[DataProvider('returnUnfilteredDataProvider')]
public function testReturnUnfiltered(mixed $input): void
#[DataProvider('basicDataProvider')]
public function testBasicBehaviour(mixed $input, mixed $expect): void
{
$filter = new ToIntFilter();

self::assertSame($input, $filter($input));
$filter = new ToInt();
self::assertSame($expect, $filter->filter($input));
self::assertSame($expect, $filter->__invoke($input));
}
}

0 comments on commit c35b8e6

Please sign in to comment.