From c35b8e6eadd213a25a0f34d8f81380812690fbc1 Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 5 Sep 2024 22:18:46 +0100 Subject: [PATCH] Refactor `ToInt` & `ToFloat` Filters - Remove inheritance and implement FilterInterface - Cleanup tests Signed-off-by: George Steel --- psalm-baseline.xml | 3 +- src/ToFloat.php | 18 +++++------- src/ToInt.php | 18 +++++------- test/ToFloatTest.php | 70 +++++++++++++++++--------------------------- test/ToIntTest.php | 63 ++++++++++++++++----------------------- 5 files changed, 70 insertions(+), 102 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index bcf28d7f..f358960a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -939,12 +939,11 @@ - - + diff --git a/src/ToFloat.php b/src/ToFloat.php index a46013d4..977bb6c9 100644 --- a/src/ToFloat.php +++ b/src/ToFloat.php @@ -6,20 +6,13 @@ use function is_scalar; -/** - * @psalm-type Options = array{} - * @extends AbstractFilter - */ -final class ToFloat extends AbstractFilter +/** @implements FilterInterface */ +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 { @@ -29,4 +22,9 @@ public function filter(mixed $value): mixed return (float) $value; } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); + } } diff --git a/src/ToInt.php b/src/ToInt.php index 467f7b79..c40fee5f 100644 --- a/src/ToInt.php +++ b/src/ToInt.php @@ -6,20 +6,13 @@ use function is_scalar; -/** - * @psalm-type Options = array{} - * @extends AbstractFilter - */ -final class ToInt extends AbstractFilter +/** @implements FilterInterface */ +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 { @@ -29,4 +22,9 @@ public function filter(mixed $value): mixed return (int) $value; } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); + } } diff --git a/test/ToFloatTest.php b/test/ToFloatTest.php index b213b1b9..bef4035e 100644 --- a/test/ToFloatTest.php +++ b/test/ToFloatTest.php @@ -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 */ + /** @return array */ 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 */ - 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)); } } diff --git a/test/ToIntTest.php b/test/ToIntTest.php index 140c5a0b..cbc3a653 100644 --- a/test/ToIntTest.php +++ b/test/ToIntTest.php @@ -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 */ + 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 */ - 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)); } }