Skip to content

Commit 6d6ec2c

Browse files
committed
Only use immutable datetime objects
1 parent 12f7357 commit 6d6ec2c

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

src/DataFixtures/AppFixtures.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function loadPosts(ObjectManager $manager): void
8585
$comment = new Comment();
8686
$comment->setAuthor($commentAuthor);
8787
$comment->setContent($this->getRandomText(random_int(255, 512)));
88-
$comment->setPublishedAt(new \DateTime('now + '.$i.'seconds'));
88+
$comment->setPublishedAt(new \DateTimeImmutable('now + '.$i.'seconds'));
8989

9090
$post->addComment($comment);
9191
}
@@ -130,7 +130,7 @@ private function getTagData(): array
130130
/**
131131
* @throws \Exception
132132
*
133-
* @return array<int, array{0: string, 1: AbstractUnicodeString, 2: string, 3: string, 4: \DateTime, 5: User, 6: array<Tag>}>
133+
* @return array<int, array{0: string, 1: AbstractUnicodeString, 2: string, 3: string, 4: \DateTimeImmutable, 5: User, 6: array<Tag>}>
134134
*/
135135
private function getPostData(): array
136136
{
@@ -147,7 +147,7 @@ private function getPostData(): array
147147
$this->slugger->slug($title)->lower(),
148148
$this->getRandomText(),
149149
$this->getPostContent(),
150-
(new \DateTime('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)),
150+
(new \DateTimeImmutable('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)),
151151
// Ensure that the first post is written by Jane Doe to simplify tests
152152
$user,
153153
$this->getRandomTags(),

src/Entity/Comment.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ class Comment
4444
#[Assert\Length(min: 5, minMessage: 'comment.too_short', max: 10000, maxMessage: 'comment.too_long')]
4545
private ?string $content = null;
4646

47-
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
48-
private \DateTime $publishedAt;
47+
#[ORM\Column]
48+
private \DateTimeImmutable $publishedAt;
4949

5050
#[ORM\ManyToOne(targetEntity: User::class)]
5151
#[ORM\JoinColumn(nullable: false)]
5252
private ?User $author = null;
5353

5454
public function __construct()
5555
{
56-
$this->publishedAt = new \DateTime();
56+
$this->publishedAt = new \DateTimeImmutable();
5757
}
5858

5959
#[Assert\IsTrue(message: 'comment.is_spam')]
@@ -79,12 +79,12 @@ public function setContent(string $content): void
7979
$this->content = $content;
8080
}
8181

82-
public function getPublishedAt(): \DateTime
82+
public function getPublishedAt(): \DateTimeImmutable
8383
{
8484
return $this->publishedAt;
8585
}
8686

87-
public function setPublishedAt(\DateTime $publishedAt): void
87+
public function setPublishedAt(\DateTimeImmutable $publishedAt): void
8888
{
8989
$this->publishedAt = $publishedAt;
9090
}

src/Entity/Post.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class Post
5858
#[Assert\Length(min: 10, minMessage: 'post.too_short_content')]
5959
private ?string $content = null;
6060

61-
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
62-
private \DateTime $publishedAt;
61+
#[ORM\Column]
62+
private \DateTimeImmutable $publishedAt;
6363

6464
#[ORM\ManyToOne(targetEntity: User::class)]
6565
#[ORM\JoinColumn(nullable: false)]
@@ -83,7 +83,7 @@ class Post
8383

8484
public function __construct()
8585
{
86-
$this->publishedAt = new \DateTime();
86+
$this->publishedAt = new \DateTimeImmutable();
8787
$this->comments = new ArrayCollection();
8888
$this->tags = new ArrayCollection();
8989
}
@@ -123,12 +123,12 @@ public function setContent(?string $content): void
123123
$this->content = $content;
124124
}
125125

126-
public function getPublishedAt(): \DateTime
126+
public function getPublishedAt(): \DateTimeImmutable
127127
{
128128
return $this->publishedAt;
129129
}
130130

131-
public function setPublishedAt(\DateTime $publishedAt): void
131+
public function setPublishedAt(\DateTimeImmutable $publishedAt): void
132132
{
133133
$this->publishedAt = $publishedAt;
134134
}

src/Form/Type/DateTimePickerType.php

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function configureOptions(OptionsResolver $resolver): void
3131
// @see https://symfony.com/doc/current/reference/forms/types/date.html#rendering-a-single-html5-text-box
3232
$resolver->setDefaults([
3333
'widget' => 'single_text',
34+
'input' => 'datetime_immutable',
3435
// if true, the browser will display the native date picker widget
3536
// however, this app uses a custom JavaScript widget, so it must be set to false
3637
'html5' => false,

src/Repository/PostRepository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function findLatest(int $page = 1, ?Tag $tag = null): Paginator
4747
->leftJoin('p.tags', 't')
4848
->where('p.publishedAt <= :now')
4949
->orderBy('p.publishedAt', 'DESC')
50-
->setParameter('now', new \DateTime())
50+
->setParameter('now', new \DateTimeImmutable())
5151
;
5252

5353
if (null !== $tag) {

0 commit comments

Comments
 (0)