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

[HttpFoundation] Cookie mutation #35212

Copy link
Copy link
Closed
@ns3777k

Description

@ns3777k
Issue body actions

Description
Cookie class is immutable and thus its settings can be set only during construction via constructor or static create method. Both methods accept 1 required argument (which is cookie name) and 8 optional arguments.

Most of the time, I don't need httpOnly flag which makes me specify 7 arguments almost every time I wanna create a cookie :-) Setters can bring much more effort (IMO. see example below). Setting sameSite will require me to specify all 9 previous arguments.

Since now only cookie name is required we can avoid BC by moving validation logic into setters and calling them from the constructor.

Example
For now, I create a cookie like this (I specify 7 arguments 'cause I wanna disable httpOnly):

$cookie = Cookie::create(
    RegionSwitcher::REGION_COOKIE, $regionSlug, new DateTime('+1 year'), '/',
    $baseDomain, null, false
);

With setters, I can only call 4 methods which looks much nicer to me:

$cookie = Cookie::create(RegionSwitcher::REGION_COOKIE)
    ->setValue($regionSlug)
    ->setExpiration(new DateTime('+1 year'))
    ->setDomain($baseDomain)
    ->disableHttpOnly();

I can make a PR if accepted. Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.