Skip to content

Commit

Permalink
Use ??= for static variable initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Apr 3, 2024
1 parent 75d9d9c commit 3244256
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 156 deletions.
8 changes: 2 additions & 6 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ private function __construct(
public static function zero(): Duration
{
/** @var Duration|null $zero */
static $zero;
static $zero = null;

if ($zero !== null) {
return $zero;
}

return $zero = new Duration(0);
return $zero ??= new Duration(0);
}

/**
Expand Down
24 changes: 6 additions & 18 deletions src/Instant.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,9 @@ public static function of(int $epochSecond, int $nanoAdjustment = 0): Instant
public static function epoch(): Instant
{
/** @var Instant|null $epoch */
static $epoch;
static $epoch = null;

if ($epoch !== null) {
return $epoch;
}

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

public static function now(?Clock $clock = null): Instant
Expand All @@ -95,13 +91,9 @@ public static function now(?Clock $clock = null): Instant
public static function min(): Instant
{
/** @var Instant|null $min */
static $min;

if ($min !== null) {
return $min;
}
static $min = null;

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

/**
Expand All @@ -112,13 +104,9 @@ public static function min(): Instant
public static function max(): Instant
{
/** @var Instant|null $max */
static $max;

if ($max !== null) {
return $max;
}
static $max = null;

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

public function plus(Duration $duration): Instant
Expand Down
16 changes: 4 additions & 12 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,9 @@ public static function now(TimeZone $timeZone, ?Clock $clock = null): LocalDate
public static function min(): LocalDate
{
/** @var LocalDate|null $min */
static $min;
static $min = null;

if ($min !== null) {
return $min;
}

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

/**
Expand All @@ -234,13 +230,9 @@ public static function min(): LocalDate
public static function max(): LocalDate
{
/** @var LocalDate|null $max */
static $max;

if ($max !== null) {
return $max;
}
static $max = null;

return $max = LocalDate::of(self::MAX_YEAR, Month::DECEMBER, 31);
return $max ??= LocalDate::of(self::MAX_YEAR, Month::DECEMBER, 31);
}

/**
Expand Down
16 changes: 4 additions & 12 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,9 @@ public static function fromNativeDateTime(DateTimeInterface $dateTime): LocalDat
public static function min(): LocalDateTime
{
/** @var LocalDateTime|null $min */
static $min;
static $min = null;

if ($min !== null) {
return $min;
}

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

/**
Expand All @@ -121,13 +117,9 @@ public static function min(): LocalDateTime
public static function max(): LocalDateTime
{
/** @var LocalDateTime|null $max */
static $max;

if ($max !== null) {
return $max;
}
static $max = null;

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

/**
Expand Down
24 changes: 6 additions & 18 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,9 @@ public static function midnight(): LocalTime
public static function noon(): LocalTime
{
/** @var LocalTime|null $noon */
static $noon;
static $noon = null;

if ($noon !== null) {
return $noon;
}

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

/**
Expand All @@ -178,13 +174,9 @@ public static function noon(): LocalTime
public static function min(): LocalTime
{
/** @var LocalTime|null $min */
static $min;

if ($min !== null) {
return $min;
}
static $min = null;

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

/**
Expand All @@ -193,13 +185,9 @@ public static function min(): LocalTime
public static function max(): LocalTime
{
/** @var LocalTime|null $max */
static $max;

if ($max !== null) {
return $max;
}
static $max = null;

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

/**
Expand Down
104 changes: 26 additions & 78 deletions src/Parser/IsoParsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,9 @@ private function __construct()
public static function localDate(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;
static $parser = null;

if ($parser !== null) {
return $parser;
}

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->appendCapturePattern(Year::PATTERN, Year::NAME)
->appendLiteral('-')
->appendCapturePattern(MonthOfYear::PATTERN, MonthOfYear::NAME)
Expand All @@ -59,13 +55,9 @@ public static function localDate(): PatternParser
public static function localTime(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;

if ($parser !== null) {
return $parser;
}
static $parser = null;

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->appendCapturePattern(HourOfDay::PATTERN, HourOfDay::NAME)
->appendLiteral(':')
->appendCapturePattern(MinuteOfHour::PATTERN, MinuteOfHour::NAME)
Expand All @@ -88,13 +80,9 @@ public static function localTime(): PatternParser
public static function localDateTime(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;
static $parser = null;

if ($parser !== null) {
return $parser;
}

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->append(self::localDate())
->appendLiteral('T')
->append(self::localTime())
Expand All @@ -107,13 +95,9 @@ public static function localDateTime(): PatternParser
public static function localDateRange(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;

if ($parser !== null) {
return $parser;
}
static $parser = null;

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->append(self::localDate())
->appendLiteral('/')

Expand Down Expand Up @@ -143,13 +127,9 @@ public static function localDateRange(): PatternParser
public static function yearMonthRange(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;
static $parser = null;

if ($parser !== null) {
return $parser;
}

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->append(self::yearMonth())
->appendLiteral('/')

Expand All @@ -172,13 +152,9 @@ public static function yearMonthRange(): PatternParser
public static function year(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;

if ($parser !== null) {
return $parser;
}
static $parser = null;

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->appendCapturePattern(Year::PATTERN, Year::NAME)
->toParser();
}
Expand All @@ -189,13 +165,9 @@ public static function year(): PatternParser
public static function yearMonth(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;

if ($parser !== null) {
return $parser;
}
static $parser = null;

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->appendCapturePattern(Year::PATTERN, Year::NAME)
->appendLiteral('-')
->appendCapturePattern(MonthOfYear::PATTERN, MonthOfYear::NAME)
Expand All @@ -208,13 +180,9 @@ public static function yearMonth(): PatternParser
public static function yearWeek(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;
static $parser = null;

if ($parser !== null) {
return $parser;
}

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->appendCapturePattern(Year::PATTERN, Year::NAME)
->appendLiteral('-W')
->appendCapturePattern(WeekOfYear::PATTERN, WeekOfYear::NAME)
Expand All @@ -227,13 +195,9 @@ public static function yearWeek(): PatternParser
public static function monthDay(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;

if ($parser !== null) {
return $parser;
}
static $parser = null;

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->appendLiteral('--')
->appendCapturePattern(MonthOfYear::PATTERN, MonthOfYear::NAME)
->appendLiteral('-')
Expand All @@ -247,13 +211,9 @@ public static function monthDay(): PatternParser
public static function timeZoneOffset(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;

if ($parser !== null) {
return $parser;
}
static $parser = null;

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->startGroup()
->appendCapturePattern('[Zz]', TimeZoneOffsetSign::NAME)
->appendOr()
Expand All @@ -277,13 +237,9 @@ public static function timeZoneOffset(): PatternParser
public static function timeZoneRegion(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;
static $parser = null;

if ($parser !== null) {
return $parser;
}

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->appendCapturePattern(TimeZoneRegion::PATTERN, TimeZoneRegion::NAME)
->toParser();
}
Expand All @@ -294,13 +250,9 @@ public static function timeZoneRegion(): PatternParser
public static function offsetDateTime(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;

if ($parser !== null) {
return $parser;
}
static $parser = null;

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->append(self::localDateTime())
->append(self::timeZoneOffset())
->toParser();
Expand All @@ -312,13 +264,9 @@ public static function offsetDateTime(): PatternParser
public static function zonedDateTime(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;

if ($parser !== null) {
return $parser;
}
static $parser = null;

return $parser = (new PatternParserBuilder())
return $parser ??= (new PatternParserBuilder())
->append(self::offsetDateTime())
->startOptional()
->appendLiteral('[')
Expand Down
8 changes: 2 additions & 6 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,9 @@ public static function ofDays(int $days): Period
public static function zero(): Period
{
/** @var Period|null $zero */
static $zero;
static $zero = null;

if ($zero !== null) {
return $zero;
}

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

/**
Expand Down
Loading

0 comments on commit 3244256

Please sign in to comment.