Skip to content

Commit

Permalink
Add assertions around createFromFormat()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BenMorel committed Apr 3, 2024
1 parent 4a1357c commit 75d9d9c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use JsonSerializable;
use Stringable;

use function assert;
use function intdiv;
use function preg_match;
use function sprintf;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/ZonedDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use JsonSerializable;
use Stringable;

use function assert;
use function intdiv;

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 75d9d9c

Please sign in to comment.