Can't seem to get sensible 'else' line handling

Often when I only need one line of executing code on an if or an else call, I like to remove the braces, and PHPStorm for the most part handles this sensibly: it puts the single executable line of code on its own line after the if call.

However, if I add an else after that (without braces), with its own single line of code, by default it will put the else on the if's executable line of code then the else's executable line of code on the next line... which makes it easy to not notice the else at the end of the line:

if (check whatever)
do a thing; else // could be long line of code before the else
do the other thing

In braced blocks, the else is put below the if end brace:

if (check whatever) {
do a thing;
do a second thing;
}
else {
do the other thing;
}

regardless of whether the else has a braced block or not :/

If I go into settings for PHP Code Style > Wrapping and Braces and enable 'else' on a new line, it makes the non-brace single-line blocks easier to read:

if (check whatever)
do a thing; // could be long line of code
else
do the other thing

which makes perfect sense here... however, any if block in braces (with multiple lines of executable code) that has an else after it, the else will be put on the next line, which is unnecessary especially if the else block has braces as well:

if (check whatever) {
do a thing;
do another thing;
}
else {
do the other thing;
}

regardless of whether the else is in a braced block or not :/

I'd honestly be OK with this:

if (check whatever) do a thing;
else do the other thing;

But I'm not seeing how to achieve this while having the else on the same line as an if closing brace, fully like this:

if (check one thing) 
just single line;
else
other single line;

if (check second thing) {
run line 1;
run line 2;
} else {
line in or out of braces;
}
1 comment

Tried to achieve the result you've described but without success. You can submit a feature request to add an option to keep the closing brace on the same line as else.

0

Please sign in to leave a comment.