Skip to content

Commit

Permalink
Use DateTime::createFromImmutable if available (#7)
Browse files Browse the repository at this point in the history
* Use DateTime::createFromImmutable if available

* Use new \DateTime in SystemClock.
Assert return value

Co-authored-by: Marko Kunic <[email protected]>

* Please PHPStan

* Use FQNS

* Remove additional space
  • Loading branch information
Rob Treacy authored and ben-challis committed Nov 6, 2019
1 parent 72df79f commit fef8a81
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 8 additions & 4 deletions lib/FixedClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ public function now(): \DateTimeImmutable

public function nowMutable(): \DateTime
{
// @TODO Use \DateTime::createFromImmutable when PHP version is >=7.3
$instance = \DateTime::createFromFormat(self::ISO8601_MICROSECONDS_FORMAT, $this->now->format(self::ISO8601_MICROSECONDS_FORMAT));
\assert($instance instanceof \DateTime);
$nowImmutable = $this->now();

return $instance;
$nowMutable = \PHP_VERSION_ID >= 70300
? \DateTime::createFromImmutable($nowImmutable)
: \DateTime::createFromFormat(self::ISO8601_MICROSECONDS_FORMAT, $nowImmutable->format(self::ISO8601_MICROSECONDS_FORMAT));

\assert($nowMutable instanceof \DateTime);

return $nowMutable;
}
}
6 changes: 1 addition & 5 deletions lib/SystemClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public function now(): \DateTimeImmutable

public function nowMutable(): \DateTime
{
// @TODO Use \DateTime::createFromImmutable when PHP version is >=7.3
$instance = \DateTime::createFromFormat(self::ISO8601_MICROSECONDS_FORMAT, $this->now()->format(self::ISO8601_MICROSECONDS_FORMAT));
\assert($instance instanceof \DateTime);

return $instance;
return new \DateTime('now', $this->timeZone);
}
}

0 comments on commit fef8a81

Please sign in to comment.