Skip to content

Commit

Permalink
feature #1516 Only use immutable datetime objects (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the main branch.

Discussion
----------

Only use immutable datetime objects

Since we use SQLite, it looks like we don't need a DB migration to make this change work. But maybe I'm wrong. Thanks for reviewing this.

Commits
-------

6d6ec2c Only use immutable datetime objects
  • Loading branch information
javiereguiluz committed Apr 16, 2024
2 parents 12f7357 + 6d6ec2c commit 31fad76
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/DataFixtures/AppFixtures.php
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
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
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
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
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 31fad76

Please sign in to comment.