Skip to content

[Form] deprecate integers as form names #30032

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

Closed
wants to merge 1 commit into from

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Jan 30, 2019

Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets
License MIT
Doc PR

@xabbuh
Copy link
Member Author

xabbuh commented Jan 30, 2019

The FormConfigBuilder is the only class accepting integers as form names. All calling code in the component already passes strings. So this will affect few (if any) developers, but allows us to add a proper type hint in Symfony 5.

@@ -179,6 +179,10 @@ public function __construct($name, ?string $dataClass, EventDispatcherInterface
{
self::validateName($name);

if (\is_int($name)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a specific reason why strings with numerical values are still allowed?

Copy link
Member Author

Choose a reason for hiding this comment

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

The CollectionType for example handles forms that have numerical names. And from an HTML point of view it does not really matter that the name does only contain digits.

@@ -23,6 +23,7 @@ Config
Form
----

* Support for using integers as form names is deprecated and will lead to a type error in 5.0, use strings instead.
Copy link
Member

Choose a reason for hiding this comment

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

will it? automatic casting rules would turn the int to a string and all will be silent, no?

@nicolas-grekas
Copy link
Member

What's the technical benefit of this change?

@OskarStark
Copy link
Contributor

From my perspective it makes Code more clear and a variable called $name should be expected as strong in most situations.

@nicolas-grekas what do you mean by automatic casting rules?

I am 👍 for this change

@nicolas-grekas
Copy link
Member

I mean that I don't understand what this will change allow from a technical pov: the name is explicitly cast to string a few lines below.
If we'll need to add more code to trigger the exception, I don't get the point :)
What did I miss?

@stof
Copy link
Member

stof commented Jan 30, 2019

There won't be any TypeError in Symfony 5. There will be a cast (as done today explicitly in the constructor) because its only caller is FormBuilder (for the parent call), which is not running using strict types (as Symfony does not have them).

And even if we had typehints on that name everywhere in Symfony (where we already document string as the expected type), you would not get a TypeError unless your own code calling Symfony APIs is using strict_types (and then, we have no way to know that to decide whether we should trigger a deprecation or no)

@xabbuh
Copy link
Member Author

xabbuh commented Jan 31, 2019

Let me clarify my intention here: This change would not have any effect for our internal code as all the callers of this constructor inside the Form component already pass a string. This would only affect consumers of the component who build FormConfigBuilder instances on their own (and they would only get a type error if they use strict types).

But reiterating over my reasoning I think it's probably not worth the effort here. Let's close it.

@xabbuh xabbuh closed this Jan 31, 2019
@xabbuh xabbuh deleted the deprecated-integer-names branch January 31, 2019 09:49
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
fabpot added a commit that referenced this pull request Jun 17, 2019
This PR was merged into the 4.4 branch.

Discussion
----------

Fine tune constructor types

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Fine tunes some constructor types that have been added in #24722

Form names as integer was only a workaround as forms names are used as array keys which get transformed to int. So it was added as a workaround in #6355 (comment)
With typehints added in #24722 those were mostly auto-cast anyway, e.g. in FormBuilder. There are only a few integer form names remaining documented, in the main entry points of the Form component (`\Symfony\Component\Form\FormInterface::add`). Internally it's always a string now. So I could remove some int docs which also fixes #30032 what @xabbuh tried to do.

Some of these changes we're just not done before because of broken tests. It's mainly a missing explicit mock for `TranslationInterface::trans` which returned null. If we had return types hints in interfaces, this wouldn't happen.

Commits
-------

507794a Fine tune constructor types
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