Skip to content

Commit 9e14f55

Browse files
committed
feature #47730 Ban DateTime from the codebase (WebMamba)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- Ban DateTime from the codebase | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | #47580 | License | MIT | Doc PR | symfony As discuss in this issue, the purpose of this PR is to remove DateTime from the code base in favor to DateTimeImmutable. I will process it component by component. Feel free to discuss! Commits ------- 689385a83f Ban DateTime from the codebase
2 parents 43c7529 + 3c12385 commit 9e14f55

File tree

6 files changed

+29
-13
lines changed

6 files changed

+29
-13
lines changed

Caster/DateCaster.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,
2828
{
2929
$prefix = Caster::PREFIX_VIRTUAL;
3030
$location = $d->getTimezone()->getLocation();
31-
$fromNow = (new \DateTime())->diff($d);
31+
$fromNow = (new \DateTimeImmutable())->diff($d);
3232

3333
$title = $d->format('l, F j, Y')
3434
."\n".self::formatInterval($fromNow).' from now'
@@ -79,7 +79,7 @@ private static function formatInterval(\DateInterval $i): string
7979
public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, bool $isNested, int $filter)
8080
{
8181
$location = $timeZone->getLocation();
82-
$formatted = (new \DateTime('now', $timeZone))->format($location ? 'e (P)' : 'P');
82+
$formatted = (new \DateTimeImmutable('now', $timeZone))->format($location ? 'e (P)' : 'P');
8383
$title = $location && \extension_loaded('intl') ? \Locale::getDisplayRegion('-'.$location['country_code']) : '';
8484

8585
$z = [Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title)];

Caster/ResourceCaster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function castOpensslX509($h, array $a, Stub $stub, bool $isNested)
7777
$a += [
7878
'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])),
7979
'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])),
80-
'expiry' => new ConstStub(date(\DateTime::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
80+
'expiry' => new ConstStub(date(\DateTimeInterface::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
8181
'fingerprint' => new EnumStub([
8282
'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)),
8383
'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)),

Tests/Caster/DateCasterTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,30 @@ public function testDumpDateTime($time, $timezone, $xDate, $xTimestamp)
4141
$this->assertDumpEquals($xDump, $date);
4242
}
4343

44+
/**
45+
* @dataProvider provideDateTimes
46+
*/
47+
public function testDumpDateTimeImmutable($time, $timezone, $xDate, $xTimestamp)
48+
{
49+
$date = new \DateTimeImmutable($time, new \DateTimeZone($timezone));
50+
51+
$xDump = <<<EODUMP
52+
DateTimeImmutable @$xTimestamp {
53+
date: $xDate
54+
}
55+
EODUMP;
56+
57+
$this->assertDumpEquals($xDump, $date);
58+
}
59+
4460
/**
4561
* @dataProvider provideDateTimes
4662
*/
4763
public function testCastDateTime($time, $timezone, $xDate, $xTimestamp, $xInfos)
4864
{
4965
$stub = new Stub();
50-
$date = new \DateTime($time, new \DateTimeZone($timezone));
51-
$cast = DateCaster::castDateTime($date, Caster::castObject($date, \DateTime::class), $stub, false, 0);
66+
$date = new \DateTimeImmutable($time, new \DateTimeZone($timezone));
67+
$cast = DateCaster::castDateTime($date, Caster::castObject($date, \DateTimeImmutable::class), $stub, false, 0);
5268

5369
$xDump = <<<EODUMP
5470
array:1 [
@@ -325,7 +341,7 @@ public function provideTimeZones()
325341
*/
326342
public function testDumpPeriod($start, $interval, $end, $options, $expected)
327343
{
328-
$p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), \is_int($end) ? $end : new \DateTime($end), $options);
344+
$p = new \DatePeriod(new \DateTimeImmutable($start), new \DateInterval($interval), \is_int($end) ? $end : new \DateTime($end), $options);
329345

330346
$xDump = <<<EODUMP
331347
DatePeriod {
@@ -341,7 +357,7 @@ public function testDumpPeriod($start, $interval, $end, $options, $expected)
341357
*/
342358
public function testCastPeriod($start, $interval, $end, $options, $xPeriod, $xDates)
343359
{
344-
$p = new \DatePeriod(new \DateTime($start, new \DateTimeZone('UTC')), new \DateInterval($interval), \is_int($end) ? $end : new \DateTime($end, new \DateTimeZone('UTC')), $options);
360+
$p = new \DatePeriod(new \DateTimeImmutable($start, new \DateTimeZone('UTC')), new \DateInterval($interval), \is_int($end) ? $end : new \DateTimeImmutable($end, new \DateTimeZone('UTC')), $options);
345361
$stub = new Stub();
346362

347363
$cast = DateCaster::castPeriod($p, [], $stub, false, 0);

Tests/Caster/SplCasterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ public function testCastObjectStorageIsntModified()
159159
public function testCastObjectStorageDumpsInfo()
160160
{
161161
$var = new \SplObjectStorage();
162-
$var->attach(new \stdClass(), new \DateTime());
162+
$var->attach(new \stdClass(), new \DateTimeImmutable());
163163

164-
$this->assertDumpMatchesFormat('%ADateTime%A', $var);
164+
$this->assertDumpMatchesFormat('%ADateTimeImmutable%A', $var);
165165
}
166166

167167
public function testCastArrayObject()

Tests/Fixtures/DateTimeChild.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Symfony\Component\VarDumper\Tests\Fixtures;
44

5-
class DateTimeChild extends \DateTime
5+
class DateTimeChild extends \DateTimeImmutable
66
{
77
private $addedProperty = 'foo';
88
}

Tests/Test/VarDumperTestTraitTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testItCanBeConfigured()
5050
{
5151
$this->setUpVarDumper($casters = [
5252
\DateTimeInterface::class => static function (\DateTimeInterface $date, array $a, Stub $stub): array {
53-
$stub->class = 'DateTime';
53+
$stub->class = 'DateTimeImmutable';
5454

5555
return ['date' => $date->format('d/m/Y')];
5656
},
@@ -63,12 +63,12 @@ public function testItCanBeConfigured()
6363
[
6464
1,
6565
2,
66-
DateTime {
66+
DateTimeImmutable {
6767
+date: "09/07/2019"
6868
}
6969
]
7070
DUMP
71-
, [1, 2, new \DateTime('2019-07-09T0:00:00+00:00')]);
71+
, [1, 2, new \DateTimeImmutable('2019-07-09T0:00:00+00:00')]);
7272

7373
$this->tearDownVarDumper();
7474

0 commit comments

Comments
 (0)