Skip to content

Commit e2be9c8

Browse files
Remove unnecessary usages of DateTime
1 parent cb76849 commit e2be9c8

File tree

8 files changed

+13
-18
lines changed

8 files changed

+13
-18
lines changed

Extension/Core/DataTransformer/DateTimeToArrayTransformer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ public function transform(mixed $dateTime): array
6767
}
6868

6969
if ($this->inputTimezone !== $this->outputTimezone) {
70-
if (!$dateTime instanceof \DateTimeImmutable) {
71-
$dateTime = clone $dateTime;
72-
}
73-
70+
$dateTime = \DateTimeImmutable::createFromInterface($dateTime);
7471
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
7572
}
7673

Extension/Core/DataTransformer/DateTimeToHtml5LocalDateTimeTransformer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ public function transform(mixed $dateTime): string
5353
}
5454

5555
if ($this->inputTimezone !== $this->outputTimezone) {
56-
if (!$dateTime instanceof \DateTimeImmutable) {
57-
$dateTime = clone $dateTime;
58-
}
59-
56+
$dateTime = \DateTimeImmutable::createFromInterface($dateTime);
6057
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
6158
}
6259

Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ public function transform(mixed $dateTime): string
3838
}
3939

4040
if ($this->inputTimezone !== $this->outputTimezone) {
41-
if (!$dateTime instanceof \DateTimeImmutable) {
42-
$dateTime = clone $dateTime;
43-
}
44-
41+
$dateTime = \DateTimeImmutable::createFromInterface($dateTime);
4542
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
4643
}
4744

Extension/Core/DataTransformer/DateTimeToStringTransformer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ public function transform(mixed $dateTime): string
8585
throw new TransformationFailedException('Expected a \DateTimeInterface.');
8686
}
8787

88-
if (!$dateTime instanceof \DateTimeImmutable) {
89-
$dateTime = clone $dateTime;
90-
}
91-
88+
$dateTime = \DateTimeImmutable::createFromInterface($dateTime);
9289
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
9390

9491
return $dateTime->format($this->generateFormat);

Extension/Core/Type/DateType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ private function listYears(array $years): array
362362
$result = [];
363363

364364
foreach ($years as $year) {
365-
$result[\PHP_INT_SIZE === 4 ? \DateTime::createFromFormat('Y e', $year.' UTC')->format('U') : gmmktime(0, 0, 0, 6, 15, $year)] = $year;
365+
$result[\PHP_INT_SIZE === 4 ? \DateTimeImmutable::createFromFormat('Y e', $year.' UTC')->format('U') : gmmktime(0, 0, 0, 6, 15, $year)] = $year;
366366
}
367367

368368
return $result;

Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public function guessTypeForConstraint(Constraint $constraint): ?TypeGuess
115115
case '\DateTime':
116116
return new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE);
117117

118+
case \DateTimeImmutable::class:
119+
case '\DateTimeImmutable':
120+
case \DateTimeInterface::class:
121+
case '\DateTimeInterface':
122+
return new TypeGuess(DateType::class, ['input' => 'datetime_immutable'], Guess::MEDIUM_CONFIDENCE);
123+
118124
case 'string':
119125
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE);
120126
}

Tests/Extension/Core/DataTransformer/Traits/DateTimeEqualsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait DateTimeEqualsTrait
1818
{
1919
public static function assertDateTimeEquals($expected, $actual)
2020
{
21-
if ($expected instanceof \DateTime && $actual instanceof \DateTime) {
21+
if ($expected instanceof \DateTimeInterface && $actual instanceof \DateTimeInterface) {
2222
$expected = $expected->format('c');
2323
$actual = $actual->format('c');
2424
}

Tests/Extension/Validator/ValidatorTypeGuesserTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public static function guessTypeProvider()
9393
[new Type('long'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)],
9494
[new Type('string'), new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE)],
9595
[new Type(\DateTime::class), new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE)],
96+
[new Type(\DateTimeImmutable::class), new TypeGuess(DateType::class, ['input' => 'datetime_immutable'], Guess::MEDIUM_CONFIDENCE)],
9697
[new Type('\DateTime'), new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE)],
9798
];
9899
}

0 commit comments

Comments
 (0)