Skip to content

Commit bcd0866

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

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use DateTimeImmutable;
7+
use InvalidArgumentException;
78
use Doctrine\DBAL\ParameterType;
89
use Doctrine\DBAL\Platforms\AbstractPlatform;
910
use Doctrine\DBAL\Types\ConversionException;
@@ -48,10 +49,18 @@ public function testConvertsDateTimeImmutableInstanceToDatabaseValue(): void
4849
$this->platform->expects(self::once())
4950
->method('getDateFormatString')
5051
->willReturn('Y-m-d');
51-
$date->expects(self::once())
52+
$date->expects(self::exactly(2))
5253
->method('format')
53-
->with('Y-m-d')
54-
->willReturn('2016-01-01');
54+
->willReturnCallback(static function (string $format): string {
55+
switch ($format) {
56+
case 'O':
57+
return 'UTC';
58+
case 'Y-m-d':
59+
return '2016-01-01';
60+
default:
61+
throw new InvalidArgumentException();
62+
}
63+
});
5564

5665
self::assertSame(
5766
'2016-01-01',

0 commit comments

Comments
 (0)