[PSR-12] Be explicit about whitespaces between empty code block, i.e. curly brackets without any code

107 views
Skip to first unread message

Michael Voříšek

unread,
Jan 27, 2020, 7:27:02 AM1/27/20
to PHP Framework Interoperability Group
While following PSR-12 implementation on PHP-CS-Fixer, a comment indicated an ambiguousness in the PSR-12 text: https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/4756

Empty code block is very common in classes (like the ones that extend \Exception), functions (to pass empty callable if no null is allowed), catch blocks (if some expressions should be caught/supressed but not handled) etc.

Currently this is not defined in the PSR-12 except one example with anonymous class: https://www.php-fig.org/psr/psr-12/#8-anonymous-classes

I do belive that PSR-12 should be very clear about this as the usage is very common.

My proposal is: add a blank line before the closing bracket if and only if a new line is before the opening bracket and there is no content inside the block (code nor comment), i.e.:
class A extends B
{
} try { // try body } catch (FirstThrowableType $e) {}


but what to with if/else like this one:
if () {
} else {}

or try/catch:
try {
} finally {
// finally body
}

or do/while like:
do {
} while();

extend my proposal condition "
a new line is before the opening bracket, there is no content inside the block and the closing bracket is the last part of that expression (except semicolon)"

What do you think?

Alexander Makarov

unread,
Feb 17, 2020, 8:49:31 AM2/17/20
to PHP Framework Interoperability Group
How about rules in section 5?


- The body MUST be on the next line after the opening brace
- The closing brace MUST be on the next line after the body
Reply all
Reply to author
Forward
0 new messages