Open paigedwight opened 2 years ago
I suggest this issue is a candidate to be closed with no action required.
Multi-line ternaries are poor readability and although PSR-12 - 6.3 Ternary Operators does not explicitly mention multi-line ternaries it certainly doesn't use any in it's examples. I'm sure consensus opinion would be against breaking them onto multiple lines.
If you want a multi-line conditional, use an if-statement or a match
expression. If your ternary is too long to fit on 1 line, it should probably be an if-statement or a match
expression.
Also, the ternary in your code sample is redundant and can be re-written as:
$x = $a === $b;
Ternaries are elegant for cases where the code blocks are so small they would look silly in an if-statement and would definitely read better were it all on 1 line such as this:
$color = $accountBalance < 0 ? 'red' : 'black';
But they are inappropriate in any other situation.
Only my opinion, I hope you understand. I'm just hoping that with my input, a few low-priority requests can be closed and allow some higher priority ones to get fixed.
Not indenting them at all makes it worse, though.
The pear style also has examples of indenting them: https://pear.php.net/pepr/pepr-proposal-show.php?id=538#toc5
If this is not officially taken up to get supported, is there a way of automatically disabling the sniff on ternary statements so that the user can at least manually keep indentation here?
Not indenting them at all makes it worse, though.
The pear style also has examples of indenting them: https://pear.php.net/pepr/pepr-proposal-show.php?id=538#toc5
If this is not officially taken up to get supported, is there a way of automatically disabling the sniff on ternary statements so that the user can at least manually keep indentation here?
The simple solution here is to set the exact
property to false
.
The exact
property being set to true
in the ruleset is the problem, not this sniff.
I also need to have this fixed. exact=false
cannot be applied as it ignores other cases to be fixed.
@roslov This has already been marked as wontfix
. As I have commended on the issue on your repo, I am yet to see someone present a good use case for multi-line ternaries so I don't think it is a good investment of time for open source maintainers to to support them.
Describe the bug phpcbf will format multiline ternaries to the same level of indentation, instead of having the lines after the first indented an additional level.
Code sample
Custom ruleset
To reproduce Steps to reproduce the behavior:
test.php
with the code sample above...phpcs test.php ...
Expected behavior Multiline ternaries should be indented
Versions (please complete the following information):
Additional context n/a