Skip to content

Only use immutable datetime objects #1516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 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 @@ -130,7 +130,7 @@ private function getTagData(): array
/**
* @throws \Exception
*
* @return array<int, array{0: string, 1: AbstractUnicodeString, 2: string, 3: string, 4: \DateTime, 5: User, 6: array<Tag>}>
* @return array<int, array{0: string, 1: AbstractUnicodeString, 2: string, 3: string, 4: \DateTimeImmutable, 5: User, 6: array<Tag>}>
*/
private function getPostData(): array
{
Expand All @@ -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]
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]
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