8000 bug #58791 [RateLimiter] handle error results of DateTime::modify() (… · symfony/symfony@da4eb8b · GitHub
[go: up one dir, main page]

Skip to content

Commit da4eb8b

Browse files
committed
bug #58791 [RateLimiter] handle error results of DateTime::modify() (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [RateLimiter] handle error results of DateTime::modify() | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Depending on the version of PHP the `modify()` method will either throw an exception or issue a warning. Commits ------- fb6549d handle error results of DateTime::modify()
2 parents d77f5d9 + fb6549d commit da4eb8b

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/Symfony/Component/RateLimiter/RateLimiterFactory.php

+12-10
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)
0