diff --git a/src/Duration.php b/src/Duration.php index 1b2f9bb..9a2d03a 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -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); } /** diff --git a/src/Instant.php b/src/Instant.php index 9c3067a..46db3d6 100644 --- a/src/Instant.php +++ b/src/Instant.php @@ -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 @@ -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); } /** @@ -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 diff --git a/src/LocalDate.php b/src/LocalDate.php index 3820318..c3abed9 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -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); } /** @@ -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); } /** diff --git a/src/LocalDateTime.php b/src/LocalDateTime.php index 6523eb3..5b24c06 100644 --- a/src/LocalDateTime.php +++ b/src/LocalDateTime.php @@ -108,7 +108,14 @@ 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()); } /** @@ -116,7 +123,14 @@ public static function min(): 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()); } /** diff --git a/src/LocalTime.php b/src/LocalTime.php index 516d64d..1611120 100644 --- a/src/LocalTime.php +++ b/src/LocalTime.php @@ -175,12 +175,19 @@ 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); } /** @@ -188,7 +195,14 @@ public static function noon(): 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); } /** @@ -196,7 +210,14 @@ public static function min(): 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); } /** diff --git a/src/Period.php b/src/Period.php index d173c14..aee2a22 100644 --- a/src/Period.php +++ b/src/Period.php @@ -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); } /** diff --git a/src/TimeZoneOffset.php b/src/TimeZoneOffset.php index 2d8cf64..4ce1471 100644 --- a/src/TimeZoneOffset.php +++ b/src/TimeZoneOffset.php @@ -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); } /** diff --git a/tests/DurationTest.php b/tests/DurationTest.php index 20c600b..c30e91f 100644 --- a/tests/DurationTest.php +++ b/tests/DurationTest.php @@ -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); + $this->assertSame($zero, Duration::zero()); } /** diff --git a/tests/InstantTest.php b/tests/InstantTest.php index 5d3f317..f8be2e1 100644 --- a/tests/InstantTest.php +++ b/tests/InstantTest.php @@ -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); + $this->assertSame($epoch, Instant::epoch()); } public function testNow(): void @@ -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); + $this->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); + $this->assertSame($max, Instant::max()); } /** diff --git a/tests/LocalDateTest.php b/tests/LocalDateTest.php index 7cb2937..30c71d4 100644 --- a/tests/LocalDateTest.php +++ b/tests/LocalDateTest.php @@ -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); + $this->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); + $this->assertSame($max, LocalDate::max()); } /** diff --git a/tests/LocalDateTimeTest.php b/tests/LocalDateTimeTest.php index 8fc4e2b..33a41fc 100644 --- a/tests/LocalDateTimeTest.php +++ b/tests/LocalDateTimeTest.php @@ -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); + $this->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); + $this->assertSame($max, LocalDateTime::max()); } public function testMinMaxOf(): void diff --git a/tests/LocalTimeTest.php b/tests/LocalTimeTest.php index c9760b7..6d91df6 100644 --- a/tests/LocalTimeTest.php +++ b/tests/LocalTimeTest.php @@ -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); + $this->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); + $this->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); + $this->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); + $this->assertSame($max, LocalTime::max()); } /** diff --git a/tests/PeriodTest.php b/tests/PeriodTest.php index d64d8ab..400f77d 100644 --- a/tests/PeriodTest.php +++ b/tests/PeriodTest.php @@ -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); + $this->assertSame($zero, Period::zero()); } /** diff --git a/tests/TimeZoneOffsetTest.php b/tests/TimeZoneOffsetTest.php index 0921060..3d99ae3 100644 --- a/tests/TimeZoneOffsetTest.php +++ b/tests/TimeZoneOffsetTest.php @@ -139,7 +139,10 @@ public function providerOfInvalidTotalSecondsThrowsException(): iterable public function testUtc(): void { - $this->assertTimeZoneOffsetIs(0, TimeZoneOffset::utc()); + $utc = TimeZoneOffset::utc(); + + $this->assertTimeZoneOffsetIs(0, $utc); + $this->assertSame($utc, TimeZoneOffset::utc()); } /** diff --git a/tests/TimeZoneTest.php b/tests/TimeZoneTest.php index f5eec88..9fd8d3f 100644 --- a/tests/TimeZoneTest.php +++ b/tests/TimeZoneTest.php @@ -68,7 +68,10 @@ public function providerParseInvalidStringThrowsException(): array public function testUtc(): void { - $this->assertTimeZoneOffsetIs(0, TimeZone::utc()); + $utc = TimeZone::utc(); + + $this->assertTimeZoneOffsetIs(0, $utc); + $this->assertSame($utc, TimeZone::utc()); } public function testIsEqualTo(): void