Skip to content

Commit

Permalink
Deprecate passing timezone information to methods where it is ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jul 21, 2023
1 parent b30518d commit bcd0866
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
38 changes: 37 additions & 1 deletion src/Types/DateImmutableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
}

if ($value instanceof DateTimeImmutable) {
$offset = $value->format('O');
$defaultOffset = (new DateTimeImmutable())->format('O');

if ($offset !== $defaultOffset) {
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/xxxx',
'Passing a timezone offset (%s) different than the default one (%s) is deprecated'
. ' as it will be lost, use %s::%s() instead.',
$offset,
$defaultOffset,
DateTimeTzImmutableType::class,
__FUNCTION__,
);
}

return $value->format($platform->getDateFormatString());
}

Expand All @@ -56,7 +72,27 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value instanceof DateTimeImmutable) {
if ($value === null) {
return null;
}

if ($value instanceof DateTimeImmutable) {
$offset = $value->format('O');
$defaultOffset = (new DateTimeImmutable())->format('O');

if ($offset !== $defaultOffset) {
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/xxxx',
'Passing a timezone offset (%s) different than the default one (%s) is deprecated'
. ' as it may be lost, use %s::%s() instead.',
$offset,
$defaultOffset,
DateTimeTzImmutableType::class,
__FUNCTION__,
);

Check warning on line 93 in src/Types/DateImmutableType.php

View check run for this annotation

Codecov / codecov/patch

src/Types/DateImmutableType.php#L84-L93

Added lines #L84 - L93 were not covered by tests
}

return $value;
}

Expand Down
15 changes: 12 additions & 3 deletions tests/Types/DateImmutableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use DateTimeImmutable;
use InvalidArgumentException;
use Doctrine\DBAL\ParameterType;

Check failure on line 8 in tests/Types/DateImmutableTypeTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Use statements should be sorted alphabetically. The first wrong one is Doctrine\DBAL\ParameterType.
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
Expand Down Expand Up @@ -48,10 +49,18 @@ public function testConvertsDateTimeImmutableInstanceToDatabaseValue(): void
$this->platform->expects(self::once())
->method('getDateFormatString')
->willReturn('Y-m-d');
$date->expects(self::once())
$date->expects(self::exactly(2))
->method('format')
->with('Y-m-d')
->willReturn('2016-01-01');
->willReturnCallback(static function (string $format): string {
switch ($format) {
case 'O':
return 'UTC';
case 'Y-m-d':
return '2016-01-01';
default:
throw new InvalidArgumentException();
}
});

self::assertSame(
'2016-01-01',
Expand Down

0 comments on commit bcd0866

Please sign in to comment.