Skip to content

Commit fb6549d

Browse files
committed
handle error results of DateTime::modify()
Depending on the version of PHP the modify() method will either throw an exception or issue a warning.
1 parent 2c7c4ba commit fb6549d

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/Symfony/Component/RateLimiter/RateLimiterFactory.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,21 @@ public function create(?string $key = null): LimiterInterface
6868
protected static function configureOptions(OptionsResolver $options): void
6969
{
7070
$intervalNormalizer = static function (Options $options, string $interval): \DateInterval {
71-
try {
72-
// Create DateTimeImmutable from unix timesatmp, so the default timezone is ignored and we don't need to
73-
// deal with quirks happening when modifying dates using a timezone with DST.
74-
$now = \DateTimeImmutable::createFromFormat('U', time());
71+
// Create DateTimeImmutable from unix timesatmp, so the default timezone is ignored and we don't need to
72+
// deal with quirks happening when modifying dates using a timezone with DST.
73+
$now = \DateTimeImmutable::createFromFormat('U', time());
7574

76-
return $now->diff($now->modify('+'.$interval));
77-
} catch (\Exception $e) {
78-
if (!preg_match('/Failed to parse time string \(\+([^)]+)\)/', $e->getMessage(), $m)) {
79-
throw $e;
80-
}
75+
try {
76+
$nowPlusInterval = @$now->modify('+' . $interval);
77+
} catch (\DateMalformedStringException $e) {
78+
throw new \LogicException(\sprintf('Cannot parse interval "%s", please use a valid unit as described on https://www.php.net/datetime.formats.relative.', $interval), 0, $e);
79+
}
8180

82-
throw new \LogicException(sprintf('Cannot parse interval "%s", please use a valid unit as described on https://www.php.net/datetime.formats.relative.', $m[1]));
81+
if (!$nowPlusInterval) {
82+
throw new \LogicException(\sprintf('Cannot parse interval "%s", please use a valid unit as described on https://www.php.net/datetime.formats.relative.', $interval));
8383
}
84+
85+
return $now->diff($nowPlusInterval);
8486
};
8587

8688
$options

0 commit comments

Comments
 (0)