Hello,
I format the if / else statement in non-PSR2 way. And I need to keep it.
Please help how to setup the configuration for this.
PSR2
if ($foo) {
return $bar;
} else {
return false;
}
My Way (I prefer this)
if ( $foo ) {
return $bar;
}
else {
return false;
}
Big difference is the new line before else.
FYI, one of the reasons to do this way is, to easily comment out the block of else/elseif statement.
I opened a pull request adding this feature some days ago if you want https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/4178
It adds the option "position_before_control_structures" to "braces" that forces the line break before else / elseif / catch / finally
I also prefer your way btw ;)
Hi @limminho and thanks for your question,
The code style you want is not something we support in this project., As you stated it is different than PSR2, but more important it is not a standard style used by a bigger PHP community.
The scope of this project is to help out formatting code following standards and promoting such standards. I feel your request does not fit in this scope.
You can always create a release a custom rule for your project and others!
Hello @SpacePossum !
Is it possible to add new line for else?
For example:
$t = date("H");
if ($t < "20")
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
If it is not standard style, then teach how to custom rules, so we do not know it.
Hi @gusbemacbe ,
That code style cannot be done with the default set of rules of this tool, as it is indeed not a widely used "standard" style.
If you really want to keep using this style you can write your own rule and use that one to reformat code in that style.
To write and use your own rules you can look at some other projects already doing this, like https://github.com/kubawerlos/php-cs-fixer-custom-fixers
@SpacePossum should this issue be reopened? As you've did with the related PR.
I believe code like:
if ($expr1) {
// if body
}
elseif ($expr2) {
// elseif body
}
else {
// else body;
}
do {
// structure body;
}
while ($expr);
try {
// try body
}
catch (FirstThrowableType $e) {
// catch body
}
catch (OtherThrowableType | AnotherThrowableType $e) {
// catch body
}
finally {
// finally body
}
is better than what PSR-12 dictates, and that really grinds my gears.
Thanks for your input and interest @nunovieira !
I think keeping the discussion focused in the PR would be best, if you think cases are being missed there please raise your concerns there as well.
Most helpful comment
I opened a pull request adding this feature some days ago if you want https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/4178
It adds the option "position_before_control_structures" to "braces" that forces the line break before else / elseif / catch / finally
I also prefer your way btw ;)