Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Remove the default values from setters with a nullable parameter #38996

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 2 commits into from
Sep 12, 2022

Conversation

derrabus
Copy link
Member

@derrabus derrabus commented Nov 5, 2020

Q A
Branch? 6.2
Bug fix? no
New feature? no
Deprecations? yes
Tickets N/A
License MIT
Doc PR N/A

Setters with a default value are a bit odd: I want to set a value, but I won't tell you which one!

We do have this kind of setters, like this example from TokenStorageInterface:

public function setToken(TokenInterface $token = null);

This means that those to calls are equivalent:

$tokenStorage->setToken(null);
$tokenStorage->setToken();

The reason for this is actually a php 5 quirk: On php 5, we did not have nullable parameter type declarations – those we added in php 7.1. The only workaround was to declare null as default value because then php would accept passing null despite the type declaration demanding an instance of a specific class/interface.

Because the days of php 5 are over, I'd like to change this. Our method signature would then look like this.

public function setToken(?TokenInterface $token);

We can do this for interfaces and abstract methods because an implementation may add a default value. Thus, removing the default value from the interface alone is not a BC break.

For the implementations of that interface, this is a different story because removing the default breaks calling code that omits the parameter entirely. This is why for the implementations I trigger a deprecation if the method is called without arguments. This enables us to remove the default in Symfony 6.

This PR performs the suggested changes for TokenStorageInterface and ContainerAwareInterface, but we have a few more setters like this. But before I continue, I'd like to collect some feedback if this is something you would want to change.

@derrabus derrabus added this to the 5.x milestone Nov 5, 2020
@derrabus derrabus force-pushed the improvement/nullable-setters branch 2 times, most recently from 729ead4 to cf1e8ba Compare November 5, 2020 00:16
@derrabus derrabus force-pushed the improvement/nullable-setters branch from cf1e8ba to 97aa4b6 Compare November 5, 2020 20:24
@derrabus derrabus marked this pull request as draft November 5, 2020 23:04
@Nyholm
Copy link
Member

Nyholm commented Apr 3, 2021

I like this idea. Do you need help to finish this PR?

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Apr 12, 2021

This is a hard BC break that provides no BC layer unfortunately. A given implementation cannot be made compatible with v5 and v6 at the same time. I think this makes this change impossible, following our policy.

@derrabus
Copy link
Member Author

This is a hard BC break that provides no BC layer unfortunately. A given implementation cannot be made compatible with v5 and v6 at the same time.

Can you elaborate?

@fabpot fabpot modified the milestones: 5.4, 6.1 Nov 3, 2021
@fabpot
Copy link
Member

fabpot commented Dec 11, 2021

Unlocked :)

@nicolas-grekas
Copy link
Member

So, I was wrong about the BC break 😬
Let's rebase and merge this?

@derrabus
Copy link
Member Author

I'll try to find some time to resume my work. 🤞

@fabpot fabpot modified the milestones: 6.1, 6.2 May 20, 2022
@nicolas-grekas nicolas-grekas force-pushed the improvement/nullable-setters branch from 97aa4b6 to bafbd28 Compare September 12, 2022 12:33
@nicolas-grekas nicolas-grekas marked this pull request as ready for review September 12, 2022 12:33
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

(I rebased and completed the patch)

@nicolas-grekas nicolas-grekas force-pushed the improvement/nullable-setters branch 3 times, most recently from e24fc67 to 56532eb Compare September 12, 2022 14:07
@derrabus
Copy link
Member Author

Thank you! ❤️

@nicolas-grekas nicolas-grekas force-pushed the improvement/nullable-setters branch from 56532eb to 78803b2 Compare September 12, 2022 14:34
@nicolas-grekas
Copy link
Member

Thank you @derrabus.

@nicolas-grekas nicolas-grekas merged commit fc550ea into symfony:6.2 Sep 12, 2022
@derrabus derrabus deleted the improvement/nullable-setters branch October 13, 2022 13:26
@fabpot fabpot mentioned this pull request Oct 24, 2022
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.

6 participants