Skip to content

Commit

Permalink
Use static variables inside every singleton constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Sep 14, 2023
1 parent c7c2107 commit 19fb90d
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 29 deletions.
9 changes: 8 additions & 1 deletion src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ private function __construct(int $seconds, int $nanos = 0)
*/
public static function zero(): Duration
{
return new Duration(0);
/** @var Duration|null $zero */
static $zero;

if ($zero) {
return $zero;
}

return $zero = new Duration(0);
}

/**
Expand Down
27 changes: 24 additions & 3 deletions src/Instant.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ public static function of(int $epochSecond, int $nanoAdjustment = 0): Instant

public static function epoch(): Instant
{
return new Instant(0, 0);
/** @var Instant|null $epoch */
static $epoch;

if ($epoch) {
return $epoch;
}

return $epoch = new Instant(0, 0);
}

public static function now(?Clock $clock = null): Instant
Expand All @@ -96,7 +103,14 @@ public static function now(?Clock $clock = null): Instant
*/
public static function min(): Instant
{
return new Instant(PHP_INT_MIN, 0);
/** @var Instant|null $min */
static $min;

if ($min) {
return $min;
}

return $min = new Instant(PHP_INT_MIN, 0);
}

/**
Expand All @@ -106,7 +120,14 @@ public static function min(): Instant
*/
public static function max(): Instant
{
return new Instant(PHP_INT_MAX, 999_999_999);
/** @var Instant|null $max */
static $max;

if ($max) {
return $max;
}

return $max = new Instant(PHP_INT_MAX, 999_999_999);
}

public function plus(Duration $duration): Instant
Expand Down
18 changes: 16 additions & 2 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,14 @@ public static function now(TimeZone $timeZone, ?Clock $clock = null): LocalDate
*/
public static function min(): LocalDate
{
return LocalDate::of(self::MIN_YEAR, 1, 1);
/** @var LocalDate|null $min */
static $min;

if ($min) {
return $min;
}

return $min = LocalDate::of(self::MIN_YEAR, 1, 1);
}

/**
Expand All @@ -231,7 +238,14 @@ public static function min(): LocalDate
*/
public static function max(): LocalDate
{
return LocalDate::of(self::MAX_YEAR, 12, 31);
/** @var LocalDate|null $max */
static $max;

if ($max) {
return $max;
}

return $max = LocalDate::of(self::MAX_YEAR, 12, 31);
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,29 @@ public static function fromNativeDateTime(DateTimeInterface $dateTime): LocalDat
*/
public static function min(): LocalDateTime
{
return new LocalDateTime(LocalDate::min(), LocalTime::min());
/** @var LocalDateTime|null $min */
static $min;

if ($min) {
return $min;
}

return $min = new LocalDateTime(LocalDate::min(), LocalTime::min());
}

/**
* Returns the highest possible value for LocalDateTime.
*/
public static function max(): LocalDateTime
{
return new LocalDateTime(LocalDate::max(), LocalTime::max());
/** @var LocalDateTime|null $max */
static $max;

if ($max) {
return $max;
}

return $max = new LocalDateTime(LocalDate::max(), LocalTime::max());
}

/**
Expand Down
29 changes: 25 additions & 4 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,49 @@ public static function now(TimeZone $timeZone, ?Clock $clock = null): LocalTime

public static function midnight(): LocalTime
{
return new LocalTime(0, 0, 0, 0);
return self::min();
}

public static function noon(): LocalTime
{
return new LocalTime(12, 0, 0, 0);
/** @var LocalTime|null $noon */
static $noon;

if ($noon) {
return $noon;
}

return $noon = new LocalTime(12, 0, 0, 0);
}

/**
* Returns the smallest possible value for LocalTime.
*/
public static function min(): LocalTime
{
return new LocalTime(0, 0, 0, 0);
/** @var LocalTime|null $min */
static $min;

if ($min) {
return $min;
}

return $min = new LocalTime(0, 0, 0, 0);
}

/**
* Returns the highest possible value for LocalTime.
*/
public static function max(): LocalTime
{
return new LocalTime(23, 59, 59, 999_999_999);
/** @var LocalTime|null $max */
static $max;

if ($max) {
return $max;
}

return $max = new LocalTime(23, 59, 59, 999_999_999);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ public static function ofDays(int $days): Period
*/
public static function zero(): Period
{
return new Period(0, 0, 0);
/** @var Period|null $zero */
static $zero;

if ($zero) {
return $zero;
}

return $zero = new Period(0, 0, 0);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/TimeZoneOffset.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ public static function ofTotalSeconds(int $totalSeconds): TimeZoneOffset

public static function utc(): TimeZoneOffset
{
return new TimeZoneOffset(0);
/** @var TimeZoneOffset|null $utc */
static $utc;

if ($utc) {
return $utc;
}

return $utc = new TimeZoneOffset(0);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class DurationTest extends AbstractTestCase
{
public function testZero(): void
{
$this->assertDurationIs(0, 0, Duration::zero());
$zero = Duration::zero();

$this->assertDurationIs(0, 0, $zero);
self::assertSame($zero, Duration::zero());
}

/**
Expand Down
15 changes: 12 additions & 3 deletions tests/InstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public function providerOf(): array

public function testEpoch(): void
{
$this->assertInstantIs(0, 0, Instant::epoch());
$epoch = Instant::epoch();

$this->assertInstantIs(0, 0, $epoch);
self::assertSame($epoch, Instant::epoch());
}

public function testNow(): void
Expand All @@ -63,12 +66,18 @@ public function testNow(): void

public function testMin(): void
{
$this->assertInstantIs(PHP_INT_MIN, 0, Instant::min());
$min = Instant::min();

$this->assertInstantIs(PHP_INT_MIN, 0, $min);
self::assertSame($min, Instant::min());
}

public function testMax(): void
{
$this->assertInstantIs(PHP_INT_MAX, 999999999, Instant::max());
$max = Instant::max();

$this->assertInstantIs(PHP_INT_MAX, 999999999, $max);
self::assertSame($max, Instant::max());
}

/**
Expand Down
10 changes: 8 additions & 2 deletions tests/LocalDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,18 @@ public function providerNow(): array

public function testMin(): void
{
$this->assertLocalDateIs(Year::MIN_VALUE, 1, 1, LocalDate::min());
$min = LocalDate::min();

$this->assertLocalDateIs(Year::MIN_VALUE, 1, 1, $min);
self::assertSame($min, LocalDate::min());
}

public function testMax(): void
{
$this->assertLocalDateIs(Year::MAX_VALUE, 12, 31, LocalDate::max());
$max = LocalDate::max();

$this->assertLocalDateIs(Year::MAX_VALUE, 12, 31, $max);
self::assertSame($max, LocalDate::max());
}

/**
Expand Down
10 changes: 8 additions & 2 deletions tests/LocalDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,18 @@ public function providerParseInvalidDateTimeThrowsException(): array

public function testMin(): void
{
$this->assertLocalDateTimeIs(Year::MIN_VALUE, 1, 1, 0, 0, 0, 0, LocalDateTime::min());
$min = LocalDateTime::min();

$this->assertLocalDateTimeIs(Year::MIN_VALUE, 1, 1, 0, 0, 0, 0, $min);
self::assertSame($min, LocalDateTime::min());
}

public function testMax(): void
{
$this->assertLocalDateTimeIs(Year::MAX_VALUE, 12, 31, 23, 59, 59, 999999999, LocalDateTime::max());
$max = LocalDateTime::max();

$this->assertLocalDateTimeIs(Year::MAX_VALUE, 12, 31, 23, 59, 59, 999999999, $max);
self::assertSame($max, LocalDateTime::max());
}

public function testMinMaxOf(): void
Expand Down
20 changes: 16 additions & 4 deletions tests/LocalTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,34 @@ public function providerNow(): array

public function testMidnight(): void
{
$this->assertLocalTimeIs(0, 0, 0, 0, LocalTime::midnight());
$midnight = LocalTime::midnight();

$this->assertLocalTimeIs(0, 0, 0, 0, $midnight);
self::assertSame($midnight, LocalTime::midnight());
}

public function testNoon(): void
{
$this->assertLocalTimeIs(12, 0, 0, 0, LocalTime::noon());
$noon = LocalTime::noon();

$this->assertLocalTimeIs(12, 0, 0, 0, $noon);
self::assertSame($noon, LocalTime::noon());
}

public function testMin(): void
{
$this->assertLocalTimeIs(0, 0, 0, 0, LocalTime::min());
$min = LocalTime::min();

$this->assertLocalTimeIs(0, 0, 0, 0, $min);
self::assertSame($min, LocalTime::min());
}

public function testMax(): void
{
$this->assertLocalTimeIs(23, 59, 59, 999999999, LocalTime::max());
$max = LocalTime::max();

$this->assertLocalTimeIs(23, 59, 59, 999999999, $max);
self::assertSame($max, LocalTime::max());
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/PeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public function testOfDays(): void

public function testZero(): void
{
$this->assertPeriodIs(0, 0, 0, Period::zero());
$zero = Period::zero();

$this->assertPeriodIs(0, 0, 0, $zero);
self::assertSame($zero, Period::zero());
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/TimeZoneOffsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ public function providerOfInvalidTotalSecondsThrowsException(): iterable

public function testUtc(): void
{
$this->assertTimeZoneOffsetIs(0, TimeZoneOffset::utc());
$utc = TimeZoneOffset::utc();

$this->assertTimeZoneOffsetIs(0, $utc);
self::assertSame($utc, TimeZoneOffset::utc());
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/TimeZoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public function providerParseInvalidStringThrowsException(): array

public function testUtc(): void
{
$this->assertTimeZoneOffsetIs(0, TimeZone::utc());
$utc = TimeZone::utc();

$this->assertTimeZoneOffsetIs(0, $utc);
self::assertSame($utc, TimeZone::utc());
}

public function testIsEqualTo(): void
Expand Down

0 comments on commit 19fb90d

Please sign in to comment.