From bcd0866778b661f7274fed507a6a78ecbcc38481 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Wed, 19 Apr 2023 13:35:24 -0300 Subject: [PATCH] Deprecate passing timezone information to methods where it is ignored --- src/Types/DateImmutableType.php | 38 ++++++++++++++++++++++++++- tests/Types/DateImmutableTypeTest.php | 15 ++++++++--- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/Types/DateImmutableType.php b/src/Types/DateImmutableType.php index da96b69d543..5bf8f0ac2ae 100644 --- a/src/Types/DateImmutableType.php +++ b/src/Types/DateImmutableType.php @@ -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()); } @@ -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__, + ); + } + return $value; } diff --git a/tests/Types/DateImmutableTypeTest.php b/tests/Types/DateImmutableTypeTest.php index f21c2802b12..052fe254caa 100644 --- a/tests/Types/DateImmutableTypeTest.php +++ b/tests/Types/DateImmutableTypeTest.php @@ -4,6 +4,7 @@ use DateTime; use DateTimeImmutable; +use InvalidArgumentException; use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\ConversionException; @@ -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',