Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Convert some parts of Date validator to php8 syntax and add some tests to check userland retrocompatibility. #177

Open
wants to merge 1 commit into
base: 2.48.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,6 @@
<PossiblyInvalidArgument>
<code>$options</code>
</PossiblyInvalidArgument>
<PossiblyUndefinedVariable>
<code>$temp</code>
</PossiblyUndefinedVariable>
</file>
<file src="src/DateStep.php">
<ArgumentTypeCoercion>
Expand Down
26 changes: 14 additions & 12 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
$options = $temp;
}
Expand Down Expand Up @@ -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
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
*/
private function addInvalidTypeError(bool $addErrors)
{
if ($addErrors) {
$this->error(self::INVALID);
}

return false;
}

Expand Down
19 changes: 19 additions & 0 deletions test/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
55 changes: 55 additions & 0 deletions test/TestAsset/CustomDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Validator\TestAsset;

use DateTime;
use DateTimeInterface;
use Laminas\Validator\Date;

final class CustomDate extends Date
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's document why we introduced this specific asset

{
/** @var string */
protected $format = self::FORMAT_DEFAULT;

/** @var bool */
protected $strict = false;

/**
* @param string|numeric|array|DateTimeInterface $value
* @return bool
*/
public function isValid($value)
{
return parent::isValid($value);
}

/**
* @param string|numeric|array|DateTimeInterface $param
* @param bool $addErrors
* @return bool|DateTime
*/
protected function convertToDateTime($param, $addErrors = true)
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
{
return parent::convertToDateTime($param, false);
}

/**
* @param integer $value
* @return false|DateTime
*/
protected function convertInteger($value)
{
return parent::convertInteger($value);
}

/**
* @param double $value
* @return false|DateTime
*/
protected function convertDouble($value)
{
return parent::convertDouble($value * 100);
}
}