Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
handle error results of DateTime::modify()
Depending on the version of PHP the modify() method will either throw an
exception or issue a warning.
  • Loading branch information
xabbuh committed Nov 7, 2024
commit fb6549dcfe6135bff1b3d5c1eacf7301d152a544
22 changes: 12 additions & 10 deletions 22 src/Symfony/Component/RateLimiter/RateLimiterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modify() returns false on failure, thus we must not pass it to diff() before checking the type

} catch (\Exception $e) {
if (!preg_match('/Failed to parse time string \(\+([^)]+)\)/', $e->getMessage(), $m)) {
throw $e;
}
try {
$nowPlusInterval = @$now->modify('+' . $interval);
Copy link
Member Author

@xabbuh xabbuh Nov 7, 2024

Choose a reason for hiding this comment

The 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

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedClass

src/Symfony/Component/RateLimiter/RateLimiterFactory.php:77:22: UndefinedClass: Class, interface or enum named DateMalformedStringException does not exist (see https://psalm.dev/019)

Check failure on line 77 in src/Symfony/Component/RateLimiter/RateLimiterFactory.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedClass

src/Symfony/Component/RateLimiter/RateLimiterFactory.php:77:22: UndefinedClass: Class, interface or enum named DateMalformedStringException does not exist (see https://psalm.dev/019)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from PHP 8.3 on a DateMalformedStringException is thrown instead

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

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Component/RateLimiter/RateLimiterFactory.php:78:185: InvalidArgument: Argument 3 of LogicException::__construct expects Throwable|null, but DateMalformedStringException provided (see https://psalm.dev/004)

Check failure on line 78 in src/Symfony/Component/RateLimiter/RateLimiterFactory.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Component/RateLimiter/RateLimiterFactory.php:78:185: InvalidArgument: Argument 3 of LogicException::__construct expects Throwable|null, but DateMalformedStringException provided (see https://psalm.dev/004)
}

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
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.