Skip to content

[Form] Name related PHPDoc fixes #32400

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
Jul 13, 2019
Merged

Conversation

vudaltsov
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets relates to #32179
License MIT
Doc PR n/a

As I started working on #32179 in #32237, I noticed some PHPDoc inconsistencies around the form's name. I have researched the issue thoroughly and here's my proposal:

  1. All "factory" methods (FormFactory::create*, ResolvedFormType::createBuilder, FormBuilder::create|add, Form::add) currently accept string and integer names. This should not be deprecated, because it's very convenient and natural to pass ints when adding children to types like ChoiceType or CollectionType.
  2. None of the "factory" methods explicitly accepts null as a name, although passing null works the same as passing ''. I consider passing null in this case to be ugly, so I corrected the builder's constructors mentioning null as a possible argument. One should pass an empty string '' when creating such a form. We could also deprecate passing null in a PR targeting 4.4.
  3. Currently the name becomes a string in the builder's (or config builder's) constructor. Which means that FormConfigInterface::getName always returns a string and thus the form's $name property is always a string. All related checks and PHPDocs should be corrected.
  4. The "children accessors" (Form::has|get|remove, FormBuilder::has|get|remove) should accept both strings and ints because they are array-accessible by design (so it does not really matter, if the key is a string or an int). If it's valid to have $collectionForm->add(1, ItemType::class), then it should be valid to do $collectionForm->get(1). And it works in code, but is not mentioned in the PHPDoc.

ping @nicolas-grekas , @xabbuh , @HeahDude

@nicolas-grekas
Copy link
Member

I'd prefer not listing int explicitly. Yes, they are accepted. But so would they if we add the string type, because of automatic PHP casts.

@vudaltsov
Copy link
Contributor Author

@nicolas-grekas , are we going to have strict types in the future?

@xabbuh
Copy link
Member

xabbuh commented Jul 8, 2019

@vudaltsov Whether or not Symfony will use strict types internally is not really important as the initial call will be coming from userland where developers need to decide if they want to use strict types and thus pass real strings or if they want to rely on PHP's implicit type casting.

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.

Please revert all docblock changes.

@vudaltsov
Copy link
Contributor Author

vudaltsov commented Jul 8, 2019

@nicolas-grekas , if we use string only, then it means that the Symfony code passing int (like in CollectionType) is working against the docblock as well as all the tests checking that passing int is ok. And if we set string type explicitly in 5.0 then user will be unable to do $form->add(int $name) in a class with strict types enabled which feels kind of wrong.

@vudaltsov vudaltsov force-pushed the form-phpdoc-fixes branch from 7f0f679 to eae95c4 Compare July 12, 2019 21:05
@vudaltsov
Copy link
Contributor Author

After discussing the issue with @nicolas-grekas and @xabbuh , finally got convinced that we don't need ints here. Still not sure that we should not explicitly deprecate passing ints, but that's not the point of this PR anyway.

@vudaltsov
Copy link
Contributor Author

status: needs review

Copy link
Contributor

@seriquynh seriquynh left a comment

Choose a reason for hiding this comment

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

Also int is available for $name but only using string is more suitable, I think.

The given "$form->add(1)" has no meaning with $name = 1

@xabbuh
Copy link
Member

xabbuh commented Jul 13, 2019

Thank you @vudaltsov.

@xabbuh xabbuh merged commit eae95c4 into symfony:3.4 Jul 13, 2019
xabbuh added a commit that referenced this pull request Jul 13, 2019
This PR was merged into the 3.4 branch.

Discussion
----------

[Form] Name related PHPDoc fixes

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | relates to #32179
| License       | MIT
| Doc PR        | n/a

As I started working on #32179 in #32237, I noticed some PHPDoc inconsistencies around the form's name. I have researched the issue thoroughly and here's my proposal:

1. All "factory" methods (`FormFactory::create*`, `ResolvedFormType::createBuilder`, `FormBuilder::create|add`, `Form::add`) currently accept string and integer names. This should not be deprecated, because it's very convenient and natural to pass ints when adding children to types like `ChoiceType` or `CollectionType`.
1. None of the "factory" methods explicitly accepts `null` as a name, although passing `null` works the same as passing `''`. I consider passing `null` in this case to be ugly, so I corrected the builder's constructors mentioning `null` as a possible argument. One should pass an empty string `''` when creating such a form. We could also deprecate passing `null` in a PR targeting 4.4.
1. Currently the name becomes a string in the builder's (or config builder's) constructor. Which means that `FormConfigInterface::getName` always returns a string and thus the form's `$name` property is always a string. All related checks and PHPDocs should be corrected.
1. The "children accessors" (`Form::has|get|remove`, `FormBuilder::has|get|remove`) should accept both strings and ints because they are array-accessible by design (so it does not really matter, if the key is a string or an int). If it's valid to have `$collectionForm->add(1, ItemType::class)`, then it should be valid to do `$collectionForm->get(1)`. And it works in code, but is not mentioned in the PHPDoc.

ping @nicolas-grekas , @xabbuh , @HeahDude

Commits
-------

eae95c4 PHPDoc fixes
@vudaltsov vudaltsov deleted the form-phpdoc-fixes branch July 13, 2019 13:35
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.

5 participants