Php-cs-fixer: Fixer for logical operators?

Created on 21 May 2015  路  19Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

Would it be possible to add a fixer that changes and and or to && and ||? Thought that was already in contrib but seems I was mistaken. Might have time for a PR later if the idea is ok?

kinfeature request

Most helpful comment

I have given the fixer a go in #2577. Please check it out and let me know if it fulfils your needs. It does work for my case.

All 19 comments

They'd be quite risky as they could totally change the code behaviour.

Example:

if ($a = foo() and bar()) {
}

// it is the same as:
if (($a = foo()) && bar()) {
}

// and not:
if ($a = foo() && bar()) {
}

They'd be quite risky as they could totally change the code behaviour.

Well no, if it'd be a fixer the other way around yes but not a fixer to && ||

Well no, if it'd be a fixer the other way around yes but not a fixer to && ||

Hmm, look at my example..

No I understand your example, but my point is most of the time when people use and they don't know about the differences and fixing it to && would most of the time make the code work as the developer originally intended. The fact that the fixer would be breaking shouldn't matter, we have a lot of breaking fixers already (strict, strict_params etc)

but my point is most of the time when people use and they don't know about the differences and fixing it to && would most of the time make the code work as the developer originally intended.

Waaaaa. I disagree. If people don't know the difference, they need to get a grip. We can't work on the assumption that everyone else is stupid!

I feel like I'm not making myself clear here, this would be a completely _optional_ fixer, in contrib. This is PHP _CS_ Fixer, a lot of CS define whether to use and/or or &&/|| so it seemed logical to me to have a fixer in that regard. We don't have to make any assumptions about the user base or anything because it _doesn't matter_, since it's optional

I'm not against the fixer, but it should be marked as risky and in contrib level.
And I wouldn't use this fixer. ;)

It would be tricky and could possible lead to troubles.
However I agree with @Anahkiasen; the new fixer would be as risky to use as the strict comparison-fixer. I would use the fixer, but I wouldn't blindly rely on the changes it made ;)

:+1: For the fixer.

@gharlan According to your sample, could be possible for the fixer to adapt condition?

I mean, before fixer:

if ($a = foo() and bar()) {
}

After fixer:

if (($a = foo()) && bar()) {
}

Should be possible IMHO. We just have to found all rules for not be wrong.

And if we can do this, the fixer will not be risky at all.

@Soullivaneuh I'm really looking forward for your solution in PR ;)

Me too. And yes, could be possible. :)

http://php.net/manual/en/language.operators.precedence.php
You have to pay extra attention to all operators between &&, || and and, or:

  • all assignment operators, example: $a = foo() and bar()
  • ternary operator, example: $a and $b ? $c : $d
  • combinations of xor and or, because xor is between or and ||, example: $a or $b xor $c

Again I didn't ask for the fixer to not be risky, we have tons of fixers that can screw up your codebase way worse than that, I just asked for a quick way to batch fix it even if it means I have to review the code before committing, like a lot of the fixers we have.

@keradus I could start a WIP with unit test cases. For the fixer, will do it when I will got some time but it could be a good start for developers that want to participate.

Any news on that @Soullivaneuh ? It would be really great to have such a fixer done. It was aked for few times already.

Not a lot of time for this at the moment.

Maybe @Anahkiasen?

:+1: for this fixer, I'm totally OK with it being marked as risky as it obviously should be.

I have given the fixer a go in #2577. Please check it out and let me know if it fulfils your needs. It does work for my case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Bilge picture Bilge  路  3Comments

vitek-rostislav picture vitek-rostislav  路  3Comments

BackEndTea picture BackEndTea  路  3Comments

Bilge picture Bilge  路  3Comments

datenmeister picture datenmeister  路  3Comments