-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] handle callable strings in PropertyAccessDecorator #18057
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
Conversation
Q | A |
---|---|
Branch | 2.7+ |
Bug fix? | yes |
New feature? | no |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | n/a |
License | MIT |
Doc PR | - |
…ecorator ref symfony#17993. Handle edge cases where a callable string is passed to the `PropertyAccessDecorator` for choice list factories. Wrapping the callable string in a closure prevents a potential fatal error, since it may be called with the wrong arguments.
ref #17993. |
@@ -172,6 +172,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null, | |||
|
|||
if (is_string($label) && !is_callable($label)) { | |||
$label = new PropertyPath($label); | |||
} elseif (is_string($label) && is_callable($label)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be the responsibility of the DefaultChoiceListFactory
as the interface is supposed to support callable
anyway.
Then it would just need to make PropertyAccessDecorator
treat callable strings as strings, instead of passing them to the decorated factory as it is.
But one using the DefaultChoiceListFactory
without decorator should not experience this bug.
What do you think @webmozart ?
Thanks for working on this PR @HeahDude! :) Why do we need to add this? I would like to stick with a deprecation error here (which can be added into the default factory, yes). |
@webmozart I got confused by your comment. It is now very clear after reading @Tobion's #17993 (comment). The purpose of #15561 is exactly why I said:
as you agreed (what confused me the most). But I was not aware that it was a "fix" introduced by #15561. So here's my suggestion:
|
I can see one reason why this might be useful: Let's say you start a project where you a string that is no callable and thus is treated as a property path. Then, some time later due to an update or whatever a global function with the same name as the string used here is added. Now, the application would fail, wouldn't it? With this fix we would be able to avoid such failures if I am not mistaken. |
@xabbuh Yes however this PR is wrong as it is as this should not be the responsibility of the
I now understand it, I've failed before because I thought string callables were common callables and the That would mean that to properly fix it, we should be aware of the signature to make a difference between #15542's example The fix of this PR contradict the current behavior and would just make the opposite happens: the first would fail as it may expect three arguments and the proposed "fix" closure only pass one. This is the same concern for any type of callable so one should just stick to the doc to know what arguments the provided callable should deal with and be aware that by default the
To prevent the case you're mentioning or deal with existing function until 4.0 is out, one should pass a property path directly:
which make me think that @weaverryan response to symfony/symfony-docs#6282 (comment) might be reconsidered, and maybe we should precise it for concerned options. Anyway closing here. |
@@ -172,6 +172,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null, | |||
|
|||
if (is_string($label) && !is_callable($label)) { | |||
$label = new PropertyPath($label); | |||
} elseif (is_string($label) && is_callable($label)) { | |||
// Prevent a fatal error since a callable string may not handle the right arguments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does it prevent a fatal error ? You call it with the same argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof Actually choice_label
, choice_name
, choice_attr
, group_by
and preferred_choices
as callable get passed 3 arguments ($choice, $key, $value) and we got a fatal error with callable such as 'end'
but I found out that was not valid because 'MyStatic::getMeChoiceName'
would then fail if it needs 3 arguments, so this is a "won't fix" and I closed this PR.
This PR was merged into the 3.1-dev branch. Discussion ---------- fix #17993 - Deprecated callable strings | Q | A | ------------- | --- | Branch | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | no | Fixed tickets | #17993 | License | MIT | Doc PR | Is this ok ? - [x] Rebase when #18057 is merged Commits ------- 191b495 fix #17993 - Deprecated callable strings