Use DatetimeImmutable instead of DateTime wherever possible

This commit is contained in:
Jan Böhmer 2024-06-22 17:36:54 +02:00
parent eebc373734
commit 235d572f8c
39 changed files with 222 additions and 112 deletions

View file

@ -41,9 +41,9 @@ use Symfony\Component\Validator\Constraints as Assert;
abstract class AbstractCompany extends AbstractPartsContainingDBElement
{
#[Groups(['company:read'])]
protected ?\DateTime $addedDate = null;
protected ?\DateTimeImmutable $addedDate = null;
#[Groups(['company:read'])]
protected ?\DateTime $lastModified = null;
protected ?\DateTimeImmutable $lastModified = null;
/**
* @var string The address of the company

View file

@ -34,28 +34,28 @@ use Symfony\Component\Serializer\Annotation\Groups;
trait TimestampTrait
{
/**
* @var \DateTime|null the date when this element was modified the last time
* @var \DateTimeImmutable|null the date when this element was modified the last time
*/
#[Groups(['extended', 'full'])]
#[ApiProperty(writable: false)]
#[ORM\Column(name: 'last_modified', type: Types::DATETIME_MUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])]
protected ?\DateTime $lastModified = null;
#[ORM\Column(name: 'last_modified', type: Types::DATETIME_IMMUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])]
protected ?\DateTimeImmutable $lastModified = null;
/**
* @var \DateTime|null the date when this element was created
* @var \DateTimeImmutable|null the date when this element was created
*/
#[Groups(['extended', 'full'])]
#[ApiProperty(writable: false)]
#[ORM\Column(name: 'datetime_added', type: Types::DATETIME_MUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])]
protected ?\DateTime $addedDate = null;
#[ORM\Column(name: 'datetime_added', type: Types::DATETIME_IMMUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])]
protected ?\DateTimeImmutable $addedDate = null;
/**
* Returns the last time when the element was modified.
* Returns null if the element was not yet saved to DB yet.
*
* @return \DateTimeInterface|null the time of the last edit
* @return \DateTimeImmutable|null the time of the last edit
*/
public function getLastModified(): ?\DateTimeInterface
public function getLastModified(): ?\DateTimeImmutable
{
return $this->lastModified;
}
@ -64,9 +64,9 @@ trait TimestampTrait
* Returns the date/time when the element was created.
* Returns null if the element was not yet saved to DB yet.
*
* @return \DateTimeInterface|null the creation time of the part
* @return \DateTimeImmutable|null the creation time of the part
*/
public function getAddedDate(): ?\DateTimeInterface
public function getAddedDate(): ?\DateTimeImmutable
{
return $this->addedDate;
}
@ -78,9 +78,9 @@ trait TimestampTrait
#[ORM\PreUpdate]
public function updateTimestamps(): void
{
$this->lastModified = new DateTime('now');
$this->lastModified = new \DateTimeImmutable('now');
if (null === $this->addedDate) {
$this->addedDate = new DateTime('now');
$this->addedDate = new \DateTimeImmutable('now');
}
}
}