Skip to content

Commit c031ad0

Browse files
authored
Stricter DateTime types (#6161)
1 parent c41e4db commit c031ad0

15 files changed

+108
-155
lines changed

UPGRADE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ awareness about deprecated code.
88

99
# Upgrade to 4.0
1010

11+
## BC BREAK: Stricter `DateTime` types
12+
13+
The following types don't accept or return `DateTimeImmutable` instances anymore:
14+
15+
* `DateTimeType`
16+
* `DateTimeTzType`
17+
* `DateType`
18+
* `TimeType`
19+
* `VarDateTimeType`
20+
21+
As a consequence, the following type classes don't extend their mutable
22+
counterparts anymore:
23+
24+
* `DateTimeImmutableType`
25+
* `DateTimeTzImmutableType`
26+
* `DateImmutableType`
27+
* `TimeImmutableType`
28+
* `VarDateTimeImmutableType`
29+
1130
## BC BREAK: Remove legacy execute and fetch methods.
1231

1332
The following methods have been removed:

src/Platforms/AbstractPlatform.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,11 +1463,11 @@ public function getDefaultValueDeclarationSQL(array $column): string
14631463
return ' DEFAULT ' . $this->getCurrentTimestampSQL();
14641464
}
14651465

1466-
if ($type instanceof Types\TimeType && $default === $this->getCurrentTimeSQL()) {
1466+
if ($type instanceof Types\PhpTimeMappingType && $default === $this->getCurrentTimeSQL()) {
14671467
return ' DEFAULT ' . $this->getCurrentTimeSQL();
14681468
}
14691469

1470-
if ($type instanceof Types\DateType && $default === $this->getCurrentDateSQL()) {
1470+
if ($type instanceof Types\PhpDateMappingType && $default === $this->getCurrentDateSQL()) {
14711471
return ' DEFAULT ' . $this->getCurrentDateSQL();
14721472
}
14731473

src/Types/DateImmutableType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
/**
1313
* Immutable type of {@see DateType}.
1414
*/
15-
class DateImmutableType extends DateType
15+
class DateImmutableType extends Type implements PhpDateMappingType
1616
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
21+
{
22+
return $platform->getDateTypeDeclarationSQL($column);
23+
}
24+
1725
/**
1826
* @param T $value
1927
*

src/Types/DateTimeImmutableType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@
1313
/**
1414
* Immutable type of {@see DateTimeType}.
1515
*/
16-
class DateTimeImmutableType extends DateTimeType
16+
class DateTimeImmutableType extends Type implements PhpDateTimeMappingType
1717
{
18+
/**
19+
* {@inheritDoc}
20+
*/
21+
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
22+
{
23+
return $platform->getDateTimeTypeDeclarationSQL($column);
24+
}
25+
1826
/**
1927
* @param T $value
2028
*

src/Types/DateTimeType.php

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
namespace Doctrine\DBAL\Types;
66

77
use DateTime;
8-
use DateTimeImmutable;
9-
use DateTimeInterface;
108
use Doctrine\DBAL\Platforms\AbstractPlatform;
119
use Doctrine\DBAL\Types\Exception\InvalidFormat;
1210
use Doctrine\DBAL\Types\Exception\InvalidType;
13-
use Doctrine\Deprecations\Deprecation;
1411
use Exception;
1512

1613
/**
@@ -39,49 +36,27 @@ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform)
3936
return $value;
4037
}
4138

42-
if ($value instanceof DateTimeImmutable) {
43-
Deprecation::triggerIfCalledFromOutside(
44-
'doctrine/dbal',
45-
'https://github.com/doctrine/dbal/pull/6017',
46-
'Passing an instance of %s is deprecated, use %s::%s() instead.',
47-
$value::class,
48-
DateTimeImmutableType::class,
49-
__FUNCTION__,
50-
);
51-
}
52-
53-
if ($value instanceof DateTimeInterface) {
39+
if ($value instanceof DateTime) {
5440
return $value->format($platform->getDateTimeFormatString());
5541
}
5642

5743
throw InvalidType::new(
5844
$value,
5945
static::class,
60-
['null', DateTime::class, DateTimeImmutable::class],
46+
['null', DateTime::class],
6147
);
6248
}
6349

6450
/**
6551
* @param T $value
6652
*
67-
* @return (T is null ? null : DateTimeInterface)
53+
* @return (T is null ? null : DateTime)
6854
*
6955
* @template T
7056
*/
71-
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DateTimeInterface
57+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DateTime
7258
{
73-
if ($value instanceof DateTimeImmutable) {
74-
Deprecation::triggerIfCalledFromOutside(
75-
'doctrine/dbal',
76-
'https://github.com/doctrine/dbal/pull/6017',
77-
'Passing an instance of %s is deprecated, use %s::%s() instead.',
78-
$value::class,
79-
DateTimeImmutableType::class,
80-
__FUNCTION__,
81-
);
82-
}
83-
84-
if ($value === null || $value instanceof DateTimeInterface) {
59+
if ($value === null || $value instanceof DateTime) {
8560
return $value;
8661
}
8762

src/Types/DateTimeTzImmutableType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
/**
1313
* Immutable type of {@see DateTimeTzType}.
1414
*/
15-
class DateTimeTzImmutableType extends DateTimeTzType
15+
class DateTimeTzImmutableType extends Type implements PhpDateTimeMappingType
1616
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
21+
{
22+
return $platform->getDateTimeTzTypeDeclarationSQL($column);
23+
}
24+
1725
/**
1826
* @psalm-param T $value
1927
*

src/Types/DateTimeTzType.php

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
namespace Doctrine\DBAL\Types;
66

77
use DateTime;
8-
use DateTimeImmutable;
9-
use DateTimeInterface;
108
use Doctrine\DBAL\Platforms\AbstractPlatform;
119
use Doctrine\DBAL\Types\Exception\InvalidFormat;
1210
use Doctrine\DBAL\Types\Exception\InvalidType;
13-
use Doctrine\Deprecations\Deprecation;
1411

1512
/**
1613
* DateTime type saving additional timezone information.
@@ -51,18 +48,7 @@ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform)
5148
return $value;
5249
}
5350

54-
if ($value instanceof DateTimeImmutable) {
55-
Deprecation::triggerIfCalledFromOutside(
56-
'doctrine/dbal',
57-
'https://github.com/doctrine/dbal/pull/6017',
58-
'Passing an instance of %s is deprecated, use %s::%s() instead.',
59-
$value::class,
60-
DateTimeTzImmutableType::class,
61-
__FUNCTION__,
62-
);
63-
}
64-
65-
if ($value instanceof DateTimeInterface) {
51+
if ($value instanceof DateTime) {
6652
return $value->format($platform->getDateTimeTzFormatString());
6753
}
6854

@@ -76,24 +62,13 @@ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform)
7662
/**
7763
* @param T $value
7864
*
79-
* @return (T is null ? null : DateTimeInterface)
65+
* @return (T is null ? null : DateTime)
8066
*
8167
* @template T
8268
*/
83-
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DateTimeInterface
69+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DateTime
8470
{
85-
if ($value instanceof DateTimeImmutable) {
86-
Deprecation::triggerIfCalledFromOutside(
87-
'doctrine/dbal',
88-
'https://github.com/doctrine/dbal/pull/6017',
89-
'Passing an instance of %s is deprecated, use %s::%s() instead.',
90-
$value::class,
91-
DateTimeTzImmutableType::class,
92-
__FUNCTION__,
93-
);
94-
}
95-
96-
if ($value === null || $value instanceof DateTimeInterface) {
71+
if ($value === null || $value instanceof DateTime) {
9772
return $value;
9873
}
9974

src/Types/DateType.php

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
namespace Doctrine\DBAL\Types;
66

77
use DateTime;
8-
use DateTimeImmutable;
9-
use DateTimeInterface;
108
use Doctrine\DBAL\Platforms\AbstractPlatform;
119
use Doctrine\DBAL\Types\Exception\InvalidFormat;
1210
use Doctrine\DBAL\Types\Exception\InvalidType;
13-
use Doctrine\Deprecations\Deprecation;
1411

1512
/**
1613
* Type that maps an SQL DATE to a PHP Date object.
1714
*/
18-
class DateType extends Type
15+
class DateType extends Type implements PhpDateMappingType
1916
{
2017
/**
2118
* {@inheritDoc}
@@ -38,18 +35,7 @@ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform)
3835
return $value;
3936
}
4037

41-
if ($value instanceof DateTimeInterface) {
42-
if ($value instanceof DateTimeImmutable) {
43-
Deprecation::triggerIfCalledFromOutside(
44-
'doctrine/dbal',
45-
'https://github.com/doctrine/dbal/pull/6017',
46-
'Passing an instance of %s is deprecated, use %s::%s() instead.',
47-
$value::class,
48-
DateImmutableType::class,
49-
__FUNCTION__,
50-
);
51-
}
52-
38+
if ($value instanceof DateTime) {
5339
return $value->format($platform->getDateFormatString());
5440
}
5541

@@ -59,24 +45,13 @@ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform)
5945
/**
6046
* @param T $value
6147
*
62-
* @return (T is null ? null : DateTimeInterface)
48+
* @return (T is null ? null : DateTime)
6349
*
6450
* @template T
6551
*/
66-
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DateTimeInterface
52+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DateTime
6753
{
68-
if ($value instanceof DateTimeImmutable) {
69-
Deprecation::triggerIfCalledFromOutside(
70-
'doctrine/dbal',
71-
'https://github.com/doctrine/dbal/pull/6017',
72-
'Passing an instance of %s is deprecated, use %s::%s() instead.',
73-
$value::class,
74-
DateImmutableType::class,
75-
__FUNCTION__,
76-
);
77-
}
78-
79-
if ($value === null || $value instanceof DateTimeInterface) {
54+
if ($value === null || $value instanceof DateTime) {
8055
return $value;
8156
}
8257

src/Types/PhpDateMappingType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Types;
6+
7+
/**
8+
* Implementations should map a database type to a PHP DateTimeInterface instance.
9+
*
10+
* @internal
11+
*/
12+
interface PhpDateMappingType
13+
{
14+
}

src/Types/PhpTimeMappingType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Types;
6+
7+
/**
8+
* Implementations should map a database type to a PHP DateTimeInterface instance.
9+
*
10+
* @internal
11+
*/
12+
interface PhpTimeMappingType
13+
{
14+
}

src/Types/TimeImmutableType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
/**
1313
* Immutable type of {@see TimeType}.
1414
*/
15-
class TimeImmutableType extends TimeType
15+
class TimeImmutableType extends Type implements PhpTimeMappingType
1616
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
21+
{
22+
return $platform->getTimeTypeDeclarationSQL($column);
23+
}
24+
1725
/**
1826
* @param T $value
1927
*

0 commit comments

Comments
 (0)