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 e2129ef
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 21 deletions.
63 changes: 56 additions & 7 deletions src/DayOfWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,55 +88,104 @@ public static function all(?DayOfWeek $first = null): array
*/
public static function monday(): DayOfWeek
{
return DayOfWeek::get(DayOfWeek::MONDAY);
/** @var DayOfWeek|null $monday */
static $monday;

if ($monday) {
return $monday;
}

return $monday = DayOfWeek::get(DayOfWeek::MONDAY);
}

/**
* Returns a day-of-week instance for Tuesday.
*/
public static function tuesday(): DayOfWeek
{
return DayOfWeek::get(DayOfWeek::TUESDAY);
/** @var DayOfWeek|null $tuesday */
static $tuesday;

if ($tuesday) {
return $tuesday;
}

return $tuesday = DayOfWeek::get(DayOfWeek::TUESDAY);
}

/**
* Returns a day-of-week instance for Wednesday.
*/
public static function wednesday(): DayOfWeek
{
return DayOfWeek::get(DayOfWeek::WEDNESDAY);
/** @var DayOfWeek|null $wednesday */
static $wednesday;

if ($wednesday) {
return $wednesday;
}

return $wednesday = DayOfWeek::get(DayOfWeek::WEDNESDAY);
}

/**
* Returns a day-of-week instance for Thursday.
*/
public static function thursday(): DayOfWeek
{
return DayOfWeek::get(DayOfWeek::THURSDAY);
/** @var DayOfWeek|null $thursday */
static $thursday;

if ($thursday) {
return $thursday;
}

return $thursday = DayOfWeek::get(DayOfWeek::THURSDAY);
}

/**
* Returns a day-of-week instance for Friday.
*/
public static function friday(): DayOfWeek
{
return DayOfWeek::get(DayOfWeek::FRIDAY);
/** @var DayOfWeek|null $friday */
static $friday;

if ($friday) {
return $friday;
}

return $friday = DayOfWeek::get(DayOfWeek::FRIDAY);
}

/**
* Returns a day-of-week instance for Saturday.
*/
public static function saturday(): DayOfWeek
{
return DayOfWeek::get(DayOfWeek::SATURDAY);
/** @var DayOfWeek|null $saturday */
static $saturday;

if ($saturday) {
return $saturday;
}

return $saturday = DayOfWeek::get(DayOfWeek::SATURDAY);
}

/**
* Returns a day-of-week instance for Sunday.
*/
public static function sunday(): DayOfWeek
{
return DayOfWeek::get(DayOfWeek::SUNDAY);
/** @var DayOfWeek|null $sunday */
static $sunday;

if ($sunday) {
return $sunday;
}

return $sunday = DayOfWeek::get(DayOfWeek::SUNDAY);
}

/**
Expand Down
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
26 changes: 23 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,13 @@ public static function min(): Instant
*/
public static function max(): Instant

Check failure on line 121 in src/Instant.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedInferredReturnType

src/Instant.php:121:35: MixedInferredReturnType: Could not verify return type 'Brick\DateTime\Instant' for Brick\DateTime\Instant::max (see https://psalm.dev/047)
{
return new Instant(PHP_INT_MAX, 999_999_999);
static $max;

if ($max) {
return $max;

Check failure on line 126 in src/Instant.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedReturnStatement

src/Instant.php:126:20: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)
}

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
7 changes: 7 additions & 0 deletions src/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public static function of(int $value): Month
*/
public static function getAll(): array
{
/** @var Month[]|null $months */
static $months;

if ($months) {
return $months;
}

$months = [];

for ($month = Month::JANUARY; $month <= Month::DECEMBER; $month++) {
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

0 comments on commit e2129ef

Please sign in to comment.