Skip to content

[BrowserKit] Allow Cookie expiration to be an int#62758

Merged
nicolas-grekas merged 1 commit intosymfony:6.4from
derrabus:bugfix/cookie-expires-as-int
Dec 13, 2025
Merged

[BrowserKit] Allow Cookie expiration to be an int#62758
nicolas-grekas merged 1 commit intosymfony:6.4from
derrabus:bugfix/cookie-expires-as-int

Conversation

@derrabus
Copy link
Copy Markdown
Member

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues N/A
License MIT

The constructor parameter $expires of the Cookie class is typed as string|null. That is surprising because that parameter is expected to be provided as a Unix timestamp which is usually an integer. Also, looking at the tests and the documentation, we commonly call that constructor with an integer ourselves, usually with a value returned by strtotime().

$cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);

Creating a Cookie object as documented in a codebase that uses strict types will cause a TypeError.

In this PR, I'm widening the parameter type to string|int|null. This should not be a problem because while validating the value, we convert it back to a string anyway.

if (null !== $expires) {
$timestampAsDateTime = \DateTimeImmutable::createFromFormat('U', $expires);
if (false === $timestampAsDateTime) {
throw new UnexpectedValueException(\sprintf('The cookie expiration time "%s" is not valid.', $expires));
}
$this->expires = $timestampAsDateTime->format('U');
}

@nicolas-grekas
Copy link
Copy Markdown
Member

Thank you @derrabus.

@nicolas-grekas nicolas-grekas merged commit 16ad4ed into symfony:6.4 Dec 13, 2025
12 checks passed
@derrabus derrabus deleted the bugfix/cookie-expires-as-int branch December 13, 2025 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants