8000 [Form] made ValueToStringTransformer convert Boolean false to empty string by shieldo · Pull Request #4180 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] made ValueToStringTransformer convert Boolean false to empty string #4180

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

shieldo
Copy link
Contributor
@shieldo shieldo commented May 2, 2012

It's common in PHP for Boolean false to represent the lack of a value (e.g. core functions like strpos()) - therefore the code here should intuit this intention to represent the lack of something with false and convert it to an empty string the same way it does for null, rather than throwing a rather opaque exception.

@Seldaek
Copy link
Member
Seldaek commented May 2, 2012

I don't agree, false indicates something failed in a way or another, but the common "lack of a value" is null. That's what properties default to when you just do public $foo;. False is a common misuse in older php code, but it has just been possible because for most code, false == null. That said, if the exception is obscure, it should be improved.

@shieldo
Copy link
Contributor Author
shieldo commented May 2, 2012

I understand that point of view, but do we have a clear set of standards for what PHP should return to represent the lack of a value? The reason for the PR is that there doesn't seem to be one - and indeed, for good or ill, the standard library hasn't been fussy. The code here is using values that are normally inferred from reflection of an entity class - which can be completely de-coupled, and therefore any userland class. In the case of a getter method that returns false because there is nothing to return, to me that does indicate a kind of failure (and, in the case of the form component, getters are called before properties are accessed). I'm not saying one should use false to represent a lack of value - I'm saying that this code, which is making inferences based on an entity class - guessing, essentially - should intuit that intention because the usage is common, and I'd argue not harmful as long as documentation is clear. The only question to my mind would be whether there are cases when a value of false should justifiably mean an exception is thrown.

Re: the exception - I agree about improvement to the message, and will have a look at that.

@Seldaek
Copy link
Member
Seldaek commented May 2, 2012

Well, I guess we'll need @bschussek to answer about the real intent behind this code, and why it's like that. That said, IMO a getter returning false because there is no value is broken, and it should be fixed. False is a value, it's a boolean, the opposite of true, a negative value of sorts, but it's not nothing. Old code written that way typically will work no matter if it's false or null, so why not fix it?

@shieldo
Copy link
Contributor Author
shieldo commented May 2, 2012

As I say, I understand that thinking, and don't disagree! (Using false, to me, was a PHP-specific idiom to positively represent the lack of something expected, rather than null, which is literally nothing.) And much of Symfony 8000 and Doctrine code adheres to this unwritten convention. So I will change my own convention in this regard when writing in the context of Symfony, and return null :)

The argument behind the PR still stands, because it's common idiomatic usage that is even used by the standard library, even if it can be argued as being misuse.

@Seldaek
Copy link
Member
Seldaek commented May 2, 2012

Fair enough, but I suppose if it's like this there is a reason, in which case all we can do is improve the exception message to describe what's happening and how to fix it best. Anyway let's wait for a word from @bschussek :)

@webmozart
Copy link
Contributor

This is invalid anyway as ValueToStringTransformer should be reverted (removed) again. See #4102 for more information.

@webmozart webmozart closed this May 2, 2012
@shieldo
Copy link
Contributor Author
shieldo commented May 2, 2012

Thanks for the clarification.

@webmozart
Copy link
Contributor

@shieldo If you are interested in working on #4102, please don't hesitate doing so! :)

@shieldo
Copy link
Contributor Author
shieldo commented May 3, 2012

@bschussek I'd love to contribute, so have bookmarked that one and intend to put a PR together when I have a moment!

@shieldo shieldo deleted the transform_false_to_string branch November 23, 2013 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0