-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RateLimiter] handle error results of DateTime::modify() #58791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Depending on the version of PHP the modify() method will either throw an exception or issue a warning.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,19 +68,21 @@ | |
protected static function configureOptions(OptionsResolver $options): void | ||
{ | ||
$intervalNormalizer = static function (Options $options, string $interval): \DateInterval { | ||
try { | ||
// Create DateTimeImmutable from unix timesatmp, so the default timezone is ignored and we don't need to | ||
// deal with quirks happening when modifying dates using a timezone with DST. | ||
$now = \DateTimeImmutable::createFromFormat('U', time()); | ||
// Create DateTimeImmutable from unix timesatmp, so the default timezone is ignored and we don't need to | ||
// deal with quirks happening when modifying dates using a timezone with DST. | ||
$now = \DateTimeImmutable::createFromFormat('U', time()); | ||
|
||
return $now->diff($now->modify('+'.$interval)); | ||
} catch (\Exception $e) { | ||
if (!preg_match('/Failed to parse time string \(\+([^)]+)\)/', $e->getMessage(), $m)) { | ||
throw $e; | ||
} | ||
try { | ||
$nowPlusInterval = @$now->modify('+' . $interval); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. up to PHP 8.2 the method emitted a warning on invalid intervals which we need to silence here |
||
} catch (\DateMalformedStringException $e) { | ||
Check failure on line 77 in src/Symfony/Component/RateLimiter/RateLimiterFactory.php
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from PHP 8.3 on a |
||
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); | ||
Check failure on line 78 in src/Symfony/Component/RateLimiter/RateLimiterFactory.php
|
||
} | ||
|
||
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])); | ||
if (!$nowPlusInterval) { | ||
throw new \LogicException(\sprintf('Cannot parse interval "%s", please use a valid unit as described on https://www.php.net/datetime.formats.relative.', $interval)); | ||
} | ||
|
||
return $now->diff($nowPlusInterval); | ||
}; | ||
|
||
$options | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
modify()
returnsfalse
on failure, thus we must not pass it todiff()
before checking the type