diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c09d39b9..95b36d4a 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 a74c1221..b00ef676 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 8a1f4984..5f6f9eff 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 00000000..d842e429 --- /dev/null +++ b/test/TestAsset/CustomDate.php @@ -0,0 +1,55 @@ +