Skip to content

[HttpFoundation] added withers to Cookie class #35215

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
Jan 30, 2020

Conversation

ns3777k
Copy link
Contributor

@ns3777k ns3777k commented Jan 4, 2020

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #35212
License MIT
Doc PR -

I was quite descriptive in the issue :-)

The main idea is to get the interface for changing a cookie to avoid every unneeded argument in the constructor.

Current:

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

This PR:

$cookie = Cookie::create('foo')
            ->withValue('bar')
            ->withExpiresTime(strtotime('Fri, 20-May-2011 15:25:52 GMT'))
            ->withDomain('.myfoodomain.com')
            ->withSecure(true);

Every wither returns a copy of current cookie with requested setting set. Cookie class remains immutable.

@Taluu
Copy link
Contributor

Taluu commented Jan 4, 2020

I'm kinda against having a fluent interface as it doesn't really make sense to have a fluent here (it's not a builder...)

Adding a mutable state why not, and maybe keep it immutable (returning a new instance with the property set correctly) would be even better IMO.

(Hence my 😕 / 👎 reaction)

@ns3777k
Copy link
Contributor Author

ns3777k commented Jan 4, 2020

@Taluu Maybe fluent interface is not correct term here, I meant a bunch of setters by it just to mutate the state (that's exactly what's been done.).

Sorry for my bad english but what's the difference between this PR and Adding a mutable state why not?

@Taluu
Copy link
Contributor

Taluu commented Jan 4, 2020

You made it fluent (returning $this) is the key difference here. :)

@ns3777k
Copy link
Contributor Author

ns3777k commented Jan 4, 2020

Oh, I got you :-) I don't mind changing the PR so setters would become something like:

public function setName(string $name): self
{
    // ...validating $name...
    $clone = clone $this;
    $clone->name = $name;

    return $clone;
}

But I personally prefer $this :-)
Anyway, let's wait on other opinions.

@Taluu
Copy link
Contributor

Taluu commented Jan 5, 2020

Even better : make withers (withName...).

Or remove the return and make it mutable.

@nicolas-grekas nicolas-grekas added this to the next milestone Jan 5, 2020
@ns3777k
Copy link
Contributor Author

ns3777k commented Jan 5, 2020

@Taluu

I updated the PR with withers keeping the Cookie immutable. Appreciate it if you take a look.

@ns3777k ns3777k changed the title [HttpFoundation] added fluent interface for Cookie class [HttpFoundation] added withers to Cookie class Jan 6, 2020
@florianajir
Copy link

I need this ! but sad to have to wait for 5.2.0 release to get mutable cookie :/

@nicolas-grekas nicolas-grekas force-pushed the ticket_35212 branch 3 times, most recently from d7bc008 to c6e60e4 Compare January 12, 2020 21:51
@nicolas-grekas
Copy link
Member

Thanks for your explanations @ns3777k, I missed that some attributes should remain nullable.
I pushed a third commit on your fork with additional changes.
As a last step, I think we should group all wither methods at the beginning of the class, before the getters.

@ns3777k
Copy link
Contributor Author

ns3777k commented Jan 12, 2020

Thanks for clarification! @nicolas-grekas I'm gonna move the withers up.

@fabpot
Copy link
Member

fabpot commented Jan 30, 2020

Thank you @ns3777k.

@fabpot fabpot closed this in a2b6085 Jan 30, 2020
@fabpot fabpot merged commit 549afaa into symfony:master Jan 30, 2020
@ns3777k ns3777k deleted the ticket_35212 branch January 31, 2020 15:27
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
@fabpot fabpot mentioned this pull request May 5, 2020
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.

[HttpFoundation] Cookie mutation
6 participants