From 78d2570267c45b5235ac8ffbcd8a63c7ea37c691 Mon Sep 17 00:00:00 2001 From: codisart Date: Sun, 19 Feb 2023 18:33:49 +0100 Subject: [PATCH] chore: Convert some parts of Date validator to php8 syntax and add some tests to check userland retrocompatibility. Signed-off-by: codisart --- psalm-baseline.xml | 3 -- src/Date.php | 26 +++++++++-------- test/DateTest.php | 19 ++++++++++++ test/TestAsset/CustomDate.php | 55 +++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 test/TestAsset/CustomDate.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c09d39b90..95b36d4ad 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -526,9 +526,6 @@ $options - - $temp - diff --git a/src/Date.php b/src/Date.php index a74c12216..b00ef676a 100644 --- a/src/Date.php +++ b/src/Date.php @@ -67,6 +67,7 @@ public function __construct($options = []) $options = iterator_to_array($options); } elseif (! is_array($options)) { $options = func_get_args(); + $temp = []; $temp['format'] = array_shift($options); $options = $temp; } @@ -152,22 +153,23 @@ protected function convertToDateTime($param, $addErrors = true) return DateTime::createFromImmutable($param); } - $type = gettype($param); - switch ($type) { - case 'string': - return $this->convertString($param, $addErrors); - case 'integer': - return $this->convertInteger($param); - case 'double': - return $this->convertDouble($param); - case 'array': - return $this->convertArray($param, $addErrors); - } + return match (gettype($param)) { + 'string' => $this->convertString($param, $addErrors), + 'integer' => $this->convertInteger($param), + 'double' => $this->convertDouble($param), + 'array' => $this->convertArray($param), + default => $this->addInvalidTypeError($addErrors), + }; + } + /** + * @return false + */ + private function addInvalidTypeError(bool $addErrors) + { if ($addErrors) { $this->error(self::INVALID); } - return false; } diff --git a/test/DateTest.php b/test/DateTest.php index 8a1f49840..5f6f9efff 100644 --- a/test/DateTest.php +++ b/test/DateTest.php @@ -7,6 +7,7 @@ use DateTime; use DateTimeImmutable; use Laminas\Validator\Date; +use LaminasTest\Validator\TestAsset\CustomDate; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; @@ -195,4 +196,22 @@ public function testConstructorWithFormatParameter(): void self::assertSame($format, $validator->getFormat()); } + + public function testAddErrorsParam(): void + { + $customDateValidator = new Date(); + /** @psalm-suppress InvalidArgument */ + self::assertFalse($customDateValidator->isValid(new stdClass())); + self::assertArrayHasKey('dateInvalid', $customDateValidator->getMessages()); + } + + public function testExtensionDateValidator(): void + { + $customDateValidator = new CustomDate(); + self::assertTrue($customDateValidator->isValid(16757802.07)); + + /** @psalm-suppress InvalidArgument */ + self::assertFalse($customDateValidator->isValid(new stdClass())); + self::assertArrayNotHasKey('dateInvalid', $customDateValidator->getMessages()); + } } diff --git a/test/TestAsset/CustomDate.php b/test/TestAsset/CustomDate.php new file mode 100644 index 000000000..d842e4293 --- /dev/null +++ b/test/TestAsset/CustomDate.php @@ -0,0 +1,55 @@ +