For the following code:
<?php
namespace TestNamespace;
class TestPSR2
{
public function __construct( // comment
$param1,
$param2
) {
if ( // comment
$param1 === $param2
) {
}
while ( // comment
$param1 < $param2
) {
}
$closure = function ( // comment
$param1,
$param2
) {
};
$class = new class( // comment
) extends \DateTime {
};
}
}
we are getting the following errors using PSR-2 standard:
$ bin/phpcs TestPSR2.php --standard=PSR2 -s
FILE: TestPSR2.php
------------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
------------------------------------------------------------------------------------------------------------------------------------------
11 | ERROR | [x] Expected 0 spaces after opening bracket; 1 found
| | (PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace)
16 | ERROR | [x] Expected 0 spaces after opening bracket; 1 found
| | (PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace)
------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------------------
Time: 54ms; Memory: 6MB
I would like to change the sniff to be consistent with the comments, so allow spaces before them (after opening bracket).
What do you think? If this issue is going be accepted I can prepare a hotfix. Thanks!
You shouldn't put comments after the opening brace, but in the line after it
@kkmuffme this is not where "I" should or shouldn't put the comment, this is about the standard consistency.
PSR-2 states:
Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before.
That's what the error message is trying to enforce, so I don't think any change is required here.
PSR2 has it's rules and PHPCS just gived the sniffs to validate them. If you want this rule, you may create your own.
@gsherwood
I understand, but the same statement we have about methods:
There MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis.
https://www.php-fig.org/psr/psr-2/#43-methods
and PHP_CodeSniffer allows space after opening parenthesis when the next content is a comment.
I think this is not specified in PSR-2. Please see Conclusion chapter:
There are many elements of style and practice intentionally omitted by this guide. These include but are not limited to:
- ...
- Comments and documentation blocks
https://www.php-fig.org/psr/psr-2/#7-conclusion
So I think we need some changes (when the next content after opening parenthesis is comment):
PSR-2 is confusing, and I don't know what it wants here. And I'm sure I'll never find out.
The reasons function declarations allow these comments is because it's a Squiz sniff enforcing the declaration. The other is a PSR-2 specific sniff, coded for PSR-2 rules alone.
Nobody has complained about the Squiz sniff allows comments, so they probably wont complain about the PSR-2 sniff allowing comments either.
@gsherwood Thanks!
Most helpful comment
@kkmuffme this is not where "I" should or shouldn't put the comment, this is about the standard consistency.