Skip to content

Commit de574d4

Browse files
committed
minor #50297 Remove unnecessary usages of DateTime (nicolas-grekas)
This PR was merged into the 6.4 branch. Discussion ---------- Remove unnecessary usages of DateTime | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Part of #47580 | License | MIT | Doc PR | - Together with #50290, this PR removes `DateTime` everywhere possible. What remains is: - test cases that test both DateTime and DateTimeImmutable - legit to keep - date/time form-types and transformers in the Form component - we'd need a separate effort for them, related to #50295 - `PersistentTokenInterface::getLastUsed(): \DateTime` - separate effort also Commits ------- 8b08294b63 Remove unnecessary usages of DateTime
2 parents 295de66 + 2cbca2b commit de574d4

File tree

5 files changed

+19
-21
lines changed

5 files changed

+19
-21
lines changed

BinaryFileResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function setChunkSize(int $chunkSize): static
125125
*/
126126
public function setAutoLastModified(): static
127127
{
128-
$this->setLastModified(\DateTime::createFromFormat('U', $this->file->getMTime()));
128+
$this->setLastModified(\DateTimeImmutable::createFromFormat('U', $this->file->getMTime()));
129129

130130
return $this;
131131
}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Make `HeaderBag::getDate()`, `Response::getDate()`, `getExpires()` and `getLastModified()` return a `DateTimeImmutable`
8+
49
6.3
510
---
611

HeaderBag.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,17 @@ public function remove(string $key)
193193
/**
194194
* Returns the HTTP header value converted to a date.
195195
*
196+
* @return \DateTimeImmutable|null
197+
*
196198
* @throws \RuntimeException When the HTTP header is not parseable
197199
*/
198-
public function getDate(string $key, \DateTime $default = null): ?\DateTimeInterface
200+
public function getDate(string $key, \DateTimeInterface $default = null): ?\DateTimeInterface
199201
{
200202
if (null === $value = $this->get($key)) {
201-
return $default;
203+
return null !== $default ? \DateTimeImmutable::createFromInterface($default) : null;
202204
}
203205

204-
if (false === $date = \DateTime::createFromFormat(\DATE_RFC2822, $value)) {
206+
if (false === $date = \DateTimeImmutable::createFromFormat(\DATE_RFC2822, $value)) {
205207
throw new \RuntimeException(sprintf('The "%s" HTTP header is not parseable (%s).', $key, $value));
206208
}
207209

Response.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ public function mustRevalidate(): bool
687687
*
688688
* @final
689689
*/
690-
public function getDate(): ?\DateTimeInterface
690+
public function getDate(): ?\DateTimeImmutable
691691
{
692692
return $this->headers->getDate('Date');
693693
}
@@ -701,10 +701,7 @@ public function getDate(): ?\DateTimeInterface
701701
*/
702702
public function setDate(\DateTimeInterface $date): static
703703
{
704-
if ($date instanceof \DateTime) {
705-
$date = \DateTimeImmutable::createFromMutable($date);
706-
}
707-
704+
$date = \DateTimeImmutable::createFromInterface($date);
708705
$date = $date->setTimezone(new \DateTimeZone('UTC'));
709706
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
710707

@@ -745,13 +742,13 @@ public function expire(): static
745742
*
746743
* @final
747744
*/
748-
public function getExpires(): ?\DateTimeInterface
745+
public function getExpires(): ?\DateTimeImmutable
749746
{
750747
try {
751748
return $this->headers->getDate('Expires');
752749
} catch (\RuntimeException) {
753750
// according to RFC 2616 invalid date formats (e.g. "0" and "-1") must be treated as in the past
754-
return \DateTime::createFromFormat('U', time() - 172800);
751+
return \DateTimeImmutable::createFromFormat('U', time() - 172800);
755752
}
756753
}
757754

@@ -775,10 +772,7 @@ public function setExpires(\DateTimeInterface $date = null): static
775772
return $this;
776773
}
777774

778-
if ($date instanceof \DateTime) {
779-
$date = \DateTimeImmutable::createFromMutable($date);
780-
}
781-
775+
$date = \DateTimeImmutable::createFromInterface($date);
782776
$date = $date->setTimezone(new \DateTimeZone('UTC'));
783777
$this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
784778

@@ -934,7 +928,7 @@ public function setClientTtl(int $seconds): static
934928
*
935929
* @final
936930
*/
937-
public function getLastModified(): ?\DateTimeInterface
931+
public function getLastModified(): ?\DateTimeImmutable
938932
{
939933
return $this->headers->getDate('Last-Modified');
940934
}
@@ -959,10 +953,7 @@ public function setLastModified(\DateTimeInterface $date = null): static
959953
return $this;
960954
}
961955

962-
if ($date instanceof \DateTime) {
963-
$date = \DateTimeImmutable::createFromMutable($date);
964-
}
965-
956+
$date = \DateTimeImmutable::createFromInterface($date);
966957
$date = $date->setTimezone(new \DateTimeZone('UTC'));
967958
$this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
968959

Tests/HeaderBagTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testGetDate()
4545
{
4646
$bag = new HeaderBag(['foo' => 'Tue, 4 Sep 2012 20:00:00 +0200']);
4747
$headerDate = $bag->getDate('foo');
48-
$this->assertInstanceOf(\DateTime::class, $headerDate);
48+
$this->assertInstanceOf(\DateTimeImmutable::class, $headerDate);
4949
}
5050

5151
public function testGetDateNull()

0 commit comments

Comments
 (0)