Skip to content

[Form] Changed DataMapperInterface $forms parameter type to \Traversable #39317

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
Dec 5, 2020

Conversation

vudaltsov
Copy link
Contributor

Q A
Branch? 5.x
Bug fix? no
New feature? no
Deprecations? yes
Tickets Fix #39311
License MIT
Doc PR no

Didn't touch PropertyPathMapper because it's deprecated anyway.

Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

Thank you! We also need to document this change in UPGRADE-5.3.md and UPGRADE-6.0.md.

@derrabus derrabus added this to the 5.x milestone Dec 5, 2020
@nicolas-grekas
Copy link
Member

That's a BC break, isn't it?
This cannot go on 5.3 if yes.

@nicolas-grekas
Copy link
Member

Can't the classes also be updated to use Traversable?
Also, I don't understand the original issue: what is this fixing? What's wrong with accepting arrays?

@nicolas-grekas
Copy link
Member

Last comment: can't we fix this right away in 5.1?

@derrabus
Copy link
Member

derrabus commented Dec 5, 2020

That's a BC break, isn't it?

The PR as it is right now is not a BC break, as far as I can tell. We can change the parameter type declaration on the interface from iterable to Traversable and existing implementations with a iterable type declaration will continue to work.

Can't the classes also be updated to use Traversable?

That's technically a BC break because they wouldn't accept arrays anymore. So if there is existing code that calls those classes with an array (for what reason ever), those calls would fail.

Also, I don't understand the original issue: what is this fixing? What's wrong with accepting arrays?

DataMapperInterface is a contract for userland code. A data mapper is usually called by the form component and not by other userland code. The component however will always call that method with a Traversable instance. The iterable type declaration somewhat forces you to handle arrays as well which is a case that never happens.

The issue gives a good example: The sample code from the documentation would break if you passed an array.

Last comment: can't we fix this right away in 5.1?

I think we need that deprecation layer which would be a show-stopper for 5.1.

@nicolas-grekas
Copy link
Member

Can't the classes also be updated to use Traversable?

That's technically a BC break because they wouldn't accept arrays anymore. So if there is existing code that calls those classes with an array (for what reason ever), those calls would fail.

How are we going to change their type hint then, since we'd like to do it, isn't it?

@derrabus
Copy link
Member

derrabus commented Dec 5, 2020

How are we going to change their type hint then, since we'd like to do it, isn't it?

Trigger a deprecation on 5.3 if we get an array and change the type declaration on 6.0.

@nicolas-grekas
Copy link
Member

Trigger a deprecation on 5.3 if we get an array and change the type declaration on 6.0.

Hm, and type widening will mean we won't break classes that extend/override these methods.
What bothers me is that ppl won't be able to fix their own type hints in their child classes.
But I guess there is no way around.

Shouldn't we improve the deprecation message to tell about the type change? At least something that will ensure we won't forget how to clean the deprecation when preparing 6.0? Eg "method foo() will only accept Traversable, array give."

@derrabus
Copy link
Member

derrabus commented Dec 5, 2020

What bothers me is that ppl won't be able to fix their own type hints in their child classes.

My understanding is that userland code is supposed to implement DataMapperInterface directly instead of extending Symfony's classes. Since we can change the interface already, developers can upgrade their type declarations right away.

@vudaltsov
Copy link
Contributor Author

What bothers me is that ppl won't be able to fix their own type hints in their child classes.

We cannot forbid users to widen parameter types in subclasses/implementations, they can keep iterable if the want to.

@derrabus derrabus force-pushed the data-mapper-traversable branch from 4556b7e to ce77be2 Compare December 5, 2020 13:50
@derrabus
Copy link
Member

derrabus commented Dec 5, 2020

Thank you @vudaltsov.

@vudaltsov vudaltsov deleted the data-mapper-traversable branch December 5, 2020 16:42
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Dec 7, 2020
…versable (vudaltsov)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[Form] Changed data mapper $forms parameter type to \Traversable

Closes #14656
See symfony/symfony#39317

Commits
-------

4677e42 Changed iterable $forms to Traversable in the data mapper implementation example
@fabpot fabpot mentioned this pull request Apr 18, 2021
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.

[Form] Argument $forms in DataMapperInterface methods should be \Traversable
4 participants