-
-
Notifications
You must be signed in to change notification settings - Fork 495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spacing for Return Types #547
Comments
It seems pretty clear-cut to me that there should be a space before the opening bracket. I think it wold be really inconsistent for WP not to adopt that (54 years from now :-). As far as the space after the closing parenthesis, I think we should just allow it to be configured either way, but not flag it by default. |
This error partly comes from an upstream sniff:
So partly this is because PHPCS doesn't fully support PHP 7 yet, I think. Although I also get this from our sniffs:
Also, it looks like
|
Most of the rest of the PHP World uses no space for return types, but the WP historical code style would suggest using a space.
My preference, is that for something "relatively" new (and not yet found in WP core, or many plugins) that there's a good case for sticking with no-space:
In terms of the space before the Along the same lines and with exactly the same arguments, there is also: declare(strict_types=1); declare( strict_types = 1 ); As well as the points I mentioned above for keeping with no-space, the extra argument here is that this isn't going to vary massively - there's only something like three possible values to go inside
It's also always going to be at the top of the file, probably with a clear line space before any I agree that exceptions make for a bad approach to writing standards. But then, the existing Handbook already has inconsistencies. Space usage of |
For return types, no space before the colon. When colon is used as a separator between different things (in the ternary operator, for example), there should be spaces. When it's used as a "here is something related to the thing I'm coming after", (case statements and return types), there should be no space before, one space after when they're on the same line. The other obvious use case of colon is as an alternative to For |
👍 for consistency with prior art here.
As - for now - it is rarely used ( Personally I'd prefer no spaces as:
|
I can't see it in core, and it's not something I've seen much use of at all - do |
@pento Yay :-) Can that be added to the Handbook then, please? |
They're used a bit internally on WordPress.com, and always in the context of a
|
Only slightly adapted from the work Michał Bundyra (@webimpress) did for the Zend Coding Standard. Required format for WordPress is the same as most others in the PHP community: - no spaces before the colon - exactly one space after colon before return type - no space after nullable operator - simple types should be given as lowercase Fixes #547.
Only slightly adapted from the work Michał Bundyra (@webimpress) did for the Zend Coding Standard. Required format for WordPress is the same as most others in the PHP community: - no spaces before the colon - exactly one space after colon before return type - no space after nullable operator - simple types should be given as lowercase Fixes #547.
…k/zend-coding-standard/blob/52b435c714879609262aa36b004c4075cd967acc/src/ZendCodingStandard/Sniffs/Formatting/ReturnTypeSniff.php) Michał Bundyra (@webimpress) did for the Zend Coding Standard. Required format for WordPress is the same as most others in the PHP community: - no space before the colon - exactly one space after colon before return type - no space after nullable operator (hasn't been discussed yet, but I see no reason to vary) - simple types should be given as lowercase End result: ```php function foo(): bool { // Simple return type. }; function foo(): ?string { // Nullable simple return type. }; function foo( $a ): \MyClass { // Return type of a class. }; ``` ❗️ Note: Not yet added to `WordPress-Core/ruleset.xml`. See #547.
Only slightly adapted from the [work](https://github.com/zendframework/zend-coding-standard/blob/52b435c714879609262aa36b004c4075cd967acc/src/ZendCodingStandard/Sniffs/Formatting/ReturnTypeSniff.php) Michał Bundyra (@webimpress) did for the Zend Coding Standard. Required format for WordPress is the same as most others in the PHP community: - no space before the colon - exactly one space after colon before return type - no space after nullable operator (hasn't been discussed yet, but I see no reason to vary) - simple types should be given as lowercase End result: ```php function foo(): bool { // Simple return type. }; function foo(): ?string { // Nullable simple return type. }; function foo( $a ): \MyClass { // Return type of a class. }; ``` ❗️ Note: Not yet added to `WordPress-Core/ruleset.xml`. See #547.
Only slightly adapted from the [work](https://github.com/zendframework/zend-coding-standard/blob/52b435c714879609262aa36b004c4075cd967acc/src/ZendCodingStandard/Sniffs/Formatting/ReturnTypeSniff.php) Michał Bundyra (@webimpress) did for the Zend Coding Standard. Required format for WordPress is the same as most others in the PHP community: - no space before the colon - exactly one space after colon before return type - no space after nullable operator (hasn't been discussed yet, but I see no reason to vary) - simple types should be given as lowercase End result: ```php function foo(): bool { // Simple return type. }; function foo(): ?string { // Nullable simple return type. }; function foo( $a ): \MyClass { // Return type of a class. }; ``` ❗️ Note: Not yet added to `WordPress-Core/ruleset.xml`. See #547.
Only slightly adapted from the [work](https://github.com/zendframework/zend-coding-standard/blob/52b435c714879609262aa36b004c4075cd967acc/src/ZendCodingStandard/Sniffs/Formatting/ReturnTypeSniff.php) Michał Bundyra (@webimpress) did for the Zend Coding Standard. Required format for WordPress is the same as most others in the PHP community: - no space before the colon - exactly one space after colon before return type - no space after nullable operator (hasn't been discussed yet, but I see no reason to vary) - simple types should be given as lowercase End result: ```php function foo(): bool { // Simple return type. }; function foo(): ?string { // Nullable simple return type. }; function foo( $a ): \MyClass { // Return type of a class. }; ``` ❗️ Note: Not yet added to `WordPress-Core/ruleset.xml`. See #547.
Only slightly adapted from the [work](https://github.com/zendframework/zend-coding-standard/blob/52b435c714879609262aa36b004c4075cd967acc/src/ZendCodingStandard/Sniffs/Formatting/ReturnTypeSniff.php) Michał Bundyra (@webimpress) did for the Zend Coding Standard. Required format for WordPress is the same as most others in the PHP community: - no space before the colon - exactly one space after colon before return type - no space after nullable operator (hasn't been discussed yet, but I see no reason to vary) - simple types should be given as lowercase End result: ```php function foo(): bool { // Simple return type. }; function foo(): ?string { // Nullable simple return type. }; function foo( $a ): \MyClass { // Return type of a class. }; ``` ❗️ Note: Not yet added to `WordPress-Core/ruleset.xml`. See #547.
Only slightly adapted from the [work](https://github.com/zendframework/zend-coding-standard/blob/52b435c714879609262aa36b004c4075cd967acc/src/ZendCodingStandard/Sniffs/Formatting/ReturnTypeSniff.php) Michał Bundyra (@webimpress) did for the Zend Coding Standard. Required format for WordPress is the same as most others in the PHP community: - no space before the colon - exactly one space after colon before return type - no space after nullable operator (hasn't been discussed yet, but I see no reason to vary) - simple types should be given as lowercase End result: ```php function foo(): bool { // Simple return type. }; function foo(): ?string { // Nullable simple return type. }; function foo( $a ): \MyClass { // Return type of a class. }; ``` ❗️ Note: Not yet added to `WordPress-Core/ruleset.xml`. See #547.
I just ran into the issue with an expected space before the return type colon, which I don't want to be there. So, did I understand correctly that this will be fixed sometime soon? For now, I will just exclude the according message: <exclude name="WordPress.WhiteSpace.ControlStructureSpacing.NoSpaceAfterCloseParenthesis"/> |
Reopening as - while PR #1421 fixes the false positive previously encountered -, there currently isn't any check in place for the desired spacing. |
The rule is (currently) inconsistent. I get:
For:
But no complaints for the exact same code without a function parameter:
|
I've experienced this as well, though I thought we fixed it. @connerbw are you on the latest release of WPCS? |
Sorry, we're on "wp-coding-standards/wpcs": "^0.14.0", |
For PHP 7, the format for adding return types appears to be of the format:
function foo( $arg ): array {
This format of characters and whitespace between
)
and{
can be seen in examples on:Just for a counter-argument, this post puts a space before the
:
.Regardless, the current WPCS flags up
):
asExpected one space after closing parenthesis
. It doesn't flag up no space between the return type and{
.We, as WPCS, either need to make a decision that
): array {
is correct for the WordPress community, or that particular combination of characters / tokens should be excluded from whitespace checks until TPTB have updated the coding standards.The text was updated successfully, but these errors were encountered: