Skip to content

Commit

Permalink
Psalm-immutable: eagerly calculate TimeZoneOffset::$id
Browse files Browse the repository at this point in the history
  • Loading branch information
nederdirk committed Feb 27, 2021
1 parent dbf3911 commit 24986dc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/Field/SecondOfDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class SecondOfDay
* @return void
*
* @throws DateTimeException If the second-of-day is not valid.
*
* @psalm-pure
*/
public static function check(int $secondOfDay) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public static function of(int $hour, int $minute, int $second = 0, int $nano = 0
* @return LocalTime
*
* @throws DateTimeException
*
* @psalm-pure
*/
public static function ofSecondOfDay(int $secondOfDay, int $nanoOfSecond = 0) : LocalTime
{
Expand Down
22 changes: 9 additions & 13 deletions src/TimeZoneOffset.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ final class TimeZoneOffset extends TimeZone
/**
* The string representation of this time-zone offset.
*
* This is generated on-the-fly, and will be null before the first call to getId().
*
* @var string|null
* @var string
*/
private $id;

Expand All @@ -38,6 +36,14 @@ final class TimeZoneOffset extends TimeZone
private function __construct(int $totalSeconds)
{
$this->totalSeconds = $totalSeconds;

if ($this->totalSeconds < 0) {
$this->id = '-' . LocalTime::ofSecondOfDay(- $this->totalSeconds);
} elseif ($this->totalSeconds > 0) {
$this->id = '+' . LocalTime::ofSecondOfDay($this->totalSeconds);
} else {
$this->id = 'Z';
}
}

/**
Expand Down Expand Up @@ -186,16 +192,6 @@ public function getTotalSeconds() : int
*/
public function getId() : string
{
if ($this->id === null) {
if ($this->totalSeconds < 0) {
$this->id = '-' . LocalTime::ofSecondOfDay(- $this->totalSeconds);
} elseif ($this->totalSeconds > 0) {
$this->id = '+' . LocalTime::ofSecondOfDay($this->totalSeconds);
} else {
$this->id = 'Z';
}
}

return $this->id;
}

Expand Down

0 comments on commit 24986dc

Please sign in to comment.