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

@ -75,10 +75,10 @@ class ApiToken implements TimeStampableInterface
#[Groups('token:read')]
private ?User $user = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
#[Groups('token:read')]
#[Year2038BugWorkaround]
private ?\DateTime $valid_until;
private ?\DateTimeImmutable $valid_until;
#[ORM\Column(length: 68, unique: true)]
private string $token;
@ -87,9 +87,9 @@ class ApiToken implements TimeStampableInterface
#[Groups('token:read')]
private ApiTokenLevel $level = ApiTokenLevel::READ_ONLY;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
#[Groups('token:read')]
private ?\DateTime $last_time_used = null;
private ?\DateTimeImmutable $last_time_used = null;
public function __construct(ApiTokenType $tokenType = ApiTokenType::PERSONAL_ACCESS_TOKEN)
{
@ -97,7 +97,7 @@ class ApiToken implements TimeStampableInterface
$this->token = $tokenType->getTokenPrefix() . bin2hex(random_bytes(32));
//By default, tokens are valid for 1 year.
$this->valid_until = new \DateTime('+1 year');
$this->valid_until = new \DateTimeImmutable('+1 year');
}
public function getTokenType(): ApiTokenType
@ -116,7 +116,7 @@ class ApiToken implements TimeStampableInterface
return $this;
}
public function getValidUntil(): ?\DateTimeInterface
public function getValidUntil(): ?\DateTimeImmutable
{
return $this->valid_until;
}
@ -127,10 +127,10 @@ class ApiToken implements TimeStampableInterface
*/
public function isValid(): bool
{
return $this->valid_until === null || $this->valid_until > new \DateTime();
return $this->valid_until === null || $this->valid_until > new \DateTimeImmutable();
}
public function setValidUntil(?\DateTime $valid_until): ApiToken
public function setValidUntil(?\DateTimeImmutable $valid_until): ApiToken
{
$this->valid_until = $valid_until;
return $this;
@ -159,19 +159,17 @@ class ApiToken implements TimeStampableInterface
/**
* Gets the last time the token was used to authenticate or null if it was never used.
* @return \DateTimeInterface|null
*/
public function getLastTimeUsed(): ?\DateTimeInterface
public function getLastTimeUsed(): ?\DateTimeImmutable
{
return $this->last_time_used;
}
/**
* Sets the last time the token was used to authenticate.
* @param \DateTime|null $last_time_used
* @return ApiToken
*/
public function setLastTimeUsed(?\DateTime $last_time_used): ApiToken
public function setLastTimeUsed(?\DateTimeImmutable $last_time_used): ApiToken
{
$this->last_time_used = $last_time_used;
return $this;

View file

@ -117,10 +117,10 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
protected ?int $id = null;
#[Groups(['user:read'])]
protected ?\DateTime $lastModified = null;
protected ?\DateTimeImmutable $lastModified = null;
#[Groups(['user:read'])]
protected ?\DateTime $addedDate = null;
protected ?\DateTimeImmutable $addedDate = null;
/**
* @var bool Determines if the user is disabled (user can not log in)
@ -277,11 +277,11 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
#[Groups(['user:read', 'user:write'])]
protected ?Attachment $master_picture_attachment = null;
/** @var \DateTimeInterface|null The time when the backup codes were generated
/** @var \DateTimeImmutable|null The time when the backup codes were generated
*/
#[Groups(['full'])]
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
protected ?\DateTimeInterface $backupCodesGenerationDate = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
protected ?\DateTimeImmutable $backupCodesGenerationDate = null;
/** @var Collection<int, LegacyU2FKeyInterface>
*/
@ -318,10 +318,10 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
protected ?PermissionData $permissions = null;
/**
* @var \DateTime|null the time until the password reset token is valid
* @var \DateTimeImmutable|null the time until the password reset token is valid
*/
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
protected ?\DateTime $pw_reset_expires = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
protected ?\DateTimeImmutable $pw_reset_expires = null;
/**
* @var bool True if the user was created by a SAML provider (and therefore cannot change its password)
@ -528,7 +528,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
/**
* Gets the datetime when the password reset token expires.
*/
public function getPwResetExpires(): \DateTimeInterface|null
public function getPwResetExpires(): \DateTimeImmutable|null
{
return $this->pw_reset_expires;
}
@ -536,7 +536,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
/**
* Sets the datetime when the password reset token expires.
*/
public function setPwResetExpires(\DateTime $pw_reset_expires): self
public function setPwResetExpires(\DateTimeImmutable $pw_reset_expires): self
{
$this->pw_reset_expires = $pw_reset_expires;
@ -897,7 +897,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
public function setBackupCodes(array $codes): self
{
$this->backupCodes = $codes;
$this->backupCodesGenerationDate = $codes === [] ? null : new DateTime();
$this->backupCodesGenerationDate = $codes === [] ? null : new \DateTimeImmutable();
return $this;
}
@ -905,7 +905,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
/**
* Return the date when the backup codes were generated.
*/
public function getBackupCodesGenerationDate(): ?\DateTimeInterface
public function getBackupCodesGenerationDate(): ?\DateTimeImmutable
{
return $this->backupCodesGenerationDate;
}

View file

@ -82,9 +82,8 @@ class WebauthnKey extends BasePublicKeyCredentialSource implements TimeStampable
/**
* Retrieve the last time when the key was used.
* @return \DateTimeInterface|null
*/
public function getLastTimeUsed(): ?\DateTimeInterface
public function getLastTimeUsed(): ?\DateTimeImmutable
{
return $this->last_time_used;
}