Skip to content
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

Boolean conversion #527

Closed
wants to merge 18 commits into from
Closed

Boolean conversion #527

wants to merge 18 commits into from

Conversation

birko
Copy link
Contributor

@birko birko commented Feb 12, 2014

Added platform specific conversion form database Boolean type to PHP bool type

Default Boolean conversion behaviour
added postgresql literals to Boolean conversion
Changed to call platform specific Boolean conversion
added postgresql platform conversion test
@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DBAL-811

We use Jira to track the state of pull requests and the versions they got
included in.

fixed bug when $item was set as bool true value
added 'off' literal
added literals off and on to the testcase
*/
public function convertFromBoolean($item)
{
if(in_array((string)$item, array('false', 'f', 'n', 'no', 'off')))
Copy link
Member

Choose a reason for hiding this comment

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

Can you fix the CS here? We follow PSR-2 with some minor additions

Copy link
Member

Choose a reason for hiding this comment

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

You can avoid the call to in_array if the returned value is either null, true or false

Changes after Ocramius comments
fixed parameter order in conditions
@birko
Copy link
Contributor Author

birko commented Feb 14, 2014

@Ocramius hope I made the changes correctly in the way you wanted

*/
public function convertFromBoolean($item)
{
if((null !== $item) && (false !== $item) && (true !== $item) && in_array((string)$item, array('false', 'f', 'n', 'no', 'off')))
Copy link
Member

Choose a reason for hiding this comment

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

Break this line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you mean the "if" statement?

@birko
Copy link
Contributor Author

birko commented Feb 14, 2014

Ok now I don't know what more I could do for the CS.

in_array((string)$item, array('false', 'f', 'n', 'no', 'off'))
) {
return false;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

You can avoid the else clause completely, since you're returning early

updated convert function  to include uppercased values
Updated test for uppercased values
@birko
Copy link
Contributor Author

birko commented Feb 15, 2014

@Ocramius You were right I forgot to think about uppercased values

@Ocramius
Copy link
Member

@deeky666 think another method in the abstract platform is ok? Otherwise, looks good to me.

Could need squashing of the commits, but I can do that before someone merges

@birko
Copy link
Contributor Author

birko commented Feb 15, 2014

@Ocramius Sorry for too much commints. I had problems cloning the repo into my PC so most of the changes I made here via Github edit function

@Ocramius
Copy link
Member

@birko that can be dealt with, no problem

*/
public function convertFromBoolean($item)
{
return (null === $item) ? null : (bool) $item;
Copy link
Member

Choose a reason for hiding this comment

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

Please use the short ternary expression: return null === $item ?: (bool) $item

@deeky666
Copy link
Member

@birko Can you please give more details about the issue here? I can see that PostgreSQL allows different boolean values but I don't understand why we need to check for those when retrieving them from the database. AFAIK pdo_pgsql already converts them internally to the proper PHP boolean type. I did some quick tests about this and my assumption seems to be true. Is there another issue I am missing here?

@birko
Copy link
Contributor Author

birko commented Feb 17, 2014

@deeky666:
The main reason I created this pull request was when I wanted to solve this issue for my project https://github.com/l3pp4rd/DoctrineExtensions/issues/980

After a bit self brainstorming I thought it would be better if there was support for all Boolean literals (http://www.postgresql.org/docs/9.3/static/datatype-boolean.html), in case someone would like to use DBAL to parse database data that come not directly from database but in a kind of CSV export. (Like export to csv from pgadmin.)

And since in the Doctrine\DBAL\Types\BooleanType::convertToPHPValue was the $platform parameter I used it to make it more universal too.

reverted @deeky666 suggestion. Since it failed on testBooleanNullConvertsToPHPValue test. 
with message Failed asserting that true is null.
Since I moved this code from BoolenType::convertToPHPValue in the first place, I presume the code there correct.
{
if ((null !== $item) &&
(false !== $item) &&
(true !== $item) &&
Copy link
Member

Choose a reason for hiding this comment

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

All these cases can be removed, because they will not in be in the array

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof I added this additional conditions after one of previous hints here, That I should add them there to avoid unnecessary in_array check

Copy link
Contributor

Choose a reason for hiding this comment

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

It avoids unnessecary in_array() checks but adds new separate checks in the if statement and makes the code more complicated which reduces readability.

*
* @param mixed $item
*
* @return bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add the null as return value also, so: @return bool|null

Updated from @return bool  -> @return bool|null since it returns null too
@Ocramius
Copy link
Member

Merged in #625

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants