Skip to content

Commit f4767aa

Browse files
committed
Deprecate passing timezone information to methods where it is ignored
1 parent b30518d commit f4767aa

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/Types/DateImmutableType.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
3535
}
3636

3737
if ($value instanceof DateTimeImmutable) {
38+
$offset = $value->format('O');
39+
$defaultOffset = (new DateTimeImmutable())->format('O');
40+
41+
if ($offset !== $defaultOffset) {
42+
Deprecation::triggerIfCalledFromOutside(
43+
'doctrine/dbal',
44+
'https://github.com/doctrine/dbal/pull/xxxx',
45+
'Passing a timezone offset (%s) different than the default one (%s) is deprecated'
46+
. ' as it will be lost, use %s::%s() instead.',
47+
$offset,
48+
$defaultOffset,
49+
DateTimeTzImmutableType::class,
50+
__FUNCTION__,
51+
);
52+
}
53+
3854
return $value->format($platform->getDateFormatString());
3955
}
4056

@@ -56,7 +72,27 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
5672
*/
5773
public function convertToPHPValue($value, AbstractPlatform $platform)
5874
{
59-
if ($value === null || $value instanceof DateTimeImmutable) {
75+
if ($value === null) {
76+
return null;
77+
}
78+
79+
if ($value instanceof DateTimeImmutable) {
80+
$offset = $value->format('O');
81+
$defaultOffset = (new DateTimeImmutable())->format('O');
82+
83+
if ($offset !== $defaultOffset) {
84+
Deprecation::triggerIfCalledFromOutside(
85+
'doctrine/dbal',
86+
'https://github.com/doctrine/dbal/pull/xxxx',
87+
'Passing a timezone offset (%s) different than the default one (%s) is deprecated'
88+
. ' as it may be lost, use %s::%s() instead.',
89+
$offset,
90+
$defaultOffset,
91+
DateTimeTzImmutableType::class,
92+
__FUNCTION__,
93+
);
94+
}
95+
6096
return $value;
6197
}
6298

tests/Types/DateImmutableTypeTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ public function testConvertsDateTimeImmutableInstanceToDatabaseValue(): void
4848
$this->platform->expects(self::once())
4949
->method('getDateFormatString')
5050
->willReturn('Y-m-d');
51-
$date->expects(self::once())
51+
$date->expects(self::exactly(2))
5252
->method('format')
53-
->with('Y-m-d')
54-
->willReturn('2016-01-01');
53+
->willReturnCallback(static function (string $format): string {
54+
switch ($format) {
55+
case 'O':
56+
return 'UTC';
57+
case 'Y-m-d':
58+
return '2016-01-01';
59+
default:
60+
throw new \InvalidArgumentException();
61+
}
62+
});
5563

5664
self::assertSame(
5765
'2016-01-01',

0 commit comments

Comments
 (0)