From b7e1827e3ffdd2a1a4dd44abb1475c9430dfa6ec Mon Sep 17 00:00:00 2001 From: alexmerlin Date: Wed, 21 Apr 2021 15:56:37 +0300 Subject: [PATCH] Issue #224: Use DatetimeImmutable instead of Datetime --- src/App/src/Common/AbstractEntity.php | 9 ++-- .../src/Common/TimestampAwareInterface.php | 10 ++-- src/App/src/Common/TimestampAwareTrait.php | 49 +++++++++++-------- src/App/src/Common/UuidAwareInterface.php | 3 ++ src/User/src/Entity/UserInterface.php | 29 ----------- src/User/src/Entity/UserResetPassword.php | 24 +++++---- 6 files changed, 54 insertions(+), 70 deletions(-) diff --git a/src/App/src/Common/AbstractEntity.php b/src/App/src/Common/AbstractEntity.php index 7726c445..bfd02885 100644 --- a/src/App/src/Common/AbstractEntity.php +++ b/src/App/src/Common/AbstractEntity.php @@ -4,10 +4,7 @@ namespace Frontend\App\Common; -use DateTime; -use Exception; -use Ramsey\Uuid\UuidInterface; -use Doctrine\ORM\Mapping as ORM; +use DateTimeImmutable; /** * Class AbstractEntity @@ -24,8 +21,8 @@ abstract class AbstractEntity implements UuidAwareInterface, TimestampAwareInter public function __construct() { $this->uuid = UuidOrderedTimeGenerator::generateUuid(); - $this->created = new DateTime('now'); - $this->updated = new DateTime('now'); + $this->created = new DateTimeImmutable(); + $this->updated = new DateTimeImmutable(); } /** diff --git a/src/App/src/Common/TimestampAwareInterface.php b/src/App/src/Common/TimestampAwareInterface.php index 34e16008..f9dab7ea 100644 --- a/src/App/src/Common/TimestampAwareInterface.php +++ b/src/App/src/Common/TimestampAwareInterface.php @@ -4,7 +4,7 @@ namespace Frontend\App\Common; -use DateTime; +use DateTimeImmutable; /** * Interface TimestampAwareInterface @@ -13,14 +13,14 @@ interface TimestampAwareInterface { /** - * @return DateTime|null + * @return DateTimeImmutable|null */ - public function getCreated(): DateTime; + public function getCreated(): ?DateTimeImmutable; /** - * @return DateTime|null + * @return DateTimeImmutable|null */ - public function getUpdated(): ?DateTime; + public function getUpdated(): ?DateTimeImmutable; /** * Update internal timestamps diff --git a/src/App/src/Common/TimestampAwareTrait.php b/src/App/src/Common/TimestampAwareTrait.php index 9cc2d743..867ce9da 100644 --- a/src/App/src/Common/TimestampAwareTrait.php +++ b/src/App/src/Common/TimestampAwareTrait.php @@ -4,7 +4,7 @@ namespace Frontend\App\Common; -use DateTime; +use DateTimeImmutable; use Doctrine\ORM\Mapping as ORM; use Exception; @@ -20,40 +20,41 @@ trait TimestampAwareTrait private $dateFormat = 'Y-m-d H:i:s'; /** - * @ORM\Column(name="created", type="datetime") - * @var DateTime + * @ORM\Column(name="created", type="datetime_immutable") + * @var DateTimeImmutable */ protected $created; /** - * @ORM\Column(name="updated", type="datetime", nullable=true) - * @var DateTime + * @ORM\Column(name="updated", type="datetime_immutable", nullable=true) + * @var DateTimeImmutable */ protected $updated; /** * @ORM\PrePersist() * @ORM\PreUpdate() + * @return void */ - public function updateTimestamps() + public function updateTimestamps(): void { $this->touch(); } /** - * @return DateTime + * @return DateTimeImmutable|null */ - public function getCreated(): DateTime + public function getCreated(): ?DateTimeImmutable { return $this->created; } /** - * @return null|string + * @return string|null */ - public function getCreatedFormatted() + public function getCreatedFormatted(): ?string { - if ($this->created instanceof DateTime) { + if ($this->created instanceof DateTimeImmutable) { return $this->created->format($this->dateFormat); } @@ -61,19 +62,19 @@ public function getCreatedFormatted() } /** - * @return DateTime + * @return DateTimeImmutable|null */ - public function getUpdated(): ?DateTime + public function getUpdated(): ?DateTimeImmutable { return $this->updated; } /** - * @return null|string + * @return string|null */ - public function getUpdatedFormatted() + public function getUpdatedFormatted(): ?string { - if ($this->updated instanceof DateTime) { + if ($this->updated instanceof DateTimeImmutable) { return $this->updated->format($this->dateFormat); } @@ -81,16 +82,24 @@ public function getUpdatedFormatted() } /** - * @return $this + * @param string $dateFormat + */ + public function setDateFormat(string $dateFormat): void + { + $this->dateFormat = $dateFormat; + } + + /** + * @return void */ public function touch(): void { try { - if (!($this->created instanceof DateTime)) { - $this->created = new DateTime('now'); + if (!($this->created instanceof DateTimeImmutable)) { + $this->created = new DateTimeImmutable(); } - $this->updated = new DateTime('now'); + $this->updated = new DateTimeImmutable(); } catch (Exception $exception) { #TODO save the error message } diff --git a/src/App/src/Common/UuidAwareInterface.php b/src/App/src/Common/UuidAwareInterface.php index c5812240..c432230a 100644 --- a/src/App/src/Common/UuidAwareInterface.php +++ b/src/App/src/Common/UuidAwareInterface.php @@ -12,5 +12,8 @@ */ interface UuidAwareInterface { + /** + * @return UuidInterface + */ public function getUuid(): UuidInterface; } diff --git a/src/User/src/Entity/UserInterface.php b/src/User/src/Entity/UserInterface.php index 02bd2868..c9a80279 100644 --- a/src/User/src/Entity/UserInterface.php +++ b/src/User/src/Entity/UserInterface.php @@ -4,9 +4,6 @@ namespace Frontend\User\Entity; -use DateTime; -use Ramsey\Uuid\UuidInterface; - /** * Interface UserInterface * @package Frontend\User\Entity @@ -85,37 +82,11 @@ public function addRole(UserRole $role): UserInterface; */ public function removeRole(UserRole $role): UserInterface; - /** - * @return UuidInterface - */ - public function getUuid(): UuidInterface; - - /** - * @return DateTime - */ - public function getCreated(): DateTime; - - /** - * @return DateTime|null - */ - public function getUpdated(): ?DateTime; - - /** - * @ORM\PrePersist() - * @ORM\PreUpdate() - */ - public function updateTimestamps(); - /** * @return bool */ public function getIsDeleted(): bool; - /** - * @return void - */ - public function touch(): void; - /** * @return array */ diff --git a/src/User/src/Entity/UserResetPassword.php b/src/User/src/Entity/UserResetPassword.php index 4c545aec..55d34036 100644 --- a/src/User/src/Entity/UserResetPassword.php +++ b/src/User/src/Entity/UserResetPassword.php @@ -4,8 +4,11 @@ namespace Frontend\User\Entity; +use DateInterval; use DateTime; +use DateTimeImmutable; use Doctrine\ORM\Mapping as ORM; +use Exception; use Frontend\App\Common\AbstractEntity; /** @@ -32,8 +35,8 @@ class UserResetPassword extends AbstractEntity protected $user; /** - * @ORM\Column(name="expires", type="datetime", nullable=false) - * @var DateTime + * @ORM\Column(name="expires", type="datetime_immutable", nullable=false) + * @var DateTimeImmutable */ protected $expires; @@ -56,8 +59,9 @@ public function __construct() { parent::__construct(); - $this->expires = new DateTime(); - $this->expires->add(new \DateInterval('P1D')); + $tomorrow = new DateTime(); + $tomorrow->add(new DateInterval('P1D')); + $this->expires = DateTimeImmutable::createFromMutable($tomorrow); } /** @@ -80,18 +84,18 @@ public function setUser(User $user) } /** - * @return DateTime + * @return DateTimeImmutable */ - public function getExpires(): DateTime + public function getExpires(): DateTimeImmutable { return $this->expires; } /** - * @param DateTime $expires + * @param DateTimeImmutable $expires * @return $this */ - public function setExpires(DateTime $expires) + public function setExpires(DateTimeImmutable $expires) { $this->expires = $expires; @@ -154,8 +158,8 @@ public function isCompleted() public function isValid(): bool { try { - return $this->getExpires() > (new DateTime()); - } catch (\Exception $exception) { + return $this->getExpires() > (new DateTimeImmutable()); + } catch (Exception $exception) { } return false;