Skip to content

Commit

Permalink
Only use immutable datetime objects
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Apr 15, 2024
1 parent 12f7357 commit 6583901
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function loadPosts(ObjectManager $manager): void
$comment = new Comment();
$comment->setAuthor($commentAuthor);
$comment->setContent($this->getRandomText(random_int(255, 512)));
$comment->setPublishedAt(new \DateTime('now + '.$i.'seconds'));
$comment->setPublishedAt(new \DateTimeImmutable('now + '.$i.'seconds'));

$post->addComment($comment);
}
Expand Down Expand Up @@ -147,7 +147,7 @@ private function getPostData(): array
$this->slugger->slug($title)->lower(),
$this->getRandomText(),
$this->getPostContent(),
(new \DateTime('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)),
(new \DateTimeImmutable('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)),
// Ensure that the first post is written by Jane Doe to simplify tests
$user,
$this->getRandomTags(),
Expand Down
10 changes: 5 additions & 5 deletions src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ class Comment
#[Assert\Length(min: 5, minMessage: 'comment.too_short', max: 10000, maxMessage: 'comment.too_long')]
private ?string $content = null;

#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private \DateTime $publishedAt;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private \DateTimeImmutable $publishedAt;

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

public function __construct()
{
$this->publishedAt = new \DateTime();
$this->publishedAt = new \DateTimeImmutable();
}

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

public function getPublishedAt(): \DateTime
public function getPublishedAt(): \DateTimeImmutable
{
return $this->publishedAt;
}

public function setPublishedAt(\DateTime $publishedAt): void
public function setPublishedAt(\DateTimeImmutable $publishedAt): void
{
$this->publishedAt = $publishedAt;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class Post
#[Assert\Length(min: 10, minMessage: 'post.too_short_content')]
private ?string $content = null;

#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private \DateTime $publishedAt;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private \DateTimeImmutable $publishedAt;

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

public function __construct()
{
$this->publishedAt = new \DateTime();
$this->publishedAt = new \DateTimeImmutable();
$this->comments = new ArrayCollection();
$this->tags = new ArrayCollection();
}
Expand Down Expand Up @@ -123,12 +123,12 @@ public function setContent(?string $content): void
$this->content = $content;
}

public function getPublishedAt(): \DateTime
public function getPublishedAt(): \DateTimeImmutable
{
return $this->publishedAt;
}

public function setPublishedAt(\DateTime $publishedAt): void
public function setPublishedAt(\DateTimeImmutable $publishedAt): void
{
$this->publishedAt = $publishedAt;
}
Expand Down
1 change: 1 addition & 0 deletions src/Form/Type/DateTimePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function configureOptions(OptionsResolver $resolver): void
// @see https://symfony.com/doc/current/reference/forms/types/date.html#rendering-a-single-html5-text-box
$resolver->setDefaults([
'widget' => 'single_text',
'input' => 'datetime_immutable',
// if true, the browser will display the native date picker widget
// however, this app uses a custom JavaScript widget, so it must be set to false
'html5' => false,
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function findLatest(int $page = 1, ?Tag $tag = null): Paginator
->leftJoin('p.tags', 't')
->where('p.publishedAt <= :now')
->orderBy('p.publishedAt', 'DESC')
->setParameter('now', new \DateTime())
->setParameter('now', new \DateTimeImmutable())
;

if (null !== $tag) {
Expand Down

0 comments on commit 6583901

Please sign in to comment.