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 f4767aa
Show file tree
Hide file tree
Showing 2 changed files with 48 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__,
);
}

return $value;
}

Expand Down
14 changes: 11 additions & 3 deletions tests/Types/DateImmutableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,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();

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

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Class \InvalidArgumentException should not be referenced via a fully qualified name, but via a use statement.
}
});

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

0 comments on commit f4767aa

Please sign in to comment.