PHP-CS-Fixer / PHP-CS-Fixer

A tool to automatically fix PHP Coding Standards issues
https://cs.symfony.com
MIT License
12.87k stars 1.58k forks source link

Ternary operator formatting - place condition, true and false variant on separate lines #3705

Closed kalessil closed 6 years ago

kalessil commented 6 years ago

Not sure how soon I get time to contribute myself, so filing the feature request.

Goal: more precise test coverage numbers.

Source code fragment:

    $variable = (condition) ? (true variant) : (false variant);

Target code fragment:

    $variable = (condition) 
        ? (true variant) 
        : (false variant);

Complexity of true/false variants is not important, the variants should go to a new line always. In my case the nested ternary is taboo, so its' support is not needed.

keradus commented 6 years ago

small :-1: for that, it can make code less readable, and may be problematic with nested ternary operators

then, if one have condition foo() && bar() || baz(), splitting this into 3 lines would make code coverage more precise, wouldn't it?

coverage metrics brings value, but it's not ultimate value to use (no metrics is), maybe instead of adjust codebase to be better handled by one metrics, add metrics that would bring more value - eg MSI (Mutation Score Indicator) ?

kalessil commented 6 years ago

Complex conditions are certainly not =) MSI might help, ths for an idea. BTW, @keradus you can close the issue if it's doesn't fit the project direction - I'm fine with it.

keradus commented 6 years ago

I tend to refrain from closing requests out of the blue. I see the value of proposed rule, yet I simply think that this value can be achieved in other, better way ;) Glad you like it as well ;)

For that, I'm not closing this PR "right now", yet it's not on top of my prio list - let us hear the voice of community. If few guys will want this rule, and maybe even one of them will help with PR, I would merge such PR for sure ;)

julienfalque commented 6 years ago

I only apply this rule for long/hard to read lines so I won't use a fixer that blindy adds newlines to all ternary expressions. Maybe better with some kind of length detection, but probably tricky in some cases.

kalessil commented 6 years ago

Closing: the MSI is the way to go here.