From 324425632ac5933b7253bde65520ca13de850f7a Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Thu, 4 Apr 2024 00:33:22 +0200 Subject: [PATCH] Use ??= for static variable initialization --- src/Duration.php | 8 +-- src/Instant.php | 24 +++------ src/LocalDate.php | 16 ++---- src/LocalDateTime.php | 16 ++---- src/LocalTime.php | 24 +++------ src/Parser/IsoParsers.php | 104 ++++++++++---------------------------- src/Period.php | 8 +-- src/TimeZoneOffset.php | 8 +-- 8 files changed, 52 insertions(+), 156 deletions(-) diff --git a/src/Duration.php b/src/Duration.php index 9838486..5d986de 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -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); } /** diff --git a/src/Instant.php b/src/Instant.php index 873f69f..8282eeb 100644 --- a/src/Instant.php +++ b/src/Instant.php @@ -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 @@ -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); } /** @@ -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 diff --git a/src/LocalDate.php b/src/LocalDate.php index 266e672..17cac42 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -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); } /** @@ -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); } /** diff --git a/src/LocalDateTime.php b/src/LocalDateTime.php index ef40a28..9521695 100644 --- a/src/LocalDateTime.php +++ b/src/LocalDateTime.php @@ -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()); } /** @@ -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()); } /** diff --git a/src/LocalTime.php b/src/LocalTime.php index d5cb22b..6d7c742 100644 --- a/src/LocalTime.php +++ b/src/LocalTime.php @@ -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); } /** @@ -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); } /** @@ -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); } /** diff --git a/src/Parser/IsoParsers.php b/src/Parser/IsoParsers.php index a9a4747..d4545dd 100644 --- a/src/Parser/IsoParsers.php +++ b/src/Parser/IsoParsers.php @@ -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) @@ -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) @@ -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()) @@ -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('/') @@ -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('/') @@ -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(); } @@ -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) @@ -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) @@ -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('-') @@ -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() @@ -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(); } @@ -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(); @@ -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('[') diff --git a/src/Period.php b/src/Period.php index d044e79..a860ecc 100644 --- a/src/Period.php +++ b/src/Period.php @@ -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); } /** diff --git a/src/TimeZoneOffset.php b/src/TimeZoneOffset.php index 0a27511..a9ac434 100644 --- a/src/TimeZoneOffset.php +++ b/src/TimeZoneOffset.php @@ -88,13 +88,9 @@ public static function ofTotalSeconds(int $totalSeconds): TimeZoneOffset public static function utc(): TimeZoneOffset { /** @var TimeZoneOffset|null $utc */ - static $utc; + static $utc = null; - if ($utc !== null) { - return $utc; - } - - return $utc = new TimeZoneOffset(0); + return $utc ??= new TimeZoneOffset(0); } /**