From 75d9d9cbdf72334bd9dda2594a45322ddd2f8747 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Thu, 4 Apr 2024 00:15:28 +0200 Subject: [PATCH] Add assertions around createFromFormat() Even though we can pretty much guarantee that we will never call these methods in a way that would return false, phpstan is right to tell us that we missed something here. --- src/Period.php | 7 ++++++- src/ZonedDateTime.php | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Period.php b/src/Period.php index 902b700..d044e79 100644 --- a/src/Period.php +++ b/src/Period.php @@ -8,6 +8,7 @@ use JsonSerializable; use Stringable; +use function assert; use function intdiv; use function preg_match; use function sprintf; @@ -363,12 +364,16 @@ public function isEqualTo(Period $that): bool */ public function toNativeDateInterval(): DateInterval { - return DateInterval::createFromDateString(sprintf( + $nativeDateInterval = DateInterval::createFromDateString(sprintf( '%d years %d months %d days', $this->years, $this->months, $this->days, )); + + assert($nativeDateInterval !== false); + + return $nativeDateInterval; } /** diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index ea89a1f..3bc9883 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -14,6 +14,7 @@ use JsonSerializable; use Stringable; +use function assert; use function intdiv; /** @@ -710,7 +711,11 @@ public function toNativeDateTime(): DateTime } } - return DateTime::createFromFormat($format, $dateTime, $dateTimeZone); + $nativeDateTime = DateTime::createFromFormat($format, $dateTime, $dateTimeZone); + + assert($nativeDateTime !== false); + + return $nativeDateTime; } public function toNativeDateTimeImmutable(): DateTimeImmutable